1 /* $RCSfile: directory.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 11:22:24 $
3 * (C) Copyright 1987, 1988, 1990 Diomidis Spinellis.
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8 * $Log: directory.c,v $
9 * Revision 4.0.1.1 91/06/07 11:22:24 lwall
10 * patch4: new copyright notice
12 * Revision 4.0 91/03/20 01:34:24 lwall
15 * Revision 3.0.1.1 90/03/27 16:07:37 lwall
16 * patch16: MSDOS support
18 * Revision 1.3 90/03/16 22:39:40 dds
19 * Fixed malloc problem.
21 * Revision 1.2 88/07/23 00:08:39 dds
22 * Added inode non-zero filling.
24 * Revision 1.1 88/07/23 00:03:50 dds
30 * UNIX compatible directory access functions
33 #include <sys/types.h>
42 * File names are converted to lowercase if the
43 * CONVERT_TO_LOWER_CASE variable is defined.
45 #define CONVERT_TO_LOWER_CASE
50 static char rcsid[] = "$RCSfile: directory.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 11:22:24 $";
54 opendir(char *filename)
57 char *oldresult, *result;
61 char scannamespc[PATHLEN];
62 char *scanname = scannamespc; /* To take address we need a pointer */
65 * Structure used by the MS-DOS directory system calls.
68 char reserved[21]; /* Reserved for MS-DOS */
69 unsigned char attribute; /* Attribute */
70 unsigned int time; /* Time */
71 unsigned int date; /* Date */
72 long size; /* Size of file */
73 char fn[13]; /* Filename */
74 } buffspc, *buff = &buffspc;
77 if (!(p = (DIR *) malloc(sizeof(DIR))))
80 /* Initialize result to use realloc on it */
81 if (!(result = malloc(1))) {
86 /* Create the search pattern */
87 strcpy(scanname, filename);
88 if (strchr("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
89 strcat(scanname, "/*.*");
91 strcat(scanname, "*.*");
94 #if ( defined(M_I86LM) || defined(M_I86CM) || defined(M_I86HM) )
95 segregs.ds = FP_SEG(buff);
96 srv.x.dx = FP_OFF(buff);
98 srv.x.dx = (unsigned int) buff;
100 srv.h.ah = 0x1a; /* Set DTA to DS:DX */
101 intdosx(&srv, &srv, &segregs);
103 #if ( defined(M_I86LM) || defined(M_I86CM) || defined(M_I86HM) )
104 segregs.ds = FP_SEG(scanname);
105 srv.x.dx = FP_OFF(scanname);
107 srv.x.dx = (unsigned int) scanname;
109 srv.x.cx = 0xff; /* Search mode */
111 for (srv.h.ah = 0x4e; !intdosx(&srv, &srv, &segregs); srv.h.ah = 0x4f) {
112 if ((result = (char *) realloc(result, reslen + strlen(buff->fn) + 1)) ==
119 #ifdef CONVERT_TO_LOWER_CASE
120 strcpy(result + reslen, strlwr(buff->fn));
122 strcpy(result + reslen, buff->fn);
124 reslen += strlen(buff->fn) + 1;
127 if (!(result = realloc(result, reslen + 1))) {
134 *(result + reslen) = '\0';
150 dirp->curr += len + 1;
151 strcpy(dirp->dirstr.d_name, p);
152 dirp->dirstr.d_namlen = len;
153 /* To fool programs */
154 dirp->dirstr.d_ino = ++dummy;
155 return &(dirp->dirstr);
163 return (long) dirp->curr; /* ouch! pointer to long cast */
167 seekdir(DIR *dirp, long loc)
169 dirp->curr = (char *) loc; /* ouch! long to pointer cast */
175 dirp->curr = dirp->start;