file: fix stat file size for large files
[jimtcl.git] / jim-readdir.c
blobd7c0e403f8f3689f703e19372766d63f2cd6db0d
1 /*
2 * Tcl readdir command.
4 * (c) 2008 Steve Bennett <steveb@worware.net.au>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as representing
32 * official policies, either expressed or implied, of the Jim Tcl Project.
34 * Based on original work by:
35 *-----------------------------------------------------------------------------
36 * Copyright 1991-1994 Karl Lehenbauer and Mark Diekhans.
38 * Permission to use, copy, modify, and distribute this software and its
39 * documentation for any purpose and without fee is hereby granted, provided
40 * that the above copyright notice appear in all copies. Karl Lehenbauer and
41 * Mark Diekhans make no representations about the suitability of this
42 * software for any purpose. It is provided "as is" without express or
43 * implied warranty.
44 *-----------------------------------------------------------------------------
47 #include <errno.h>
48 #include <stdio.h>
49 #include <string.h>
51 #include <jim.h>
52 #include <jimautoconf.h>
54 #ifdef HAVE_DIRENT_H
55 #include <dirent.h>
56 #endif
59 *-----------------------------------------------------------------------------
61 * Jim_ReaddirCmd --
62 * Implements the rename TCL command:
63 * readdir ?-nocomplain? dirPath
65 * Results:
66 * Standard TCL result.
67 *-----------------------------------------------------------------------------
69 int Jim_ReaddirCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
71 const char *dirPath;
72 DIR *dirPtr;
73 struct dirent *entryPtr;
74 int nocomplain = 0;
76 if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) {
77 nocomplain = 1;
79 if (argc != 2 && !nocomplain) {
80 Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath");
81 return JIM_ERR;
84 dirPath = Jim_String(argv[1 + nocomplain]);
86 dirPtr = opendir(dirPath);
87 if (dirPtr == NULL) {
88 if (nocomplain) {
89 return JIM_OK;
91 Jim_SetResultString(interp, strerror(errno), -1);
92 return JIM_ERR;
94 else {
95 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
97 while ((entryPtr = readdir(dirPtr)) != NULL) {
98 if (entryPtr->d_name[0] == '.') {
99 if (entryPtr->d_name[1] == '\0') {
100 continue;
102 if ((entryPtr->d_name[1] == '.') && (entryPtr->d_name[2] == '\0'))
103 continue;
105 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, entryPtr->d_name, -1));
107 closedir(dirPtr);
109 Jim_SetResult(interp, listObj);
111 return JIM_OK;
115 int Jim_readdirInit(Jim_Interp *interp)
117 if (Jim_PackageProvide(interp, "readdir", "1.0", JIM_ERRMSG))
118 return JIM_ERR;
120 Jim_CreateCommand(interp, "readdir", Jim_ReaddirCmd, NULL, NULL);
121 return JIM_OK;