home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dirfvac4.zip / direct.c next >
C/C++ Source or Header  |  1999-02-25  |  9KB  |  272 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. #define INCL_DOSERRORS
  12.  
  13. #include <os2.h>
  14.  
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20.  
  21. #define DIRMAGIC 0xddaa
  22.  
  23. #define SEARCH_ATTR (FILE_HIDDEN|FILE_SYSTEM|FILE_DIRECTORY)
  24.  
  25. /*----------------------------------------------------------------------
  26. Name            opendir - open a directory stream
  27.  
  28. Usage           #include <dirent.h>
  29.                 DIR *opendir(const char *dirname);
  30.  
  31. Related
  32. functions usage struct dirent *readdir(DIR *dirp);
  33.                 void rewinddir(DIR *dirp);
  34.                 int closedir(DIR *dirp);
  35.  
  36. Prototype in    dirent.h
  37.  
  38. Description     The opendir() function opens a directory stream for reading
  39.                 The name of the directory to read is dirname.  The stream
  40.                 is set to read the first entry in the directory.
  41.  
  42. Return value    On a successful open, opendir() returns a pointer to
  43.                 an object of type DIR.  On an error, opendir() returns
  44.                 NULL and sets errno as follows:
  45.  
  46.                 ENOENT  The directory does not exist.
  47.                 ENOMEM  Not enough memory to allocate a DIR object.
  48.  
  49. *---------------------------------------------------------------------*/
  50.  
  51. DIR * opendir(const char *dirname)
  52. {
  53.     char *name;
  54.     int len;
  55.     DIR *dir;
  56.     ULONG nfiles;
  57.     APIRET apiret;
  58.  
  59.     /* Allocate space for a copy of the directory name, plus
  60.      * room for the "*.*" we will concatenate to the end.
  61.      */
  62.     len = strlen(dirname);
  63.     if ((name = (char*)malloc(len+5)) == NULL)
  64.     {
  65.         errno = ENOMEM;
  66.         return (NULL);
  67.     }
  68.     strcpy(name,dirname);
  69.     if (len-- && name[len] != ':' && name[len] != '\\' && name[len] != '/')
  70.         strcat(name,"\\*.*");
  71.     else
  72.         strcat(name,"*.*");
  73.  
  74.     /* Allocate space for a DIR structure.
  75.      */
  76.     if ((dir = (DIR*)malloc(sizeof(DIR))) == NULL)
  77.     {
  78.         errno = ENOMEM;
  79.         free(name);
  80.         return (NULL);
  81.     }
  82.  
  83.     /* Search for the first few files to see if the directory exists,
  84.      * and to obtain directory handle for future DosFindNext() calls.
  85.      */
  86.     dir->_d_hdir = HDIR_CREATE;
  87.     nfiles = 256;
  88.     if ((apiret = DosFindFirst((PSZ)name, &dir->_d_hdir, SEARCH_ATTR,
  89.             (PVOID)&dir->_d_buf[0], sizeof(dir->_d_buf),
  90.             &nfiles,FIL_STANDARD)) != 0)
  91.     {
  92.         free(name);
  93.         free(dir);
  94.         /* set errno */
  95.         if ( apiret == ERROR_PATH_NOT_FOUND || apiret == ERROR_FILE_NOT_FOUND )
  96.             errno = ENOENT;
  97.         else
  98.             errno = EOS2ERR;
  99.         _doserrno = (int)apiret;
  100.         return (NULL);
  101.     }
  102.  
  103.     /* Everything is OK.  Save information in the DIR structure, return it.
  104.      */
  105.     dir->_d_dirname = name;
  106.     dir->_d_magic = DIRMAGIC;
  107.     dir->_d_nfiles = (unsigned)nfiles;
  108.     dir->_d_bufp = &dir->_d_buf[0];
  109.     return dir;
  110. }
  111.  
  112. /*----------------------------------------------------------------------
  113. Name            rewinddir - rewind a directory stream
  114.  
  115. Usage           #include <dirent.h>
  116.                 void rewinddir(DIR *dirp);
  117.  
  118. Related
  119. functions usage struct dirent *readdir(DIR *dirp);
  120.                 DIR *opendir(const char *dirname);
  121.                 int closedir(DIR *dirp);
  122.  
  123. Prototype in    dirent.h
  124.  
  125. Description     The rewinddir() function resets the directory stream dirp to the
  126.                 first entry in the directory.
  127.  
  128. Return value    The rewinddir() function does not return a value.
  129.  
  130. *---------------------------------------------------------------------*/
  131.  
  132. void rewinddir(DIR *dir)
  133. {
  134.     ULONG nfiles;
  135.  
  136.     /* Verify the handle.
  137.      */
  138.     if (dir->_d_magic != DIRMAGIC)
  139.             return;
  140.  
  141.     /* Search for the first few files.
  142.      */
  143.     nfiles = 256;
  144.     if (DosFindFirst((PSZ)dir->_d_dirname, &dir->_d_hdir, SEARCH_ATTR,
  145.             (PVOID)&dir->_d_buf[0], sizeof(dir->_d_buf),
  146.             &nfiles, FIL_STANDARD) == 0)
  147.     {
  148.         dir->_d_nfiles = (unsigned)nfiles;
  149.         dir->_d_bufp = &dir->_d_buf[0];
  150.     }
  151. }
  152.  
  153.  
  154. /*----------------------------------------------------------------------
  155. Name            readdir - read directory entry from a directory stream
  156.  
  157. Usage           #include <dirent.h>
  158.                 struct dirent *readdir(DIR *dirp);
  159. Related
  160. functions usage void rewinddir(DIR *dirp);
  161.                 DIR *opendir(const char *dirname);
  162.                 int closedir(DIR *dirp);
  163.  
  164. Prototype in    dirent.h
  165.  
  166. Description     The readdir() function reads the directory entry at the
  167.                 current position in the directory stream dirp, and
  168.                 advances the directory stream position to the next entry.
  169.                 The directory entry is an object of type 'struct dirent'
  170.                 that contains the member
  171.                         char d_name[]
  172.                 which is an array of characters containing the null-terminated
  173.                 filename.
  174.  
  175.                 The readdir() function reads directory entries for all files,
  176.                 including directories.  On DOS, it also reads directory
  177.                 entries for system and hidden files.  It does not read
  178.                 volume labels or unused directory entries.
  179.  
  180. Return value    On a successful read, readdir() returns a pointer
  181.                 to an object of type 'struct direct'.  This structure
  182.                 will be overwritten by subsequent operations on the
  183.                 same directory stream.  It will not be overwritten
  184.                 by operations on other directory streams.
  185.  
  186.                 When the end of the directory is reached, readdir()
  187.                 returns NULL but does not set errno.
  188.  
  189.                 On an error, readdir() returns NULL and sets errno:
  190.  
  191.                 EBADF   The dirp parameter does not point to a valid
  192.                         open directory stream.
  193.                 
  194. *---------------------------------------------------------------------*/
  195.  
  196. struct dirent * readdir(DIR *dir)
  197. {
  198.     ULONG nfiles;
  199.     FILEFINDBUF3 *ff;
  200.  
  201.     /* Verify the handle.
  202.      */
  203.     if (dir->_d_magic != DIRMAGIC)
  204.     {
  205.         errno = EBADF;
  206.         return (NULL);
  207.     }
  208.  
  209.     /* If all files in the buffer have been returned, find some more files.
  210.      */
  211.     if (dir->_d_nfiles == 0)
  212.     {
  213.         nfiles = 256;
  214.         if (DosFindNext(dir->_d_hdir,(PFILEFINDBUF)&dir->_d_buf[0],
  215.                 sizeof(dir->_d_buf), &nfiles) != 0)
  216.             return (NULL);
  217.         dir->_d_nfiles = (unsigned)nfiles;
  218.         dir->_d_bufp = &dir->_d_buf[0];
  219.     }
  220.  
  221.     /* Return the filename of the current file in the buffer,
  222.      * advance the buffer pointer.
  223.      */
  224.     ff = (FILEFINDBUF3 *)(dir->_d_bufp);
  225.     dir->_d_bufp += (int)ff->oNextEntryOffset;
  226.     dir->_d_nfiles--;
  227.  
  228.     return ((struct dirent *)&ff->achName[0]);
  229. }
  230.  
  231. /*----------------------------------------------------------------------
  232. Name            closedir - close directory stream
  233.  
  234. Usage           #include <dirent.h>
  235.                 int closedir(DIR *dirp);
  236.  
  237. Related
  238. functions usage void rewinddir(DIR *dirp);
  239.                 struct dirent *readdir(DIR *dirp);
  240.                 DIR *opendir(const char *dirname);
  241.  
  242. Prototype in    dirent.h
  243.  
  244. Description     The closedir() function closes the directory stream dirp.
  245.                 Subsequently, dirp will not refer to a valid directory
  246.                 stream.
  247.  
  248. Return value    On a successful close, closedir() returns 0.
  249.                 On an error, closedir() returns -1 and sets errno:
  250.  
  251.                 EBADF   The dirp parameter does not point to a valid
  252.                         open directory stream.
  253.                 
  254. *---------------------------------------------------------------------*/
  255.  
  256. int closedir(DIR *dir)
  257. {
  258.     /* Verify the handle.
  259.      */
  260.     if (dir == NULL || dir->_d_magic != DIRMAGIC)
  261.     {
  262.         errno = EBADF;
  263.         return (-1);
  264.     }
  265.  
  266.     dir->_d_magic = 0;          /* prevent accidental use after closing */
  267.     DosFindClose(dir->_d_hdir); /* close directory handle */
  268.     free(dir->_d_dirname);      /* free directory name */
  269.     free(dir);                  /* free directory structure */
  270.     return 0;
  271. }
  272.