File: dir.c

package info (click to toggle)
fkiss 0.33a.patch-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 804 kB
  • ctags: 1,074
  • sloc: ansic: 9,141; sh: 2,924; makefile: 98; sed: 14
file content (250 lines) | stat: -rw-r--r-- 5,720 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* $Header: /home/yav/catty/fkiss/RCS/dir.c,v 1.14 2000/09/02 03:23:52 yav Exp $
 * Simple directory access routine
 * written by yav <yav@bigfoot.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * #define USE_BSD_DIRLIB 1
 *  use opendir, readdir and closedir library routines.
 * #define USE_BSD_DIRLIB 0
 *  use popen to execute "ls -af".
 *
 * #define DIR_STANDALONE_TEST
 * for self checking
 *
 */

char id_dir[] = "$Id: dir.c,v 1.14 2000/09/02 03:23:52 yav Exp $";

#include <stdio.h>

#ifdef DIR_STANDALONE_TEST
# include "config.h"
#else
# include <X11/Xlib.h>
# include "config.h"
# include "headers.h"
# include "fkiss.h"
# define PUBLIC_DIR_C
# include "extern.h"
#endif

#if HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# if HAVE_SYS_NDIR_H
#  include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
#  include <sys/dir.h>
# endif
# if HAVE_NDIR_H
#  include <ndir.h>
# endif
#endif


#if HAVE_SYS_STAT_H
# include <sys/stat.h>
#else
# if HAVE_STAT_H
#  include <stat.h>
# endif
#endif

#ifdef DIR_STANDALONE_TEST
#define ks_malloc malloc
#define ks_realloc realloc
#define ks_strdup strdup
extern char *malloc();
extern char *realloc();
extern void free();
extern char *strdup();
extern char *strncpy();
#endif

#ifndef USE_BSD_DIRLIB
#warning USE_BSD_DIRLIB not defined!
#define USE_BSD_DIRLIB 0
#endif

#if !USE_BSD_DIRLIB
# ifndef MAXPATH
#  ifdef MAXPATHLEN
#   define MAXPATH MAXPATHLEN
#  else
#   define MAXPATH 1024
#  endif
# endif
#endif

/* free all allocated filename list */
void dir_free(list)
     char **list;
{
  char **p;
  
  if (list != NULL) {
    for (p = list; *p != NULL; p++)
      free(*p);
    free(list);
  }    
}

/* dir_ls - get filename list in directory
 * return filename pointer list (NULL : directory open error)
 */
char **dir_ls(path, func)
     char *path;		/* directory */
     int (*func)();		/* filename matching function (NULL:all) */
{
  int r;
  int i;
  char **list;
#if USE_BSD_DIRLIB
  DIR *dp;
  struct dirent *p;
#else
  FILE *fp;
#endif
  char *buf;
  char *subdir;
  struct stat st;
  char **sublist, **sublist0;
  char *ss;
  
  r = 0;
  list = NULL;
#if USE_BSD_DIRLIB
  if ((dp = opendir(path)) != NULL) {
    list = (char **)ks_malloc(sizeof(char *));
    while ((p = readdir(dp)) != NULL) {
      i = NAMLEN(p);
      buf = ks_malloc(i + 1);
      strncpy(buf, p->d_name, i);
      buf[i] = '\0';
      subdir = ks_malloc(strlen(path) + 1 + strlen(buf) + 1);
      sprintf(subdir, "%s/%s", path, buf);
      if (strcmp(buf, ".") && strcmp(buf, "..") &&
	  stat(subdir, &st) == 0 && (st.st_mode & S_IFDIR)) {
	sublist0 = sublist = dir_ls(subdir, func);
	if (sublist0 != NULL) {
	  while (*sublist) {
	    ss = ks_malloc(strlen(buf) + 1 + strlen(*sublist) + 1);
	    sprintf(ss, "%s/%s", buf, *sublist);
	    *(list+r++) = ss;
	    list = (char **)ks_realloc(list, sizeof(char *) * (r+1));
	    sublist++;
	  }
	  dir_free(sublist0);
	}
	free(buf);
      } else {
	if (func == NULL || (*func)(buf)) {
	  *(list+r++) = buf;
	  list = (char **)ks_realloc(list, sizeof(char *) * (r+1));
	} else {
	  free(buf);
	}
      }
      buf = NULL;
    }
    *(list+r) = NULL;
    closedir(dp);
  }
#else /* USE_BSD_DIRLIB */
  buf = ks_malloc(7 + strlen(path) + 1);
  sprintf(buf, "ls -af %s", path);
  if ((fp = popen(buf, "r")) != NULL) {
    list = (char **)ks_malloc(sizeof(char *));
    buf = ks_realloc(buf, MAXPATH);
    while (fgets(buf, MAXPATH, fp) != NULL) {
      i = strlen(buf) - 1;
      if (i >= 0 && buf[i] == '\n')
	buf[i] = '\0';		/* cut '\n' */
      subdir = ks_malloc(strlen(path) + 1 + strlen(buf) + 1);
      sprintf(subdir, "%s/%s", path, buf);
      if (strcmp(buf, ".") && strcmp(buf, "..") &&
	  stat(subdir, &st) == 0 && (st.st_mode & S_IFDIR)) {
	sublist0 = sublist = dir_ls(subdir, func);
	if (sublist0 != NULL) {
	  while (*sublist) {
	    ss = ks_malloc(strlen(buf) + 1 + strlen(*sublist) + 1);
	    sprintf(ss, "%s/%s", buf, *sublist);
	    *(list+r++) = ss;
	    list = (char **)ks_realloc(list, sizeof(char *) * (r+1));
	    sublist++;
	  }
	  dir_free(sublist0);
	}
      } else {
	if (func == NULL || (*func)(buf)) {
	  *(list+r++) = ks_strdup(buf);
	  list = (char **)ks_realloc(list, sizeof(char *) * (r+1));
	}
      }
    }
    free(buf);
    *(list+r) = NULL;
    pclose(fp);
  }
#endif /* USE_BSD_DIRLIB */
  return list;
}

#ifdef DIR_STANDALONE_TEST

int cmpfunc(char *name)
{
#if 0
  return (*name == 'f');
#else
  return 1;
#endif
}

int main(argc, argv)
     int argc;
     char **argv;
{
  int i;
  char **buf;
  
  if (argc < 2)
    exit(1);
#if USE_BSD_DIRLIB
  printf("* dir lib *\n");
#else
  printf("* dir ls  *\n");
#endif
  buf = dir_ls(*++argv, cmpfunc);
  if (buf == NULL) {
    fprintf(stderr, "``%s'' read error!\n", *argv);
    exit(1);
  }
  for (i = 0; *(buf+i) != NULL; i++) {
    printf("[%s]\n", *(buf+i));
  }
  dir_free(buf);
  printf("Total %d files\n", i);
  return 0;
}

#endif /* DIR_STANDALONE_TEST */

/* End of file */