40
40
41
41
#include "regex/cdefs.h"
42
42
43
+ #include <stdio.h>
43
44
#include <stdarg.h>
44
45
45
46
#include <sys/ioctl.h>
46
47
#include <sys/param.h>
47
48
48
- /* IRIX doesn't do 'long long' in va_arg(), so use a typedef */
49
+ /*
50
+ * We do all internal arithmetic in the widest available integer type,
51
+ * here called long_long (or ulong_long for unsigned).
52
+ */
49
53
#ifdef HAVE_LONG_LONG_INT_64
50
- typedef long long long_long ;
51
- typedef unsigned long long ulong_long ;
54
+ typedef long long long_long ;
55
+ typedef unsigned long long ulong_long ;
56
+ #else
57
+ typedef long long_long ;
58
+ typedef unsigned long ulong_long ;
52
59
#endif
53
60
54
61
/*
@@ -64,7 +71,7 @@ typedef unsigned long long ulong_long;
64
71
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995
65
72
* A bombproof version of doprnt (dopr) included.
66
73
* Sigh. This sort of thing is always nasty do deal with.Note that
67
- * the version here does not include floating point...
74
+ * the version here does not include floating point. (now it does ... tgl)
68
75
*
69
76
* snprintf() is used instead of sprintf() as it does limit checks
70
77
* for string length. This covers a nasty loophole.
@@ -73,7 +80,7 @@ typedef unsigned long long ulong_long;
73
80
* causing nast effects.
74
81
**************************************************************/
75
82
76
- /*static char _id[] = "$Id: snprintf.c,v 1.19 1999/02/03 21:17:00 momjian Exp $";*/
83
+ /*static char _id[] = "$Id: snprintf.c,v 1.20 1999/02/06 21:51:03 tgl Exp $";*/
77
84
static char * end ;
78
85
static int SnprfOverflow ;
79
86
@@ -113,28 +120,22 @@ vsnprintf(char *str, size_t count, const char *fmt, va_list args)
113
120
* dopr(): poor man's version of doprintf
114
121
*/
115
122
116
- static void fmtstr __P ((char * value ,int ljust ,int len ,int zpad ,int maxwidth ));
117
-
118
- #ifndef HAVE_LONG_LONG_INT_64
119
- static void fmtnum __P ((longvalue ,int base ,int dosign ,int ljust ,int len ,int zpad ));
120
- #else
121
- static void fmtnum __P ((long_long value ,int base ,int dosign ,int ljust ,int len ,int zpad ));
122
- #endif
123
+ static void fmtstr (char * value ,int ljust ,int len ,int zpad ,int maxwidth );
124
+ static void fmtnum (long_long value ,int base ,int dosign ,int ljust ,int len ,int zpad );
125
+ static void fmtfloat (double value ,char type ,int ljust ,int len ,int precision ,int pointflag );
126
+ static void dostr (char * str ,int cut );
127
+ static void dopr_outch (int c );
123
128
124
- static void dostr __P ((char * ,int ) );
125
129
static char * output ;
126
- static void dopr_outch __P (( int c ));
130
+
127
131
128
132
static void
129
133
dopr (char * buffer ,const char * format ,va_list args )
130
134
{
131
135
int ch ;
132
- #ifdef HAVE_LONG_LONG_INT_64
133
136
long_long value ;
137
+ double fvalue ;
134
138
int longlongflag = 0 ;
135
- #else
136
- long value ;
137
- #endif
138
139
int longflag = 0 ;
139
140
int pointflag = 0 ;
140
141
int maxwidth = 0 ;
@@ -150,10 +151,7 @@ dopr(char *buffer, const char *format, va_list args)
150
151
{
151
152
case '%' :
152
153
ljust = len = zpad = maxwidth = 0 ;
153
- longflag = pointflag = 0 ;
154
- #ifdef HAVE_LONG_LONG_INT_64
155
- longlongflag = 0 ;
156
- #endif
154
+ longflag = longlongflag = pointflag = 0 ;
157
155
nextch :
158
156
ch = * format ++ ;
159
157
switch (ch )
@@ -191,23 +189,19 @@ dopr(char *buffer, const char *format, va_list args)
191
189
pointflag = 1 ;
192
190
gotonextch ;
193
191
case 'l' :
194
- #ifdef HAVE_LONG_LONG_INT_64
195
192
if (longflag )
196
193
longlongflag = 1 ;
197
194
else
198
- #endif
199
195
longflag = 1 ;
200
196
gotonextch ;
201
197
case 'u' :
202
198
case 'U' :
203
199
/* fmtnum(value,base,dosign,ljust,len,zpad) */
204
200
if (longflag )
205
201
{
206
- #ifdef HAVE_LONG_LONG_INT_64
207
202
if (longlongflag )
208
203
value = va_arg (args ,long_long );
209
204
else
210
- #endif
211
205
value = va_arg (args ,long );
212
206
}
213
207
else
@@ -219,12 +213,10 @@ dopr(char *buffer, const char *format, va_list args)
219
213
/* fmtnum(value,base,dosign,ljust,len,zpad) */
220
214
if (longflag )
221
215
{
222
- #ifdef HAVE_LONG_LONG_INT_64
223
216
if (longlongflag )
224
217
value = va_arg (args ,long_long );
225
218
else
226
- #endif
227
- value = va_arg (args ,long );
219
+ value = va_arg (args ,long );
228
220
}
229
221
else
230
222
value = va_arg (args ,int );
@@ -234,11 +226,9 @@ dopr(char *buffer, const char *format, va_list args)
234
226
case 'D' :
235
227
if (longflag )
236
228
{
237
- #ifdef HAVE_LONG_LONG_INT_64
238
229
if (longlongflag )
239
230
value = va_arg (args ,long_long );
240
231
else
241
- #endif
242
232
value = va_arg (args ,long );
243
233
}
244
234
else
@@ -249,12 +239,10 @@ dopr(char *buffer, const char *format, va_list args)
249
239
case 'x' :
250
240
if (longflag )
251
241
{
252
- #ifdef HAVE_LONG_LONG_INT_64
253
242
if (longlongflag )
254
243
value = va_arg (args ,long_long );
255
244
else
256
- #endif
257
- value = va_arg (args ,long );
245
+ value = va_arg (args ,long );
258
246
}
259
247
else
260
248
value = va_arg (args ,int );
@@ -263,11 +251,9 @@ dopr(char *buffer, const char *format, va_list args)
263
251
case 'X' :
264
252
if (longflag )
265
253
{
266
- #ifdef HAVE_LONG_LONG_INT_64
267
254
if (longlongflag )
268
255
value = va_arg (args ,long_long );
269
256
else
270
- #endif
271
257
value = va_arg (args ,long );
272
258
}
273
259
else
@@ -287,6 +273,14 @@ dopr(char *buffer, const char *format, va_list args)
287
273
ch = va_arg (args ,int );
288
274
dopr_outch (ch );
289
275
break ;
276
+ case 'e' :
277
+ case 'E' :
278
+ case 'f' :
279
+ case 'g' :
280
+ case 'G' :
281
+ fvalue = va_arg (args ,double );
282
+ fmtfloat (fvalue ,ch ,ljust ,len ,maxwidth ,pointflag );
283
+ break ;
290
284
case '%' :
291
285
dopr_outch (ch );
292
286
continue ;
@@ -303,12 +297,7 @@ dopr(char *buffer, const char *format, va_list args)
303
297
}
304
298
305
299
static void
306
- fmtstr (value ,ljust ,len ,zpad ,maxwidth )
307
- char * value ;
308
- int ljust ,
309
- len ,
310
- zpad ,
311
- maxwidth ;
300
+ fmtstr (char * value ,int ljust ,int len ,int zpad ,int maxwidth )
312
301
{
313
302
int padlen ,
314
303
strlen ;/* amount to pad */
@@ -337,25 +326,11 @@ intljust,
337
326
}
338
327
339
328
static void
340
- fmtnum (value ,base ,dosign ,ljust ,len ,zpad )
341
- #ifdef HAVE_LONG_LONG_INT_64
342
- long_long value ;
343
- #else
344
- long value ;
345
- #endif
346
- int base ,
347
- dosign ,
348
- ljust ,
349
- len ,
350
- zpad ;
329
+ fmtnum (long_long value ,int base ,int dosign ,int ljust ,int len ,int zpad )
351
330
{
352
331
int signvalue = 0 ;
353
- #ifdef HAVE_LONG_LONG_INT_64
354
- ulong_long uvalue ;
355
- #else
356
- unsigned long uvalue ;
357
- #endif
358
- char convert [20 ];
332
+ ulong_long uvalue ;
333
+ char convert [64 ];
359
334
int place = 0 ;
360
335
int padlen = 0 ;/* amount to pad */
361
336
int caps = 0 ;
@@ -385,6 +360,13 @@ intbase,
385
360
uvalue = (uvalue / (unsigned )base );
386
361
}while (uvalue );
387
362
convert [place ]= 0 ;
363
+
364
+ if (len < 0 )
365
+ {
366
+ /* this could happen with a "*" width spec */
367
+ ljust = 1 ;
368
+ len = - len ;
369
+ }
388
370
padlen = len - place ;
389
371
if (padlen < 0 )
390
372
padlen = 0 ;
@@ -426,9 +408,46 @@ intbase,
426
408
}
427
409
428
410
static void
429
- dostr (str ,cut )
430
- char * str ;
431
- int cut ;
411
+ fmtfloat (double value ,char type ,int ljust ,int len ,int precision ,int pointflag )
412
+ {
413
+ char fmt [32 ];
414
+ char convert [512 ];
415
+ int padlen = 0 ;/* amount to pad */
416
+
417
+ /* we rely on regular C library's sprintf to do the basic conversion */
418
+ if (pointflag )
419
+ sprintf (fmt ,"%%.%d%c" ,precision ,type );
420
+ else
421
+ sprintf (fmt ,"%%%c" ,type );
422
+ sprintf (convert ,fmt ,value );
423
+
424
+ if (len < 0 )
425
+ {
426
+ /* this could happen with a "*" width spec */
427
+ ljust = 1 ;
428
+ len = - len ;
429
+ }
430
+ padlen = len - strlen (convert );
431
+ if (padlen < 0 )
432
+ padlen = 0 ;
433
+ if (ljust )
434
+ padlen = - padlen ;
435
+
436
+ while (padlen > 0 )
437
+ {
438
+ dopr_outch (' ' );
439
+ -- padlen ;
440
+ }
441
+ dostr (convert ,0 );
442
+ while (padlen < 0 )
443
+ {
444
+ dopr_outch (' ' );
445
+ ++ padlen ;
446
+ }
447
+ }
448
+
449
+ static void
450
+ dostr (char * str ,int cut )
432
451
{
433
452
if (cut )
434
453
{
@@ -443,8 +462,7 @@ intcut;
443
462
}
444
463
445
464
static void
446
- dopr_outch (c )
447
- int c ;
465
+ dopr_outch (int c )
448
466
{
449
467
#if 0
450
468
if (iscntrl (c )&& c != '\n' && c != '\t' )