home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / pcomm / part02 / d_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-17  |  6.3 KB  |  263 lines

  1. /*
  2.  * Routines to manipulate the dialing directory file pcomm.dial_dir
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "dial_dir.h"
  7. #include "param.h"
  8. #include "status.h"
  9.  
  10. /*
  11.  * Read the dialing directory.  Returns a pointer to a static area
  12.  * containing the DIAL_DIR structure.  All of the entries are created
  13.  * reguardless of the number of physical entries in the file.  Element
  14.  * number zero is reserved for the "manual" entry.  All errors are fatal.
  15.  */
  16.  
  17. struct DIAL_DIR *
  18. read_dir()
  19. {
  20.     FILE *fp;
  21.     int i, j, oops;
  22.     char *strdup(), buf[80], *temp_token, *str, *str_tok(), token[20];
  23.     char message[80], *sep;
  24.     static struct DIAL_DIR d;
  25.     void error_win();
  26.     extern char *null_ptr;
  27.  
  28.     if (!(fp = fopen(status->d_path, "r"))) {
  29.         sprintf(buf, "'%s' for read", status->d_path);
  30.         error_win(1, "Can't open dialing directory file", buf);
  31.     }
  32.  
  33.     sep = ";;---;;\n";
  34.     i = 0;
  35.     oops = 0;
  36.     while (fgets(buf, 80, fp) != NULL) {
  37.         i++;
  38.         if (i > NUM_DIR)
  39.             break;
  40.                     /* get the token */
  41.         if (!(temp_token = str_tok(buf, '='))) {
  42.             sprintf(message, "is missing a token at line %d", i);
  43.             oops++;
  44.             break;
  45.         }
  46.         /*
  47.          * Parse the rest of the line.  This is similar to using
  48.          * the "real" strtok() function, but my version returns
  49.          * a null pointer if the parameter is missing.  Note the
  50.          * array of field separators.
  51.          */
  52.         for (j=0; j<8; j++) {
  53.             if (!(str = str_tok((char *) NULL, sep[j]))) {
  54.                 sprintf(message, "is missing a parameter at line %d", i);
  55.                 oops++;
  56.                 break;
  57.             }
  58.             switch(j) {
  59.                 case 0:
  60.                     d.name[i] = strdup(str);
  61.                     break;
  62.                 case 1:
  63.                     d.number[i] = strdup(str);
  64.                     break;
  65.                 case 2:
  66.                     d.baud[i] = atoi(str);
  67.                     break;
  68.                 case 3:
  69.                     d.parity[i] = *str;
  70.                     break;
  71.                 case 4:
  72.                     d.dbits[i] = atoi(str);
  73.                     break;
  74.                 case 5:
  75.                     d.sbits[i] = atoi(str);
  76.                     break;
  77.                 case 6:
  78.                     d.duplex[i] = *str;
  79.                     break;
  80.                 case 7:
  81.                     d.index[i] = strdup(str);
  82.                     break;
  83.             }
  84.         }
  85.         if (oops)
  86.             break;
  87.                     /* sanity checking */
  88.         sprintf(token, "DIR_%d", i);
  89.         if (strcmp(temp_token, token)) {
  90.             sprintf(message, "is corrupted at line %d", i);
  91.             oops++;
  92.             break;
  93.         }
  94.     }
  95.     fclose(fp);
  96.  
  97.     if (oops) {
  98.         sprintf(buf, "Directory file '%s'", status->d_path);
  99.         error_win(1, buf, message);
  100.     }
  101.     d.d_entries = i;
  102.                     /* fill in the rest with defaults */
  103.     i++;
  104.     for (; i<=NUM_DIR; i++) {
  105.         d.name[i] = null_ptr;
  106.         d.number[i] = null_ptr;
  107.         d.baud[i] = param->d_baud;
  108.         d.parity[i] = param->d_parity;
  109.         d.dbits[i] = param->d_dbits;
  110.         d.sbits[i] = param->d_sbits;
  111.         d.duplex[i] = *param->d_duplex;
  112.         d.index[i] = null_ptr;
  113.     }
  114.                     /* create an empty "manual" entry */
  115.     d.name[0] = null_ptr;
  116.     d.number[0] = null_ptr;
  117.     d.baud[0] = param->d_baud;
  118.     d.parity[0] = param->d_parity;
  119.     d.dbits[0] = param->d_dbits;
  120.     d.sbits[0] = param->d_sbits;
  121.     d.duplex[0] = *param->d_duplex;
  122.     d.index[0] = null_ptr;
  123.                     /* create an empty queue */
  124.     for (i=0; i<NUM_QUEUE; i++) {
  125.         d.q_ld[i] = NULL;
  126.         d.q_num[i] = -1;
  127.     }
  128.                     /* the startup d_cur is 0 */
  129.     d.d_cur = 0;
  130.     return(&d);
  131. }
  132.  
  133. /*
  134.  * Update a dialing directory entry.  Update only the one entry asked for,
  135.  * not the entire image in memory.  If the new entry is beyond the end of
  136.  * the physical file, then fill in the holes, and update "dir->d_entries".
  137.  * A return code of 1 means a non-fatal error.
  138.  */
  139.  
  140. int
  141. update_dir(entry)
  142. int entry;
  143. {
  144.     FILE *fp_in, *fp_out;
  145.     char *temp[NUM_DIR+1], buf[80], *strdup();
  146.     int i;
  147.     void error_win(), free_ptr();
  148.  
  149.                     /* open for read */
  150.     if (!(fp_in = fopen(status->d_path, "r"))) {
  151.         sprintf(buf, "'%s' for read", status->d_path);
  152.         error_win(1, "Can't open dialing directory file", buf);
  153.     }
  154.                     /* read in a temporary version */
  155.     i = 0;
  156.     while (fgets(buf, 80, fp_in) != NULL)
  157.         temp[++i] = strdup(buf);
  158.     
  159.     fclose(fp_in);
  160.                     /* alter only 1 entry */
  161.     sprintf(buf, "DIR_%d=%s;%s;%d-%c-%d-%d;%c;%s\n", entry,
  162.      dir->name[entry], dir->number[entry], dir->baud[entry],
  163.      dir->parity[entry], dir->dbits[entry], dir->sbits[entry],
  164.      dir->duplex[entry], dir->index[entry]);
  165.  
  166.     if (entry <= dir->d_entries)
  167.         free_ptr(temp[entry]);
  168.     temp[entry] = strdup(buf);
  169.                     /* fill in holes if beyond end */
  170.     if (entry > dir->d_entries+1) {
  171.         for (i=dir->d_entries+1; i<entry; i++) {
  172.             sprintf(buf, "DIR_%d=;;%d-%c-%d-%d;%c;\n", i,
  173.              param->d_baud, param->d_parity, param->d_dbits,
  174.              param->d_sbits, *param->d_duplex);
  175.             temp[i] = strdup(buf);
  176.         }
  177.     }
  178.                     /* update "dir->d_entries" */
  179.     if (entry > dir->d_entries)
  180.         dir->d_entries = entry;
  181.  
  182.                     /* open for read */
  183.     if (!(fp_out = fopen(status->d_path, "w"))) {
  184.         for (i=1; i<=dir->d_entries; i++)
  185.             free_ptr(temp[i]);
  186.         sprintf(buf, "'%s'", status->d_path);
  187.         error_win(0, "No write permission on directory file", buf);
  188.         return(1);
  189.     }
  190.                     /* put it back */
  191.     for (i=1; i<=dir->d_entries; i++) {
  192.         fputs(temp[i], fp_out);
  193.         free_ptr(temp[i]);
  194.     }
  195.  
  196.     fclose(fp_out);
  197.     return(0);
  198. }
  199.  
  200. /*
  201.  * Delete a range of dialing directory entries.  Actually, just copies
  202.  * default (empty) entries in place of deleted entries.  However, it will
  203.  * shrink the file if deletions occur at the physical EOF.  A return code
  204.  * of 1 means a non-fatal error.
  205.  */
  206.  
  207. int
  208. del_dir(first, last)
  209. int first, last;
  210. {
  211.     FILE *fp_in, *fp_out;
  212.     int i;
  213.     char *temp[NUM_DIR+1], buf[80], *strdup();
  214.     void error_win(), free_ptr();
  215.                     /* sanity checking */
  216.     if (first > dir->d_entries)
  217.         return(0);
  218.     if (last > dir->d_entries)
  219.         last = dir->d_entries;
  220.  
  221.                     /* open for read */
  222.     if (!(fp_in = fopen(status->d_path, "r"))) {
  223.         sprintf(buf, "'%s' for read", status->d_path);    
  224.         error_win(1, "Can't open dialing directory file", buf);
  225.     }
  226.                     /* read in a temporary version */
  227.     i = 0;
  228.     while (fgets(buf, 80, fp_in) != NULL)
  229.         temp[++i] = strdup(buf);
  230.  
  231.     fclose(fp_in);
  232.                     /* delete the range of values */
  233.     for (i=first; i<=last; i++) {
  234.         sprintf(buf, "DIR_%d=;;%d-%c-%d-%d;%c;\n", i, param->d_baud,
  235.          param->d_parity, param->d_dbits, param->d_sbits,
  236.          *param->d_duplex);
  237.         free_ptr(temp[i]);
  238.         temp[i] = strdup(buf);
  239.     }
  240.                     /* shrink the file? */
  241.     if (last >= dir->d_entries) {
  242.         for (i=first; i<=last; i++)
  243.             free_ptr(temp[i]);
  244.         dir->d_entries = first-1;
  245.     }
  246.                     /* open for write */
  247.     if (!(fp_out = fopen(status->d_path, "w"))) {
  248.         for (i=1; i<=dir->d_entries; i++)
  249.             free_ptr(temp[i]);
  250.         sprintf(buf, "'%s'", status->d_path);
  251.         error_win(0, "No write permission on dialing directory file", buf);
  252.         return(1);
  253.     }
  254.                     /* put it all back */
  255.     for (i=1; i<=dir->d_entries; i++) {
  256.         fputs(temp[i], fp_out);
  257.         free_ptr(temp[i]);
  258.     }
  259.  
  260.     fclose(fp_out);
  261.     return(0);
  262. }
  263.