home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / readnews / funcs.c < prev    next >
C/C++ Source or Header  |  1994-09-08  |  6KB  |  343 lines

  1. #include "defs.h"
  2.  
  3. /*
  4.  * string handling functions
  5.  */
  6. char *
  7. myalloc(size)
  8. int size;
  9. {
  10.     register char *cp;
  11.  
  12.     if ((cp = malloc((unsigned) size)) == NIL(char))
  13.         error("No more memory.");
  14.     return cp;
  15. }
  16.  
  17.  
  18. char *
  19. myrealloc(ptr, size)
  20. char *ptr;
  21. int size;
  22. {
  23.     register char *cp;
  24.  
  25.     if ((cp = realloc(ptr, (unsigned) size)) == NIL(char))
  26.         error("No more memory.");
  27.     return cp;
  28. }
  29.  
  30.  
  31. char *
  32. newstr(s)
  33. char *s;
  34. {
  35.     return strcpy(myalloc(strlen(s) + 1), s);
  36. }
  37.  
  38.  
  39. char *
  40. newstr2(s1, s2)
  41. char *s1, *s2;
  42. {
  43.     return strcat(strcpy(myalloc(strlen(s1) + strlen(s2) + 1), s1), s2);
  44. }
  45.  
  46.  
  47. char *
  48. newstr3(s1, s2, s3)
  49. char *s1, *s2, *s3;
  50. {
  51.     return strcat(strcat(strcpy(myalloc(strlen(s1) + strlen(s2) + strlen(s3) +
  52.         1), s1), s2), s3);
  53. }
  54.  
  55.  
  56. char *
  57. newstr4(s1, s2, s3, s4)
  58. char *s1, *s2, *s3, *s4;
  59. {
  60.     return strcat(strcat(strcat(strcpy(myalloc(strlen(s1) + strlen(s2) +
  61.         strlen(s3) + strlen(s4) + 1), s1), s2), s3), s4);
  62. }
  63.  
  64.  
  65. char *
  66. newstr5(s1, s2, s3, s4, s5)
  67. char *s1, *s2, *s3, *s4, *s5;
  68. {
  69.     return strcat(strcat(strcat(strcat(strcpy(myalloc(strlen(s1) + strlen(s2) +
  70.         strlen(s3) + strlen(s4) + strlen(s5) + 1), s1), s2), s3), s4), s5);
  71. }
  72.  
  73.  
  74. char *
  75. newstr6(s1, s2, s3, s4, s5, s6)
  76. char *s1, *s2, *s3, *s4, *s5, *s6;
  77. {
  78.     return strcat(strcat(strcat(strcat(strcat(strcpy(myalloc(strlen(s1) +
  79.         strlen(s2) + strlen(s3) + strlen(s4) + strlen(s5) + strlen(s6) + 1),
  80.          s1), s2), s3), s4), s5), s6);
  81. }
  82.  
  83.  
  84. char *
  85. catstr(old, s)
  86. char *old, *s;
  87. {
  88.     return strcat(myrealloc(old, strlen(old) + strlen(s) + 1), s);
  89. }
  90.  
  91.  
  92. char *
  93. catstr2(old, s1, s2)
  94. char *old, *s1, *s2;
  95. {
  96.     return strcat(strcat(myrealloc(old, strlen(old) + strlen(s1) + strlen(s2) +
  97.         1), s1), s2);
  98. }
  99.  
  100.  
  101. /*
  102.  * Compare two newsgroups for equality.
  103.  * The first one may be a "meta" newsgroup.
  104.  */
  105. static
  106. ptrncmp(ng1, ng2)
  107. register char *ng1, *ng2;
  108. {
  109.  
  110.     while (1) {
  111.         if (ng1[0] == 'a' && ng1[1] == 'l' && ng1[2] == 'l' && (ng1[3] ==
  112.             '\0' || ng1[3] == '.')) {
  113.             if (ng1[3] == '\0')    /* "all" matches anything */
  114.                 return 1;
  115.             while (*ng2 && *ng2 != '.')
  116.                 ng2++;
  117.             if (*ng2 != '.')        /* "all." doesn't match "xx" */
  118.                 return 0;
  119.             ng1 += 4, ng2++;
  120.             continue;
  121.         }
  122.         while (*ng1 && *ng1 != '.' && *ng1 == *ng2)
  123.             ng1++, ng2++;
  124.         if (*ng1 == '.') {
  125.             if (*ng2 != '.' && *ng2 != '\0')
  126.                 return 0;    /* "."'s don't line up */
  127.             if (*ng2)
  128.                 ng2++;
  129.             ng1++;            /* "."'s line up - keep going */
  130.         } else if (*ng1 == '\0')
  131.             return (*ng2 == '\0' || *ng2 == '.');
  132.             /* full match or X matching X.thing */
  133.         else
  134.             return 0;
  135.     }
  136.     /* NOTREACHED */
  137. }
  138.  
  139.  
  140.  
  141. /*
  142.  * News group matching.
  143.  *
  144.  * nglist is a list of newsgroups.
  145.  * sublist is a list of subscriptions.
  146.  * sublist may have "meta newsgroups" in it.
  147.  * All fields are NGSEPCHAR separated.
  148.  *
  149.  * sublist uses "all" like shell uses "*", and "." like shell uses "/"
  150.  * if subscription X matches Y, it also matches Y.anything
  151.  */
  152. ngmatch(nglist, sublist)
  153. char *nglist, *sublist;
  154. {
  155.     register char *n, *s, *nd, *sd;
  156.     register int rc;
  157.  
  158.     rc = 0;
  159.     n = nglist;
  160.     while (*n && rc == 0) {
  161.         if (nd = strchr(n, NGSEPCHAR))
  162.             *nd = '\0';
  163.         s = sublist;
  164.         while (*s) {
  165.             if (sd = strchr(s, NGSEPCHAR))
  166.                 *sd = '\0';
  167.             if (*s != NEGCHAR)
  168.                 rc |= ptrncmp(s, n);
  169.             else
  170.                 rc &= ~ptrncmp(s + 1, n);
  171.             if (sd)
  172.                 *sd = NGSEPCHAR, s = sd + 1;
  173.             else
  174.                 break;
  175.         }
  176.         if (nd)
  177.             *nd = NGSEPCHAR, n = nd + 1;
  178.         else
  179.             break;
  180.     }
  181.     return rc;
  182. }
  183.  
  184.  
  185. /*
  186.  * open a file
  187.  */
  188. FILE *
  189. fopenf(name, mode)
  190. char *name, *mode;
  191. {
  192.     register FILE    *f;
  193.  
  194.     if ((f = fopen(name, mode)) == NULL)
  195.         error("Can't %s %s", *mode == 'r' ? "open" : "create", name);
  196.     return f;
  197. }
  198.  
  199.  
  200. /*
  201.  * replace all '.''s with '/'
  202.  */
  203. char *
  204. convg(s)
  205. register char *s;
  206. {
  207.     register char *sav;
  208.  
  209.     sav = s;
  210.     while (s = strchr(s, '.'))
  211.         *s = '/';
  212.     return sav;
  213. }
  214.  
  215.  
  216. /*
  217.  * get a line from stdin
  218.  * trim leading and trailing blanks
  219.  */
  220. char *
  221. mgets()
  222. {
  223.     register char *s;
  224.     static char buf[BUFSIZ];
  225.  
  226.     fflush(stdout);
  227.     if (fgets(buf, sizeof(buf), stdin) == NULL) {
  228.         (void) printf("\n");
  229.         return NIL(char);
  230.     }
  231.     if (s = strchr(buf, '\n'))
  232.         while (isspace(*s) && s > buf)
  233.             *s-- = '\0';
  234.     else {
  235.         (void) printf("Input line too long.\n");
  236.         return NULL;
  237.     }
  238.     s = buf;
  239.     while (isspace(*s))
  240.         s++;
  241.     return s;
  242. }
  243.  
  244.  
  245. /*
  246.  * apply the given function to each member in the newsgroup
  247.  */
  248. /* VARARGS2 */
  249. applyng(ng, func, arg1)
  250. register char *ng;
  251. register int (*func)();
  252. char *arg1;
  253. {
  254.     register char *delim;
  255.     register int err;
  256.  
  257.     err = 0;
  258.     while (*ng) {
  259.         if (delim = strchr(ng, NGSEPCHAR))
  260.             *delim = '\0';
  261.         err += (*func)(ng, arg1);
  262.         if (delim)
  263.             *delim = NGSEPCHAR, ng = delim + 1;
  264.         else
  265.             break;
  266.     }
  267.     return err;
  268. }
  269.  
  270.  
  271. /*
  272.  * generate a return address
  273.  */
  274. char *
  275. getretaddr(hp)
  276. header *hp;
  277. {
  278.     register char *ra;
  279.  
  280.     extern char *exaddress();
  281.  
  282.     if (hp->h_replyto)
  283.         ra = exaddress(hp->h_replyto);
  284.     else if (hp->h_from)
  285.         ra = exaddress(hp->h_from);
  286.     else
  287.         ra = NIL(char);
  288.     if (hp->h_path && !ra)
  289.         ra = hp->h_path;
  290.     return ra;
  291. }
  292.  
  293.  
  294. /*
  295.  * try and make a proper address
  296.  */
  297. char *
  298. exaddress(addr)
  299. char *addr;
  300. {
  301.     register char *space, *dot, *at;
  302.     register char *raddr;
  303.     extern char mailvia[];
  304.  
  305.     raddr = NIL(char);
  306.     if (strcmp(mailvia, "<path>") == 0)
  307.         return raddr;
  308.     if (space = strchr(addr, ' '))
  309.         *space = '\0';
  310.     if (mailvia[0] != '\0' && (at = strchr(addr, '@')) != NULL) {
  311.         *at = '\0';
  312.         raddr = newstr5(mailvia, PSEPS, at + 1, PSEPS, addr);
  313.         *at = '@';
  314.     } else
  315.         raddr = newstr(addr);
  316.     if (space)
  317.         *space = ' ';
  318.     return raddr;
  319. }
  320.  
  321.  
  322. /*
  323.  * remove extra spaces, and insert separators if necessary in
  324.  * newsgroups specification
  325.  */
  326. convgrps(sp)
  327. register char *sp;
  328. {
  329.     register char *sep = NULL;
  330.  
  331.     while (*sp) {
  332.         if (sep)
  333.             sp++;
  334.         while (*sp && (isspace(*sp) || *sp == NGSEPCHAR))
  335.             strcpy(sp, sp + 1);
  336.         if (sep)
  337.             *sep = (*sp ? NGSEPCHAR : '\0');
  338.         while (*sp && !isspace(*sp) && *sp != NGSEPCHAR)
  339.             sp++;
  340.         sep = sp;
  341.     }
  342. }
  343.