home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #2 / RBBS_vol1_no2.iso / add2 / fc_v003.exe / FCLIST.C < prev    next >
Text File  |  1989-04-24  |  7KB  |  242 lines

  1. /***************************************************************************
  2.  *
  3.  *                            FCLIST.C
  4.  *
  5.  *                Linked List Management functions for
  6.  *                File Commentor.
  7.  *
  8.  *                Author:   Ronald C. Bieber
  9.  *
  10.  *
  11.  ***************************************************************************/
  12. #include <stdio.h>
  13. #include <dos.h>
  14. #include <dir.h>
  15. #include "fc.h"
  16.  
  17. /****************************************************************************
  18.  *
  19.  *          Name:   mk_info
  20.  *      Synopsis:   int mk_info(void);
  21.  *                  
  22.  *                  
  23.  *
  24.  *   Description:   Creates new COMMENTS.FC file
  25.  *
  26.  *       Returns:   File handle of new file.
  27.  *
  28.  ***************************************************************************/
  29. int mk_info()
  30. {
  31.     int     fh;
  32.     unsigned wr;
  33.  
  34.     fh = _creat(comment_file, 0);
  35.  
  36.     if (fh == -1) {
  37.         fprintf(stderr, "Can't open COMMENTS.FC\n");
  38.         quit(FAILURE);
  39.     }
  40.  
  41.     write(fh, "RBCI", 4);
  42.  
  43.     return(fh);
  44.  
  45. }
  46. /****************************************************************************
  47.  *
  48.  *          Name:   mk_node
  49.  *      Synopsis:   FILELIST    *mk_node(phead);
  50.  *                  FILELIST    *phead;     beginning of linked list
  51.  *                  
  52.  *
  53.  *   Description:   Creates new node to store more information
  54.  *
  55.  *       Returns:   a pointer of type FILELIST to the new node.
  56.  *
  57.  ***************************************************************************/
  58. FILELIST *mk_node(phead)
  59. FILELIST    *phead;
  60. {
  61.     FILELIST    *new,
  62.                 *tmp,
  63.                 *pred;
  64.  
  65.     tmp = phead;
  66.     
  67.     while (tmp != NULL) {
  68.         pred = tmp;
  69.         tmp = tmp->next;
  70.     }
  71.  
  72.     if ((new = (FILELIST *) calloc(1, sizeof(FILELIST))) == NULL)
  73.         return(NULL);
  74.  
  75.     new->prev = pred;
  76.     new->next = NULL;
  77.     new->fileinfo.sig = sig;
  78.     pred->next = new;
  79.     
  80.     return(new);
  81. }
  82. /****************************************************************************
  83.  *
  84.  *          Name:   search
  85.  *      Synopsis:   FILELIST    *search(phead, s);
  86.  *                  FILELIST    *phead;     beginning of linked list
  87.  *                  char        s;          pointer to filename to search for
  88.  *
  89.  *   Description:   Searches list PHEAD for filename S
  90.  *       Returns:   pointer to node containing information for S
  91.  *
  92.  ***************************************************************************/
  93. FILELIST    *search(phead, s)
  94. FILELIST    *phead;
  95. char        *s;
  96. {
  97.     FILELIST    *p = phead;
  98.  
  99.     while(p != NULL) 
  100.         if (wildcards(s, p->fileinfo.filename)) 
  101.             return(p);
  102.         else
  103.             p = p->next;
  104.     
  105.     return(NULL);
  106.  
  107. }
  108. /****************************************************************************
  109.  *
  110.  *          Name:   read_info
  111.  *      Synopsis:   int     read_info(phead, fh);
  112.  *                  FILELIST    *phead;     beginning of linked list
  113.  *                  int         fh;         handle of file to read from
  114.  *
  115.  *   Description:   Reads the contents of file FH and stores it in 
  116.  *                  linked list PHEAD 
  117.  *
  118.  *       Returns:   How many items were read
  119.  *
  120.  ***************************************************************************/
  121. int read_info(phead, fh)
  122. FILELIST    *phead;
  123. int         fh;
  124. {
  125.     int             count = 0;
  126.     char            tmp[4];
  127.     struct _fi      fi;
  128.     FILELIST        *next;
  129.  
  130.     next = phead;
  131.         
  132.     read(fh, tmp, sizeof(tmp));
  133.  
  134.     while (read(fh, &fi, sizeof(fi)) == sizeof(fi)) {
  135.         if (fi.sig != sig) {
  136.             printf("Comment file COMMENTS.FC is corrupted!\n");
  137.             quit(FAILURE);
  138.         }
  139.         next = mk_node(phead);
  140.         memmove(&(next)->fileinfo, &fi, sizeof(fi));
  141.         count++;
  142.     }
  143.     return(count);
  144. }
  145. /****************************************************************************
  146.  *
  147.  *          Name:   write_info
  148.  *      Synopsis:   int     write_info(phead, fh);
  149.  *                  FILELIST    *phead;     beginning of linked list
  150.  *                  int         fh;         handle of file to write to
  151.  *
  152.  *   Description:   Writes the contents of list PHEAD to file FH
  153.  *
  154.  *       Returns:   How many items were written
  155.  *
  156.  ***************************************************************************/
  157. int write_info(phead, fh)
  158. FILELIST    *phead;
  159. int         fh;
  160. {
  161.     int         numread,
  162.                 count = 0;
  163.     FILELIST    *next;
  164.     FILE        *fp;
  165.  
  166.     next = phead->next;
  167.  
  168.     lseek(fh, 4L, SEEK_SET);
  169.  
  170.     while(next != NULL) {
  171.         if (strlen(next->fileinfo.comment) > 0) 
  172.             write(fh, &(next)->fileinfo, sizeof(next->fileinfo));
  173.         next = next->next;
  174.         count++;
  175.     }
  176. }
  177. /****************************************************************************
  178.  *
  179.  *          Name:   node_delete
  180.  *      Synopsis:   void        node_delete(phead, s);
  181.  *                  FILELIST    *phead;     beginning of linked list
  182.  *                  char        s;          pointer to filename to search for
  183.  *
  184.  *   Description:   Deletes reference to file 's' from phead list
  185.  *
  186.  ***************************************************************************/
  187. void node_delete(phead, s)
  188. FILELIST    *phead;
  189. char        *s;
  190. {
  191.     FILELIST    *next = phead;
  192.  
  193.     while (next != NULL) {
  194.         if (stricmp(next->fileinfo.filename, s) == 0) {
  195.  
  196.             if (next->prev != NULL)
  197.                 next->next->prev = next->prev;
  198.             else
  199.                 next->next->prev = NULL;
  200.  
  201.             if (next->next != NULL)
  202.                 next->prev->next = next->next;
  203.             else
  204.                 next->prev->next = NULL;
  205.  
  206.             free(next);
  207.             return;
  208.         } else
  209.             next = next->next;
  210.     }
  211. }
  212. /****************************************************************************
  213.  *
  214.  *          Name:   compress
  215.  *      Synopsis:   void compress(phead);
  216.  *                  FILELIST    *phead;
  217.  *
  218.  *   Description:   Removes all references of files not present on the
  219.  *                  disk from the list (phead).
  220.  *
  221.  ***************************************************************************/
  222. void compress(phead)
  223. FILELIST    *phead;
  224. {
  225.  
  226.     struct      ffblk files;
  227.     FILELIST    *next = phead;
  228.     int         attr = FA_ARCH + FA_HIDDEN + FA_RDONLY + FA_SYSTEM + FA_DIREC;
  229.  
  230.     while (next != NULL) {
  231.  
  232.         if (findfirst(next->fileinfo.filename, &files, attr) != 0)
  233.             node_delete(phead, next->fileinfo.filename);
  234.  
  235.         next = next->next;
  236.     }
  237.  
  238. }
  239.  
  240.  
  241.  
  242.