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
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
44 *-----------------------------------------------------------------------------
52 #include <jimautoconf.h>
59 *-----------------------------------------------------------------------------
62 * Implements the rename TCL command:
63 * readdir ?-nocomplain? dirPath
66 * Standard TCL result.
67 *-----------------------------------------------------------------------------
69 int Jim_ReaddirCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
73 struct dirent
*entryPtr
;
76 if (argc
== 3 && Jim_CompareStringImmediate(interp
, argv
[1], "-nocomplain")) {
79 if (argc
!= 2 && !nocomplain
) {
80 Jim_WrongNumArgs(interp
, 1, argv
, "?-nocomplain? dirPath");
84 dirPath
= Jim_String(argv
[1 + nocomplain
]);
86 dirPtr
= opendir(dirPath
);
91 Jim_SetResultString(interp
, strerror(errno
), -1);
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') {
102 if ((entryPtr
->d_name
[1] == '.') && (entryPtr
->d_name
[2] == '\0'))
105 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, entryPtr
->d_name
, -1));
109 Jim_SetResult(interp
, listObj
);
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
);