Increase the default TCP maximum segment size from 512 to 1460.
[dragonfly.git] / usr.bin / make / util.c
1 /*
2  * Copyright (c) 2002 Juli Mallett.  All rights reserved.
3  * Copyright (c) 1988, 1989, 1990, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1989 by Berkeley Softworks
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Adam de Boor.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * $FreeBSD: src/usr.bin/make/util.c,v 1.5.2.2 2001/02/13 03:13:58 will Exp $
28  * $DragonFly: src/usr.bin/make/util.c,v 1.12 2005/01/06 21:06:25 okumoto Exp $
29  */
30
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include "globals.h"
41 #include "job.h"
42 #include "targ.h"
43 #include "util.h"
44
45 static void enomem(void);
46
47 /*-
48  * Debug --
49  *      Print a debugging message given its format.
50  *
51  * Results:
52  *      None.
53  *
54  * Side Effects:
55  *      The message is printed.
56  */
57 /* VARARGS */
58 void
59 Debug(const char *fmt, ...)
60 {
61         va_list ap;
62
63         va_start(ap, fmt);
64         vfprintf(stderr, fmt, ap);
65         va_end(ap);
66         fflush(stderr);
67 }
68
69 /*-
70  * Error --
71  *      Print an error message given its format.
72  *
73  * Results:
74  *      None.
75  *
76  * Side Effects:
77  *      The message is printed.
78  */
79 /* VARARGS */
80 void
81 Error(const char *fmt, ...)
82 {
83         va_list ap;
84
85         va_start(ap, fmt);
86         vfprintf(stderr, fmt, ap);
87         va_end(ap);
88         fprintf(stderr, "\n");
89         fflush(stderr);
90 }
91
92 /*-
93  * Fatal --
94  *      Produce a Fatal error message. If jobs are running, waits for them
95  *      to finish.
96  *
97  * Results:
98  *      None
99  *
100  * Side Effects:
101  *      The program exits
102  */
103 /* VARARGS */
104 void
105 Fatal(const char *fmt, ...)
106 {
107         va_list ap;
108
109         va_start(ap, fmt);
110         if (jobsRunning)
111                 Job_Wait();
112
113         vfprintf(stderr, fmt, ap);
114         va_end(ap);
115         fprintf(stderr, "\n");
116         fflush(stderr);
117
118         if (DEBUG(GRAPH2))
119                 Targ_PrintGraph(2);
120         exit(2);                /* Not 1 so -q can distinguish error */
121 }
122
123 /*
124  * Punt --
125  *      Major exception once jobs are being created. Kills all jobs, prints
126  *      a message and exits.
127  *
128  * Results:
129  *      None
130  *
131  * Side Effects:
132  *      All children are killed indiscriminately and the program Lib_Exits
133  */
134 /* VARARGS */
135 void
136 Punt(const char *fmt, ...)
137 {
138         va_list ap;
139
140         va_start(ap, fmt);
141         fprintf(stderr, "make: ");
142         vfprintf(stderr, fmt, ap);
143         va_end(ap);
144         fprintf(stderr, "\n");
145         fflush(stderr);
146
147         DieHorribly();
148 }
149
150 /*-
151  * DieHorribly --
152  *      Exit without giving a message.
153  *
154  * Results:
155  *      None
156  *
157  * Side Effects:
158  *      A big one...
159  */
160 void
161 DieHorribly(void)
162 {
163         if (jobsRunning)
164                 Job_AbortAll();
165         if (DEBUG(GRAPH2))
166                 Targ_PrintGraph(2);
167         exit(2);                /* Not 1, so -q can distinguish error */
168 }
169
170 /*
171  * Finish --
172  *      Called when aborting due to errors in child shell to signal
173  *      abnormal exit, with the number of errors encountered in Make_Make.
174  *
175  * Results:
176  *      None
177  *
178  * Side Effects:
179  *      The program exits
180  */
181 void
182 Finish(int errors)
183 {
184
185         Fatal("%d error%s", errors, errors == 1 ? "" : "s");
186 }
187
188 /*
189  * emalloc --
190  *      malloc, but die on error.
191  */
192 void *
193 emalloc(size_t len)
194 {
195         void *p;
196
197         if ((p = malloc(len)) == NULL)
198                 enomem();
199         return (p);
200 }
201
202 /*
203  * estrdup --
204  *      strdup, but die on error.
205  */
206 char *
207 estrdup(const char *str)
208 {
209         char *p;
210
211         if ((p = strdup(str)) == NULL)
212                 enomem();
213         return (p);
214 }
215
216 /*
217  * erealloc --
218  *      realloc, but die on error.
219  */
220 void *
221 erealloc(void *ptr, size_t size)
222 {
223
224         if ((ptr = realloc(ptr, size)) == NULL)
225                 enomem();
226         return (ptr);
227 }
228
229 /*
230  * enomem --
231  *      die when out of memory.
232  */
233 static void
234 enomem(void)
235 {
236         err(2, NULL);
237 }
238
239 /*
240  * enunlink --
241  *      Remove a file carefully, avoiding directories.
242  */
243 int
244 eunlink(const char *file)
245 {
246         struct stat st;
247
248         if (lstat(file, &st) == -1)
249                 return (-1);
250
251         if (S_ISDIR(st.st_mode)) {
252                 errno = EISDIR;
253                 return (-1);
254         }
255         return (unlink(file));
256 }
257
258 /*
259  * Printaddr --
260  *      Print the address of a node, used as an interative function.
261  */
262 int
263 PrintAddr(void *a, void *b __unused)
264 {
265
266     printf("%p ", a);
267     return (0);
268 }