summaryrefslogtreecommitdiffabout
path: root/lib
authorAlain Magloire <alainm@gnu.org>2003-05-24 23:52:34 (GMT)
committer Alain Magloire <alainm@gnu.org>2003-05-24 23:52:34 (GMT)
commitb7b91675fdcf99e73ca4eb537e416bf48c883dcc (patch) (side-by-side diff)
treee667a1ce4bf8b95e4408bde4cc546c347f1b2b2d /lib
parentf8b102aaeda909573539c0845a66becef1c80eeb (diff)
downloadmailutils-b7b91675fdcf99e73ca4eb537e416bf48c883dcc.tar.gz
mailutils-b7b91675fdcf99e73ca4eb537e416bf48c883dcc.tar.bz2
Implement %lld, %qd and %Lg for snprintf
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/snprintf.c73
-rw-r--r--lib/snprintf.h18
2 files changed, 73 insertions, 18 deletions
diff --git a/lib/snprintf.c b/lib/snprintf.c
index 9e03912..57ffb6c 100644
--- a/lib/snprintf.c
+++ b/lib/snprintf.c
@@ -446,10 +446,12 @@ struct DATA * p;
char number[MAX_FIELD/2];
int i;
+ /* reset the flags. */
p->precision = p->width = NOT_FOUND;
p->star_w = p->star_p = NOT_FOUND;
p->square = p->space = NOT_FOUND;
p->a_long = p->justify = NOT_FOUND;
+ p->a_longlong = NOT_FOUND;
p->pad = ' ';
for(;s && *s ;s++) {
@@ -523,7 +525,10 @@ va_list args;
break;
case 'f': /* float, double */
STAR_ARGS(&data);
- d = va_arg(args, double);
+ if (data.a_long == FOUND)
+ d = va_arg(args, LONG_DOUBLE);
+ else
+ d = va_arg(args, double);
floating(&data, d);
state = 0;
break;
@@ -531,7 +536,10 @@ va_list args;
case 'G':
STAR_ARGS(&data);
DEF_PREC(&data);
- d = va_arg(args, double);
+ if (data.a_long == FOUND)
+ d = va_arg(args, LONG_DOUBLE);
+ else
+ d = va_arg(args, double);
i = log_10(d);
/*
* for '%g|%G' ANSI: use f if exponent
@@ -547,22 +555,29 @@ va_list args;
case 'e':
case 'E': /* Exponent double */
STAR_ARGS(&data);
- d = va_arg(args, double);
+ if (data.a_long == FOUND)
+ d = va_arg(args, LONG_DOUBLE);
+ else
+ d = va_arg(args, double);
exponent(&data, d);
state = 0;
break;
case 'u': /* unsigned decimal */
STAR_ARGS(&data);
- if (data.a_long == FOUND)
- d = va_arg(args, unsigned long);
- else
- d = va_arg(args, unsigned int);
+ if (data.a_longlong == FOUND)
+ d = va_arg(args, unsigned LONG_LONG);
+ else if (data.a_long == FOUND)
+ d = va_arg(args, unsigned long);
+ else
+ d = va_arg(args, unsigned int);
decimal(&data, d);
- state = 0;
- break;
+ state = 0;
+ break;
case 'd': /* decimal */
STAR_ARGS(&data);
- if (data.a_long == FOUND)
+ if (data.a_longlong == FOUND)
+ d = va_arg(args, LONG_LONG);
+ else if (data.a_long == FOUND)
d = va_arg(args, long);
else
d = va_arg(args, int);
@@ -571,7 +586,9 @@ va_list args;
break;
case 'o': /* octal */
STAR_ARGS(&data);
- if (data.a_long == FOUND)
+ if (data.a_longlong == FOUND)
+ d = va_arg(args, LONG_LONG);
+ else if (data.a_long == FOUND)
d = va_arg(args, long);
else
d = va_arg(args, int);
@@ -581,7 +598,9 @@ va_list args;
case 'x':
case 'X': /* hexadecimal */
STAR_ARGS(&data);
- if (data.a_long == FOUND)
+ if (data.a_longlong == FOUND)
+ d = va_arg(args, LONG_LONG);
+ else if (data.a_long == FOUND)
d = va_arg(args, long);
else
d = va_arg(args, int);
@@ -599,11 +618,18 @@ va_list args;
state = 0;
break;
case 'n':
- *(va_arg(args, int *)) = data.counter; /* what's the count ? */
- state = 0;
- break;
+ *(va_arg(args, int *)) = data.counter; /* what's the count ? */
+ state = 0;
+ break;
+ case 'q':
+ data.a_longlong = FOUND;
+ break;
+ case 'L':
case 'l':
- data.a_long = FOUND;
+ if (data.a_long == FOUND)
+ data.a_longlong = FOUND;
+ else
+ data.a_long = FOUND;
break;
case 'h':
break;
@@ -676,7 +702,7 @@ va_dcl
#include <stdio.h>
/* set of small tests for snprintf() */
-void main()
+int main()
{
char holder[100];
int i;
@@ -709,6 +735,17 @@ void main()
printf("/%-10d/\n", 336);
printf("%s\n", holder);
+/* long long */
+
+ printf("/%%lld/, 336\n");
+ snprintf(holder, sizeof holder, "/%lld/\n", (LONG_LONG)336);
+ printf("/%lld/\n", (LONG_LONG)336);
+ printf("%s\n", holder);
+
+ printf("/%%2qd/, 336\n");
+ snprintf(holder, sizeof holder, "/%2qd/\n", (LONG_LONG)336);
+ printf("/%2qd/\n", (LONG_LONG)336);
+ printf("%s\n", holder);
/* floating points */
@@ -825,5 +862,7 @@ void main()
i = snprintf(holder, 10, "%s\n", BIG);
printf("<%s>\n", BIG);
printf("<%s>\n", holder);
+
+ return 0;
}
#endif
diff --git a/lib/snprintf.h b/lib/snprintf.h
index 16db7cf..7a8ad40 100644
--- a/lib/snprintf.h
+++ b/lib/snprintf.h
@@ -79,6 +79,22 @@ Alain Magloire: alainm@rcsm.ee.mcgill.ca
#define MAX_FRACT 29 + 1
/*
+ * If the compiler supports (long long)
+ */
+#ifndef LONG_LONG
+# define LONG_LONG long long
+/*# define LONG_LONG int64_t*/
+#endif
+
+/*
+ * If the compiler supports (long double)
+ */
+#ifndef LONG_DOUBLE
+# define LONG_DOUBLE long double
+/*# define LONG_DOUBLE double*/
+#endif
+
+/*
* numtoa() uses PRIVATE buffers to store the results,
* So this function is not reentrant
*/
@@ -102,7 +118,7 @@ struct DATA {
/* FLAGS */
int width, precision;
int justify; char pad;
- int square, space, star_w, star_p, a_long ;
+ int square, space, star_w, star_p, a_long, a_longlong;
};
#define PRIVATE static

Return to:

Send suggestions and report system problems to the System administrator.