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
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
45 *-----------------------------------------------------------------------------
53 #include <jimautoconf.h>
60 *-----------------------------------------------------------------------------
63 * Implements the rename TCL command:
64 * readdir ?-nocomplain? dirPath
67 * Standard TCL result.
68 *-----------------------------------------------------------------------------
70 int Jim_ReaddirCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
74 struct dirent
*entryPtr
;
77 if (argc
== 3 && Jim_CompareStringImmediate(interp
, argv
[1], "-nocomplain")) {
80 if (argc
!= 2 && !nocomplain
) {
81 Jim_WrongNumArgs(interp
, 1, argv
, "?-nocomplain? dirPath");
85 dirPath
= Jim_String(argv
[1 + nocomplain
]);
87 dirPtr
= opendir(dirPath
);
92 Jim_SetResultString(interp
, strerror(errno
), -1);
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') {
104 if ((entryPtr
->d_name
[1] == '.') && (entryPtr
->d_name
[2] == '\0'))
107 Jim_ListAppendElement(interp
, Jim_GetResult(interp
), Jim_NewStringObj(interp
,
108 entryPtr
->d_name
, -1));
115 int Jim_readdirInit(Jim_Interp
*interp
)
117 if (Jim_PackageProvide(interp
, "readdir", "1.0", JIM_ERRMSG
))
120 Jim_CreateCommand(interp
, "readdir", Jim_ReaddirCmd
, NULL
, NULL
);