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.
6 This file is part of GNU Emacs.
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)
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.
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. */
26 doprnt (buffer
, bufsize
, format
, format_end
, nargs
, args
)
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.. */
42 int size
; /* Field width factor; e.g., %90d */
45 format_end
= format
+ strlen (format
);
48 while (fmt
!= format_end
&& bufsize
> 0) /* Loop until end of format string or buffer full */
50 if (*fmt
== '%') /* Check for a '%' character */
53 /* Copy this one %-spec into fmtcopy. */
59 if (! (*fmt
>= '0' && *fmt
<= '9') && *fmt
!= '-' && *fmt
!= ' ')
68 error ("Invalid format operation %%%c", fmt
[-1]);
75 error ("Format string wants too many arguments");
76 sprintf (tembuf
, fmtcpy
, args
[cnt
++]);
77 /* Now copy tembuf into final output, truncating as nec. */
85 error ("Format string wants too many arguments");
88 minlen
= atoi (&fmtcpy
[1]);
89 /* Copy string into final output, truncating if no room. */
91 tem
= strlen (string
);
94 while (minlen
> tem
&& bufsize
> 0)
104 strncpy (bufptr
, string
, tem
);
109 while (minlen
< - tem
&& bufsize
> 0)
121 error ("Format string wants too many arguments");
122 *bufptr
++ = (int) args
[cnt
++];
127 fmt
--; /* Drop thru and this % will be treated as normal */
130 *bufptr
++ = *fmt
++; /* Just some characters; Copy 'em */
134 *bufptr
= 0; /* Make sure our string end with a '\0' */
135 return bufptr
- buffer
;