2 * @(#)dir.c 1.4 87/11/06 Public Domain.
4 * A public domain implementation of BSD directory routines for
5 * MS-DOS. Written by Michael Rendell ({uunet,utai}michael@garfield),
7 * Ported to OS/2 by Kai Uwe Rommel
11 #include <sys/types.h>
23 int attributes = A_DIR | A_HIDDEN;
26 static char *getdirent(char *);
27 static void free_dircontents(struct _dircontents *);
31 static FILEFINDBUF find;
34 DIR *opendir(char *name)
40 struct _dircontents *dp;
41 char nbuf[MAXPATHLEN + 1];
45 if ( ((c = nbuf[strlen(nbuf) - 1]) == '\\' || c == '/') &&
48 nbuf[strlen(nbuf) - 1] = 0;
50 if ( nbuf[strlen(nbuf) - 1] == ':' )
54 if ( nbuf[strlen(nbuf) - 1] == ':' )
57 if (stat(nbuf, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
60 if ( (dirp = malloc(sizeof(DIR))) == NULL )
63 if ( nbuf[strlen(nbuf) - 1] == '.' )
64 strcpy(nbuf + strlen(nbuf) - 1, "*.*");
66 if ( ((c = nbuf[strlen(nbuf) - 1]) == '\\' || c == '/') &&
70 strcat(nbuf, "\\*.*");
73 dirp -> dd_contents = dirp -> dd_cp = NULL;
75 if ((s = getdirent(nbuf)) == NULL)
80 if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
81 ((dp -> _d_entry = malloc(strlen(s) + 1)) == NULL) )
85 free_dircontents(dirp -> dd_contents);
90 if (dirp -> dd_contents)
91 dirp -> dd_cp = dirp -> dd_cp -> _d_next = dp;
93 dirp -> dd_contents = dirp -> dd_cp = dp;
95 strcpy(dp -> _d_entry, s);
98 dp -> _d_size = find.cbFile;
99 dp -> _d_mode = find.attrFile;
100 dp -> _d_time = *(unsigned *) &(find.ftimeLastWrite);
101 dp -> _d_date = *(unsigned *) &(find.fdateLastWrite);
103 while ((s = getdirent(NULL)) != NULL);
105 dirp -> dd_cp = dirp -> dd_contents;
111 void closedir(DIR * dirp)
113 free_dircontents(dirp -> dd_contents);
118 struct direct *readdir(DIR * dirp)
120 static struct direct dp;
122 if (dirp -> dd_cp == NULL)
125 dp.d_namlen = dp.d_reclen =
126 strlen(strcpy(dp.d_name, dirp -> dd_cp -> _d_entry));
128 strlwr(dp.d_name); /* JF */
131 dp.d_size = dirp -> dd_cp -> _d_size;
132 dp.d_mode = dirp -> dd_cp -> _d_mode;
133 dp.d_time = dirp -> dd_cp -> _d_time;
134 dp.d_date = dirp -> dd_cp -> _d_date;
136 dirp -> dd_cp = dirp -> dd_cp -> _d_next;
143 void seekdir(DIR * dirp, long off)
146 struct _dircontents *dp;
150 for (dp = dirp -> dd_contents; --i >= 0 && dp; dp = dp -> _d_next);
152 dirp -> dd_loc = off - (i + 1);
158 long telldir(DIR * dirp)
160 return dirp -> dd_loc;
164 static void free_dircontents(struct _dircontents * dp)
166 struct _dircontents *odp;
171 free(dp -> _d_entry);
173 dp = (odp = dp) -> _d_next;
179 static char *getdirent(char *dir)
184 { /* get first entry */
187 done = DosFindFirst(dir, &hdir, attributes,
188 &find, sizeof(find), &count, 0L);
190 else /* get next entry */
191 done = DosFindNext(hdir, &find, sizeof(find), &count);