home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / communic / pcmail / main / params.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.4 KB  |  138 lines

  1. /*++
  2. /* NAME
  3. /*    params 3
  4. /* SUMMARY
  5. /*    communication parameter access
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    library
  10. /* SYNOPSIS
  11. /*    #include "params.h"
  12. /*
  13. /*    Info *getparams();
  14. /* DESCRIPTION
  15. /*    getparams() returns a pointer to a table with communications
  16. /*    parameters. Usually communications parameters are set with the
  17. /*    "setup" option in the main menu of the interactive mail program.
  18. /*
  19. /*    First getparams() attempts to read from the setup file.
  20. /*    If that fails it creates an empty parameter table with
  21. /*    null string pointers as parameter values.
  22. /* FUNCTIONS AND MACROS
  23. /*    myalloc()
  24. /* BUGS
  25. /*    getparams() silently ignores any information in the
  26. /*    parameter file that it does not recognize.
  27. /*    getparams() will read the parameter file upon each call, even
  28. /*    if nothing has changed since the last read. Let us say that
  29. /*    it anticipates on multi-user environments.
  30. /* AUTHOR(S)
  31. /*    W.Z. Venema
  32. /*    Eindhoven University of Technology
  33. /*    Department of Mathematics and Computer Science
  34. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  35. /* CREATION DATE
  36. /*    Wed Apr  8 15:39:23 GMT+1:00 1987
  37. /* LAST MODIFICATION
  38. /*    90/01/22 13:02:24
  39. /* VERSION/RELEASE
  40. /*    2.1
  41. /*--*/
  42.  
  43. #include <stdio.h>
  44. #include <ctype.h>
  45.  
  46. #include "defs.h"
  47. #include "path.h"
  48. #include "params.h"
  49.  
  50. /* Storage area for setup parameters */
  51.  
  52. hidden Info params[] = {
  53.     /* name */    /* name length */    /* value */    /* default */
  54.     S_IGNORE,    sizeof(S_IGNORE) -1,    0,        D_IGNORE,
  55. #ifndef    DAEMON
  56.     S_PORT,    sizeof(S_PORT) - 1,    0,        0,
  57.     S_BAUD,    sizeof(S_BAUD) - 1,    0,        0,
  58.     S_HOST,    sizeof(S_HOST) - 1,    0,        0,
  59.     S_LOGIN,    sizeof(S_LOGIN) - 1,    0,        0,
  60.     S_DIAL,    sizeof(S_DIAL) - 1,    0,        0,
  61.     S_DISC,    sizeof(S_DISC) - 1,    0,        D_DISC,
  62. #endif
  63.     0,        0,            0,        0,
  64. };
  65.  
  66. hidden char *hackstr();            /* forward declaration */
  67.  
  68. /* getparams - try to get info from file, else make empty table */
  69.  
  70. public Info *getparams()
  71. {
  72.     char    line[BUFSIZ];
  73.     register Info *ip;
  74.     FILE   *fp;
  75.  
  76.     /* for cleanliness, we first clear all table entries */
  77.  
  78.     for (ip = params; ip->ident; ip++) {
  79.     if (ip->strval)
  80.         free(ip->strval);
  81.     ip->strval = 0;
  82.     }
  83.  
  84.     /* then, try to copy parameter file info to the table */
  85.  
  86.     if (fp = fopen(parm_file(), "r")) {
  87.     while (fgets(line, sizeof(line), fp)) {
  88.         for (ip = params; ip->ident; ip++) {
  89.         if (strncmp(ip->ident, line, ip->length) == 0) {
  90.             ip->strval = hackstr(line + ip->length);
  91.             break;
  92.         }
  93.         }
  94.     }
  95.     (void) fclose(fp);
  96.     }
  97.  
  98.     /* set defaults for undefined values */
  99.  
  100.     for (ip = params; ip->ident; ip++)
  101.     if (ip->strval == 0 && ip->defval != 0)
  102.         ip->strval = hackstr(ip->defval);
  103.  
  104.     return (params);
  105. }
  106.  
  107. /* hackstr - cut away blanks around string and make copy */
  108.  
  109. hidden char *hackstr(s)
  110. register char *s;
  111. {
  112.     register char *r;
  113.     int     len;
  114.  
  115.     /* strip leading and trailing blanks */
  116.  
  117.     while (*s && isspace(*s))
  118.     s++;
  119.     for (r = s + strlen(s); r > s && isspace(r[-1]); r--)
  120.     /* void */ ;
  121.  
  122.     /*
  123.      * s is at the terminator or first non-blank char. r is at the terminator
  124.      * or first blank after the last non-blank char. Thus, the actual string
  125.      * length is r-s. We add one for the terminator. We don't allocate memory
  126.      * if the string is empty.
  127.      */
  128.  
  129.     if (len = r - s) {
  130.     char   *cp = strncpy(myalloc(len + 1), s, len);
  131.  
  132.     cp[len] = '\0';
  133.     return (cp);
  134.     } else {
  135.     return (0);
  136.     }
  137. }
  138.