home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / ADDFNAME.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  4KB  |  136 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) addfname.c 2.11 88/02/06 20:17:17";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  8. */
  9. #include "options.h"
  10.  
  11. /* Adds a filename to global list.  (This global list will eventually
  12. be searched by the inlist() function.)  The second and subsequent
  13. parameters suppplied are stored with the name of the file and
  14. returned by inlist. */
  15.  
  16. #include "zooio.h"
  17. #include "various.h"
  18. #include "zoo.h"
  19. #include "zoofns.h"
  20. #include "zoomem.h" /* to get LIST_SIZE */
  21.  
  22. #define FENTRY_BSIZE        80        /* allocation granularity for fentry below */
  23. static struct item **fentry;    /* array of ptrs to file information structs */
  24. static unsigned sz_fentry;        /* its current size */
  25. static int lastname = 0;                  /* index of last name */
  26.  
  27. struct item {              /* global filename list entry */
  28.    char *fname;
  29.    long position;
  30.    unsigned int date;
  31.    unsigned int time;
  32.     unsigned vflag;
  33.     unsigned version_no;
  34. };
  35.  
  36. void addfname(fname, position, date, time, vflag, version_no)
  37. char *fname;
  38. long position;
  39. unsigned int date, time;
  40. unsigned vflag;
  41. unsigned version_no;
  42. {
  43.    if (lastname == 0) {
  44.         sz_fentry = FENTRY_BSIZE;
  45.         fentry = (struct item **) ealloc(sizeof(struct item *) * sz_fentry);
  46.       fentry[0] = (struct item *) ealloc (sizeof(struct item));
  47.     }
  48.  
  49.     /* allocated more memory if needed */
  50.    if (lastname >= sz_fentry - 3) {
  51.         sz_fentry += FENTRY_BSIZE;
  52.         fentry = (struct item **)
  53.             erealloc(fentry, sizeof(struct item *) * sz_fentry);
  54.     }
  55.  
  56.    fentry[lastname]->fname = str_dup(fname);
  57.    fentry[lastname]->position = position;
  58.    fentry[lastname]->date = date;
  59.    fentry[lastname]->time = time;
  60.     fentry[lastname]->vflag = vflag;
  61.     fentry[lastname]->version_no = version_no;
  62.    lastname++;
  63.    /* allocate memory for empty entry at end */
  64.    fentry[lastname] = (struct item *) ealloc (sizeof(struct item));
  65. } /* addfname */
  66.  
  67. /* inlist() */
  68. /* Examines global list built by addfname() to see if supplied filename
  69. is in the list.
  70.  
  71. If found, returns the file's position within the archive as the function
  72. value and the date, time, version flag, and version number as parameters.
  73. If not found, returns -1.  Also returns the highest version no. seen
  74. for this filename and the vflag associated with that version.
  75.  
  76. A simple sequential search is done.
  77.  
  78. If justname is nonzero, then the search is for the filename only
  79. without the directory prefix;  else it is for the full
  80. pathname.
  81. */
  82.  
  83. long inlist (fname, date, time, this_version_no, high_vflag,
  84.                     high_version_no, high_pos, justname)
  85. char *fname;
  86. unsigned int *date, *time;
  87. unsigned *high_vflag;
  88. unsigned *this_version_no;
  89. unsigned *high_version_no;
  90. long *high_pos;
  91. int justname;
  92. {
  93.    register int i = 0;
  94.  
  95.     *high_version_no = 0;
  96.     if (justname)
  97.         fname = nameptr (fname);                    /* if directory wanted */
  98.    fentry[lastname]->fname = fname;          /* sentinel */
  99.     fentry[lastname]->version_no = 0;
  100.  
  101. #ifdef IGNORECASE
  102. #define    COMPARE    str_icmp
  103. #else
  104. #define    COMPARE    strcmp
  105. #endif
  106.  
  107.    while (COMPARE(fname,
  108.             (justname ? nameptr (fentry[i]->fname) : fentry[i]->fname)) != 0) {
  109.       i++;
  110.    }
  111.  
  112.    if (i == lastname)
  113.       return (-1L);
  114.    else {
  115.         int j;
  116.         *date = fentry[i]->date;
  117.         *time = fentry[i]->time;
  118.         *high_pos = fentry[i]->position;
  119.         *high_vflag = fentry[i]->vflag;
  120.         for (j = i; j < lastname; j++) {    /* find highest version no. for file */
  121.             if (COMPARE(fname,
  122.                 (justname ? nameptr (fentry[j]->fname) : fentry[j]->fname)) == 0) {
  123.                 if (*high_version_no < fentry[j]->version_no) {
  124.                     *high_version_no = fentry[j]->version_no;
  125.                     *high_vflag = fentry[j]->vflag;
  126.                     *high_pos = fentry[j]->position;
  127.                     *date = fentry[j]->date;
  128.                     *time = fentry[j]->time;
  129.                 }
  130.             }
  131.         }
  132.         *this_version_no = fentry[i]->version_no;
  133.       return (fentry[i]->position);
  134.    }
  135. } /* inlist() */
  136.