"Fossies" - the Fresh Open Source Software Archive

Member "cvs-1.11.23/windows-NT/ndir.c" (7 May 2008, 4228 Bytes) of package /linux/misc/old/cvs-1.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ndir.c" see the Fossies "Dox" file reference documentation.

    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 /* Everything non trivial in this code is from: @(#)msd_dir.c 1.4
   15    87/11/06.  A public domain implementation of BSD directory routines
   16    for MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
   17    August 1897 */
   18 
   19 
   20 #include <io.h>
   21 #include <stdio.h>
   22 #include <stdlib.h>
   23 #include <string.h>
   24 #include <sys/types.h>
   25 #include <sys/stat.h>
   26 
   27 #include <dos.h>
   28 
   29 #include <ndir.h>
   30 extern void *xmalloc (size_t bytes);
   31 
   32 static void free_dircontents (struct _dircontents *);
   33 
   34 /* find ALL files! */
   35 #define ATTRIBUTES  (_A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR)
   36 
   37 
   38 
   39 DIR *
   40 opendir (const char *name)
   41 {
   42   struct _finddata_t find_buf;
   43   DIR *dirp;
   44   struct _dircontents *dp;
   45   char name_buf[_MAX_PATH + 1];
   46   char *slash = "";
   47   long hFile;
   48 
   49   if (!name)
   50     name = "";
   51   else if (*name)
   52     {
   53       const char *s;
   54       int l = strlen (name);
   55 
   56       s = name + l - 1;
   57       if ( !(l == 2 && *s == ':') && *s != '\\' && *s != '/')
   58     slash = "/";    /* save to insert slash between path and "*.*" */
   59     }
   60 
   61   strcat (strcat (strcpy (name_buf, name), slash), "*.*");
   62 
   63   dirp = (DIR *) xmalloc (sizeof (DIR));
   64   if (dirp == (DIR *)0)
   65     return (DIR *)0;
   66 
   67   dirp->dd_loc = 0;
   68   dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) 0;
   69 
   70   if ((hFile = _findfirst (name_buf, &find_buf)) < 0)
   71     {
   72       free (dirp);
   73       return (DIR *)0;
   74     }
   75 
   76   do
   77     {
   78       dp = (struct _dircontents *) xmalloc (sizeof (struct _dircontents));
   79       if (dp == (struct _dircontents *)0)
   80     {
   81       free_dircontents (dirp->dd_contents);
   82       return (DIR *)0;
   83     }
   84 
   85       dp->_d_entry = xmalloc (strlen (find_buf.name) + 1);
   86       if (dp->_d_entry == (char *)0)
   87     {
   88       free (dp);
   89       free_dircontents (dirp->dd_contents);
   90       return (DIR *)0;
   91     }
   92 
   93       if (dirp->dd_contents)
   94     dirp->dd_cp = dirp->dd_cp->_d_next = dp;
   95       else
   96     dirp->dd_contents = dirp->dd_cp = dp;
   97 
   98       strcpy (dp->_d_entry, find_buf.name);
   99 
  100       dp->_d_next = (struct _dircontents *)0;
  101 
  102     } while (!_findnext (hFile, &find_buf));
  103 
  104   dirp->dd_cp = dirp->dd_contents;
  105 
  106   _findclose(hFile);
  107 
  108   return dirp;
  109 }
  110 
  111 
  112 void
  113 closedir (DIR *dirp)
  114 {
  115   free_dircontents (dirp->dd_contents);
  116   free ((char *) dirp);
  117 }
  118 
  119 
  120 struct direct *
  121 readdir (DIR *dirp)
  122 {
  123   static struct direct dp;
  124 
  125   if (dirp->dd_cp == (struct _dircontents *)0)
  126     return (struct direct *)0;
  127   dp.d_namlen = dp.d_reclen =
  128     strlen (strcpy (dp.d_name, dirp->dd_cp->_d_entry));
  129 #if 0 /* JB */
  130   strlwr (dp.d_name);       /* JF */
  131 #endif
  132   dp.d_ino = 0;
  133   dirp->dd_cp = dirp->dd_cp->_d_next;
  134   dirp->dd_loc++;
  135 
  136   return &dp;
  137 }
  138 
  139 
  140 void
  141 seekdir (DIR *dirp, long off)
  142 {
  143   long i = off;
  144   struct _dircontents *dp;
  145 
  146   if (off < 0)
  147     return;
  148   for (dp = dirp->dd_contents; --i >= 0 && dp; dp = dp->_d_next)
  149     ;
  150   dirp->dd_loc = off - (i + 1);
  151   dirp->dd_cp = dp;
  152 }
  153 
  154 
  155 long
  156 telldir (DIR *dirp)
  157 {
  158   return dirp->dd_loc;
  159 }
  160 
  161 
  162 /* Garbage collection */
  163 
  164 static void
  165 free_dircontents (struct _dircontents *dp)
  166 {
  167   struct _dircontents *odp;
  168 
  169   while (dp)
  170     {
  171       if (dp->_d_entry)
  172     free (dp->_d_entry);
  173       dp = (odp = dp)->_d_next;
  174       free (odp);
  175     }
  176 }
  177 
  178 
  179 #ifdef TEST
  180 
  181 void main (int argc, char *argv[]);
  182 
  183 void
  184 main (int argc, char *argv[])
  185 {
  186   static DIR *directory;
  187   struct direct *entry = (struct direct *)0;
  188 
  189   char *name = "";
  190 
  191   if (argc > 1)
  192     name = argv[1];
  193 
  194   directory = opendir (name);
  195 
  196   if (!directory)
  197     {
  198       fprintf (stderr, "can't open directory `%s'.\n", name);
  199       exit (2);
  200     }
  201 
  202   while (entry = readdir (directory))
  203     printf ("> %s\n", entry->d_name);
  204 
  205   printf ("done.\n");
  206 }
  207 
  208 #endif /* TEST */
  209 
  210 /* 
  211  * Local Variables:
  212  * mode:C
  213  * ChangeLog:ChangeLog
  214  * compile-command:make
  215  * End:
  216  */