re-enable munmap().
[minix.git] / commands / aal / format.c
blobffb3a65b7f9bed5e7e8ba26099c390a5f5828975
1 /*
2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
4 */
5 /* $Header$ */
7 #if __STDC__
8 #include <stdarg.h>
9 #else
10 #include <varargs.h>
11 #endif
13 extern char *long2str();
15 static int
16 integral(c)
18 switch (c) {
19 case 'b':
20 return -2;
21 case 'd':
22 return 10;
23 case 'o':
24 return -8;
25 case 'u':
26 return -10;
27 case 'x':
28 return -16;
30 return 0;
33 /*VARARGS2*/
34 /*FORMAT1 $
35 %s = char *
36 %l = long
37 %c = int
38 %[uxbo] = unsigned int
39 %d = int
40 $ */
41 int
42 _format(buf, fmt, argp)
43 char *buf, *fmt;
44 register va_list argp;
46 register char *pf = fmt;
47 register char *pb = buf;
49 while (*pf) {
50 if (*pf == '%') {
51 register width, base, pad, npad;
52 char *arg;
53 char cbuf[2];
54 char *badformat = "<bad format>";
56 /* get padder */
57 if (*++pf == '0') {
58 pad = '0';
59 ++pf;
61 else
62 pad = ' ';
64 /* get width */
65 width = 0;
66 while (*pf >= '0' && *pf <= '9')
67 width = 10 * width + *pf++ - '0';
69 if (*pf == 's') {
70 arg = va_arg(argp, char *);
72 else
73 if (*pf == 'c') {
74 cbuf[0] = va_arg(argp, int);
75 cbuf[1] = '\0';
76 arg = &cbuf[0];
78 else
79 if (*pf == 'l') {
80 /* alignment ??? */
81 if (base = integral(*++pf)) {
82 arg = long2str(va_arg(argp,long), base);
84 else {
85 pf--;
86 arg = badformat;
89 else
90 if (base = integral(*pf)) {
91 arg = long2str((long)va_arg(argp,int), base);
93 else
94 if (*pf == '%')
95 arg = "%";
96 else
97 arg = badformat;
99 npad = width - strlen(arg);
101 while (npad-- > 0)
102 *pb++ = pad;
104 while (*pb++ = *arg++);
105 pb--;
106 pf++;
108 else
109 *pb++ = *pf++;
111 return pb - buf;