home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / zoo / part01 / addfname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-16  |  2.4 KB  |  96 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) addfname.c 1.5 87/05/03 16:00:28";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. */
  8. #include "options.h"
  9.  
  10. /* Adds a filename to global list.  (This global list will eventually
  11. be searched by the inlist() function.)  The second through fourth 
  12. parameters suppplied are stored with the name of the file and 
  13. returned by inlist. */
  14.  
  15. #include <stdio.h>
  16. #include "various.h"
  17. #include "zoo.h"
  18. #include "zoofns.h"
  19. #include "zoomem.h" /* to get LIST_SIZE */
  20.  
  21. static struct item *fentry[LIST_SIZE];
  22. static int lastname = 0;                  /* index of last name */
  23.  
  24. struct item {              /* global filename list entry */
  25.    char *fname;
  26.    long position;
  27.    unsigned int date;
  28.    unsigned int time;
  29. };
  30.  
  31. void addfname(fname,position,date,time)
  32. char *fname;
  33. long position;
  34. unsigned int date, time;
  35. {
  36.    if (lastname == 0)
  37.       fentry[lastname] = (struct item *) emalloc (sizeof(struct item));
  38.  
  39.    /* keep a few empty spaces at end to avoid off by one errors */
  40.    if (lastname >= LIST_SIZE - 3)
  41.       memerr();
  42.  
  43.    fentry[lastname]->fname = strdup(fname);
  44.    fentry[lastname]->position = position;
  45.    fentry[lastname]->date = date;
  46.    fentry[lastname]->time = time;
  47.    lastname++;
  48.    /* allocate memory for empty entry at end */
  49.    fentry[lastname] = (struct item *) emalloc (sizeof(struct item)); 
  50. } /* addfname */
  51.  
  52. /* inlist() */
  53. /* Examines global list built by addfname() to see if supplied filename
  54. is in the list.  
  55.  
  56. If found, returns the file's position within the archive as the function 
  57. value and the date and time as parameters.  If not found, returns -1.
  58.  
  59. A simple sequential search is done.
  60.  
  61. If justname is nonzero, then the search is for the filename only
  62. without the directory prefix;  else it is for the full
  63. pathname.
  64. */
  65.  
  66. long inlist (fname, date, time, justname)
  67. char *fname;
  68. unsigned int *date, *time;
  69. int justname;
  70. {
  71.    register int i = 0;
  72.     if (justname)
  73.         fname = nameptr (fname);                    /* if directory wanted */
  74.    fentry[lastname]->fname = fname;          /* sentinel */
  75.  
  76. #ifdef IGNORECASE
  77. #define    COMPARE    strcmpi
  78. #else
  79. #define    COMPARE    strcmp
  80. #endif
  81.  
  82.    while (COMPARE(fname, 
  83.                 (justname ? nameptr (fentry[i]->fname) : fentry[i]->fname)) != 0) {
  84.       i++;
  85.    }
  86.  
  87.    if (i == lastname)
  88.       return (-1L);
  89.    else {
  90.       *date = fentry[i]->date;
  91.       *time = fentry[i]->time;
  92.       return (fentry[i]->position);
  93.    }
  94. } /* inlist() */
  95.  
  96.