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

  1. /*--------------------------------------------------------------------*/
  2. /*       n d i r w i n . c                                            */
  3. /*                                                                    */
  4. /*       UUPC/extended directory search routine for MS-Windows 3.x    */
  5. /*       environment.                                                 */
  6. /*--------------------------------------------------------------------*/
  7.  
  8. /*--------------------------------------------------------------------*/
  9. /*       Changes Copyright (c) 1993 by Robert Denny                   */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*--------------------------------------------------------------------*/
  13. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  14. /*       Wonderworks.                                                 */
  15. /*                                                                    */
  16. /*       All rights reserved except those explicitly granted by       */
  17. /*       the UUPC/extended license agreement.                         */
  18. /*--------------------------------------------------------------------*/
  19.  
  20. /*--------------------------------------------------------------------*/
  21. /*                          RCS Information                           */
  22. /*--------------------------------------------------------------------*/
  23.  
  24. /*
  25.  *    $Id: ndirwin.c 1.2 1993/07/31 16:22:16 ahd Exp $
  26.  *
  27.  *    Revision history:
  28.  *    $Log: ndirwin.c $
  29.  * Revision 1.2  1993/07/31  16:22:16  ahd
  30.  * Changes in support of Robert Denny's Windows 3.x support
  31.  *
  32.  * Revision 1.2  1993/07/31  16:22:16  ahd
  33.  * Changes in support of Robert Denny's Windows 3.x support
  34.  *
  35.  * Revision 1.1  1993/07/22  23:19:50  ahd
  36.  * Initial revision
  37.  *
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <ctype.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <dir.h>
  45. #include <dos.h>
  46. #include <errno.h>
  47. #include <time.h>
  48.  
  49. /*--------------------------------------------------------------------*/
  50. /*                    UUPC/extended include files                     */
  51. /*--------------------------------------------------------------------*/
  52.  
  53. #include "lib.h"
  54. #include "uundir.h"
  55. #include "dos2unix.h"
  56.  
  57. #define USHORT unsigned short
  58.  
  59. static char *pathname = NULL;
  60. static struct ffblk findbuf;
  61.  
  62. currentfile();
  63.  
  64. /*--------------------------------------------------------------------*/
  65. /*    o p e n d i r x                                                 */
  66. /*                                                                    */
  67. /*    Open a directory                                                */
  68. /*--------------------------------------------------------------------*/
  69.  
  70. extern DIR *opendirx( const char *dirname, char *pattern)
  71. {
  72.  
  73.    DIR *dirp;
  74.    USHORT rc;
  75.  
  76.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  77.    strcpy(pathname, dirname);
  78.  
  79.    if ((*pattern != '/') && (dirname[ strlen(dirname) - 1] != '/'))
  80.       strcat(pathname,"/");
  81.    strcat(pathname,pattern);
  82.    printmsg(5,"opendir: Opening directory %s", pathname );
  83.  
  84. /*--------------------------------------------------------------------*/
  85. /*                Read the first file in the directory                */
  86. /*--------------------------------------------------------------------*/
  87.  
  88.    rc = findfirst( pathname, &findbuf, FA_NORMAL);
  89.  
  90. /*--------------------------------------------------------------------*/
  91. /*            Process the return code from the first file             */
  92. /*--------------------------------------------------------------------*/
  93.  
  94.    if ( rc == 0 )
  95.    {
  96.       dirp = malloc( sizeof( DIR ));
  97.       dirp->dirfirst = 1;
  98.       strcpy(dirp->dirid, "DIR");
  99.       return dirp;
  100.    }
  101.    else
  102.    {
  103.           if (( rc != ENMFILE ) && ( rc != ENOENT ))
  104.          printmsg(4,"opendir: Error %d on directory %s",
  105.                   (int) rc, pathname );
  106.       return NULL;
  107.    } /* else */
  108.  
  109. } /*opendir*/
  110.  
  111. /*--------------------------------------------------------------------*/
  112. /*    r e a d d i r                                                   */
  113. /*                                                                    */
  114. /*    Get next entry in a directory                                   */
  115. /*--------------------------------------------------------------------*/
  116.  
  117. struct direct *readdir(DIR *dirp)
  118. {
  119.    USHORT rc = 0;
  120.  
  121.    if ( ! equal(dirp->dirid, "DIR" ))
  122.    {
  123.       printmsg(0,"readdir: No directory open to read");
  124.       panic();
  125.    }
  126.  
  127.    if (dirp->dirfirst)
  128.    {
  129.       printmsg(5,"readdir: Opening directory %s", pathname );
  130.       dirp->dirfirst = 0;
  131.    }
  132.    else
  133.           rc = findnext(&findbuf);
  134.  
  135.    if ( rc == 0 )
  136.    {
  137.       dirp->dirent.d_ino = -1;   /* no inode information */
  138.       strlwr(strcpy(dirp->dirent.d_name, findbuf.ff_name ));
  139.           dirp->dirent.d_namlen = strlen(findbuf.ff_name);
  140.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  141.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  142.  
  143.       dirp->dirent.d_modified = dos2unix( *((FDATE *) &findbuf.ff_fdate),
  144.                                           *((FTIME *) &findbuf.ff_ftime));
  145.  
  146.       printmsg(4,"readdir: Returning \"%s\"", dirp->dirent.d_name);
  147.       return &(dirp->dirent);
  148.    }
  149.    else {
  150.           if (( errno != ENMFILE ) && ( errno != ENOENT ))
  151.          printmsg(0,"readdir: Error %d on directory %s",
  152.                   (int) rc, pathname );
  153.       return NULL;
  154.    } /* else */
  155.  
  156. } /*readdir*/
  157.  
  158. /*--------------------------------------------------------------------*/
  159. /*    c l o s e d i r                                                 */
  160. /*                                                                    */
  161. /*    Close a directory                                               */
  162. /*--------------------------------------------------------------------*/
  163.  
  164. void closedir(DIR *dirp)
  165. {
  166.  
  167.    if ( ! equal(dirp->dirid, "DIR" ))
  168.    {
  169.       printmsg(0,"closedir: No directory open");
  170.       panic();
  171.    }
  172.  
  173.    printmsg(5,"closedir: Closing directory %s", pathname );
  174.    free( dirp );
  175.    dirp = NULL;
  176.    free( pathname );
  177.    pathname = NULL;
  178.  
  179. } /*closedir*/
  180.