printf.c (135751) | printf.c (143906) |
---|---|
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 30 unchanged lines hidden (view full) --- 39#endif /* not lint */ 40#endif 41 42#ifndef lint 43#if 0 44static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93"; 45#endif 46static const char rcsid[] = | 1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 30 unchanged lines hidden (view full) --- 39#endif /* not lint */ 40#endif 41 42#ifndef lint 43#if 0 44static char const sccsid[] = "@(#)printf.c 8.1 (Berkeley) 7/20/93"; 45#endif 46static const char rcsid[] = |
47 "$FreeBSD: head/usr.bin/printf/printf.c 135751 2004-09-24 18:20:43Z keramida $"; | 47 "$FreeBSD: head/usr.bin/printf/printf.c 143906 2005-03-21 08:01:09Z das $"; |
48#endif /* not lint */ 49 50#include <sys/types.h> 51 52#include <err.h> 53#include <errno.h> 54#include <limits.h> 55#include <stdio.h> --- 30 unchanged lines hidden (view full) --- 86 (void)fputs(b, stdout); \ 87 free(b); \ 88 } \ 89} while (0) 90 91static int asciicode(void); 92static int escape(char *, int); 93static int getchr(void); | 48#endif /* not lint */ 49 50#include <sys/types.h> 51 52#include <err.h> 53#include <errno.h> 54#include <limits.h> 55#include <stdio.h> --- 30 unchanged lines hidden (view full) --- 86 (void)fputs(b, stdout); \ 87 free(b); \ 88 } \ 89} while (0) 90 91static int asciicode(void); 92static int escape(char *, int); 93static int getchr(void); |
94static int getdouble(double *); | 94static int getfloating(long double *, int); |
95static int getint(int *); 96static int getquads(quad_t *, u_quad_t *, int); 97static const char 98 *getstr(void); 99static char *mkquad(char *, int); 100static void usage(void); 101 102static char **gargv; 103 104int 105#ifdef BUILTIN 106progprintf(int argc, char *argv[]) 107#else 108main(int argc, char *argv[]) 109#endif 110{ 111 static const char *skip1, *skip2; 112 int ch, chopped, end, fieldwidth, haveprec, havewidth, precision, rval; | 95static int getint(int *); 96static int getquads(quad_t *, u_quad_t *, int); 97static const char 98 *getstr(void); 99static char *mkquad(char *, int); 100static void usage(void); 101 102static char **gargv; 103 104int 105#ifdef BUILTIN 106progprintf(int argc, char *argv[]) 107#else 108main(int argc, char *argv[]) 109#endif 110{ 111 static const char *skip1, *skip2; 112 int ch, chopped, end, fieldwidth, haveprec, havewidth, precision, rval; |
113 int mod_ldbl; |
|
113 char convch, nextch, *format, *fmt, *start; 114 115#ifndef BUILTIN 116 (void) setlocale(LC_NUMERIC, ""); 117#endif 118 while ((ch = getopt(argc, argv, "")) != -1) 119 switch (ch) { 120 case '?': --- 85 unchanged lines hidden (view full) --- 206 } 207 } else 208 haveprec = 0; 209 if (!*fmt) { 210 warnx1("missing format character", NULL, NULL); 211 return (1); 212 } 213 | 114 char convch, nextch, *format, *fmt, *start; 115 116#ifndef BUILTIN 117 (void) setlocale(LC_NUMERIC, ""); 118#endif 119 while ((ch = getopt(argc, argv, "")) != -1) 120 switch (ch) { 121 case '?': --- 85 unchanged lines hidden (view full) --- 207 } 208 } else 209 haveprec = 0; 210 if (!*fmt) { 211 warnx1("missing format character", NULL, NULL); 212 return (1); 213 } 214 |
215 /* 216 * Look for a length modifier. POSIX doesn't have these, so 217 * we only support them for floating-point conversions, which 218 * are extensions. This is useful because the L modifier can 219 * be used to gain extra range and precision, while omitting 220 * it is more likely to produce consistent results on different 221 * architectures. This is not so important for integers 222 * because overflow is the only bad thing that can happen to 223 * them, but consider the command printf %a 1.1 224 */ 225 if (*fmt == 'L') { 226 mod_ldbl = 1; 227 fmt++; 228 if (!strchr("aAeEfFgG", *fmt)) { 229 warnx2("bad modifier L for %%%c", *fmt, NULL); 230 return (1); 231 } 232 } else { 233 mod_ldbl = 0; 234 } 235 |
|
214 convch = *fmt; 215 nextch = *++fmt; 216 *fmt = '\0'; 217 switch(convch) { 218 case 'b': { 219 char *p; 220 int getout; 221 --- 49 unchanged lines hidden (view full) --- 271 else 272 PF(f, uval); 273 break; 274 } 275 case 'e': case 'E': 276 case 'f': case 'F': 277 case 'g': case 'G': 278 case 'a': case 'A': { | 236 convch = *fmt; 237 nextch = *++fmt; 238 *fmt = '\0'; 239 switch(convch) { 240 case 'b': { 241 char *p; 242 int getout; 243 --- 49 unchanged lines hidden (view full) --- 293 else 294 PF(f, uval); 295 break; 296 } 297 case 'e': case 'E': 298 case 'f': case 'F': 299 case 'g': case 'G': 300 case 'a': case 'A': { |
279 double p; | 301 long double p; |
280 | 302 |
281 if (getdouble(&p)) | 303 if (getfloating(&p, mod_ldbl)) |
282 rval = 1; | 304 rval = 1; |
283 PF(start, p); | 305 if (mod_ldbl) 306 PF(start, p); 307 else 308 PF(start, (double)p); |
284 break; 285 } 286 default: 287 warnx2("illegal format character %c", convch, NULL); 288 return (1); 289 } 290 *fmt = nextch; 291 } --- 168 unchanged lines hidden (view full) --- 460 warnx3("%s: %s", *gargv, strerror(ERANGE)); 461 rval = 1; 462 } 463 ++gargv; 464 return (rval); 465} 466 467static int | 309 break; 310 } 311 default: 312 warnx2("illegal format character %c", convch, NULL); 313 return (1); 314 } 315 *fmt = nextch; 316 } --- 168 unchanged lines hidden (view full) --- 485 warnx3("%s: %s", *gargv, strerror(ERANGE)); 486 rval = 1; 487 } 488 ++gargv; 489 return (rval); 490} 491 492static int |
468getdouble(double *dp) | 493getfloating(long double *dp, int mod_ldbl) |
469{ 470 char *ep; 471 int rval; 472 473 if (!*gargv) 474 return (0); 475 if (**gargv == '"' || **gargv == '\'') { 476 *dp = asciicode(); 477 return (0); 478 } 479 rval = 0; 480 errno = 0; | 494{ 495 char *ep; 496 int rval; 497 498 if (!*gargv) 499 return (0); 500 if (**gargv == '"' || **gargv == '\'') { 501 *dp = asciicode(); 502 return (0); 503 } 504 rval = 0; 505 errno = 0; |
481 *dp = strtod(*gargv, &ep); | 506 if (mod_ldbl) 507 *dp = strtold(*gargv, &ep); 508 else 509 *dp = strtod(*gargv, &ep); |
482 if (ep == *gargv) { 483 warnx2("%s: expected numeric value", *gargv, NULL); 484 rval = 1; 485 } else if (*ep != '\0') { 486 warnx2("%s: not completely converted", *gargv, NULL); 487 rval = 1; 488 } 489 if (errno == ERANGE) { --- 24 unchanged lines hidden --- | 510 if (ep == *gargv) { 511 warnx2("%s: expected numeric value", *gargv, NULL); 512 rval = 1; 513 } else if (*ep != '\0') { 514 warnx2("%s: not completely converted", *gargv, NULL); 515 rval = 1; 516 } 517 if (errno == ERANGE) { --- 24 unchanged lines hidden --- |