home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / MAK358AS.ZIP / MSD_DIR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  4.8 KB  |  233 lines

  1. /*  msd_dir.c - portable directory routines
  2.     Copyright (C) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  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.     IMPORTANT:
  19.  
  20.     This code is not an official part of the GNU project and the
  21.     author is not affiliated to the Free Software Foundation.
  22.     He just likes their code and spirit.
  23.  
  24.     $Header: e:/gnu/make/RCS/msd_dir.c'v 2.0 90/06/29 00:35:36 tho Stable $
  25.  */
  26.  
  27. /* Everything non trivial in this code is from: @(#)msd_dir.c 1.4
  28.    87/11/06.  A public domain implementation of BSD directory routines
  29.    for MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
  30.    August 1897 */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38.  
  39. #include <dos.h>
  40.  
  41. #include "msd_dir.h"
  42.  
  43. static void free_dircontents (struct _dircontents *);
  44.  
  45. /* find ALL files! */
  46. #define ATTRIBUTES    (_A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR)
  47.  
  48.  
  49.  
  50. DIR *
  51. opendir (char *name)
  52. {
  53.   struct find_t find_buf;
  54.   DIR *dirp;
  55.   struct _dircontents *dp;
  56.   char name_buf[_MAX_PATH + 1];
  57.   char *slash = "";
  58.  
  59.   if (!name)
  60.     name = "";
  61.   else if (*name)
  62.     {
  63.       char *s;
  64.       int l = strlen (name);
  65.  
  66.       s = name + l - 1;
  67.       if ( !(l == 2 && *s == ':') && *s != '\\' && *s != '/')
  68.     slash = "/";    /* save to insert slash between path and "*.*" */
  69.     }
  70.  
  71.   strcat (strcat (strcpy (name_buf, name), slash), "*.*");
  72.  
  73.   dirp = (DIR *) malloc (sizeof (DIR));
  74.   if (dirp == (DIR *)0)
  75.     return (DIR *)0;
  76.  
  77.   dirp->dd_loc = 0;
  78.   dirp->dd_contents = dirp->dd_cp = (struct _dircontents *) 0;
  79.  
  80.   if (_dos_findfirst (name_buf, ATTRIBUTES, &find_buf))
  81.     {
  82.       free (dirp);
  83.       return (DIR *)0;
  84.     }
  85.  
  86.   do
  87.     {
  88.       dp = (struct _dircontents *) malloc (sizeof (struct _dircontents));
  89.       if (dp == (struct _dircontents *)0)
  90.     {
  91.       free_dircontents (dirp->dd_contents);
  92.       return (DIR *)0;
  93.     }
  94.  
  95.       dp->_d_entry = malloc (strlen (find_buf.name) + 1);
  96.       if (dp->_d_entry == (char *)0)
  97.     {
  98.       free (dp);
  99.       free_dircontents (dirp->dd_contents);
  100.       return (DIR *)0;
  101.     }
  102.  
  103.       if (dirp->dd_contents)
  104.     dirp->dd_cp = dirp->dd_cp->_d_next = dp;
  105.       else
  106.     dirp->dd_contents = dirp->dd_cp = dp;
  107.  
  108.       strcpy (dp->_d_entry, find_buf.name);
  109.  
  110.       dp->_d_next = (struct _dircontents *)0;
  111.  
  112.     } while (! _dos_findnext (&find_buf));
  113.  
  114.   dirp->dd_cp = dirp->dd_contents;
  115.  
  116.   return dirp;
  117. }
  118.  
  119.  
  120. void
  121. closedir (DIR *dirp)
  122. {
  123.   free_dircontents (dirp->dd_contents);
  124.   free ((char *) dirp);
  125. }
  126.  
  127.  
  128. struct direct *
  129. readdir (DIR *dirp)
  130. {
  131.   static struct direct dp;
  132.  
  133.   if (dirp->dd_cp == (struct _dircontents *)0)
  134.     return (struct direct *)0;
  135.   dp.d_namlen = dp.d_reclen =
  136.     strlen (strcpy (dp.d_name, dirp->dd_cp->_d_entry));
  137.   strlwr (dp.d_name);        /* JF */
  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. /* 
  218.  * Local Variables:
  219.  * mode:C
  220.  * ChangeLog:ChangeLog
  221.  * compile-command:make
  222.  * End:
  223.  */
  224.  
  225.  
  226. /* 
  227.  * Local Variables:
  228.  * mode:C
  229.  * ChangeLog:ChangeLog
  230.  * compile-command:make
  231.  * End:
  232.  */
  233.