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