format.c

Go to the documentation of this file.
00001 /*
00002  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
00003  * See the copyright notice in the ACK home directory, in the file "Copyright".
00004  */
00005 /* $Header: /opt/proj/minix/cvsroot/src/commands/aal/format.c,v 1.1.1.1 2005/04/21 14:53:57 beng Exp $ */
00006 
00007 #if __STDC__
00008 #include <stdarg.h>
00009 #else
00010 #include <varargs.h>
00011 #endif
00012 
00013 extern char *long2str();
00014 
00015 static int
00016 integral(c)
00017 {
00018         switch (c) {
00019         case 'b':
00020                 return -2;
00021         case 'd':
00022                 return 10;
00023         case 'o':
00024                 return -8;
00025         case 'u':
00026                 return -10;
00027         case 'x':
00028                 return -16;
00029         }
00030         return 0;
00031 }
00032 
00033 /*VARARGS2*/
00034 /*FORMAT1 $
00035         %s = char *
00036         %l = long
00037         %c = int
00038         %[uxbo] = unsigned int
00039         %d = int
00040 $ */
00041 int
00042 _format(buf, fmt, argp)
00043         char *buf, *fmt;
00044         register va_list argp;
00045 {
00046         register char *pf = fmt;
00047         register char *pb = buf;
00048 
00049         while (*pf) {
00050                 if (*pf == '%') {
00051                         register width, base, pad, npad;
00052                         char *arg;
00053                         char cbuf[2];
00054                         char *badformat = "<bad format>";
00055                         
00056                         /* get padder */
00057                         if (*++pf == '0') {
00058                                 pad = '0';
00059                                 ++pf;
00060                         }
00061                         else
00062                                 pad = ' ';
00063                         
00064                         /* get width */
00065                         width = 0;
00066                         while (*pf >= '0' && *pf <= '9')
00067                                 width = 10 * width + *pf++ - '0';
00068                         
00069                         if (*pf == 's') {
00070                                 arg = va_arg(argp, char *);
00071                         }
00072                         else
00073                         if (*pf == 'c') {
00074                                 cbuf[0] = va_arg(argp, int);
00075                                 cbuf[1] = '\0';
00076                                 arg = &cbuf[0];
00077                         }
00078                         else
00079                         if (*pf == 'l') {
00080                                 /* alignment ??? */
00081                                 if (base = integral(*++pf)) {
00082                                         arg = long2str(va_arg(argp,long), base);
00083                                 }
00084                                 else {
00085                                         pf--;
00086                                         arg = badformat;
00087                                 }
00088                         }
00089                         else
00090                         if (base = integral(*pf)) {
00091                                 arg = long2str((long)va_arg(argp,int), base);
00092                         }
00093                         else
00094                         if (*pf == '%')
00095                                 arg = "%";
00096                         else
00097                                 arg = badformat;
00098 
00099                         npad = width - strlen(arg);
00100 
00101                         while (npad-- > 0)
00102                                 *pb++ = pad;
00103                         
00104                         while (*pb++ = *arg++);
00105                         pb--;
00106                         pf++;
00107                 }
00108                 else
00109                         *pb++ = *pf++;
00110         }
00111         return pb - buf;
00112 }

Generated on Fri Apr 14 22:56:37 2006 for minix by  doxygen 1.4.6