Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

ndir.c

Go to the documentation of this file.
00001 /*  msd_dir.c - portable directory routines
00002     Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 1, or (at your option)
00007     any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018     $Header: /cvsroot/e32frodo/e32frodo/src/ndir.c,v 1.3 2001/12/08 11:52:30 alfredh Exp $
00019  */
00020 
00021 /* Everything non trivial in this code is from: @(#)msd_dir.c 1.4
00022    87/11/06.  A public domain implementation of BSD directory routines
00023    for MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
00024    August 1897 */
00025 
00026 
00027 #include <io.h>
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <sys/types.h>
00032 #include <sys/stat.h>
00033 
00034 #include <dos.h>
00035 
00036 #include <ndir.h>
00037 
00038 static void free_dircontents (struct _dircontents *);
00039 
00040 /* find ALL files! */
00041 #define ATTRIBUTES      (_A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR)
00042 
00043 
00044 
00045 DIR *
00046 opendir (const char *name)
00047 {
00048   struct _finddata_t find_buf;
00049   DIR *dirp;
00050   struct _dircontents *dp;
00051   char name_buf[_MAX_PATH + 1];
00052   char *slash = "";
00053   long hFile;
00054 
00055   if (!name)
00056     name = "";
00057   else if (*name)
00058     {
00059       const char *s;
00060       int l = strlen (name);
00061 
00062       s = name + l - 1;
00063       if ( !(l == 2 && *s == ':') && *s != '\\' && *s != '/')
00064         slash = "/";    /* save to insert slash between path and "*.*" */
00065     }
00066 
00067   strcat (strcat (strcpy (name_buf, name), slash), "*.*");
00068 
00069   dirp = (DIR *) malloc (sizeof (DIR));
00070   if (dirp == (DIR *)0)
00071     return (DIR *)0;
00072 
00073   dirp->dd_loc = 0;
00074   dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) 0;
00075 
00076   if ((hFile = _findfirst (name_buf, &find_buf)) < 0)
00077     {
00078       free (dirp);
00079       return (DIR *)0;
00080     }
00081 
00082   do
00083     {
00084       dp = (struct _dircontents *) malloc (sizeof (struct _dircontents));
00085       if (dp == (struct _dircontents *)0)
00086         {
00087           free_dircontents (dirp->dd_contents);
00088           return (DIR *)0;
00089         }
00090 
00091       dp->_d_entry = malloc (strlen (find_buf.name) + 1);
00092       if (dp->_d_entry == (char *)0)
00093         {
00094           free (dp);
00095           free_dircontents (dirp->dd_contents);
00096           return (DIR *)0;
00097         }
00098 
00099       if (dirp->dd_contents)
00100         dirp->dd_cp = dirp->dd_cp->_d_next = dp;
00101       else
00102         dirp->dd_contents = dirp->dd_cp = dp;
00103 
00104       strcpy (dp->_d_entry, find_buf.name);
00105 
00106       dp->_d_next = (struct _dircontents *)0;
00107 
00108     } while (!_findnext (hFile, &find_buf));
00109 
00110   dirp->dd_cp = dirp->dd_contents;
00111 
00112   _findclose(hFile);
00113 
00114   return dirp;
00115 }
00116 
00117 
00118 void
00119 closedir (DIR *dirp)
00120 {
00121   free_dircontents (dirp->dd_contents);
00122   free ((char *) dirp);
00123 }
00124 
00125 
00126 struct direct *
00127 readdir (DIR *dirp)
00128 {
00129   static struct direct dp;
00130 
00131   if (dirp->dd_cp == (struct _dircontents *)0)
00132     return (struct direct *)0;
00133   dp.d_namlen = dp.d_reclen =
00134     strlen (strcpy (dp.d_name, dirp->dd_cp->_d_entry));
00135 #if 0 /* JB */
00136   strlwr (dp.d_name);           /* JF */
00137 #endif
00138   dp.d_ino = 0;
00139   dirp->dd_cp = dirp->dd_cp->_d_next;
00140   dirp->dd_loc++;
00141 
00142   return &dp;
00143 }
00144 
00145 
00146 void
00147 seekdir (DIR *dirp, long off)
00148 {
00149   long i = off;
00150   struct _dircontents *dp;
00151 
00152   if (off < 0)
00153     return;
00154   for (dp = dirp->dd_contents; --i >= 0 && dp; dp = dp->_d_next)
00155     ;
00156   dirp->dd_loc = off - (i + 1);
00157   dirp->dd_cp = dp;
00158 }
00159 
00160 
00161 long
00162 telldir (DIR *dirp)
00163 {
00164   return dirp->dd_loc;
00165 }
00166 
00167 
00168 /* Garbage collection */
00169 
00170 static void
00171 free_dircontents (struct _dircontents *dp)
00172 {
00173   struct _dircontents *odp;
00174 
00175   while (dp)
00176     {
00177       if (dp->_d_entry)
00178         free (dp->_d_entry);
00179       dp = (odp = dp)->_d_next;
00180       free (odp);
00181     }
00182 }
00183 
00184 
00185 #ifdef TEST
00186 
00187 void main (int argc, char *argv[]);
00188 
00189 void
00190 main (int argc, char *argv[])
00191 {
00192   static DIR *directory;
00193   struct direct *entry = (struct direct *)0;
00194 
00195   char *name = "";
00196 
00197   if (argc > 1)
00198     name = argv[1];
00199 
00200   directory = opendir (name);
00201 
00202   if (!directory)
00203     {
00204       fprintf (stderr, "can't open directory `%s'.\n", name);
00205       exit (2);
00206     }
00207 
00208   while (entry = readdir (directory))
00209     printf ("> %s\n", entry->d_name);
00210 
00211   printf ("done.\n");
00212 }
00213 
00214 #endif /* TEST */
00215 
00216 /* 
00217  * Local Variables:
00218  * mode:C
00219  * ChangeLog:ChangeLog
00220  * compile-command:make
00221  * End:
00222  */

Generated on Tue Feb 8 04:08:02 2005 for E32frodo by doxygen 1.3.3