home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / scandir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  2.1 KB  |  105 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)scandir.c    1.3    (Berkeley) 6/26/87";
  3. #endif
  4.  
  5. #include "common.h"
  6.  
  7. /*
  8.  * scan_dir -- scan the current directory for news articles,
  9.  *    loading the article numbers into art_array.  Return
  10.  *    number of articles loaded.
  11.  *
  12.  *    Parameters:    "low_msg", "high_msg" are the low
  13.  *            and high messages numbers in this
  14.  *            group; we ignore numbers outside this
  15.  *            range.
  16.  *
  17.  *    Returns:    Number of articles loaded into
  18.  *            array.
  19.  *
  20.  *    Side effects:    Changes "art_array".
  21.  */
  22.  
  23. extern    int    intcmp();
  24. extern char *malloc(), *realloc();
  25.  
  26. scan_dir(low_msg, high_msg)
  27. int    low_msg, high_msg;
  28. {
  29.     register struct direct    *dirent;
  30.     register DIR        *dirp;
  31.     int            artnum;
  32.  
  33.     num_arts = 0;
  34.  
  35.     dirp = opendir(".");
  36.  
  37.     if (dirp == NULL)
  38.         return (0);
  39.  
  40.     while ((dirent = readdir(dirp)) != NULL) {
  41.         artnum = atoi(dirent->d_name);
  42. #ifdef DYNAMIC_ART_ARRAY
  43.         if (artnum == 0 || artnum < low_msg || artnum > high_msg)
  44.             continue;
  45.  
  46.         ++num_arts;
  47.         /* Expand/allocate art_array elements as necessary */
  48.         if (num_arts >= size_art_array) {
  49.             size_art_array += 1024;
  50.             if (art_array) {
  51. #ifdef SYSLOG
  52.                 syslog(LOG_INFO,
  53.                     "increasing art_array to %d elements",
  54.                     size_art_array);
  55. #endif
  56.                 art_array = (int *)realloc(art_array,
  57.                     size_art_array * sizeof(*art_array));
  58.             } else
  59.                 art_array = (int *)
  60.                     malloc(size_art_array * sizeof(*art_array));
  61.             if (art_array == 0) {
  62. #ifdef SYSLOG
  63.                 syslog(LOG_ERR,
  64.                     "scan_dir(): malloc/realloc failed");
  65. #endif
  66.                 num_arts = 0;
  67.                 size_art_array = 0;
  68.                 size_art_array = 0;
  69.                 closedir(dirp);
  70.                 return(0);
  71.             }
  72.         }
  73.         art_array[num_arts] = artnum;
  74. #else
  75.         if (artnum != 0 && artnum >= low_msg && artnum <= high_msg)
  76.             art_array[num_arts++] = artnum;
  77. #endif
  78.  
  79.     }
  80.     closedir(dirp);
  81.  
  82.     qsort((char *) art_array, num_arts, sizeof(int), intcmp);
  83.  
  84.     return (num_arts);
  85. }
  86.  
  87.  
  88. /*
  89.  * intcmp -- compare to integers.
  90.  *
  91.  *    Parameters:    "x", "y" point to the integers to be compared.
  92.  *
  93.  *    Returns:    -1 if "x" is less than "y",
  94.  *            0 if "x" equals "y", and
  95.  *            1 if "x" is greater than "y".
  96.  *
  97.  *    Side effects:    None.
  98.  */
  99.  
  100. intcmp(x, y)
  101. register int    *x, *y;
  102. {
  103.     return (*x - *y);
  104. }
  105.