"Fossies" - the Fresh Open Source Software Archive

Member "mlmmj-1.3.0/contrib/receivestrip/mlmmj_src/strgen.c" (13 Mar 2012, 7426 Bytes) of package /linux/privat/mlmmj-1.3.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2003 Mads Martin Joergensen <mmj at mmj.dk>
    2  *
    3  * $Id$
    4  *
    5  * Permission is hereby granted, free of charge, to any person obtaining a copy
    6  * of this software and associated documentation files (the "Software"), to
    7  * deal in the Software without restriction, including without limitation the
    8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    9  * sell copies of the Software, and to permit persons to whom the Software is
   10  * furnished to do so, subject to the following conditions:
   11  *
   12  * The above copyright notice and this permission notice shall be included in
   13  * all copies or substantial portions of the Software.
   14  *
   15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   21  * IN THE SOFTWARE.
   22  */
   23 
   24 #include <stdio.h>
   25 #include <stdlib.h>
   26 #include <string.h>
   27 #include <stdarg.h>
   28 #include <unistd.h>
   29 #include <netdb.h>
   30 #include <libgen.h>
   31 #include <time.h>
   32 #include <ctype.h>
   33 #include <errno.h>
   34 
   35 #include "mlmmj.h"
   36 #include "strgen.h"
   37 #include "wrappers.h"
   38 #include "memory.h"
   39 #include "log_error.h"
   40 
   41 char *random_str()
   42 {
   43     size_t len = 17;
   44     char *dest = mymalloc(len);
   45 
   46     snprintf(dest, len, "%08x%08x", random_int(), random_int());
   47 
   48     return dest;
   49 }
   50 
   51 char *random_plus_addr(const char *addr)
   52 {
   53     size_t len = strlen(addr) + 128;
   54     char *dest = mymalloc(len);
   55     char *atsign;
   56     char *tmpstr;
   57 
   58     tmpstr = mymalloc(len);
   59     snprintf(tmpstr, len, "%s", addr);
   60 
   61     atsign = strchr(tmpstr, '@');
   62     MY_ASSERT(atsign);
   63     *atsign = '=';
   64 
   65     snprintf(dest, len, "%08x%08x-%s", random_int(), random_int(), tmpstr);
   66 
   67     myfree(tmpstr);
   68     
   69     return dest;
   70 }
   71 
   72 char *headerstr(const char *headertoken, const char *str)
   73 {
   74     size_t len = strlen(headertoken) + strlen(str) + 2;
   75     char *dest = mymalloc(len);
   76 
   77     snprintf(dest, len, "%s%s\n", headertoken, str);
   78 
   79     return dest;
   80 }
   81 
   82 char *genlistname(const char *listaddr)
   83 {
   84     size_t len;
   85     char *dest, *atsign;
   86 
   87     atsign = strchr(listaddr, '@');
   88     MY_ASSERT(atsign);
   89     len = atsign - listaddr + 1;
   90     dest = mymalloc(len);
   91     
   92     snprintf(dest, len, "%s", listaddr);
   93 
   94     return dest;
   95 }
   96 
   97 char *genlistfqdn(const char *listaddr)
   98 {
   99     size_t len;
  100     char *dest, *atsign;
  101 
  102     atsign = strchr(listaddr, '@');
  103     MY_ASSERT(atsign);
  104     len = strlen(listaddr) - (atsign - listaddr);
  105     dest = mymalloc(len);
  106     snprintf(dest, len, "%s", atsign + 1);
  107 
  108     return dest;
  109 }
  110 
  111 char *concatstr(int count, ...)
  112 {
  113     va_list arg;
  114         const char *str;
  115         int i;
  116         size_t len = 0;
  117         char *retstr;
  118 
  119         va_start(arg, count);
  120 
  121         for(i = 0; i < count; i++) {
  122                 str = va_arg(arg, const char *);
  123                 if(str)
  124                         len += strlen(str);
  125         }
  126 
  127         retstr = mymalloc(len + 1);
  128         retstr[0] = retstr[len] = 0;
  129 
  130         va_start(arg, count);
  131 
  132         for(i = 0; i < count; i++) {
  133                 str = va_arg(arg, const char *);
  134                 if(str)
  135                         strcat(retstr, str);
  136         }
  137 
  138         va_end(arg);
  139 
  140         return retstr;
  141 }
  142 
  143 char *hostnamestr()
  144 {
  145     struct hostent *hostlookup;
  146     char *hostname = NULL;
  147     size_t len = 512;
  148 
  149     for (;;) {
  150         len *= 2;
  151         myfree(hostname);
  152 
  153         hostname = mymalloc(len);
  154         hostname[len-1] = '\0';
  155 
  156         /* gethostname() is allowed to:
  157          * a) return -1 and undefined in hostname
  158          * b) return 0 and an unterminated string in hostname
  159          * c) return 0 and a NUL-terminated string in hostname
  160          *
  161          * We keep expanding the buffer until the hostname is
  162          * NUL-terminated (and pray that it is not truncated)
  163          * or an error occurs.
  164          */
  165         if (gethostname(hostname, len - 1)) {
  166             if (errno == ENAMETOOLONG) {
  167                 continue;
  168             }
  169             myfree(hostname);
  170             return mystrdup("localhost");
  171         }
  172         
  173         if (hostname[len-1] == '\0') {
  174             break;
  175         }
  176     }
  177 
  178     if (strchr(hostname, '.')) {
  179         /* hostname is FQDN */
  180         return hostname;
  181     }
  182 
  183     if ((hostlookup = gethostbyname(hostname))) {
  184         myfree(hostname);
  185         return mystrdup(hostlookup->h_name);
  186     }
  187 
  188     return hostname;
  189 }
  190 
  191 char *mydirname(const char *path)
  192 {
  193     char *mypath, *dname, *ret;
  194 
  195     mypath = mystrdup(path);
  196     dname = dirname(mypath);
  197     ret = mystrdup(dname);
  198 
  199     /* We don't free mypath until we have strdup()'ed dname, because
  200      * dirname() returns a pointer into mypath  -- mortenp 20040527 */
  201     myfree(mypath);
  202 
  203     return ret;
  204 }
  205 
  206 char *mybasename(const char *path)
  207 {
  208     char *mypath, *bname, *ret;
  209 
  210     mypath = mystrdup(path);
  211     bname = basename(mypath);
  212     ret = mystrdup(bname);
  213 
  214     /* We don't free mypath until we have strdup()'ed bname, because
  215      * basename() returns a pointer into mypath  -- mortenp 20040527 */
  216     myfree(mypath);
  217     
  218     return ret;
  219 }
  220 
  221 char *cleanquotedp(const char *qpstr)
  222 {
  223     char *retstr;
  224     char qc[3];
  225     const char *c = qpstr;
  226     long qcval;
  227     int i = 0;
  228     size_t len;
  229 
  230     /* XXX: We only use this function for checking whether the subject
  231      * prefix is only present, so the recoding is neither guaranteed
  232      * complete nor correct */
  233 
  234     len = strlen(qpstr);
  235     retstr = mymalloc(len + 1);
  236     retstr[len] = '\0';
  237     qc[2] = '\0';
  238     while(c < qpstr+len) {
  239         switch(*c) {
  240             case '=':
  241                 c++;
  242                 if (!isxdigit(*c))
  243                     break;
  244                 qc[0] = *(c++);
  245                 if (!isxdigit(*c))
  246                     break;
  247                 qc[1] = *(c++);
  248                 qcval = strtol(qc, NULL, 16);
  249                 if(qcval)
  250                     retstr[i++] = (char)qcval;
  251                 break;
  252             case '_':
  253                 retstr[i++] = ' ';
  254                 c++;
  255                 break;
  256             default:
  257                 retstr[i++] = *(c++);
  258                 break;
  259         }
  260     }
  261     
  262     retstr[i] = '\0';
  263 
  264     return retstr;
  265 }
  266 
  267 char *genmsgid(const char *fqdn)
  268 {
  269     char buf[256];
  270 
  271     snprintf(buf, sizeof(buf), "Message-ID: <%ld-%d-mlmmj-%08x@%s>\n",
  272             (long int)time(NULL), (int)getpid(), random_int(), fqdn);
  273 
  274     return mystrdup(buf);
  275 }
  276 
  277 char *gendatestr()
  278 {
  279     time_t t;
  280     struct tm gmttm, lttm;
  281     int dayyear;
  282     char *timestr;
  283     const char *weekday = NULL, *month = NULL;
  284 
  285     /* 6 + 26 + ' ' + timezone which is 5 + '\n\0' == 40 */
  286     timestr = (char *)mymalloc(40);
  287     t = time(NULL);
  288 
  289     localtime_r(&t, &lttm);
  290     gmtime_r(&t, &gmttm);
  291 
  292     t = (((lttm.tm_hour - gmttm.tm_hour) * 60) +
  293         (lttm.tm_min - gmttm.tm_min)) * 60;
  294     
  295     dayyear = lttm.tm_yday - gmttm.tm_yday;
  296     if(dayyear) {
  297         if (dayyear == -1 || dayyear > 1)
  298             t -= 24 * 60 * 60;
  299         else
  300             t += 24 * 60 * 60;
  301     }
  302 
  303     switch(lttm.tm_wday) {
  304         case 0: weekday = "Sun";
  305             break;
  306         case 1: weekday = "Mon";
  307             break;
  308         case 2: weekday = "Tue";
  309             break;
  310         case 3: weekday = "Wed";
  311             break;
  312         case 4: weekday = "Thu";
  313             break;
  314         case 5: weekday = "Fri";
  315             break;
  316         case 6: weekday = "Sat";
  317             break;
  318         default:
  319             break;
  320     }
  321     switch(lttm.tm_mon) {
  322         case 0: month = "Jan";
  323             break;
  324         case 1: month = "Feb";
  325             break;
  326         case 2: month = "Mar";
  327             break;
  328         case 3: month = "Apr";
  329             break;
  330         case 4: month = "May";
  331             break;
  332         case 5: month = "Jun";
  333             break;
  334         case 6: month = "Jul";
  335             break;
  336         case 7: month = "Aug";
  337             break;
  338         case 8: month = "Sep";
  339             break;
  340         case 9: month = "Oct";
  341             break;
  342         case 10: month = "Nov";
  343              break;
  344         case 11: month = "Dec";
  345              break;
  346         default:
  347              break;
  348     }
  349 
  350     
  351     snprintf(timestr, 40, "Date: %s, %02d %s %04d %02d:%02d:%02d %+05d\n",  
  352             weekday, lttm.tm_mday, month, lttm.tm_year + 1900,
  353             lttm.tm_hour, lttm.tm_min, lttm.tm_sec, ((int)t)/36);
  354 
  355     return timestr;
  356 }