Debian Codegraph @

src/misc.c

raw symbols SCIP
1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "makeint.h"
18 #include "filedef.h"
19 #include "dep.h"
20 #include "debug.h"
21
22 /* GNU make no longer supports pre-ANSI89 environments. */
23
24 #include <stdarg.h>
25
26 #ifdef WINDOWS32
27 # include <windows.h>
28 # include <io.h>
29 #endif
30
31 #ifdef HAVE_FCNTL_H
32 # include <fcntl.h>
33 #else
34 # include <sys/file.h>
35 #endif
36
37 /* Compare strings *S1 and *S2.
38 Return negative if the first is less, positive if it is greater,
39 zero if they are equal. */
40
41 int
42 alpha_compare (const void *v1, const void *v2)
43 {
44 const char *s1 = *((char **)v1);
45 const char *s2 = *((char **)v2);
46
47 if (*s1 != *s2)
48 return *s1 - *s2;
49 return strcmp (s1, s2);
50 }
51
52 /* Discard each backslash-newline combination from LINE.
53 Backslash-backslash-newline combinations become backslash-newlines.
54 This is done by copying the text at LINE into itself. */
55
56 void
57 collapse_continuations (char *line)
58 {
59 char *out = line;
60 char *in = line;
61 char *q;
62
63 q = strchr(in, '\n');
64 if (q == 0)
65 return;
66
67 do
68 {
69 char *p = q;
70 int i;
71 size_t out_line_length;
72
73 if (q > line && q[-1] == '\\')
74 {
75 /* Search for more backslashes. */
76 i = -2;
77 while (&p[i] >= line && p[i] == '\\')
78 --i;
79 ++i;
80 }
81 else
82 i = 0;
83
84 /* The number of backslashes is now -I, keep half of them. */
85 out_line_length = (p - in) + i - i/2;
86 if (out != in)
87 memmove (out, in, out_line_length);
88 out += out_line_length;
89
90 /* When advancing IN, skip the newline too. */
91 in = q + 1;
92
93 if (i & 1)
94 {
95 /* Backslash/newline handling:
96 In traditional GNU make all trailing whitespace, consecutive
97 backslash/newlines, and any leading non-newline whitespace on the
98 next line is reduced to a single space.
99 In POSIX, each backslash/newline and is replaced by a space. */
100 while (ISBLANK (*in))
101 ++in;
102 if (! posix_pedantic)
103 while (out > line && ISBLANK (out[-1]))
104 --out;
105 *out++ = ' ';
106 }
107 else
108 {
109 /* If the newline isn't quoted, put it in the output. */
110 *out++ = '\n';
111 }
112
113 q = strchr(in, '\n');
114 }
115 while (q);
116
117 memmove(out, in, strlen(in) + 1);
118 }
119
120 /* Print N spaces (used in debug for target-depth). */
121
122 void
123 print_spaces (unsigned int n)
124 {
125 while (n-- > 0)
126 putchar (' ');
127 }
128
129
130 /* Return a string whose contents concatenate the NUM strings provided
131 This string lives in static, re-used memory. */
132
133 const char *
134 concat (unsigned int num, ...)
135 {
136 static size_t rlen = 0;
137 static char *result = NULL;
138 size_t ri = 0;
139 va_list args;
140
141 va_start (args, num);
142
143 while (num-- > 0)
144 {
145 const char *s = va_arg (args, const char *);
146 size_t l = xstrlen (s);
147
148 if (l == 0)
149 continue;
150
151 if (ri + l > rlen)
152 {
153 rlen = ((rlen ? rlen : 60) + l) * 2;
154 result = xrealloc (result, rlen);
155 }
156
157 memcpy (result + ri, s, l);
158 ri += l;
159 }
160
161 va_end (args);
162
163 /* Get some more memory if we don't have enough space for the
164 terminating '\0'. */
165 if (ri == rlen)
166 {
167 rlen = (rlen ? rlen : 60) * 2;
168 result = xrealloc (result, rlen);
169 }
170
171 result[ri] = '\0';
172
173 return result;
174 }
175
176 #ifndef HAVE_MEMRCHR
177 void *
178 memrchr(const void* str, int ch, size_t len)
179 {
180 const char* sp = str;
181 const char* cp = sp;
182
183 if (len == 0)
184 return NULL;
185
186 cp += len - 1;
187
188 while (cp[0] != ch)
189 {
190 if (cp == sp)
191 return NULL;
192 --cp;
193 }
194
195 return (void*)cp;
196 }
197 #endif
198
199
200
201 /* Limited INDEX:
202 Search through the string STRING, which ends at LIMIT, for the character C.
203 Returns a pointer to the first occurrence, or nil if none is found.
204 Like INDEX except that the string searched ends where specified
205 instead of at the first null. */
206
207 char *
208 lindex (const char *s, const char *limit, int c)
209 {
210 while (s < limit)
211 if (*s++ == c)
212 return (char *)(s - 1);
213
214 return 0;
215 }
216
217 /* Return the address of the first whitespace or null in the string S. */
218
219 char *
220 end_of_token (const char *s)
221 {
222 END_OF_TOKEN (s);
223 return (char *)s;
224 }
225
226 /* Return the address of the first nonwhitespace or null in the string S. */
227
228 char *
229 next_token (const char *s)
230 {
231 NEXT_TOKEN (s);
232 return (char *)s;
233 }
234
235 /* Find the next token in PTR; return the address of it, and store the length
236 of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
237 of the token, so this function can be called repeatedly in a loop. */
238
239 char *
240 find_next_token (const char **ptr, size_t *lengthptr)
241 {
242 const char *p = next_token (*ptr);
243
244 if (*p == '\0')
245 return 0;
246
247 *ptr = end_of_token (p);
248 if (lengthptr != 0)
249 *lengthptr = *ptr - p;
250
251 return (char *)p;
252 }
253
254 /* Write a BUFFER of size LEN to file descriptor FD.
255 Retry short writes from EINTR. Return LEN, or -1 on error. */
256 ssize_t
257 writebuf (int fd, const void *buffer, size_t len)
258 {
259 const char *msg = buffer;
260 size_t l = len;
261 while (l)
262 {
263 ssize_t r;
264
265 EINTRLOOP (r, write (fd, msg, l));
266 if (r < 0)
267 return r;
268
269 l -= r;
270 msg += r;
271 }
272
273 return (ssize_t)len;
274 }
275
276 /* Read until we get LEN bytes from file descriptor FD, into BUFFER.
277 Retry short reads on EINTR. If we get an error, return it.
278 Return 0 at EOF. */
279 ssize_t
280 readbuf (int fd, void *buffer, size_t len)
281 {
282 char *msg = buffer;
283 while (len)
284 {
285 ssize_t r;
286
287 EINTRLOOP (r, read (fd, msg, len));
288 if (r < 0)
289 return r;
290 if (r == 0)
291 break;
292
293 len -= r;
294 msg += r;
295 }
296
297 return (ssize_t)(msg - (char*)buffer);
298 }
299
300
301 /* Copy a chain of 'struct dep'. For 2nd expansion deps, dup the name. */
302
303 struct dep *
304 copy_dep_chain (const struct dep *d)
305 {
306 struct dep *firstnew = 0;
307 struct dep *lastnew = 0;
308
309 while (d != 0)
310 {
311 struct dep *c = xmalloc (sizeof (struct dep));
312 memcpy (c, d, sizeof (struct dep));
313
314 if (c->need_2nd_expansion)
315 c->name = xstrdup (c->name);
316
317 c->next = 0;
318 if (firstnew == 0)
319 firstnew = lastnew = c;
320 else
321 lastnew = lastnew->next = c;
322
323 d = d->next;
324 }
325
326 return firstnew;
327 }
328
329 /* Free a chain of struct nameseq.
330 For struct dep chains use free_dep_chain. */
331
332 void
333 free_ns_chain (struct nameseq *ns)
334 {
335 while (ns != 0)
336 {
337 struct nameseq *t = ns;
338 ns = ns->next;
339 free_ns (t);
340 }
341 }
342
343
344 #ifdef MAKE_MAINTAINER_MODE
345
346 void
347 spin (const char* type)
348 {
349 char filenm[256];
350 struct stat dummy;
351
352 sprintf (filenm, ".make-spin-%s", type);
353
354 if (stat (filenm, &dummy) == 0)
355 {
356 fprintf (stderr, "SPIN on %s\n", filenm);
357 do
358 #ifdef WINDOWS32
359 Sleep (1000);
360 #else
361 sleep (1);
362 #endif
363 while (stat (filenm, &dummy) == 0);
364 }
365 }
366
367 #endif
368
369
370
371 /* Provide support for temporary files. */
372
373 #ifndef HAVE_STDLIB_H
374 # ifdef HAVE_MKSTEMP
375 int mkstemp (char *template);
376 # else
377 char *mktemp (char *template);
378 # endif
379 #endif
380
381 #ifndef HAVE_UMASK
382 mode_t
383 umask (mode_t mask)
384 {
385 return 0;
386 }
387 #endif
388
389 FILE *
390 get_tmpfile (char **name, const char *template)
391 {
392 FILE *file;
393 #ifdef HAVE_FDOPEN
394 int fd;
395 #endif
396
397 /* Preserve the current umask, and set a restrictive one for temp files. */
398 mode_t mask = umask (0077);
399
400 #if defined(HAVE_MKSTEMP) || defined(HAVE_MKTEMP)
401 # define TEMPLATE_LEN strlen (template)
402 #else
403 # define TEMPLATE_LEN L_tmpnam
404 #endif
405 *name = xmalloc (TEMPLATE_LEN + 1);
406 strcpy (*name, template);
407
408 #if defined(HAVE_MKSTEMP) && defined(HAVE_FDOPEN)
409 /* It's safest to use mkstemp(), if we can. */
410 EINTRLOOP (fd, mkstemp (*name));
411 if (fd == -1)
412 file = NULL;
413 else
414 file = fdopen (fd, "w");
415 #else
416 # ifdef HAVE_MKTEMP
417 (void) mktemp (*name);
418 # else
419 (void) tmpnam (*name);
420 # endif
421
422 # ifdef HAVE_FDOPEN
423 /* Can't use mkstemp(), but guard against a race condition. */
424 EINTRLOOP (fd, open (*name, O_CREAT|O_EXCL|O_WRONLY, 0600));
425 if (fd == -1)
426 return 0;
427 file = fdopen (fd, "w");
428 # else
429 /* Not secure, but what can we do? */
430 file = fopen (*name, "w");
431 # endif
432 #endif
433
434 umask (mask);
435
436 return file;
437 }
438
439
440 #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
441 /* If we don't have strcasecmp() (from POSIX), or anything that can substitute
442 for it, define our own version. */
443
444 int
445 strcasecmp (const char *s1, const char *s2)
446 {
447 while (1)
448 {
449 int c1 = (int) *(s1++);
450 int c2 = (int) *(s2++);
451
452 if (isalpha (c1))
453 c1 = tolower (c1);
454 if (isalpha (c2))
455 c2 = tolower (c2);
456
457 if (c1 != '\0' && c1 == c2)
458 continue;
459
460 return (c1 - c2);
461 }
462 }
463 #endif
464
465 #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
466 /* If we don't have strncasecmp() (from POSIX), or anything that can
467 substitute for it, define our own version. */
468
469 int
470 strncasecmp (const char *s1, const char *s2, int n)
471 {
472 while (n-- > 0)
473 {
474 int c1 = (int) *(s1++);
475 int c2 = (int) *(s2++);
476
477 if (isalpha (c1))
478 c1 = tolower (c1);
479 if (isalpha (c2))
480 c2 = tolower (c2);
481
482 if (c1 != '\0' && c1 == c2)
483 continue;
484
485 return (c1 - c2);
486 }
487
488 return 0;
489 }
490 #endif
491
492 #ifdef GETLOADAVG_PRIVILEGED
493
494 #ifdef POSIX
495
496 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
497 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
498 for example) which claim to be POSIX.1 also have the BSD setreuid and
499 setregid functions, but they don't work as in BSD and only the POSIX.1
500 way works. */
501
502 #undef HAVE_SETREUID
503 #undef HAVE_SETREGID
504
505 #else /* Not POSIX. */
506
507 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
508 POSIX-like system, they are the best thing to use. However, some
509 non-POSIX systems have them too but they do not work in the POSIX style
510 and we must use setreuid and setregid instead. */
511
512 #undef HAVE_SETEUID
513 #undef HAVE_SETEGID
514
515 #endif /* POSIX. */
516
517 #ifndef HAVE_UNISTD_H
518 extern int getuid (), getgid (), geteuid (), getegid ();
519 extern int setuid (), setgid ();
520 #ifdef HAVE_SETEUID
521 extern int seteuid ();
522 #else
523 #ifdef HAVE_SETREUID
524 extern int setreuid ();
525 #endif /* Have setreuid. */
526 #endif /* Have seteuid. */
527 #ifdef HAVE_SETEGID
528 extern int setegid ();
529 #else
530 #ifdef HAVE_SETREGID
531 extern int setregid ();
532 #endif /* Have setregid. */
533 #endif /* Have setegid. */
534 #endif /* No <unistd.h>. */
535
536 /* Keep track of the user and group IDs for user- and make- access. */
537 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
538 #define access_inited (user_uid != -1)
539 static enum { make, user } current_access;
540
541
542 /* Under -d, write a message describing the current IDs. */
543
544 static void
545 log_access (const char *flavor)
546 {
547 if (! ISDB (DB_JOBS))
548 return;
549
550 /* All the other debugging messages go to stdout,
551 but we write this one to stderr because it might be
552 run in a child fork whose stdout is piped. */
553
554 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
555 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
556 (unsigned long) getegid (), (unsigned long) getgid ());
557 fflush (stderr);
558 }
559
560
561 static void
562 init_access (void)
563 {
564 #ifndef VMS
565 user_uid = getuid ();
566 user_gid = getgid ();
567
568 make_uid = geteuid ();
569 make_gid = getegid ();
570
571 /* Do these ever fail? */
572 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
573 pfatal_with_name ("get{e}[gu]id");
574
575 log_access (_("Initialized access"));
576
577 current_access = make;
578 #endif
579 }
580
581 #endif /* GETLOADAVG_PRIVILEGED */
582
583 /* Give the process appropriate permissions for access to
584 user data (i.e., to stat files, or to spawn a child process). */
585 void
586 user_access (void)
587 {
588 #ifdef GETLOADAVG_PRIVILEGED
589
590 if (!access_inited)
591 init_access ();
592
593 if (current_access == user)
594 return;
595
596 /* We are in "make access" mode. This means that the effective user and
597 group IDs are those of make (if it was installed setuid or setgid).
598 We now want to set the effective user and group IDs to the real IDs,
599 which are the IDs of the process that exec'd make. */
600
601 #ifdef HAVE_SETEUID
602
603 /* Modern systems have the seteuid/setegid calls which set only the
604 effective IDs, which is ideal. */
605
606 if (seteuid (user_uid) < 0)
607 pfatal_with_name ("user_access: seteuid");
608
609 #else /* Not HAVE_SETEUID. */
610
611 #ifndef HAVE_SETREUID
612
613 /* System V has only the setuid/setgid calls to set user/group IDs.
614 There is an effective ID, which can be set by setuid/setgid.
615 It can be set (unless you are root) only to either what it already is
616 (returned by geteuid/getegid, now in make_uid/make_gid),
617 the real ID (return by getuid/getgid, now in user_uid/user_gid),
618 or the saved set ID (what the effective ID was before this set-ID
619 executable (make) was exec'd). */
620
621 if (setuid (user_uid) < 0)
622 pfatal_with_name ("user_access: setuid");
623
624 #else /* HAVE_SETREUID. */
625
626 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
627 They may be set to themselves or each other. So you have two alternatives
628 at any one time. If you use setuid/setgid, the effective will be set to
629 the real, leaving only one alternative. Using setreuid/setregid, however,
630 you can toggle between your two alternatives by swapping the values in a
631 single setreuid or setregid call. */
632
633 if (setreuid (make_uid, user_uid) < 0)
634 pfatal_with_name ("user_access: setreuid");
635
636 #endif /* Not HAVE_SETREUID. */
637 #endif /* HAVE_SETEUID. */
638
639 #ifdef HAVE_SETEGID
640 if (setegid (user_gid) < 0)
641 pfatal_with_name ("user_access: setegid");
642 #else
643 #ifndef HAVE_SETREGID
644 if (setgid (user_gid) < 0)
645 pfatal_with_name ("user_access: setgid");
646 #else
647 if (setregid (make_gid, user_gid) < 0)
648 pfatal_with_name ("user_access: setregid");
649 #endif
650 #endif
651
652 current_access = user;
653
654 log_access (_("User access"));
655
656 #endif /* GETLOADAVG_PRIVILEGED */
657 }
658
659 /* Give the process appropriate permissions for access to
660 make data (i.e., the load average). */
661 void
662 make_access (void)
663 {
664 #ifdef GETLOADAVG_PRIVILEGED
665
666 if (!access_inited)
667 init_access ();
668
669 if (current_access == make)
670 return;
671
672 /* See comments in user_access, above. */
673
674 #ifdef HAVE_SETEUID
675 if (seteuid (make_uid) < 0)
676 pfatal_with_name ("make_access: seteuid");
677 #else
678 #ifndef HAVE_SETREUID
679 if (setuid (make_uid) < 0)
680 pfatal_with_name ("make_access: setuid");
681 #else
682 if (setreuid (user_uid, make_uid) < 0)
683 pfatal_with_name ("make_access: setreuid");
684 #endif
685 #endif
686
687 #ifdef HAVE_SETEGID
688 if (setegid (make_gid) < 0)
689 pfatal_with_name ("make_access: setegid");
690 #else
691 #ifndef HAVE_SETREGID
692 if (setgid (make_gid) < 0)
693 pfatal_with_name ("make_access: setgid");
694 #else
695 if (setregid (user_gid, make_gid) < 0)
696 pfatal_with_name ("make_access: setregid");
697 #endif
698 #endif
699
700 current_access = make;
701
702 log_access (_("Make access"));
703
704 #endif /* GETLOADAVG_PRIVILEGED */
705 }
706
707 /* Give the process appropriate permissions for a child process.
708 This is like user_access, but you can't get back to make_access. */
709 void
710 child_access (void)
711 {
712 #ifdef GETLOADAVG_PRIVILEGED
713
714 if (!access_inited)
715 abort ();
716
717 /* Set both the real and effective UID and GID to the user's.
718 They cannot be changed back to make's. */
719
720 #ifndef HAVE_SETREUID
721 if (setuid (user_uid) < 0)
722 pfatal_with_name ("child_access: setuid");
723 #else
724 if (setreuid (user_uid, user_uid) < 0)
725 pfatal_with_name ("child_access: setreuid");
726 #endif
727
728 #ifndef HAVE_SETREGID
729 if (setgid (user_gid) < 0)
730 pfatal_with_name ("child_access: setgid");
731 #else
732 if (setregid (user_gid, user_gid) < 0)
733 pfatal_with_name ("child_access: setregid");
734 #endif
735
736 log_access (_("Child access"));
737
738 #endif /* GETLOADAVG_PRIVILEGED */
739 }
740
741 #ifdef NEED_GET_PATH_MAX
742 unsigned int
743 get_path_max (void)
744 {
745 static unsigned int value;
746
747 if (value == 0)
748 {
749 long int x = pathconf ("/", _PC_PATH_MAX);
750 if (x > 0)
751 value = x;
752 else
753 return MAXPATHLEN;
754 }
755
756 return value;
757 }
758 #endif
759
760 /* Map of possible stop characters for searching strings. */
761 #ifndef UCHAR_MAX
762 # define UCHAR_MAX 255
763 #endif
764 unsigned short stopchar_map[UCHAR_MAX + 1] = {0};
765
766 /* This character map locate stop chars when parsing GNU makefiles.
767 Each element is true if we should stop parsing on that character. */
768
769 extern void
770 initialize_stopchar_map (void)
771 {
772 int i;
773 stopchar_map[(int)'\0'] = MAP_NUL;
774 stopchar_map[(int)'#'] = MAP_COMMENT;
775 stopchar_map[(int)';'] = MAP_SEMI;
776 stopchar_map[(int)'='] = MAP_EQUALS;
777 stopchar_map[(int)':'] = MAP_COLON;
778 stopchar_map[(int)'|'] = MAP_PIPE;
779 stopchar_map[(int)'.'] = MAP_DOT | MAP_USERFUNC;
780 stopchar_map[(int)','] = MAP_COMMA;
781 stopchar_map[(int)'('] = MAP_VARSEP;
782 stopchar_map[(int)'{'] = MAP_VARSEP;
783 stopchar_map[(int)'}'] = MAP_VARSEP;
784 stopchar_map[(int)')'] = MAP_VARSEP;
785 stopchar_map[(int)'$'] = MAP_VARIABLE;
786
787 stopchar_map[(int)'-'] = MAP_USERFUNC;
788 stopchar_map[(int)'_'] = MAP_USERFUNC;
789
790 stopchar_map[(int)' '] = MAP_BLANK;
791 stopchar_map[(int)'\t'] = MAP_BLANK;
792
793 stopchar_map[(int)'/'] = MAP_DIRSEP;
794
795 #if defined(HAVE_DOS_PATHS)
796 stopchar_map[(int)'\\'] = MAP_DIRSEP;
797 #endif
798
799 for (i = 1; i <= UCHAR_MAX; ++i)
800 {
801 if (isspace (i) && NONE_SET (stopchar_map[i], MAP_BLANK))
802 /* Don't mark blank characters as newline characters. */
803 stopchar_map[i] |= MAP_NEWLINE;
804 else if (isalnum (i))
805 stopchar_map[i] |= MAP_USERFUNC;
806 }
807 }