Back to index

courier  0.68.2
rfc2045mkboundary.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2011 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 #if    HAVE_CONFIG_H
00007 #include "rfc2045_config.h"
00008 #endif
00009 #include      "rfc2045.h"
00010 #if    HAVE_UNISTD_H
00011 #include      <unistd.h>
00012 #endif
00013 #include      <stdio.h>
00014 #include      <stdlib.h>
00015 #include      <time.h>
00016 #include      "numlib/numlib.h"
00017 
00018 
00019 #if    HAS_GETHOSTNAME
00020 
00021 #else
00022 
00023 extern int gethostname(char *, size_t);
00024 #endif
00025 
00026 extern void rfc2045_enomem();
00027 
00028 char *rfc2045_mk_boundary(struct rfc2045 *s, struct rfc2045src *src)
00029 {
00030 char   hostnamebuf[256];
00031 pid_t  mypid;
00032 char   pidbuf[NUMBUFSIZE];
00033 time_t mytime;
00034 char   timebuf[NUMBUFSIZE];
00035 static size_t cnt=0;
00036 char   cntbuf[NUMBUFSIZE];
00037 char   *p;
00038 int    rc;
00039 
00040        hostnamebuf[sizeof(hostnamebuf)-1]=0;
00041        if (gethostname(hostnamebuf, sizeof(hostnamebuf)-1))
00042               hostnamebuf[0]=0;
00043        mypid=getpid();
00044        time(&mytime);
00045        libmail_str_pid_t(mypid, pidbuf);
00046        libmail_str_time_t(mytime, timebuf);
00047        for (;;)
00048        {
00049               char tempbuf[NUMBUFSIZE];
00050 
00051               libmail_str_size_t(++cnt, tempbuf);
00052               sprintf(cntbuf, "%4s", tempbuf);
00053               for (p=cntbuf; *p == ' '; *p++ = '0')
00054                      ;
00055               p=malloc(strlen(hostnamebuf)+strlen(pidbuf)
00056                       +strlen(timebuf)+strlen(cntbuf)+11);
00057               if (!p)
00058               {
00059                      rfc2045_enomem();
00060                      return (NULL);
00061               }
00062 
00063               sprintf(p, "=_%-1.30s-%s-%s-%s", hostnamebuf,
00064                      pidbuf, timebuf, cntbuf);
00065               if ((rc=rfc2045_try_boundary(s, src, p)) == 0)
00066                      break;
00067               free(p);
00068               if (rc < 0)
00069                      return (NULL);
00070        }
00071        return (p);
00072 }