summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/ld/etc.c
blob: 22fdfd8f1603d93d4e51581c194c37597512bcba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * $Id: etc.c,v 1.5 1993/12/04 00:52:55 jkh Exp $
 */

#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <fcntl.h>
#include <ar.h>
#include <ranlib.h>
#include <a.out.h>
#include <stab.h>
#include <string.h>
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#include "ld.h"

/*
 * Report a nonfatal error.
 */

void
#if __STDC__
error(char *fmt, ...)
#else
error(fmt, va_alist)
	char	*fmt;
	va_dcl
#endif
{
	va_list	ap;
#if __STDC__
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	(void)fprintf(stderr, "%s: ", progname);
	(void)vfprintf(stderr, fmt, ap);
	(void)fprintf(stderr, "\n");
	va_end(ap);
}

/*
 * Report a fatal error.
 */

void
#if __STDC__
fatal(char *fmt, ...)
#else
fatal(fmt, va_alist)
	char	*fmt;
	va_dcl
#endif
{
	va_list	ap;
#if __STDC__
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	(void)fprintf(stderr, "%s: ", progname);
	(void)vfprintf(stderr, fmt, ap);
	(void)fprintf(stderr, "\n");
	va_end(ap);

	if (outdesc >= 0)
		unlink(output_filename);
	exit(1);
}


/*
 * Return a newly-allocated string whose contents concatenate
 * the strings S1, S2, S3.
 */

char *
concat(s1, s2, s3)
	const char *s1, *s2, *s3;
{
	register int	len1 = strlen (s1),
			len2 = strlen (s2),
			len3 = strlen (s3);

	register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);

	strcpy (result, s1);
	strcpy (result + len1, s2);
	strcpy (result + len1 + len2, s3);
	result[len1 + len2 + len3] = 0;

	return result;
}

/* Parse the string ARG using scanf format FORMAT, and return the result.
   If it does not parse, report fatal error
   generating the error message using format string ERROR and ARG as arg.  */

int
parse(arg, format, error)
	char *arg, *format, *error;
{
	int x;
	if (1 != sscanf (arg, format, &x))
		fatal (error, arg);
	return x;
}

/* Like malloc but get fatal error if memory is exhausted.  */

void *
xmalloc(size)
	int size;
{
	register void	*result = (void *)malloc (size);

	if (!result)
		fatal ("virtual memory exhausted", 0);

	return result;
}

/* Like realloc but get fatal error if memory is exhausted.  */

void *
xrealloc(ptr, size)
	void *ptr;
	int size;
{
	register void	*result;

	if (ptr == NULL)
		result = (void *)malloc (size);
	else
		result = (void *)realloc (ptr, size);

	if (!result)
		fatal ("virtual memory exhausted", 0);

	return result;
}



/* These must move */

#ifndef RTLD
/*
 * Output COUNT*ELTSIZE bytes of data at BUF to the descriptor DESC.
 */
void
mywrite (buf, count, eltsize, desc)
     char *buf;
     int count;
     int eltsize;
     int desc;
{
	register int val;
	register int bytes = count * eltsize;

	while (bytes > 0) {
		val = write (desc, buf, bytes);
		if (val <= 0)
			perror(output_filename);
		buf += val;
		bytes -= val;
	}
}

/*
 * Output PADDING zero-bytes to descriptor OUTDESC.
 * PADDING may be negative; in that case, do nothing.
 */

void
padfile (padding, outdesc)
     int padding;
     int outdesc;
{
	register char *buf;
	if (padding <= 0)
		return;

	buf = (char *) alloca (padding);
	bzero (buf, padding);
	mywrite (buf, padding, 1, outdesc);
}
#endif
OpenPOWER on IntegriCloud