home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / READNEXT.C < prev    next >
C/C++ Source or Header  |  1993-08-26  |  5KB  |  135 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    r e a d n e x t . c                                             */
  3. /*                                                                    */
  4. /*    Reads a spooling directory with optional pattern matching       */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1990-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: readnext.c 1.5 1993/08/26 05:00:25 ahd Exp $
  21.  *
  22.  *    $Log: readnext.c $
  23.  *     Revision 1.5  1993/08/26  05:00:25  ahd
  24.  *     Debugging code for odd failures on J. McBride's network
  25.  *
  26.  *     Revision 1.4  1993/04/05  04:32:19  ahd
  27.  *     Add timestamp, size to information returned by directory searches
  28.  *
  29.  *     Revision 1.3  1992/11/22  20:58:55  ahd
  30.  *     Use strpool to allocate const strings
  31.  *
  32.  */
  33.  
  34. /*--------------------------------------------------------------------*/
  35. /*                        System include files                        */
  36. /*--------------------------------------------------------------------*/
  37.  
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*                    UUPC/extended include files                     */
  45. /*--------------------------------------------------------------------*/
  46.  
  47. #include "lib.h"
  48. #include "readnext.h"
  49. #include "uundir.h"
  50. #include "hostable.h"
  51. #include "security.h"
  52.  
  53. currentfile();
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*    r e a d n e x t                                                 */
  57. /*                                                                    */
  58. /*    Read a directory into a linked list                             */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. char     *readnext(char *xname,
  62.                    const char *remote,
  63.                    const char *subdir,
  64.                    char *pattern,
  65.                    time_t *modified,
  66.                    long   *size )
  67. {
  68.    static DIR *dirp = NULL;
  69.    static char *SaveRemote = NULL;
  70.    static char remotedir[FILENAME_MAX];
  71.  
  72.    struct direct *dp;
  73.  
  74. /*--------------------------------------------------------------------*/
  75. /*          Determine if we must restart the directory scan           */
  76. /*--------------------------------------------------------------------*/
  77.  
  78.    if ( (remote == NULL) || ( SaveRemote == NULL ) ||
  79.         !equal(remote, SaveRemote ) )
  80.    {
  81.       if ( SaveRemote != NULL )   /* Clean up old directory? */
  82.       {                           /* Yes --> Do so           */
  83.          closedir(dirp);
  84.          dirp = NULL;
  85.          SaveRemote = NULL;
  86.       } /* if */
  87.  
  88.       if ( remote == NULL )      /* Clean up only, no new search? */
  89.          return NULL;            /* Yes --> Return to caller      */
  90.  
  91.       if ( pattern == NULL )
  92.          pattern = "*.*";
  93.  
  94.       sprintf(remotedir,"%s/%.8s/%s",E_spooldir,remote, subdir);
  95.       if ((dirp = opendirx(remotedir,pattern)) == nil(DIR))
  96.       {
  97.          printmsg(5, "readnext: couldn't opendir() %s", remotedir);
  98.          dirp = NULL;
  99.          return NULL;
  100.       } /* if */
  101.  
  102.       SaveRemote = newstr( remote );
  103.                               /* Flag we have an active search    */
  104.    } /* if */
  105.  
  106. /*--------------------------------------------------------------------*/
  107. /*              Look for the next file in the directory               */
  108. /*--------------------------------------------------------------------*/
  109.  
  110.    if ((dp = readdir(dirp)) != nil(struct direct))
  111.    {
  112.       sprintf(xname, "%s/%s", remotedir, dp->d_name);
  113.       printmsg(5, "readnext: matched \"%s\"",xname);
  114.  
  115.       if ( modified != NULL )
  116.          *modified = dp->d_modified;
  117.  
  118.       if ( size != NULL )
  119.          *size = dp->d_size;
  120.  
  121.       return xname;
  122.    }
  123.  
  124. /*--------------------------------------------------------------------*/
  125. /*     No hit; clean up after ourselves and return to the caller      */
  126. /*--------------------------------------------------------------------*/
  127.  
  128.    printmsg(5, "readnext: \"%s\" not matched", remotedir);
  129.    closedir(dirp);
  130.    SaveRemote = NULL;
  131.    dirp = NULL;
  132.    return NULL;
  133.  
  134. } /*readnext*/
  135.