aio recvfrom was not null terminating the result
[jimtcl.git] / jim-readdir.c
blob3f49ed453ece61b6ecfc1e6a8c4595c5d09cf224
2 /*
3 * (c) 2008 Steve Bennett <steveb@worware.net.au>
5 * Tcl readdir command.
7 * The FreeBSD license
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * The views and conclusions contained in the software and documentation
34 * are those of the authors and should not be interpreted as representing
35 * official policies, either expressed or implied, of the Jim Tcl Project.
37 * Based on original work by:
38 *-----------------------------------------------------------------------------
39 * Copyright 1991-1994 Karl Lehenbauer and Mark Diekhans.
41 * Permission to use, copy, modify, and distribute this software and its
42 * documentation for any purpose and without fee is hereby granted, provided
43 * that the above copyright notice appear in all copies. Karl Lehenbauer and
44 * Mark Diekhans make no representations about the suitability of this
45 * software for any purpose. It is provided "as is" without express or
46 * implied warranty.
47 *-----------------------------------------------------------------------------
50 #include <errno.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <dirent.h>
55 #define JIM_EXTENSION
56 #include "jim.h"
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_GetString(argv[1 + nocomplain], NULL);
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 Jim_SetResultString(interp, strerror(errno), -1);
96 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
98 while ((entryPtr = readdir(dirPtr)) != NULL) {
99 if (entryPtr->d_name[0] == '.') {
100 if (entryPtr->d_name[1] == '\0') {
101 continue;
103 if ((entryPtr->d_name[1] == '.') && (entryPtr->d_name[2] == '\0'))
104 continue;
106 Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp,
107 entryPtr->d_name, -1));
109 closedir(dirPtr);
111 return JIM_OK;
114 int Jim_readdirInit(Jim_Interp *interp)
116 Jim_CreateCommand(interp, "readdir", Jim_ReaddirCmd, NULL, NULL);
117 return JIM_OK;