namespace: disallow array element syntax for variables
[jimtcl.git] / jim-readdir.c
bloba4056924a92c39825449b2aeb089f606cedad40f
2 /*
3 * Tcl readdir command.
5 * (c) 2008 Steve Bennett <steveb@worware.net.au>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * The views and conclusions contained in the software and documentation
32 * are those of the authors and should not be interpreted as representing
33 * official policies, either expressed or implied, of the Jim Tcl Project.
35 * Based on original work by:
36 *-----------------------------------------------------------------------------
37 * Copyright 1991-1994 Karl Lehenbauer and Mark Diekhans.
39 * Permission to use, copy, modify, and distribute this software and its
40 * documentation for any purpose and without fee is hereby granted, provided
41 * that the above copyright notice appear in all copies. Karl Lehenbauer and
42 * Mark Diekhans make no representations about the suitability of this
43 * software for any purpose. It is provided "as is" without express or
44 * implied warranty.
45 *-----------------------------------------------------------------------------
48 #include <errno.h>
49 #include <stdio.h>
50 #include <string.h>
52 #include <jim.h>
53 #include <jimautoconf.h>
55 #ifdef HAVE_DIRENT_H
56 #include <dirent.h>
57 #endif
60 *-----------------------------------------------------------------------------
62 * Jim_ReaddirCmd --
63 * Implements the rename TCL command:
64 * readdir ?-nocomplain? dirPath
66 * Results:
67 * Standard TCL result.
68 *-----------------------------------------------------------------------------
70 int Jim_ReaddirCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
72 const char *dirPath;
73 DIR *dirPtr;
74 struct dirent *entryPtr;
75 int nocomplain = 0;
77 if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) {
78 nocomplain = 1;
80 if (argc != 2 && !nocomplain) {
81 Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath");
82 return JIM_ERR;
85 dirPath = Jim_String(argv[1 + nocomplain]);
87 dirPtr = opendir(dirPath);
88 if (dirPtr == NULL) {
89 if (nocomplain) {
90 return JIM_OK;
92 Jim_SetResultString(interp, strerror(errno), -1);
93 return JIM_ERR;
95 Jim_SetResultString(interp, strerror(errno), -1);
97 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
99 while ((entryPtr = readdir(dirPtr)) != NULL) {
100 if (entryPtr->d_name[0] == '.') {
101 if (entryPtr->d_name[1] == '\0') {
102 continue;
104 if ((entryPtr->d_name[1] == '.') && (entryPtr->d_name[2] == '\0'))
105 continue;
107 Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp,
108 entryPtr->d_name, -1));
110 closedir(dirPtr);
112 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;