home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libcnews / config.c < prev    next >
C/C++ Source or Header  |  1994-08-17  |  4KB  |  238 lines

  1. /*
  2.  * news configuration inquiry
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include "libc.h"
  8. #include "news.h"
  9. #include "config.h"
  10.  
  11. #ifndef NULL
  12. #define    NULL    0
  13. #endif
  14.  
  15. #ifndef NEWSCTL
  16. /* =()<#define    NEWSCTL    "@<NEWSCTL>@">()= */
  17. #define    NEWSCTL    "/etc/news"
  18. #endif
  19. #ifndef NEWSPATH
  20. /* =()<#define    NEWSPATH    "@<NEWSPATH>@">()= */
  21. #define    NEWSPATH    "/bin:/usr/bin:/usr/contrib/bin"
  22. #endif
  23. #ifndef NEWSARTS
  24. /* =()<#define    NEWSARTS    "@<NEWSARTS>@">()= */
  25. #define    NEWSARTS    "/var/news"
  26. #endif
  27. #ifndef NEWSBIN
  28. /* =()<#define    NEWSBIN    "@<NEWSBIN>@">()= */
  29. #define    NEWSBIN    "/usr/libexec/news"
  30. #endif
  31. #ifndef NEWSUMASK
  32. /* =()<#define    NEWSUMASK    @<NEWSUMASK>@>()= */
  33. #define    NEWSUMASK    022
  34. #endif
  35. #ifndef NEWSCONFIG
  36. /* =()<#define    NEWSCONFIG    "@<NEWSCONFIG>@">()= */
  37. #define    NEWSCONFIG    "/etc/news/bin/config"
  38. #endif
  39.  
  40. static char *pwd = NULL;    /* Current directory, NULL means unknown. */
  41. static int dirsset = NO;    /* Have the following been set up? */
  42. static char *arts = NEWSARTS;
  43. static char *bin = NEWSBIN;
  44. static char *ctl = NEWSCTL;
  45. static char *path = NEWSPATH;
  46. static int numask = NEWSUMASK;
  47. static char *nconfig = NEWSCONFIG;
  48. #define    DIRS()    if (!dirsset) setdirs()
  49.  
  50. extern char *getenv();
  51.  
  52. /*
  53.  - setdirs - set up stuff from environment, for use by other functions
  54.  *
  55.  * Invokes user-supplied function unprivileged() if non-standard values used.
  56.  */
  57. static void
  58. setdirs()
  59. {
  60.     register char *p;
  61.     register int mask;
  62.     register char *scan;
  63.     register int ns = 0;
  64. #    define    NONSTD(reason)    { if (!ns) { unprivileged(reason); ns = 1; } }
  65.  
  66.     if (dirsset)
  67.         return;
  68.  
  69.     p = getenv("NEWSARTS");
  70.     if (p != NULL && !STREQ(p, arts)) {
  71.         arts = p;
  72.         NONSTD("NEWSARTS");
  73.     }
  74.  
  75.     p = getenv("NEWSCTL");
  76.     if (p != NULL && !STREQ(p, ctl)) {
  77.         ctl = p;
  78.         NONSTD("NEWSCTL");
  79.     }
  80.  
  81.     p = getenv("NEWSPATH");
  82.     if (p != NULL && !STREQ(p, path)) {
  83.         path = p;
  84.         NONSTD("NEWSPATH");
  85.     }
  86.  
  87.     p = getenv("NEWSBIN");
  88.     if (p != NULL && !STREQ(p, bin)) {
  89.         bin = p;
  90.         NONSTD("NEWSBIN");
  91.     }
  92.  
  93.     p = getenv("NEWSUMASK");
  94.     if (p != NULL) {
  95.         mask = 0;
  96.         for (scan = p; *scan != '\0'; scan++)
  97.             if ('0' <= *scan && *scan <= '7' && mask <= 077)
  98.                 mask = (mask << 3) | (*scan - '0');
  99.             else {    /* Garbage, ignore it. */
  100.                 mask = numask;
  101.                 break;            /* NOTE BREAK OUT */
  102.             }
  103.         if (mask != numask) {
  104.             numask = mask;
  105.             NONSTD("NEWSUMASK");
  106.         }
  107.     }
  108.  
  109.     p = getenv("NEWSCONFIG");
  110.     if (p != NULL && !STREQ(p, nconfig)) {
  111.         nconfig = p;
  112.         NONSTD("NEWSCONFIG");
  113.     }
  114.  
  115.     dirsset = YES;
  116. }
  117.  
  118. /*
  119.  - artfile - best pathname for a file in NEWSARTS
  120.  */
  121. char *
  122. artfile(base)
  123. char *base;
  124. {
  125.     static char *artf = NULL;
  126.  
  127.     DIRS();
  128.  
  129.     if (base == NULL)    /* he just wants the directory */
  130.         return (arts);
  131.  
  132.     if (artf != NULL)
  133.         free(artf);    /* toss old returned value */
  134.     if (pwd != NULL && STREQ(pwd, arts))
  135.         artf = strsave(base);
  136.     else
  137.         artf = str3save(arts, SFNDELIM, base);
  138.  
  139.     return (artf);
  140. }
  141.  
  142. /*
  143.  - fullartfile - full pathname for a file in NEWSARTS
  144.  */
  145. char *
  146. fullartfile(base)
  147. char *base;
  148. {
  149.     register char *p;
  150.     register char *pwdsave;
  151.  
  152.     pwdsave = pwd;
  153.     pwd = NULL;        /* fool artfile() into giving full path */
  154.     p = artfile(base);
  155.     pwd = pwdsave;
  156.     return (p);
  157. }
  158.  
  159. /*
  160.  - ctlfile - full pathname for a file in NEWSCTL
  161.  */
  162. char *
  163. ctlfile(base)
  164. char *base;
  165. {
  166.     static char *ctlf = NULL;
  167.  
  168.     DIRS();
  169.  
  170.     if (ctlf != NULL)
  171.         free(ctlf);        /* toss old returned value */
  172.  
  173.     if (base == NULL) {
  174.         ctlf = NULL;
  175.         return(ctl);
  176.     } else {
  177.         ctlf = str3save(ctl, SFNDELIM, base);
  178.         return(ctlf);
  179.     }
  180. }
  181.  
  182. /*
  183.  - binfile - full pathname for a file in NEWSBIN
  184.  */
  185. char *
  186. binfile(base)
  187. char *base;
  188. {
  189.     static char *binf = NULL;
  190.  
  191.     DIRS();
  192.  
  193.     if (binf != NULL)
  194.         free(binf);        /* toss old returned value */
  195.  
  196.     if (base == NULL) {
  197.         binf = NULL;
  198.         return(bin);
  199.     } else {
  200.         binf = str3save(bin, SFNDELIM, base);
  201.         return (binf);
  202.     }
  203. }
  204.  
  205. /*
  206.  - cd - change to a directory, with checking
  207.  */
  208. void
  209. cd(dir)
  210. char *dir;
  211. {
  212.     if (pwd != NULL)
  213.         free(pwd);
  214.     if (chdir(dir) < 0)
  215.         errunlock("cannot chdir(%s)", dir);
  216.     pwd = strsave(dir);
  217. }
  218.  
  219. /*
  220.  - newspath - search path for normal system commands
  221.  */
  222. char *
  223. newspath()
  224. {
  225.     DIRS();
  226.     return(path);
  227. }
  228.  
  229. /*
  230.  - newsumask - suitable value of umask for news stuff
  231.  */
  232. int
  233. newsumask()
  234. {
  235.     DIRS();
  236.     return(numask);
  237. }
  238.