ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/Frodo4/Src/ndir.c
Revision: 1.1
Committed: 2003-07-01T17:09:43Z (19 years, 7 months ago) by cebix
Content type: text/plain
Branch: MAIN
CVS Tags: VERSION_4_2, HEAD
Log Message:
imported files

File Contents

# User Rev Content
1 cebix 1.1 /* msd_dir.c - portable directory routines
2     Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
3    
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 1, or (at your option)
7     any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17    
18     $Header: /u/src/master/ccvs/windows-NT/ndir.c,v 1.3 1995/09/08 00:34:09 jimb Exp $
19     */
20    
21     /* Everything non trivial in this code is from: @(#)msd_dir.c 1.4
22     87/11/06. A public domain implementation of BSD directory routines
23     for MS-DOS. Written by Michael Rendell ({uunet,utai}michael@garfield),
24     August 1987 */
25    
26    
27     #include <io.h>
28     #include <stdio.h>
29     #include <stdlib.h>
30     #include <string.h>
31     #include <sys/types.h>
32     #include <sys/stat.h>
33    
34     #include <dos.h>
35    
36     #include <ndir.h>
37    
38     static void free_dircontents (struct _dircontents *);
39    
40     /* find ALL files! */
41     #define ATTRIBUTES (_A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR)
42    
43    
44    
45     DIR *
46     opendir (const char *name)
47     {
48     struct _finddata_t find_buf;
49     DIR *dirp;
50     struct _dircontents *dp;
51     char name_buf[_MAX_PATH + 1];
52     char *slash = "";
53     long hFile;
54    
55     if (!name)
56     name = "";
57     else if (*name)
58     {
59     const char *s;
60     int l = strlen (name);
61    
62     s = name + l - 1;
63     if ( !(l == 2 && *s == ':') && *s != '\\' && *s != '/')
64     slash = "/"; /* save to insert slash between path and "*.*" */
65     }
66    
67     strcat (strcat (strcpy (name_buf, name), slash), "*.*");
68    
69     dirp = (DIR *) malloc (sizeof (DIR));
70     if (dirp == (DIR *)0)
71     return (DIR *)0;
72    
73     dirp->dd_loc = 0;
74     dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) 0;
75    
76     if ((hFile = _findfirst (name_buf, &find_buf)) < 0)
77     {
78     free (dirp);
79     return (DIR *)0;
80     }
81    
82     do
83     {
84     dp = (struct _dircontents *) malloc (sizeof (struct _dircontents));
85     if (dp == (struct _dircontents *)0)
86     {
87     free_dircontents (dirp->dd_contents);
88     return (DIR *)0;
89     }
90    
91     dp->_d_entry = malloc (strlen (find_buf.name) + 1);
92     if (dp->_d_entry == (char *)0)
93     {
94     free (dp);
95     free_dircontents (dirp->dd_contents);
96     return (DIR *)0;
97     }
98    
99     if (dirp->dd_contents)
100     dirp->dd_cp = dirp->dd_cp->_d_next = dp;
101     else
102     dirp->dd_contents = dirp->dd_cp = dp;
103    
104     strcpy (dp->_d_entry, find_buf.name);
105    
106     dp->_d_next = (struct _dircontents *)0;
107    
108     } while (!_findnext (hFile, &find_buf));
109    
110     dirp->dd_cp = dirp->dd_contents;
111    
112     _findclose(hFile);
113    
114     return dirp;
115     }
116    
117    
118     void
119     closedir (DIR *dirp)
120     {
121     free_dircontents (dirp->dd_contents);
122     free ((char *) dirp);
123     }
124    
125    
126     struct direct *
127     readdir (DIR *dirp)
128     {
129     static struct direct dp;
130    
131     if (dirp->dd_cp == (struct _dircontents *)0)
132     return (struct direct *)0;
133     dp.d_namlen = dp.d_reclen =
134     strlen (strcpy (dp.d_name, dirp->dd_cp->_d_entry));
135     #if 0 /* JB */
136     strlwr (dp.d_name); /* JF */
137     #endif
138     dp.d_ino = 0;
139     dirp->dd_cp = dirp->dd_cp->_d_next;
140     dirp->dd_loc++;
141    
142     return &dp;
143     }
144    
145    
146     void
147     seekdir (DIR *dirp, long off)
148     {
149     long i = off;
150     struct _dircontents *dp;
151    
152     if (off < 0)
153     return;
154     for (dp = dirp->dd_contents; --i >= 0 && dp; dp = dp->_d_next)
155     ;
156     dirp->dd_loc = off - (i + 1);
157     dirp->dd_cp = dp;
158     }
159    
160    
161     long
162     telldir (DIR *dirp)
163     {
164     return dirp->dd_loc;
165     }
166    
167    
168     /* Garbage collection */
169    
170     static void
171     free_dircontents (struct _dircontents *dp)
172     {
173     struct _dircontents *odp;
174    
175     while (dp)
176     {
177     if (dp->_d_entry)
178     free (dp->_d_entry);
179     dp = (odp = dp)->_d_next;
180     free (odp);
181     }
182     }
183    
184    
185     #ifdef TEST
186    
187     void main (int argc, char *argv[]);
188    
189     void
190     main (int argc, char *argv[])
191     {
192     static DIR *directory;
193     struct direct *entry = (struct direct *)0;
194    
195     char *name = "";
196    
197     if (argc > 1)
198     name = argv[1];
199    
200     directory = opendir (name);
201    
202     if (!directory)
203     {
204     fprintf (stderr, "can't open directory `%s'.\n", name);
205     exit (2);
206     }
207    
208     while (entry = readdir (directory))
209     printf ("> %s\n", entry->d_name);
210    
211     printf ("done.\n");
212     }
213    
214     #endif /* TEST */
215    
216     /*
217     * Local Variables:
218     * mode:C
219     * ChangeLog:ChangeLog
220     * compile-command:make
221     * End:
222     */