usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / usbmodeswitch / jim / jim-readdir.c
blob67fc956e4f4352062351847a0b1050491c280ae7
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>
51 #include <dirent.h>
53 #include "jim.h"
54 #include "jimautoconf.h"
57 *-----------------------------------------------------------------------------
59 * Jim_ReaddirCmd --
60 * Implements the rename TCL command:
61 * readdir ?-nocomplain? dirPath
63 * Results:
64 * Standard TCL result.
65 *-----------------------------------------------------------------------------
67 int Jim_ReaddirCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
69 const char *dirPath;
70 DIR *dirPtr;
71 struct dirent *entryPtr;
72 int nocomplain = 0;
74 if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) {
75 nocomplain = 1;
77 if (argc != 2 && !nocomplain) {
78 Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath");
79 return JIM_ERR;
82 dirPath = Jim_String(argv[1 + nocomplain]);
84 dirPtr = opendir(dirPath);
85 if (dirPtr == NULL) {
86 if (nocomplain) {
87 return JIM_OK;
89 Jim_SetResultString(interp, strerror(errno), -1);
90 return JIM_ERR;
92 Jim_SetResultString(interp, strerror(errno), -1);
94 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
96 while ((entryPtr = readdir(dirPtr)) != NULL) {
97 if (entryPtr->d_name[0] == '.') {
98 if (entryPtr->d_name[1] == '\0') {
99 continue;
101 if ((entryPtr->d_name[1] == '.') && (entryPtr->d_name[2] == '\0'))
102 continue;
104 Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp,
105 entryPtr->d_name, -1));
107 closedir(dirPtr);
109 return JIM_OK;
112 int Jim_readdirInit(Jim_Interp *interp)
114 if (Jim_PackageProvide(interp, "readdir", "1.0", JIM_ERRMSG))
115 return JIM_ERR;
117 Jim_CreateCommand(interp, "readdir", Jim_ReaddirCmd, NULL, NULL);
118 return JIM_OK;