xref: /minix3/minix/commands/cawf/output.c (revision d9494baa34075b158415cc42d68bd0927d8124c1)
1 /*
2  *	output-c - output support functions for cawf(1)
3  */
4 
5 /*
6  *	Copyright (c) 1991 Purdue University Research Foundation,
7  *	West Lafayette, Indiana 47907.  All rights reserved.
8  *
9  *	Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
10  *	University Computing Center.  Not derived from licensed software;
11  *	derived from awf(1) by Henry Spencer of the University of Toronto.
12  *
13  *	Permission is granted to anyone to use this software for any
14  *	purpose on any computer system, and to alter it and redistribute
15  *	it freely, subject to the following restrictions:
16  *
17  *	1. The author is not responsible for any consequences of use of
18  *	   this software, even if they arise from flaws in it.
19  *
20  *	2. The origin of this software must not be misrepresented, either
21  *	   by explicit claim or by omission.  Credits must appear in the
22  *	   documentation.
23  *
24  *	3. Altered versions must be plainly marked as such, and must not
25  *	   be misrepresented as being the original software.  Credits must
26  *	   appear in the documentation.
27  *
28  *	4. This notice may not be removed or altered.
29  */
30 
31 #include "cawf.h"
32 
33 
34 /*
35  * LenprtHF(s, p, t) - get length of print header or footer with page number
36  *		       interpolation
37  */
38 
LenprtHF(unsigned char * s,int p,int t)39 int LenprtHF(unsigned char *s, int p, int t) {
40 /* header/footer string s
41  * page number p
42  * type t: 0 = get interpolated length
43  *	 1 = print
44  */
45 	unsigned char buf[10];		/* buffer for page number */
46 	int len;			/* line length */
47 	unsigned char *s1;		/* temporary string pointer */
48 
49 	if (s == NULL)
50 		return(0);
51 	for (len = 0; *s && *s != '%'; s++) {
52 		len++;
53 		if (t)
54 			Charput((int)*s);
55 	}
56 	if (*s) {
57 		(void) sprintf((char *)buf, "%d", p);
58 		for (s1 = buf; *s1; s1++) {
59 			len++;
60 			if (t)
61 				Charput((int)*s1);
62 		}
63 		for (s++; *s; s++) {
64 			len++;
65 			if (t)
66 				Charput((int)*s);
67 		}
68 	}
69 	return(len);
70 }
71 
72 
73 /*
74  * Charput(s) - put a character to output, subject to diversion
75  */
76 
Charput(int c)77 void Charput(int c) {
78 /* character to put c */
79 	if (Divert == 0)
80 		putchar((unsigned char)c);
81 }
82 
83 
84 /*
85  * Stringput(s) - put a string to output, subject to diversion
86  */
87 
Stringput(unsigned char * s)88 void Stringput(unsigned char *s) {
89 /* string to put s */
90 	if (Divert == 0)
91 		fputs((char *)s, stdout);
92 }
93