Annotation of GNUtools/emacs/src/doprnt.c, revision 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 free software; you can redistribute it and/or modify
        !             9: it under the terms of the GNU General Public License as published by
        !            10: the Free Software Foundation; either version 1, or (at your option)
        !            11: any later version.
        !            12: 
        !            13: GNU Emacs is distributed in the hope that it will be useful,
        !            14: but WITHOUT ANY WARRANTY; without even the implied warranty of
        !            15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        !            16: GNU General Public License for more details.
        !            17: 
        !            18: You should have received a copy of the GNU General Public License
        !            19: along with GNU Emacs; see the file COPYING.  If not, write to
        !            20: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
        !            21: 
        !            22: 
        !            23: #include <stdio.h>
        !            24: #include <ctype.h>
        !            25: 
        !            26: doprnt (buffer, bufsize, format, nargs, args)
        !            27:      char *buffer;
        !            28:      register int bufsize;
        !            29:      char *format;
        !            30:      int nargs;
        !            31:      char **args;
        !            32: {
        !            33:   int cnt = 0;                 /* Number of arg to gobble next */
        !            34:   register char *fmt = format; /* Pointer into format string */
        !            35:   register char *bufptr = buffer; /* Pointer into output buffer.. */
        !            36:   char tembuf[80];
        !            37:   register int tem;
        !            38:   char *string;
        !            39:   char fmtcpy[20];
        !            40:   int minlen;
        !            41: 
        !            42:   bufsize--;
        !            43:   while (*fmt && bufsize > 0)  /* Loop until end of format string or buffer full */
        !            44:     {
        !            45:       if (*fmt == '%') /* Check for a '%' character */
        !            46:        {
        !            47:          fmt++;
        !            48:          /* Copy this one %-spec into fmtcopy.  */
        !            49:          string = fmtcpy;
        !            50:          *string++ = '%';
        !            51:          while (1)
        !            52:            {
        !            53:              *string++ = *fmt;
        !            54:              if (! (*fmt >= '0' && *fmt <= '9') && *fmt != '-' && *fmt != ' ')
        !            55:                break;
        !            56:              fmt++;
        !            57:            }
        !            58:          *string = 0;
        !            59:          minlen = 0;
        !            60:          switch (*fmt++)
        !            61:            {
        !            62:            default:
        !            63:              error ("Invalid format operation %%%c", fmt[-1]);
        !            64: 
        !            65:            case 'b':
        !            66:            case 'd':
        !            67:            case 'o':
        !            68:            case 'x':
        !            69:              if (cnt == nargs)
        !            70:                error ("Format string wants too many arguments");
        !            71:              sprintf (tembuf, fmtcpy, args[cnt++]);
        !            72:              /* Now copy tembuf into final output, truncating as nec.  */
        !            73:              string = tembuf;
        !            74:              goto doit;
        !            75: 
        !            76:            case 's':
        !            77:              if (cnt == nargs)
        !            78:                error ("Format string wants too many arguments");
        !            79:              string = args[cnt++];
        !            80:              if (fmtcpy[1] != 's')
        !            81:                minlen = atoi (&fmtcpy[1]);
        !            82:              /* Copy string into final output, truncating if no room.  */
        !            83:            doit:
        !            84:              tem = strlen (string);
        !            85:              if (minlen > 0)
        !            86:                {
        !            87:                  while (minlen > tem && bufsize > 0)
        !            88:                    {
        !            89:                      *bufptr++ = ' ';
        !            90:                      bufsize--;
        !            91:                      minlen--;
        !            92:                    }
        !            93:                  minlen = 0;
        !            94:                }
        !            95:              if (tem > bufsize)
        !            96:                tem = bufsize;
        !            97:              strncpy (bufptr, string, tem);
        !            98:              bufptr += tem;
        !            99:              bufsize -= tem;
        !           100:              if (minlen < 0)
        !           101:                {
        !           102:                  while (minlen < - tem && bufsize > 0)
        !           103:                    {
        !           104:                      *bufptr++ = ' ';
        !           105:                      bufsize--;
        !           106:                      minlen++;
        !           107:                    }
        !           108:                  minlen = 0;
        !           109:                }
        !           110:              continue;
        !           111: 
        !           112:            case 'c':
        !           113:              if (cnt == nargs)
        !           114:                error ("Format string wants too many arguments");
        !           115:              *bufptr++ = (int) args[cnt++];
        !           116:              bufsize--;
        !           117:              continue;
        !           118: 
        !           119:            case '%':
        !           120:              fmt--;    /* Drop thru and this % will be treated as normal */
        !           121:            }
        !           122:        }
        !           123:       *bufptr++ = *fmt++;      /* Just some characters; Copy 'em */
        !           124:       bufsize--;
        !           125:     };
        !           126: 
        !           127:   *bufptr = 0;         /* Make sure our string end with a '\0' */
        !           128:   return bufptr - buffer;
        !           129: }

unix.superglobalmegacorp.com

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