source: trunk/minix/commands/aal/format.c@ 22

Last change on this file since 22 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.8 KB
Line 
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: /cvsup/minix/src/commands/aal/format.c,v 1.1.1.1 2005/04/21 14:53:57 beng Exp $ */
6
7#if __STDC__
8#include <stdarg.h>
9#else
10#include <varargs.h>
11#endif
12
13extern char *long2str();
14
15static int
16integral(c)
17{
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;
29 }
30 return 0;
31}
32
33/*VARARGS2*/
34/*FORMAT1 $
35 %s = char *
36 %l = long
37 %c = int
38 %[uxbo] = unsigned int
39 %d = int
40$ */
41int
42_format(buf, fmt, argp)
43 char *buf, *fmt;
44 register va_list argp;
45{
46 register char *pf = fmt;
47 register char *pb = buf;
48
49 while (*pf) {
50 if (*pf == '%') {
51 register width, base, pad, npad;
52 char *arg;
53 char cbuf[2];
54 char *badformat = "<bad format>";
55
56 /* get padder */
57 if (*++pf == '0') {
58 pad = '0';
59 ++pf;
60 }
61 else
62 pad = ' ';
63
64 /* get width */
65 width = 0;
66 while (*pf >= '0' && *pf <= '9')
67 width = 10 * width + *pf++ - '0';
68
69 if (*pf == 's') {
70 arg = va_arg(argp, char *);
71 }
72 else
73 if (*pf == 'c') {
74 cbuf[0] = va_arg(argp, int);
75 cbuf[1] = '\0';
76 arg = &cbuf[0];
77 }
78 else
79 if (*pf == 'l') {
80 /* alignment ??? */
81 if (base = integral(*++pf)) {
82 arg = long2str(va_arg(argp,long), base);
83 }
84 else {
85 pf--;
86 arg = badformat;
87 }
88 }
89 else
90 if (base = integral(*pf)) {
91 arg = long2str((long)va_arg(argp,int), base);
92 }
93 else
94 if (*pf == '%')
95 arg = "%";
96 else
97 arg = badformat;
98
99 npad = width - strlen(arg);
100
101 while (npad-- > 0)
102 *pb++ = pad;
103
104 while (*pb++ = *arg++);
105 pb--;
106 pf++;
107 }
108 else
109 *pb++ = *pf++;
110 }
111 return pb - buf;
112}
Note: See TracBrowser for help on using the repository browser.