1: /*
   2:  *      Program Name:   mkerrlst.c
   3:  *      Date: March 5, 1996
   4:  *      Author: S.M. Schultz
   5:  *
   6:  *      -----------------   Modification History   ---------------
   7:  *      Version Date            Reason For Modification
   8:  *      1.0     05Mar96         1. Initial release into the public domain.
   9:  *	1.1	14Mar96		2. Add ability to take messages from a file
  10:  *				   in place of a fixed array.
  11: */
  12: 
  13: /*
  14:  * mkerrlst - writes the error messages from sys_errlist[] or a input file of
  15:  *	      strings to a formatted file prepending a header for quick access.
  16:  *
  17:  * This code can be used as a template for creating additional error message
  18:  * files.  The "-o" option can be used to specify a different output file
  19:  * than _PATH_SYSERRLST.  The "-i" option is used to specify an input file
  20:  * of error messages (one per line, maximum length 80 characters).  Error
  21:  * messages are numbered starting at 0.
  22:  *
  23:  * If no "-i" option is used the sys_errlist[] array is used by default.
  24: */
  25: 
  26: #include <stdio.h>
  27: #include <stdlib.h>
  28: #include <sys/types.h>
  29: #include <sys/uio.h>
  30: #include <fcntl.h>
  31: #include <errlst.h>
  32: #include <string.h>
  33: 
  34:     int outf;
  35:     struct  iovec   iov[2];
  36:     off_t   msgoff;
  37:     char    *outfn, *infn, *msg, *getmsg();
  38:     struct  ERRLSTHDR   ehdr;
  39:     FILE    *infp;
  40: 
  41: extern  char    *__progname;
  42: extern  char    *sys_errlist[];
  43: extern  int sys_nerr;
  44: 
  45: main(argc,argv)
  46:     {
  47:     register int    c;
  48:     int len, maxlen = -1;
  49: 
  50:     while   ((c = getopt(argc, argv, "o:i:")) != EOF)
  51:         {
  52:         switch  (c)
  53:             {
  54:             case    'o':
  55:                 outfn = optarg;
  56:                 break;
  57:             case    'i':
  58:                 infn = optarg;
  59:                 break;
  60:             case    '?':
  61:             default:
  62:                 usage();
  63:             }
  64:         }
  65: 
  66:     if  (!outfn)
  67:         outfn = _PATH_SYSERRLST;
  68: 
  69:     if  ((outf = open(outfn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
  70:         err(1, "can't create %s", outfn);
  71:     if  (fchmod(outf, 0644) < 0)
  72:         err(1, "fchmod(%d,644)", outf);
  73: 
  74:     if  (infn)
  75:         {
  76:         infp = fopen(infn, "r");
  77:         if  (!infp)
  78:             err(1, "fopen(%s, r) failed\n", infn);
  79:         }
  80: 
  81:     for (c = 0; msg = getmsg(c, infp); c++)
  82:         {
  83:         len = strlen(msg);
  84:         if  (len > maxlen)
  85:             maxlen = len;
  86:         }
  87: 
  88:     if  (infp)
  89:         rewind(infp);
  90: 
  91: /*
  92:  * Adjust the count so that the int written to the file is the highest valid
  93:  * message number rather than the number of the first invalid error number.
  94:  *
  95:  * At the present time nothing much uses the maximum message length.
  96: */
  97:     ehdr.magic = ERRMAGIC;
  98:     ehdr.maxmsgnum = c - 1;
  99:     ehdr.maxmsglen = maxlen;
 100:     if  (write(outf, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
 101:         err(1, "write");
 102: 
 103:     msgoff = sizeof (ehdr) + ((off_t)sizeof (struct ERRLST) * sys_nerr);
 104:     iov[0].iov_base = (caddr_t)&msgoff;
 105:     iov[0].iov_len = sizeof (off_t);
 106: 
 107:     iov[1].iov_len = sizeof (int);
 108: 
 109:     for (c = 0; msg = getmsg(c, infp); c++)
 110:         {
 111:         len = strlen(msg);
 112:         iov[1].iov_base = (caddr_t)&len;
 113:         writev(outf, iov, 2);
 114:         msgoff += len;
 115:         msgoff++;       /* \n between messages */
 116:         }
 117: 
 118:     iov[1].iov_base = "\n";
 119:     iov[1].iov_len = 1;
 120: 
 121:     if  (infp)
 122:         rewind(infp);
 123: 
 124:     for (c = 0; msg = getmsg(c, infp); c++)
 125:         {
 126:         iov[0].iov_base = msg;
 127:         iov[0].iov_len = strlen(iov[0].iov_base);
 128:         writev(outf, iov, 2);
 129:         }
 130:     close(outf);
 131:     if  (infp)
 132:         fclose(infp);
 133:     exit(0);
 134:     }
 135: 
 136: char *
 137: getmsg(c, fp)
 138:     int c;
 139:     FILE    *fp;
 140:     {
 141:     static  char    buf[81];
 142:     register char   *cp;
 143: 
 144:     if  (fp)
 145:         {
 146:         cp = fgets(buf, sizeof (buf) - 1, fp);
 147:         if  (!cp)
 148:             return(NULL);
 149:         cp = index(buf, '\n');
 150:         if  (cp)
 151:             *cp = '\0';
 152:         return(buf);
 153:         }
 154:     if  (c < sys_nerr)
 155:         return(sys_errlist[c]);
 156:     return(NULL);
 157:     }
 158: 
 159: usage()
 160:     {
 161:     fprintf(stderr, "usage: %s -o filename\n", __progname);
 162:     exit(1);
 163:     }

Defined functions

getmsg defined in line 136; used 4 times
main defined in line 45; never used
usage defined in line 159; used 1 times
  • in line 62

Defined variables

ehdr defined in line 38; used 7 times
infn defined in line 37; used 4 times
iov defined in line 35; used 11 times
msg defined in line 37; used 6 times
msgoff defined in line 36; used 4 times
outf defined in line 34; used 7 times
outfn defined in line 37; used 5 times
Last modified: 1996-03-15
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1080
Valid CSS Valid XHTML 1.0 Strict