1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate * Copyright (c) 1987, 1988, 1989, 1990 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate */
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
29*0Sstevel@tonic-gate /* All Rights Reserved */
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate * Subroutines for the 4.0 compatibility run-time link editor.
33*0Sstevel@tonic-gate */
34*0Sstevel@tonic-gate #include <varargs.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate * Local "printf" & stdio facilities.
39*0Sstevel@tonic-gate */
40*0Sstevel@tonic-gate int stdout = 1; /* File descriptor for output */
41*0Sstevel@tonic-gate int stderr = 2; /* File descriptor for errors */
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate static char *printn();
44*0Sstevel@tonic-gate static void prf();
45*0Sstevel@tonic-gate static void doprf();
46*0Sstevel@tonic-gate static int _write();
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate * printf
50*0Sstevel@tonic-gate */
51*0Sstevel@tonic-gate /*VARARGS1*/
printf(fmt,va_alist)52*0Sstevel@tonic-gate printf(fmt, va_alist)
53*0Sstevel@tonic-gate char *fmt;
54*0Sstevel@tonic-gate va_dcl
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate va_list x1;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate va_start(x1);
59*0Sstevel@tonic-gate prf(stdout, fmt, x1);
60*0Sstevel@tonic-gate va_end(x1);
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate * fprintf
65*0Sstevel@tonic-gate */
66*0Sstevel@tonic-gate /*VARARGS2*/
fprintf(fd,fmt,va_alist)67*0Sstevel@tonic-gate fprintf(fd, fmt, va_alist)
68*0Sstevel@tonic-gate int fd;
69*0Sstevel@tonic-gate char *fmt;
70*0Sstevel@tonic-gate va_dcl
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate va_list x1;
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate va_start(x1);
75*0Sstevel@tonic-gate prf(fd, fmt, x1);
76*0Sstevel@tonic-gate va_end(x1);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate * panic
81*0Sstevel@tonic-gate */
82*0Sstevel@tonic-gate /*VARARGS2*/
panic(fmt,va_alist)83*0Sstevel@tonic-gate panic(fmt, va_alist)
84*0Sstevel@tonic-gate char *fmt;
85*0Sstevel@tonic-gate va_dcl
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate va_list x1;
88*0Sstevel@tonic-gate extern char *program_name;
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate va_start(x1);
91*0Sstevel@tonic-gate prf(stderr, "%s (4.x.ld.so): ", program_name);
92*0Sstevel@tonic-gate prf(stderr, fmt, x1);
93*0Sstevel@tonic-gate prf(stderr, "\n", x1);
94*0Sstevel@tonic-gate va_end(x1);
95*0Sstevel@tonic-gate _exit(127);
96*0Sstevel@tonic-gate /* NOTREACHED */
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate * sprintf
101*0Sstevel@tonic-gate */
102*0Sstevel@tonic-gate /*VARARGS2*/
sprintf(cp,fmt,va_alist)103*0Sstevel@tonic-gate sprintf(cp, fmt, va_alist)
104*0Sstevel@tonic-gate char *cp;
105*0Sstevel@tonic-gate char *fmt;
106*0Sstevel@tonic-gate va_dcl
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate va_list x1;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate va_start(x1);
111*0Sstevel@tonic-gate doprf(-1, fmt, x1, cp);
112*0Sstevel@tonic-gate va_end(x1);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate /*
116*0Sstevel@tonic-gate * printf worker functions
117*0Sstevel@tonic-gate */
118*0Sstevel@tonic-gate static void
prf(fd,fmt,adx)119*0Sstevel@tonic-gate prf(fd, fmt, adx)
120*0Sstevel@tonic-gate int fd;
121*0Sstevel@tonic-gate char *fmt;
122*0Sstevel@tonic-gate va_list adx;
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate char linebuf[128];
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate doprf(fd, fmt, adx, linebuf);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate static void
doprf(fd,fmt,adx,linebuf)130*0Sstevel@tonic-gate doprf(fd, fmt, adx, linebuf)
131*0Sstevel@tonic-gate int fd;
132*0Sstevel@tonic-gate register char *fmt;
133*0Sstevel@tonic-gate register va_list adx;
134*0Sstevel@tonic-gate char *linebuf;
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate register int c; /* Character temporary */
137*0Sstevel@tonic-gate register char *lbp; /* Pointer into stack buffer */
138*0Sstevel@tonic-gate register char *s; /* %s temporary */
139*0Sstevel@tonic-gate int i; /* General integer temporary */
140*0Sstevel@tonic-gate int b; /* Conversion base */
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate #define PUTCHAR(c) { \
143*0Sstevel@tonic-gate if (lbp >= &linebuf[128]) { \
144*0Sstevel@tonic-gate _write(fd, linebuf, lbp - &linebuf[0]); \
145*0Sstevel@tonic-gate lbp = &linebuf[0]; \
146*0Sstevel@tonic-gate } \
147*0Sstevel@tonic-gate *lbp++ = (c); \
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate lbp = &linebuf[0];
151*0Sstevel@tonic-gate loop:
152*0Sstevel@tonic-gate while ((c = *fmt++) != '%') {
153*0Sstevel@tonic-gate if (c == '\0') {
154*0Sstevel@tonic-gate _write(fd, linebuf, lbp - &linebuf[0]);
155*0Sstevel@tonic-gate return;
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate PUTCHAR(c);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate again:
160*0Sstevel@tonic-gate c = *fmt++;
161*0Sstevel@tonic-gate /* THIS CODE IS VAX DEPENDENT IN HANDLING %l? AND %c */
162*0Sstevel@tonic-gate switch (c) {
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate case 'x': case 'X':
165*0Sstevel@tonic-gate b = 16;
166*0Sstevel@tonic-gate goto number;
167*0Sstevel@tonic-gate case 'd': case 'D':
168*0Sstevel@tonic-gate case 'u': /* what a joke */
169*0Sstevel@tonic-gate b = 10;
170*0Sstevel@tonic-gate goto number;
171*0Sstevel@tonic-gate case 'o': case 'O':
172*0Sstevel@tonic-gate b = 8;
173*0Sstevel@tonic-gate number:
174*0Sstevel@tonic-gate lbp = printn(fd, va_arg(adx, u_long), b, &linebuf[0], lbp,
175*0Sstevel@tonic-gate &linebuf[128]);
176*0Sstevel@tonic-gate break;
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate case 'c':
179*0Sstevel@tonic-gate b = va_arg(adx, int);
180*0Sstevel@tonic-gate for (i = 24; i >= 0; i -= 8)
181*0Sstevel@tonic-gate if (c = (b >> i) & 0x7f) {
182*0Sstevel@tonic-gate PUTCHAR(c);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate break;
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate case 's':
187*0Sstevel@tonic-gate s = va_arg(adx, char *);
188*0Sstevel@tonic-gate while (c = *s++) {
189*0Sstevel@tonic-gate PUTCHAR(c);
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate break;
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate case '%':
194*0Sstevel@tonic-gate PUTCHAR('%');
195*0Sstevel@tonic-gate break;
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate goto loop;
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate /*
201*0Sstevel@tonic-gate * Printn prints a number n in base b.
202*0Sstevel@tonic-gate */
203*0Sstevel@tonic-gate static char *
printn(fd,n,b,linebufp,lbp,linebufend)204*0Sstevel@tonic-gate printn(fd, n, b, linebufp, lbp, linebufend)
205*0Sstevel@tonic-gate int fd; /* File descriptor to get output */
206*0Sstevel@tonic-gate u_long n; /* Number */
207*0Sstevel@tonic-gate int b; /* Base */
208*0Sstevel@tonic-gate char *linebufp; /* Buffer location */
209*0Sstevel@tonic-gate register char *lbp; /* Current offset in buffer */
210*0Sstevel@tonic-gate char *linebufend; /* Where buffer ends */
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate char prbuf[11]; /* Local result accumulator */
213*0Sstevel@tonic-gate register char *cp;
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gate #undef PUTCHAR
216*0Sstevel@tonic-gate #define PUTCHAR(c) { \
217*0Sstevel@tonic-gate if (lbp >= linebufend) { \
218*0Sstevel@tonic-gate _write(fd, linebufp, lbp - linebufp); \
219*0Sstevel@tonic-gate lbp = linebufp; \
220*0Sstevel@tonic-gate } \
221*0Sstevel@tonic-gate *lbp++ = (c); \
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate if (b == 10 && (int)n < 0) {
225*0Sstevel@tonic-gate PUTCHAR('-');
226*0Sstevel@tonic-gate n = (unsigned)(-(int)n);
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate cp = prbuf;
229*0Sstevel@tonic-gate do {
230*0Sstevel@tonic-gate *cp++ = "0123456789abcdef"[n%b];
231*0Sstevel@tonic-gate n /= b;
232*0Sstevel@tonic-gate } while (n);
233*0Sstevel@tonic-gate do {
234*0Sstevel@tonic-gate PUTCHAR(*--cp);
235*0Sstevel@tonic-gate } while (cp > prbuf);
236*0Sstevel@tonic-gate return (lbp);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate static int
_write(fd,buf,len)240*0Sstevel@tonic-gate _write(fd, buf, len)
241*0Sstevel@tonic-gate int fd;
242*0Sstevel@tonic-gate char *buf;
243*0Sstevel@tonic-gate int len;
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate if (fd == -1) {
247*0Sstevel@tonic-gate *(buf + len) = '\0';
248*0Sstevel@tonic-gate return (0);
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate return (write(fd, buf, len));
251*0Sstevel@tonic-gate }
252