Initial revision
authorceriel <none@none>
Mon, 5 Jan 1987 17:37:37 +0000 (17:37 +0000)
committerceriel <none@none>
Mon, 5 Jan 1987 17:37:37 +0000 (17:37 +0000)
modules/src/print/doprnt.c [new file with mode: 0644]
modules/src/print/format.c [new file with mode: 0644]
modules/src/print/fprint.c [new file with mode: 0644]
modules/src/print/param.h [new file with mode: 0644]
modules/src/print/print.3 [new file with mode: 0644]
modules/src/print/print.c [new file with mode: 0644]
modules/src/print/print.h [new file with mode: 0644]
modules/src/print/sprint.c [new file with mode: 0644]

diff --git a/modules/src/print/doprnt.c b/modules/src/print/doprnt.c
new file mode 100644 (file)
index 0000000..7dca0ea
--- /dev/null
@@ -0,0 +1,14 @@
+/* $Header$ */
+
+#include <system.h>
+#include "param.h"
+
+doprnt(fp, fmt, argp)
+       File *fp;
+       char *fmt;
+       int argp[];
+{
+       char buf[SSIZE];
+
+       sys_write(fp, buf, _format(buf, fmt, (char *)argp));
+}
diff --git a/modules/src/print/format.c b/modules/src/print/format.c
new file mode 100644 (file)
index 0000000..31f1f6d
--- /dev/null
@@ -0,0 +1,99 @@
+/* $Header$ */
+
+char *long2str();
+
+static int
+integral(c)
+{
+       switch (c) {
+       case 'b':
+               return -2;
+       case 'd':
+               return 10;
+       case 'o':
+               return -8;
+       case 'u':
+               return -10;
+       case 'x':
+               return -16;
+       }
+       return 0;
+}
+
+int
+_format(buf, fmt, argp)
+       char *buf, *fmt;
+       char *argp;
+{
+       register char *pf = fmt, *pa = argp;
+       register char *pb = buf;
+
+       while (*pf) {
+               if (*pf == '%') {
+                       register width, base, pad, npad;
+                       char *arg;
+                       char cbuf[2];
+                       char *badformat = "<bad format>";
+                       
+                       /* get padder */
+                       if (*++pf == '0') {
+                               pad = '0';
+                               ++pf;
+                       }
+                       else
+                               pad = ' ';
+                       
+                       /* get width */
+                       width = 0;
+                       while (*pf >= '0' && *pf <= '9')
+                               width = 10 * width + *pf++ - '0';
+                       
+                       /* get text and move pa */
+                       if (*pf == 's') {
+                               arg = *(char **)pa;
+                               pa += sizeof(char *);
+                       }
+                       else
+                       if (*pf == 'c') {
+                               cbuf[0] = * (char *) pa;
+                               cbuf[1] = '\0';
+                               pa += sizeof(int);
+                               arg = &cbuf[0];
+                       }
+                       else
+                       if (*pf == 'l') {
+                               /* alignment ??? */
+                               if (base = integral(*++pf)) {
+                                       arg = long2str(*(long *)pa, base);
+                                       pa += sizeof(long);
+                               }
+                               else {
+                                       pf--;
+                                       arg = badformat;
+                               }
+                       }
+                       else
+                       if (base = integral(*pf)) {
+                               arg = long2str((long)*(int *)pa, base);
+                               pa += sizeof(int);
+                       }
+                       else
+                       if (*pf == '%')
+                               arg = "%";
+                       else
+                               arg = badformat;
+
+                       npad = width - strlen(arg);
+
+                       while (npad-- > 0)
+                               *pb++ = pad;
+                       
+                       while (*pb++ = *arg++);
+                       pb--;
+                       pf++;
+               }
+               else
+                       *pb++ = *pf++;
+       }
+       return pb - buf;
+}
diff --git a/modules/src/print/fprint.c b/modules/src/print/fprint.c
new file mode 100644 (file)
index 0000000..97b8ebb
--- /dev/null
@@ -0,0 +1,15 @@
+/* $Header$ */
+
+#include <system.h>
+#include "param.h"
+
+/*VARARGS1*/
+fprint(fp, fmt, args)
+       File *fp;
+       char *fmt;
+       int args;
+{
+       char buf[SSIZE];
+
+       sys_write(fp, buf, _format(buf, fmt, &args));
+}
diff --git a/modules/src/print/param.h b/modules/src/print/param.h
new file mode 100644 (file)
index 0000000..45bb5c8
--- /dev/null
@@ -0,0 +1,3 @@
+/* $Header$ */
+
+#define SSIZE 1024
diff --git a/modules/src/print/print.3 b/modules/src/print/print.3
new file mode 100644 (file)
index 0000000..a71f98c
--- /dev/null
@@ -0,0 +1,124 @@
+.TH PRINT 3ACK "86/04/02"
+.SH NAME
+print, fprint, sprint, doprnt -- very simple formatted-output routines
+.SH SYNOPSIS
+.nf
+.B #include <system.h>
+.PP
+.B print(format [, arg] ... )
+.B char *format;
+.PP
+.B fprint(filep, format [, arg] ... )
+.B File *filep;
+.B char *format;
+.PP
+.B sprint(s, format [, arg] ... )
+.B char *s, *format;
+.PP
+.B doprnt(filep, format, args)
+.B File *filep;
+.B char *format;
+.B int args[];
+.fi
+.SH DESCRIPTION
+.I Print
+writes output on standard output.
+.I Fprint
+and
+.I doprnt
+place output on the open file known by
+.IR filep .
+.I Sprint
+places `output' in the string
+.IR s ,
+followed by the character `\\0'.
+.PP
+Each of these functions converts, formats and prints its arguments, following
+the 
+.I format
+argument, under control of
+.IR format .
+.I Format
+is a character string which contains two types of objects: plain characters,
+which are simply copied to the output destination, and conversion
+specifications, each of which causes conversion and printing of the next
+successive argument.
+.PP
+A conversion specification is introduced by the character %.
+Following the %, there may be
+.IP \(bu
+an optional row of decimal digits specifying the field width;
+if the converted integral value has fewer characters than
+the field width, it will be blank-padded on the left;
+if the field width begins with a zero, zero-padding will be done;
+.IP \(bu
+the character
+.B l
+specifying that a following 
+.BR b ,
+.BR d ,
+.BR o ,
+.B u
+or
+.B x
+corresponds to a long-integer argument;
+.IP \(bu
+a character which indicates the type of conversion to be applied.
+.LP
+.PP
+The conversion characters and their meanings are
+.IP \fBbdox\fP
+The next argument is an integer and is converted to binary, decimal, octal
+or hexadecimal notation respectively.
+.IP \fBc\fP
+Next argument is a character and is put directly into the resulting string.
+the field width is one character.
+.IP \fBs\fP
+Next argument is taken to be a character pointer and characters from the
+string are taken until a null character is reached; a specified field width
+is not taken into account.
+.IP \fBu\fP
+The unsigned integer argument is converted to decimal.
+.LP
+.PP
+Integral arguments are not truncated, even if their size exceeds the specified
+field width.
+Padding takes place only if the specified field width exceeds the actual width.
+.PP
+The printing routines build the string to be printed internally and use
+.I sys_write
+to print it.
+.I Doprnt
+takes
+.I args
+as the address of the arguments of the format string.
+This allows routines, e.g.
+.IR print ,
+to be defined as follows:
+.br
+.RS
+.nf
+/*VARARGS1*/
+print(fmt, argv)
+       char *fmt;
+       int argv;
+{
+       doprnt(STDOUT, fmt, &argv);
+}
+.fi
+.RE
+.SH FILES
+.nf
+~em/modules/lib/libprint.a
+.fi
+.SH MODULES
+system(3), string(3)
+.SH DIAGNOSTICS
+.PP
+Each illegal conversion specification is replaced by the string "<bad\ format>".
+.SH BUGS
+The maximum length of the string to be printed is 1024 characters.
+.SH SEE ALSO
+printf(3)
+.SH AUTHOR
+Erik Baalbergen <erikb@vu44.UUCP>
diff --git a/modules/src/print/print.c b/modules/src/print/print.c
new file mode 100644 (file)
index 0000000..dee93c2
--- /dev/null
@@ -0,0 +1,14 @@
+/* $Header$ */
+
+#include <system.h>
+#include "param.h"
+
+/*VARARGS1*/
+print(fmt, args)
+       char *fmt;
+       int args;
+{
+       char buf[SSIZE];
+
+       sys_write(STDOUT, buf, _format(buf, fmt, &args));
+}
diff --git a/modules/src/print/print.h b/modules/src/print/print.h
new file mode 100644 (file)
index 0000000..06bfc66
--- /dev/null
@@ -0,0 +1,11 @@
+/* $Header$ */
+
+#define stdin  STDIN
+#define stdout STDOUT
+#define stderr STDERR
+
+#define printf print
+#define sprintf        sprint
+#define fprintf        fprint
+
+#define FILE   File
diff --git a/modules/src/print/sprint.c b/modules/src/print/sprint.c
new file mode 100644 (file)
index 0000000..64360ff
--- /dev/null
@@ -0,0 +1,14 @@
+/* $Header$ */
+
+#include <system.h>
+#include "param.h"
+
+/*VARARGS1*/
+char *
+sprint(buf, fmt, args)
+       char *buf, *fmt;
+       int args;
+{
+       buf[_format(buf, fmt, &args)] = '\0';
+       return buf;
+}