Back to index

courier  0.68.2
comerrmsg.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "courier.h"
00007 #include      <stdlib.h>
00008 #include      <string.h>
00009 #include      <ctype.h>
00010 
00011 /*
00012        Take an error number, and a possibly multiline error message text.
00013        Prefix each line in the error message with the error number, as in
00014        RFC821.
00015 
00016        makeerrmsgtext also ignores existing RFC821 error numbers in errtext,
00017        so that you can take the output of makeerrmsgtext, append some
00018        additional text to it, and call makeerrmsgtext again, to reformat it.
00019        
00020 */
00021 
00022 char   *makeerrmsgtext(int errnum, const char *errtext)
00023 {
00024 char   errnumbuf[4];
00025 int    i;
00026 const char *p;
00027 char   *buf, *q, *r;
00028 int    lastchar;
00029 
00030        if (!errtext || !*errtext)  errtext="Failed.";
00031 
00032        errnumbuf[3]=0;
00033        for (i=2; i >= 0; --i)
00034        {
00035               errnumbuf[i]= (errnum % 10)+'0';
00036               errnum /= 10;
00037        }
00038 
00039        /*
00040        ** Calculate size of buffer we'll need.  For each line, we
00041        ** add four bytes for the error number.
00042        ** One line errors may need \n appended.
00043        */
00044 
00045        i=6+strlen(errtext);
00046               /*
00047               ** First line's toll, plus optional trailing \r\n,
00048               ** and null byte.
00049               */
00050 
00051        for (p=errtext; *p; p++)
00052               if (*p == '\n')      i += 4;
00053 
00054        if ((buf=malloc(i)) == 0)   clog_msg_errno();
00055        for (q=r=buf, p=errtext; *p; )
00056        {
00057               r=q;
00058               strcpy(q, errnumbuf);
00059               q += 3;
00060               *q++ = '-';
00061 
00062               if (isdigit((int)(unsigned char)p[0]) &&
00063                      isdigit((int)(unsigned char)p[1]) &&
00064                      isdigit((int)(unsigned char)p[2]) &&
00065                      (p[3] == '-' || p[3] == ' '))
00066                      p += 4;       /* Remove old error message number */
00067 
00068               while (*p)
00069               {
00070                      lastchar=*q++ = *p++;
00071 
00072                      if (lastchar != '\n' && *p == '\0')
00073                      {
00074                             *q++='\n';
00075                      }
00076                      if (lastchar == '\n')       break;
00077               }
00078        }
00079        r[3]=' ';
00080        *q=0;
00081        return (buf);
00082 }