home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLIBSRC3.ZIP / DIRECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  7.6 KB  |  243 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - direct.c
  3.  *
  4.  * functions:
  5.  *        diropen       - opens a directory stream
  6.  *        readdir       - read entry from directory stream
  7.  *        rewinddir     - position directory stream at first entry
  8.  *        closedir      - close directory stream
  9.  *-----------------------------------------------------------------------*/
  10.  
  11. /*
  12.  *      C/C++ Run Time Library - Version 5.0
  13.  *
  14.  *      Copyright (c) 1991, 1992 by Borland International
  15.  *      All Rights Reserved.
  16.  *
  17.  */
  18.  
  19.  
  20. #include <dirent.h>
  21. #include <dos.h>
  22. #include <errno.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #define DIRMAGIC 0xdd
  27.  
  28. /*----------------------------------------------------------------------
  29. Name            opendir - open a directory stream
  30.  
  31. Usage           #include <dirent.h>
  32.                 DIR *opendir(char *dirname);
  33.  
  34. Related
  35. functions usage struct dirent *readdir(DIR *dirp);
  36.                 void rewinddir(DIR *dirp);
  37.                 int closedir(DIR *dirp);
  38.  
  39. Prototype in    dirent.h
  40.  
  41. Description     The opendir() function opens a directory stream for reading
  42.                 The name of the directory to read is dirname.  The stream
  43.                 is set to read the first entry in the directory.
  44.  
  45. Return value    On a successful open, opendir() returns a pointer to
  46.                 an object of type DIR.  On an error, opendir() returns
  47.                 NULL and sets errno as follows:
  48.  
  49.                 ENOENT  The directory does not exist.
  50.                 ENOMEM  Not enough memory to allocate a DIR object.
  51.  
  52. *---------------------------------------------------------------------*/
  53.  
  54. DIR * _FARFUNC opendir(char *dirname)
  55. {
  56.     char *name;
  57.     DIR *dir;
  58.     int len;
  59.  
  60.     /* Allocate space for a copy of the directory name, plus
  61.      * room for the "*.*" we will concatenate to the end.
  62.      */
  63.     len = strlen(dirname);
  64.     if ((name = malloc(len+5)) == NULL)
  65.         {
  66.             errno = ENOMEM;
  67.             return (NULL);
  68.         }
  69.     strcpy(name,dirname);
  70.     if (len-- && name[len] != ':' && name[len] != '\\' && name[len] != '/')
  71.             strcat(name,"\\*.*");
  72.     else
  73.             strcat(name,"*.*");
  74.  
  75.     /* Allocate space for a DIR structure.
  76.      */
  77.     if ((dir = malloc(sizeof(DIR))) == NULL)
  78.         {
  79.             errno = ENOMEM;
  80.             free(name);
  81.             return (NULL);
  82.         }
  83.  
  84.     /* Search for the first file to see if the directory exists,
  85.      * and to set up the DTA for future _dos_findnext() calls.
  86.      */
  87.     if (_dos_findfirst(name, _A_HIDDEN|_A_SYSTEM|_A_RDONLY|_A_SUBDIR,
  88.                 (struct find_t *)&dir->_d_reserved) != 0)
  89.         {
  90.             free(name);
  91.             free(dir);
  92.             return (NULL);              /* findfirst sets errno for us */
  93.         }
  94.  
  95.     /* Everything is OK.  Save information in the DIR structure, return it.
  96.      */
  97.     dir->_d_dirname = name;
  98.     dir->_d_first = 1;
  99.     dir->_d_magic = DIRMAGIC;
  100.     return dir;
  101. }
  102.  
  103. /*----------------------------------------------------------------------
  104. Name            rewinddir - rewind a directory stream
  105.  
  106. Usage           #include <dirent.h>
  107.                 void rewinddir(DIR *dirp);
  108.  
  109. Related
  110. functions usage struct dirent *readdir(DIR *dirp);
  111.                 DIR *opendir(char *dirname);
  112.                 int closedir(DIR *dirp);
  113.  
  114. Prototype in    dirent.h
  115.  
  116. Description     The rewinddir() function resets the directory stream dirp to the
  117.                 first entry in the directory.
  118.  
  119. Return value    The rewinddir() function does not return a value.
  120.  
  121. *---------------------------------------------------------------------*/
  122.  
  123. void _FARFUNC rewinddir(DIR *dir)
  124. {
  125.     /* Verify the handle.
  126.      */
  127.     if (dir->_d_magic != DIRMAGIC)
  128.             return;
  129.  
  130.     /* Search for the first file and set up the DTA for future
  131.      * _dos_findnext() calls.
  132.      */
  133.     _dos_findfirst(dir->_d_dirname,_A_HIDDEN|_A_SYSTEM|_A_RDONLY|_A_SUBDIR,
  134.                 (struct find_t *)&dir->_d_reserved);
  135.     dir->_d_first = 1;
  136. }
  137.  
  138.  
  139. /*----------------------------------------------------------------------
  140. Name            readdir - read directory entry from a directory stream
  141.  
  142. Usage           #include <dirent.h>
  143.                 struct dirent *readdir(DIR *dirp);
  144. Related
  145. functions usage void rewinddir(DIR *dirp);
  146.                 DIR *opendir(char *dirname);
  147.                 int closedir(DIR *dirp);
  148.  
  149. Prototype in    dirent.h
  150.  
  151. Description     The readdir() function reads the directory entry at the
  152.                 current position in the directory stream dirp, and
  153.                 advances the directory stream position to the next entry.
  154.                 The directory entry is an object of type 'struct dirent'
  155.                 that contains the member
  156.                         char d_name[]
  157.                 which is an array of characters containing the null-terminated
  158.                 filename.
  159.  
  160.                 The readdir() function reads directory entries for all files,
  161.                 including directories.  On DOS, it also reads directory
  162.                 entries for system and hidden files.  It does not read
  163.                 volume labels or unused directory entries.
  164.  
  165. Return value    On a successful read, readdir() returns a pointer
  166.                 to an object of type 'struct direct'.  This structure
  167.                 will be overwritten by subsequent operations on the
  168.                 same directory stream.  It will not be overwritten
  169.                 by operations on other directory streams.
  170.  
  171.                 When the end of the directory is reached, readdir()
  172.                 returns NULL but does not set errno.
  173.  
  174.                 On an error, readdir() returns NULL and sets errno:
  175.  
  176.                 EBADF   The dirp parameter does not point to a valid
  177.                         open directory stream.
  178.  
  179. *---------------------------------------------------------------------*/
  180.  
  181. struct dirent * _FARFUNC readdir(DIR *dir)
  182. {
  183.     /* Verify the handle.
  184.      */
  185.     if (dir->_d_magic != DIRMAGIC)
  186.         {
  187.             errno = EBADF;
  188.             return (NULL);
  189.         }
  190.  
  191.     /* If this isn't the first file, call _dos_findnext() to get the next
  192.      * directory entry.  Opendir() fetches the first one.
  193.      */
  194.     if (!dir->_d_first)
  195.         {
  196.             if (_dos_findnext((struct find_t *)&dir->_d_reserved) != 0)
  197.                 return (NULL);
  198.         }
  199.     dir->_d_first = 0;
  200.     return &dir->_d_dirent;
  201. }
  202.  
  203. /*----------------------------------------------------------------------
  204. Name            closedir - close directory stream
  205.  
  206. Usage           #include <dirent.h>
  207.                 int closedir(DIR *dirp);
  208.  
  209. Related
  210. functions usage void rewinddir(DIR *dirp);
  211.                 struct dirent *readdir(DIR *dirp);
  212.                 DIR *opendir(char *dirname);
  213.  
  214. Prototype in    dirent.h
  215.  
  216. Description     The closedir() function closes the directory stream dirp.
  217.                 Subsequently, dirp will not refer to a valid directory
  218.                 stream.
  219.  
  220. Return value    On a successful close, closedir() returns 0.
  221.                 On an error, closedir() returns -1 and sets errno:
  222.  
  223.                 EBADF   The dirp parameter does not point to a valid
  224.                         open directory stream.
  225.  
  226. *---------------------------------------------------------------------*/
  227.  
  228. int _FARFUNC closedir(DIR *dir)
  229. {
  230.     /* Verify the handle.
  231.      */
  232.     if (dir == NULL || dir->_d_magic != DIRMAGIC)
  233.         {
  234.             errno = EBADF;
  235.             return (-1);
  236.         }
  237.     dir->_d_magic = 0;          /* prevent use after closing */
  238.  
  239.     free(dir->_d_dirname);
  240.     free(dir);
  241.     return 0;
  242. }
  243.