home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3138 / anne.misc.c < prev    next >
C/C++ Source or Header  |  1991-03-26  |  5KB  |  282 lines

  1. /*
  2.  * anne.jones - anne.misc.c - 03/26/91
  3.  *
  4.  * Miscellaneous routines used by anne.jones
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #ifndef AIX        /* AIX doesn't have malloc.h.  Oh well. */
  10. #include <malloc.h>
  11. #endif
  12. #include <sys/types.h>
  13. #include <time.h>
  14. #include <sys/timeb.h>
  15. #include "anne.h"
  16.  
  17. char *legaldate(), *fixgmt(), *fix_date(), *fix_expire();  
  18. extern char    *safemalloc(), *saferealloc(), *get_a_line();
  19.  
  20. FILE *
  21. trypath(route, name)
  22.      register char *route, *name;
  23. {
  24.   register char *s;
  25.   
  26.   if ((s = safemalloc(strlen(route) + strlen(name) + 2)) == (char *) NULL) {
  27.     perror("trypath safemalloc");
  28.     exit(1);
  29.   }
  30.   strcpy(s, route);
  31.   strcat(s, "/");
  32.   strcat(s, name);
  33.   
  34.   return (fopen(s, "r"));
  35. }
  36.  
  37. char *
  38. squeeze(str)
  39.      char *str;
  40. {
  41.   register char *s, *t, *u;
  42.  
  43. #ifdef    DEBUG
  44.   fprintf(debug, "squeeze got ->%s<-\n", str);
  45. #endif
  46.   s = str;
  47.   
  48.   /*
  49.     worst case it'll be as long as s .. never longer
  50.     */
  51.   t = u = safemalloc(strlen(s) + 1);
  52.  
  53.   /*
  54.     fixed by fk@u222.rci.dk (Flemming Kraglund); it stored one too many
  55.     \0's, which can become a problem if the length of the string is a
  56.     multiple of the memory allocation size
  57.    */
  58.   for (; *s; ++s) if (*s != ' ' && *s != '\t') *t++ = *s;
  59.   
  60.   *t = '\0';
  61. #ifdef    DEBUG
  62.   fprintf(debug, "squeeze gave back ->%s<-\n", u);
  63. #endif
  64.   return (u);
  65. }
  66.  
  67. void
  68. squeeze2(s)
  69.      register char *s;
  70. {
  71.   register char *t, *u, *orig;
  72.  
  73.   t = u = safemalloc(strlen(s) + 8);
  74. #ifdef    DEBUG
  75.   fprintf(debug, "squeeze2 got ->%s<-\n", s);
  76. #endif
  77.   orig = s;
  78.   do {
  79.     if (!((*s < '\040') &&
  80.       ((*s >= '\015') || (*s == '\013') || (*s < '\010'))
  81.       )) {
  82.       *(t++) = *s;
  83.     }
  84.   } while (*(++s));
  85.   *t = '\0';
  86.   
  87.   strcpy(orig, u);
  88. #ifdef    DEBUG
  89.   fprintf(debug, "squeeze2 put ->%s<-\n", orig);
  90. #endif
  91.   free(u);
  92. }
  93.  
  94. char *
  95. getmname(f)
  96.      FILE *f;
  97. {
  98.   char *s, *t;
  99.   s = safemalloc(NAMESIZE + 1);
  100.   if ((get_a_line(s, NAMESIZE, f)) == (char *) NULL) {
  101.     perror("getmname");
  102.     exit(1);
  103.   }
  104.   fclose(f);
  105.   if (*(t = (s + strlen(s) - 1)) == '\n')
  106.     *t = '\0';
  107.   return (s);
  108. }
  109.  
  110. int
  111. know_head(s)
  112.      char *s;
  113. {
  114.   register char *t;
  115.   
  116.   if ((t = strstr(allheads, s)) != (char *) NULL)
  117.     return (((t - allheads) / 13) + 1);
  118.   else
  119.     return (NOTKNOWN);
  120. }
  121.  
  122. char **
  123. buildbook(dim)
  124.      int dim;
  125. {
  126.   register int i;
  127.   register char *pages, **book;
  128.   
  129.   pages = safemalloc(dim * dim);
  130.   if (pages == (char *)NULL) {
  131.     fprintf(stderr, "No heap space for header book items!\n");
  132.     exit(1);
  133.   }
  134.   book = (char **) calloc(dim, sizeof(char *));
  135.   if (book == (char **)NULL) {
  136.     fprintf(stderr, "No heap space for header book!\n");
  137.     exit(1);
  138.   }
  139.   for (i = 0; i < dim; i++) {
  140.     *(book + i) = pages;
  141.     pages += dim;
  142.   }
  143.   return (book);
  144. }
  145.  
  146. void
  147. termin()
  148. {
  149.   creat("/tmp/termin", 0644);
  150.   exit(0);
  151. }
  152.  
  153. /* This was ripped from rn 4.3 */
  154.  
  155. #ifndef    HAVESTRSTR
  156. /* return ptr to little string in big string, NULL if not found */
  157. char *
  158. strstr(big, little)
  159.      char *big, *little;
  160. {
  161.   register char *t, *s, *x;
  162.   
  163.   for (t = big; *t; t++) {
  164.     for (x = t, s = little; *s; x++, s++) {
  165.       if (!*x)
  166.     return ((char *) NULL);
  167.       if (*s != *x)
  168.     break;
  169.     }
  170.     if (!*s)
  171.       return (t);
  172.   }
  173.   return ((char *) NULL);
  174. }
  175. #endif    /* HAVESTRSTR */
  176.  
  177. #ifdef MINIMALIST
  178.   /*
  179.    * The following is based on a program apparently written by Jon Zeeff
  180.    * (zeeff@b-tech.ann-arbor.mi.us). Palkovic@linac.fnal.gov, 3/22/91.
  181.    *
  182.    * A string of some valid message id characters
  183.    */
  184.  
  185. void
  186. rand_id(s)
  187. char *s;
  188. {
  189.    static char string[] = "!#._+-=ABCDFGHJKLMNPQRSTVWXYZ1234567890";
  190.    register int size = sizeof(string) - 1;
  191.    long num;
  192.  
  193.    num = (time((long *)0) - 658216800) / 60;
  194.    do {
  195.     *s++ = string[num % size];
  196.     num /= size;
  197.    } while (num);
  198.  
  199.    num = (long) getpid();
  200.    do {
  201.     *s++ = string[num % size];
  202.     num /= size;
  203.    } while (num);
  204.    *s = '\0';
  205. }
  206. #endif /* MINIMALIST */
  207.  
  208. static struct timeb epoch;
  209.  
  210. char *
  211. fix_date(s)
  212.      char *s;
  213. {
  214.   time_t absdate;
  215.   
  216.   ftime(&epoch);
  217.   if ((absdate = getabsdate(s, &epoch)) < 0)
  218.     drop("Date:");
  219.   else
  220.       return(fixgmt(absdate, s, "Date:"));
  221. }
  222.  
  223. /*
  224.  * in anne.jones, there's a note that this will use getreldate instead
  225.  *  at some point in the future
  226.  */
  227. char *
  228. fix_expire(s)
  229.      char *s;
  230. {
  231.   time_t absdate;
  232.  
  233.   ftime(&epoch);
  234.   if ((absdate = getdate(s, &epoch)) < (time_t)0)
  235.     drop("Expires:");
  236.   else
  237.       return(fixgmt(absdate, s, "Expires:"));
  238. }
  239.  
  240. drop(s)
  241.      char *s;
  242. {
  243.   fprintf(stderr, "anne.jones: bad %s header\n", s);
  244.   exit(1);
  245. }
  246.  
  247. char *
  248. fixgmt(t, s, head)
  249.      time_t t;
  250.      char *s;
  251.      char *head;
  252. {
  253.   register struct tm *prstime = gmtime(&t);
  254.   return (legaldate(prstime, s, head));
  255. }
  256.  
  257. char *mon[12] = {
  258.   "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  259.   "Aug", "Sep", "Oct", "Nov", "Dec",
  260. };
  261.  
  262. char *
  263. legaldate(tm, s, head)
  264.      struct tm *tm;
  265.      char *s;
  266.      char *head;
  267. {
  268.   static char *dayname[8] = {  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
  269.                    "Sat", "Sun",
  270.                 };
  271.   char *_tmp = safemalloc(29 + strlen(head));
  272.  
  273.   sprintf(_tmp,
  274.       "%s %s, %02d %s %02d %02d:%02d:%02d GMT",
  275.       head,
  276.       *(dayname + tm->tm_wday), tm->tm_mday,
  277.       *(mon + tm->tm_mon), tm->tm_year,
  278.       tm->tm_hour, tm->tm_min, tm->tm_sec);
  279.   free(s);
  280.   return(_tmp);
  281. }
  282.