1 /* $Header: /home/yav/xpx/RCS/dir.c,v 1.11 1995/11/23 17:05:13 yav Exp $
2  * directory access
3  * written by yav (UHD98984@pcvan.or.jp)
4  */
5 
6 #include "headers.h"
7 
8 #ifdef HAVE_SYS_DIR_H
9 # ifdef HAVE_OPENDIR
10 #  define USE_OPENDIR
11 # endif
12 #endif
13 
14 #ifdef USE_OPENDIR
15 # include <sys/types.h>
16 # include <sys/dir.h>
17 #else
18 # ifdef HAVE_SYS_WAIT_H
19 #  include <sys/wait.h>
20 # endif
21 #endif
22 
23 #include <sys/stat.h>
24 
25 char rcsid_dir[] = "$Id: dir.c,v 1.11 1995/11/23 17:05:13 yav Exp $";
26 
get_current_dir(char * buf)27 void get_current_dir(
28 #ifdef __STDC__
29 		     char *buf)
30 #else
31      buf)
32      char *buf;
33 #endif
34 {
35 #ifdef HAVE_GETCWD
36   getcwd(buf, MAXPATH);
37 #else
38   getwd(buf);
39 #endif
40 }
41 
mark_dir(char * dir,char * filebuf,int len,int n)42 void mark_dir(
43 #ifdef __STDC__
44 	      char *dir, char *filebuf, int len, int n)
45 #else
46      dir, filebuf, len, n)
47      char *dir;
48      char *filebuf;
49      int len;
50      int n;
51 #endif
52 {
53   int i;
54   struct stat statbuf;
55   char buf[MAXPATH];
56 
57   for (i = 0; i < n; i++) {
58     sprintf(buf, "%s/%s", dir, filebuf+i*len);
59     stat(buf, &statbuf);
60     if (statbuf.st_mode & S_IFDIR)
61       strcat(filebuf+i*len, "/");
62   }
63 }
64 
65 #ifdef USE_OPENDIR
66 /*
67  * return : 0 error
68  */
get_dir(char * name,char * filebuf,int len,int slotn)69 int get_dir(
70 #ifdef __STDC__
71 	    char *name, char *filebuf, int len, int slotn)
72 #else
73      name, filebuf, len, slotn)
74      char *name;
75      char *filebuf;
76      int len;
77      int slotn;
78 #endif
79 {
80   int n;
81   DIR *dp;
82   struct direct *dir;
83 
84   if ((dp = opendir(name)) == NULL)
85     return 0;
86   for (n = 0; n < slotn && (dir = readdir(dp)) != NULL; n++) {
87     if (dir->d_ino == 0)
88       continue;
89     strncpy(filebuf+n*len, dir->d_name, len);
90     *(filebuf+n*len+len-1) = '\0';
91   }
92   closedir(dp);
93 #ifdef __STDC__
94   qsort(filebuf, n, len, (int (*)(const void *, const void *))strcmp);
95 #else
96   qsort(filebuf, n, len, strcmp);
97 #endif
98   mark_dir(name, filebuf, len, n);
99   return n;
100 }
101 
102 #else /* USE_OPENDIR */
103 
get_dir(char * name,char * filebuf,int len,int slotn)104 int get_dir(
105 #ifdef __STDC__
106 	    char *name, char *filebuf, int len, int slotn)
107 #else
108      name, filebuf, len, slotn)
109      char *name;
110      char *filebuf;
111      int len;
112      int slotn;
113 #endif
114 {
115   FILE *fp;
116   int i, n;
117   int fd[2];
118   char buf[MAXPATH];
119 
120   pipe(fd);
121 #ifdef HAVE_VFORK
122   i = vfork();
123 #else
124   i = fork();
125 #endif
126   if (i == -1) {
127     fprintf(stderr, "fork error!\n");
128     return 0;
129   }
130   if (i == 0) {
131     close(1);
132     dup(fd[1]);
133     close(fd[1]);
134     close(fd[0]);
135     execlp("ls", "ls", "-a", name, NULL);
136     fprintf(stderr, "exec ls error!\n");
137 #ifdef HAVE_VFORK
138     _exit(1);
139 #else
140     exit(1);
141 #endif
142   }
143   fp = fdopen(fd[0], "r");
144   close(fd[1]);
145   for (n = 0; n < slotn && fgets(buf, sizeof(buf), fp) != NULL; n++) {
146     i = strlen(buf);
147     if (i && buf[i-1] == '\n')
148       buf[i-1] = '\0';
149     if (strlen(buf) == 0)
150       break;
151     strncpy(filebuf+n*len, buf, len-1);
152     *(filebuf+n*len+len-1) = '\0';
153   }
154   fclose(fp);
155   wait(&i);
156   mark_dir(name, filebuf, len, n);
157   return n;
158 }
159 
160 #endif /* USE_OPENDIR */
161 
162 /* End of file */
163