home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES1.ZIP / LIB / ndiros2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-11  |  8.1 KB  |  249 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       n d i r o s 2 . c                                            */
  3. /*                                                                    */
  4. /*       UUPC/extended OS/2 directory search functions                */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: ndiros2.c 1.7 1993/10/03 20:37:34 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: ndiros2.c $
  24.  *     Revision 1.7  1993/10/03  20:37:34  ahd
  25.  *     Delete debugging message of returned value to make consistent with
  26.  *     other environment messages from ndir()
  27.  *
  28.  *     Revision 1.7  1993/10/03  20:37:34  ahd
  29.  *     Delete debugging message of returned value to make consistent with
  30.  *     other environment messages from ndir()
  31.  *
  32.  *     Revision 1.6  1993/09/20  04:39:51  ahd
  33.  *     OS/2 2.x support
  34.  *
  35.  *     Revision 1.5  1993/07/20  21:45:37  ahd
  36.  *     Clean up header
  37.  *
  38.  *     Revision 1.4  1993/04/05  12:26:01  ahd
  39.  *     Drop RCSID
  40.  *
  41.  *     Revision 1.3  1993/04/05  04:32:19  ahd
  42.  *     Add timestamp, size to information returned by directory searches
  43.  *
  44.  *     Revision 1.2  1993/04/04  19:35:14  ahd
  45.  *     Include time_t timestamp
  46.  *
  47.  */
  48.  
  49. /*--------------------------------------------------------------------*/
  50. /*    ndir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87      */
  51. /*    ndir.c for MS-OS2 by Drew Derbyshire (help@kendra.kew.com>,     */
  52. /*           April/91                                                 */
  53. /*                                                                    */
  54. /*         Berkeley-style directory reading routine on MS-OS2         */
  55. /*--------------------------------------------------------------------*/
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                        System include files                        */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. #include <stdio.h>
  62. #include <ctype.h>
  63. #include <string.h>
  64. #include <stdlib.h>
  65. #include <assert.h>
  66. #include <time.h>
  67.  
  68. /*--------------------------------------------------------------------*/
  69. /*                         OS/2 include files                         */
  70. /*--------------------------------------------------------------------*/
  71.  
  72. #define INCL_BASE
  73. #include <os2.h>
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*                    UUPC/extended include files                     */
  77. /*--------------------------------------------------------------------*/
  78.  
  79. #define FAMILY_API
  80.  
  81. #include "lib.h"
  82. #include "uundir.h"
  83. #include "dos2unix.h"
  84.  
  85. static HDIR dir_handle;
  86. static char *pathname = NULL;
  87.  
  88. #if defined(__OS2__)
  89.  
  90. #define FINDFIRST_LEVEL (FIL_STANDARD)    /* OS/2 2.x requires different
  91.                                              parameters to find first
  92.                                              file */
  93.  
  94. static FILEFINDBUF3 findbuf;
  95.  
  96. #else
  97.  
  98. #define FINDFIRST_LEVEL (0L)
  99. static FILEFINDBUF findbuf;
  100.  
  101. #endif
  102.  
  103. currentfile();
  104.  
  105. /*--------------------------------------------------------------------*/
  106. /*    o p e n d i r                                                   */
  107. /*                                                                    */
  108. /*    Open a directory                                                */
  109. /*--------------------------------------------------------------------*/
  110.  
  111. extern DIR *opendirx( const char *dirname, char *pattern)
  112. {
  113.  
  114.    DIR *dirp;
  115.  
  116. #ifdef __OS2__
  117.    APIRET rc;
  118.    ULONG count = 1L;
  119. #else
  120.    USHORT rc;
  121.    USHORT count = 1;
  122. #endif
  123.  
  124.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  125.    strcpy(pathname, dirname);
  126.  
  127.    if ((*pattern != '/') && (dirname[ strlen(dirname) - 1] != '/'))
  128.       strcat(pathname,"/");
  129.    strcat(pathname,pattern);
  130.    printmsg(5,"opendir: Opening directory %s", pathname );
  131.  
  132. /*--------------------------------------------------------------------*/
  133. /*                Read the first file in the directory                */
  134. /*--------------------------------------------------------------------*/
  135.  
  136.    dir_handle = HDIR_CREATE;
  137.    rc = DosFindFirst( pathname,
  138.             &dir_handle,
  139.             FILE_NORMAL,
  140.             &findbuf,
  141.             sizeof( findbuf ),
  142.             &count,
  143.             FINDFIRST_LEVEL );
  144.  
  145. /*--------------------------------------------------------------------*/
  146. /*            Process the return code from the first file             */
  147. /*--------------------------------------------------------------------*/
  148.  
  149.    if ( rc == 0 )
  150.    {
  151.       dirp = malloc( sizeof( DIR ));
  152.       dirp->dirfirst = 1;
  153.       strcpy(dirp->dirid, "DIR");
  154.       return dirp;
  155.    }
  156.    else {
  157.       if (( rc != ERROR_NO_MORE_FILES ) &&
  158.           ( rc != ERROR_PATH_NOT_FOUND))
  159.          printmsg(4,"opendir: Error %d on directory %s",
  160.                   (int) rc, pathname );
  161.       return NULL;
  162.    } /* else */
  163.  
  164. } /*opendir*/
  165.  
  166. /*--------------------------------------------------------------------*/
  167. /*    r e a d d i r                                                   */
  168. /*                                                                    */
  169. /*    Get next entry in a directory                                   */
  170. /*--------------------------------------------------------------------*/
  171.  
  172. struct direct *readdir(DIR *dirp)
  173. {
  174.  
  175. #if defined(__OS2__)
  176.    ULONG count = 1L;
  177.    APIRET rc = 0;
  178. #else
  179.    USHORT count = 1;
  180.    USHORT rc = 0;
  181. #endif
  182.  
  183.    if ( ! equal(dirp->dirid, "DIR" ))
  184.    {
  185.       printmsg(0,"readdir: No directory open to read");
  186.       panic();
  187.    }
  188.  
  189.    if (dirp->dirfirst)
  190.    {
  191.       printmsg(5,"readdir: Opening directory %s", pathname );
  192.       dirp->dirfirst = 0;
  193.    }
  194.    else
  195.       rc = DosFindNext( dir_handle,
  196.                &findbuf,
  197.                sizeof( findbuf ) ,
  198.                &count );
  199.  
  200.    if ( rc == 0 )
  201.    {
  202.       dirp->dirent.d_ino = -1;   /* no inode information */
  203.       strlwr(strcpy(dirp->dirent.d_name, findbuf.achName ));
  204.       dirp->dirent.d_namlen = findbuf.cchName;
  205.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  206.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  207.       dirp->dirent.d_modified = dos2unix( findbuf.fdateLastWrite,
  208.                                          findbuf.ftimeLastWrite );
  209.       dirp->dirent.d_size     = findbuf.cbFile;
  210.  
  211.       return &(dirp->dirent);
  212.    }
  213.    else {
  214.       if ( rc != ERROR_NO_MORE_FILES )
  215.          printmsg(0,"readdir: Error %d on directory %s",
  216.                   (int) rc, pathname );
  217.       return NULL;
  218.    } /* else */
  219.  
  220. } /*readdir*/
  221.  
  222. /*--------------------------------------------------------------------*/
  223. /*    c l o s e d i r                                                 */
  224. /*                                                                    */
  225. /*    Close a directory                                               */
  226. /*--------------------------------------------------------------------*/
  227.  
  228. void closedir(DIR *dirp)
  229. {
  230.    USHORT rc;
  231.  
  232.    if ( ! equal(dirp->dirid, "DIR" ))
  233.    {
  234.       printmsg(0,"closedir: No directory open");
  235.       panic();
  236.    }
  237.  
  238.    printmsg(5,"closedir: Closing directory %s", pathname );
  239.    rc = DosFindClose( dir_handle );
  240.    if ( rc != 0 )
  241.      printmsg(0,"closedir: Error %d on directory %s",
  242.               (int) rc, pathname );
  243.    free( dirp );
  244.    dirp = NULL;
  245.    free( pathname );
  246.    pathname = NULL;
  247.  
  248. } /*closedir*/
  249.