home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / sm-smtp / misc.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  4KB  |  229 lines

  1.  
  2. /*
  3. **  Miscellaneous support functions for smtp (borrowed from smail)
  4. */
  5.  
  6. # include    "smtp.h"
  7. #ifdef BSD
  8. # include    <sys/timeb.h>
  9. #endif
  10. #ifdef SYSV
  11. # include    <sys/utsname.h>
  12. #endif
  13.  
  14. char hostdomain[256];
  15. char hostname[256];
  16.  
  17. extern struct tm *localtime();
  18.  
  19. struct tm *gmt, *loc;        /* GMT and local time structure    */
  20. time_t now;            /* current system time        */
  21. char nows[50];            /* time in ctime format        */
  22. char arpanows[50];        /* time in arpa format        */
  23.  
  24. setdates()
  25. {
  26.     time_t time();
  27.     struct tm *gmtime();
  28.     char *ctime(), *arpadate();
  29.  
  30.     (void) time(&now);
  31.     (void) strcpy(nows, ctime(&now));
  32.     gmt = gmtime(&now);
  33.     loc = localtime(&now);
  34.     (void) strcpy(arpanows, arpadate(nows));
  35. }
  36.  
  37. /*
  38. **  Note: This routine was taken from sendmail
  39. **
  40. **  ARPADATE -- Create date in ARPANET format
  41. **
  42. **    Parameters:
  43. **        ud -- unix style date string.  if NULL, one is created.
  44. **
  45. **    Returns:
  46. **        pointer to an ARPANET date field
  47. **
  48. **    Side Effects:
  49. **        none
  50. **
  51. **    WARNING:
  52. **        date is stored in a local buffer -- subsequent
  53. **        calls will overwrite.
  54. **
  55. **    Bugs:
  56. **        Timezone is computed from local time, rather than
  57. **        from whereever (and whenever) the message was sent.
  58. **        To do better is very hard.
  59. **
  60. **        Some sites are now inserting the timezone into the
  61. **        local date.  This routine should figure out what
  62. **        the format is and work appropriately.
  63. */
  64.  
  65. char *
  66. arpadate(ud)
  67.     register char *ud;
  68. {
  69.     register char *p;
  70.     register char *q;
  71.     static char b[40];
  72.     extern char *ctime();
  73.     register int i;
  74. #ifndef BSD
  75.     extern char *tzname[];
  76.     extern long timezone;
  77.     char dspace[40];
  78.     time_t t, time();
  79.     int dst;            /* dst active */
  80.     long tz;
  81. #else
  82.     /* V7 and 4BSD */
  83.     struct timeb t;
  84.     extern struct timeb *ftime();
  85.     extern char *timezone();
  86. #endif
  87.  
  88.     /*
  89.     **  Get current time.
  90.     **    This will be used if a null argument is passed and
  91.     **    to resolve the timezone.
  92.     */
  93.  
  94. #ifndef BSD
  95.     (void) time(&t);
  96.     if (ud == NULL)
  97.         ud = ctime(&t);
  98. #else
  99.     /* V7 or 4BSD */
  100.     ftime(&t);
  101.     if (ud == NULL)
  102.         ud = ctime(&t.time);
  103. #endif
  104.  
  105.     /*
  106.     **  Crack the UNIX date line in a singularly unoriginal way.
  107.     */
  108.  
  109.     q = b;
  110.  
  111.     p = &ud[0];        /* Mon */
  112.     *q++ = *p++;
  113.     *q++ = *p++;
  114.     *q++ = *p++;
  115.     *q++ = ',';
  116.     *q++ = ' ';
  117.  
  118.     p = &ud[8];        /* 16 */
  119.     if (*p == ' ')
  120.         p++;
  121.     else
  122.         *q++ = *p++;
  123.     *q++ = *p++;
  124.     *q++ = ' ';
  125.  
  126.     p = &ud[4];        /* Sep */
  127.     *q++ = *p++;
  128.     *q++ = *p++;
  129.     *q++ = *p++;
  130.     *q++ = ' ';
  131.  
  132.     p = &ud[22];        /* 1979 */
  133.     *q++ = *p++;
  134.     *q++ = *p++;
  135.     *q++ = ' ';
  136.  
  137.     p = &ud[11];        /* 01:03:52 */
  138.     for (i = 8; i > 0; i--)
  139.         *q++ = *p++;
  140.  
  141.                 /* -PST or -PDT */
  142. #ifndef BSD
  143.     dst = localtime(&t)->tm_isdst;
  144.     tz = timezone - (dst ? 3600 : 0);
  145.     p = tzname[dst];
  146. #else
  147.     p = timezone(t.timezone, localtime(&t.time)->tm_isdst);
  148.     if (p[3] != '\0')
  149.     {
  150.         /* hours from GMT */
  151.         p += 3;
  152.         *q++ = *p++;
  153.         if (p[1] == ':')
  154.             *q++ = '0';
  155.         else
  156.             *q++ = *p++;
  157.         *q++ = *p++;
  158.         p++;        /* skip ``:'' */
  159.         *q++ = *p++;
  160.         *q++ = *p++;
  161.     }
  162.     else
  163. #endif
  164.     {
  165.         *q++ = ' ';
  166.         while (*p) *q++ = *p++;
  167.     }
  168. #ifndef BSD
  169.     if (tz != 0) {
  170.         (void) sprintf (q, " (%c%02d%02d)", 
  171.             ((tz > 0) ? '-' : '+'),
  172.             abs(tz/3600),
  173.             abs(tz%3600)/60);
  174.         q += strlen (q);
  175.     }
  176. #endif
  177.  
  178.     *q = '\0';
  179.     return (b);
  180. }
  181.  
  182. /*
  183. **
  184. **  getmynames(): what is my host name and host domain?
  185. **
  186. **  Hostname set by -h, failing that by #define HOSTNAME, failing
  187. **  that by gethostname() or uname().
  188. **  
  189. **  Hostdomain set by -h, failing that by #define HOSTDOMAIN,
  190. **  failing that as hostname.MYDOM, or as just hostname.
  191. **
  192. **  See defs.h for the inside story.
  193. **
  194. */
  195.  
  196. getmynames()
  197. {
  198. #ifdef HOSTNAME
  199.     if (!*hostname)
  200.         (void) strcpy(hostname, HOSTNAME);
  201. #endif
  202. #ifdef BSD
  203.     if (!*hostname)
  204.         gethostname(hostname, SMLBUF - 1);
  205. #endif
  206. #ifdef SYSV
  207.     if (!*hostname) {
  208.         struct utsname site;
  209.  
  210.         if (uname(&site) == 0)
  211.             (void) strcpy(hostname, site.nodename);
  212.     }
  213. #endif
  214.     if (!*hostname)
  215.         return -1;
  216. #ifdef HOSTDOMAIN
  217.     if (!*hostdomain)
  218.         (void) strcpy(hostdomain, HOSTDOMAIN);
  219. #endif
  220. #ifdef MYDOM
  221.     if (!*hostdomain)
  222.         (void) strcat(strcpy(hostdomain, hostname), MYDOM);
  223. #endif
  224.     if (!*hostdomain)
  225.         (void) strcpy(hostdomain, hostname);
  226.  
  227.     return 1;
  228. }
  229.