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. */
27 /* Generate output from a format-spec FORMAT,
28 terminated at position FORMAT_END.
29 Output goes in BUFFER, which has room for BUFSIZE chars.
30 If the output does not fit, truncate it to fit.
31 Returns the number of characters stored into BUFFER.
32 ARGS points to the vector of arguments, and NARGS says how many.
33 A double counts as two arguments. */
35 doprnt (buffer
, bufsize
, format
, format_end
, nargs
, args
)
43 int cnt
= 0; /* Number of arg to gobble next */
44 register char *fmt
= format
; /* Pointer into format string */
45 register char *bufptr
= buffer
; /* Pointer into output buffer.. */
47 /* Use this for sprintf unless we need something really big. */
50 /* Size of sprintf_buffer. */
51 int size_allocated
= 100;
53 /* Buffer to use for sprintf. Either tembuf or same as BIG_BUFFER. */
54 char *sprintf_buffer
= tembuf
;
56 /* Buffer we have got with malloc. */
61 char fixed_buffer
[20]; /* Default buffer for small formatting. */
64 int size
; /* Field width factor; e.g., %90d */
65 char charbuf
[2]; /* Used for %c. */
68 format_end
= format
+ strlen (format
);
70 if ((format_end
- format
+ 1) < sizeof (fixed_buffer
))
71 fmtcpy
= fixed_buffer
;
73 fmtcpy
= (char *) alloca (format_end
- format
+ 1);
77 /* Loop until end of format string or buffer full. */
78 while (fmt
!= format_end
&& bufsize
> 0)
80 if (*fmt
== '%') /* Check for a '%' character */
85 /* Copy this one %-spec into fmtcpy. */
91 if (! (*fmt
>= '0' && *fmt
<= '9')
92 && *fmt
!= '-' && *fmt
!= ' '&& *fmt
!= '.')
97 /* Get an idea of how much space we might need. */
98 size_bound
= atoi (&fmtcpy
[1]);
100 /* Avoid pitfall of negative "size" parameter ("%-200d"). */
102 size_bound
= -size_bound
;
105 /* Make sure we have that much. */
106 if (size_bound
> size_allocated
)
109 big_buffer
= (char *) xrealloc (big_buffer
, size_bound
);
111 big_buffer
= (char *) xmalloc (size_bound
);
112 sprintf_buffer
= big_buffer
;
113 size_allocated
= size_bound
;
119 error ("Invalid format operation %%%c", fmt
[-1]);
126 error ("not enough arguments for format string");
127 sprintf (sprintf_buffer
, fmtcpy
, args
[cnt
++]);
128 /* Now copy into final output, truncating as nec. */
129 string
= sprintf_buffer
;
136 union { double d
; char *half
[2]; } u
;
137 if (cnt
+ 1 == nargs
)
138 error ("not enough arguments for format string");
139 u
.half
[0] = args
[cnt
++];
140 u
.half
[1] = args
[cnt
++];
141 sprintf (sprintf_buffer
, fmtcpy
, u
.d
);
142 /* Now copy into final output, truncating as nec. */
143 string
= sprintf_buffer
;
151 error ("not enough arguments for format string");
152 string
= args
[cnt
++];
153 if (fmtcpy
[1] != 's')
154 minlen
= atoi (&fmtcpy
[1]);
155 /* Copy string into final output, truncating if no room. */
157 tem
= strlen (string
);
161 while (minlen
> tem
&& bufsize
> 0)
171 strncpy (bufptr
, string
, tem
);
176 while (minlen
< - tem
&& bufsize
> 0)
188 error ("not enough arguments for format string");
189 *charbuf
= (int) args
[cnt
++];
192 if (fmtcpy
[1] != 'c')
193 minlen
= atoi (&fmtcpy
[1]);
197 fmt
--; /* Drop thru and this % will be treated as normal */
200 *bufptr
++ = *fmt
++; /* Just some characters; Copy 'em */
204 /* If we had to malloc something, free it. */
208 *bufptr
= 0; /* Make sure our string end with a '\0' */
209 return bufptr
- buffer
;