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