Annotation of 43BSDReno/contrib/emacs-18.55/src/doprnt.c, revision 1.1.1.1

1.1       root        1: /* Output like sprintf to a buffer of specified size.
                      2:    Also takes args differently: pass one pointer to an array of strings
                      3:    in addition to the format string which is separate.
                      4:    Copyright (C) 1985 Free Software Foundation, Inc.
                      5: 
                      6: This file is part of GNU Emacs.
                      7: 
                      8: GNU Emacs is distributed in the hope that it will be useful,
                      9: but WITHOUT ANY WARRANTY.  No author or distributor
                     10: accepts responsibility to anyone for the consequences of using it
                     11: or for whether it serves any particular purpose or works at all,
                     12: unless he says so in writing.  Refer to the GNU Emacs General Public
                     13: License for full details.
                     14: 
                     15: Everyone is granted permission to copy, modify and redistribute
                     16: GNU Emacs, but only under the conditions described in the
                     17: GNU Emacs General Public License.   A copy of this license is
                     18: supposed to have been given to you along with GNU Emacs so you
                     19: can know your rights and responsibilities.  It should be in a
                     20: file named COPYING.  Among other things, the copyright notice
                     21: and this notice must be preserved on all copies.  */
                     22: 
                     23: 
                     24: #include <stdio.h>
                     25: #include <ctype.h>
                     26: 
                     27: doprnt (buffer, bufsize, format, nargs, args)
                     28:      char *buffer;
                     29:      register int bufsize;
                     30:      char *format;
                     31:      int nargs;
                     32:      char **args;
                     33: {
                     34:   int cnt = 0;                 /* Number of arg to gobble next */
                     35:   register char *fmt = format; /* Pointer into format string */
                     36:   register char *bufptr = buffer; /* Pointer into output buffer.. */
                     37:   char tembuf[80];
                     38:   register int tem;
                     39:   char *string;
                     40:   char fmtcpy[20];
                     41:   int minlen;
                     42: 
                     43:   bufsize--;
                     44:   while (*fmt && bufsize > 0)  /* Loop until end of format string or buffer full */
                     45:     {
                     46:       if (*fmt == '%') /* Check for a '%' character */
                     47:        {
                     48:          fmt++;
                     49:          /* Copy this one %-spec into fmtcopy.  */
                     50:          string = fmtcpy;
                     51:          *string++ = '%';
                     52:          while (1)
                     53:            {
                     54:              *string++ = *fmt;
                     55:              if (! (*fmt >= '0' && *fmt <= '9') && *fmt != '-' && *fmt != ' ')
                     56:                break;
                     57:              fmt++;
                     58:            }
                     59:          *string = 0;
                     60:          minlen = 0;
                     61:          switch (*fmt++)
                     62:            {
                     63:            default:
                     64:              error ("Invalid format operation %%%c", fmt[-1]);
                     65: 
                     66:            case 'b':
                     67:            case 'd':
                     68:            case 'o':
                     69:            case 'x':
                     70:              if (cnt == nargs)
                     71:                error ("Format string wants too many arguments");
                     72:              sprintf (tembuf, fmtcpy, args[cnt++]);
                     73:              /* Now copy tembuf into final output, truncating as nec.  */
                     74:              string = tembuf;
                     75:              goto doit;
                     76: 
                     77:            case 's':
                     78:              if (cnt == nargs)
                     79:                error ("Format string wants too many arguments");
                     80:              string = args[cnt++];
                     81:              if (fmtcpy[1] != 's')
                     82:                minlen = atoi (&fmtcpy[1]);
                     83:              /* Copy string into final output, truncating if no room.  */
                     84:            doit:
                     85:              tem = strlen (string);
                     86:              minlen -= tem;
                     87:              while (minlen > 0 && bufsize > 0)
                     88:                {
                     89:                  *bufptr++ = ' ';
                     90:                  bufsize--;
                     91:                  minlen--;
                     92:                }
                     93:              if (tem > bufsize)
                     94:                tem = bufsize;
                     95:              strncpy (bufptr, string, tem);
                     96:              bufptr += tem;
                     97:              bufsize -= tem;
                     98:              continue;
                     99: 
                    100:            case 'c':
                    101:              if (cnt == nargs)
                    102:                error ("Format string wants too many arguments");
                    103:              *bufptr++ = (int) args[cnt++];
                    104:              bufsize--;
                    105:              continue;
                    106: 
                    107:            case '%':
                    108:              fmt--;    /* Drop thru and this % will be treated as normal */
                    109:            }
                    110:        }
                    111:       *bufptr++ = *fmt++;      /* Just some characters; Copy 'em */
                    112:       bufsize--;
                    113:     };
                    114: 
                    115:   *bufptr = 0;         /* Make sure our string end with a '\0' */
                    116:   return bufptr - buffer;
                    117: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.