Ignore:
Timestamp:
Mar 12, 2018, 8:32:29 PM (7 years ago)
Author:
bird
Message:

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/gnumake/current/misc.c

    r2596 r3138  
    11/* Miscellaneous generic support functions for GNU Make.
    2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
    4 2010 Free Software Foundation, Inc.
     2Copyright (C) 1988-2016 Free Software Foundation, Inc.
    53This file is part of GNU Make.
    64
     
    1715this program.  If not, see <http://www.gnu.org/licenses/>.  */
    1816
    19 #include "make.h"
     17#include "makeint.h"
     18#include "filedef.h"
    2019#include "dep.h"
    2120#include "debug.h"
    2221
    23 /* Variadic functions.  We go through contortions to allow proper function
    24    prototypes for both ANSI and pre-ANSI C compilers, and also for those
    25    which support stdarg.h vs. varargs.h, and finally those which have
    26    vfprintf(), etc. and those who have _doprnt... or nothing.
    27 
    28    This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
    29    VA_END macros used here since we have multiple print functions.  */
    30 
    31 #if USE_VARIADIC
    32 # if HAVE_STDARG_H
    33 #  include <stdarg.h>
    34 #  define VA_START(args, lastarg) va_start(args, lastarg)
    35 # else
    36 #  include <varargs.h>
    37 #  define VA_START(args, lastarg) va_start(args)
    38 # endif
    39 # if HAVE_VPRINTF
    40 #  define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
    41 # else
    42 #  define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
    43 # endif
    44 # define VA_END(args) va_end(args)
    45 #else
    46 /* We can't use any variadic interface! */
    47 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
    48 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
    49 # define VA_START(args, lastarg)
    50 # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
    51 # define VA_END(args)
    52 #endif
    53 
     22/* GNU make no longer supports pre-ANSI89 environments.  */
     23
     24#include <stdarg.h>
     25
     26#ifdef HAVE_FCNTL_H
     27# include <fcntl.h>
     28#else
     29# include <sys/file.h>
     30#endif
    5431
    5532/* Compare strings *S1 and *S2.
     
    7653collapse_continuations (char *line)
    7754{
    78   register char *in, *out, *p;
    79   register int backslash;
    80   register unsigned int bs_write;
     55  char *in, *out, *p;
    8156
    8257  in = strchr (line, '\n');
     
    9166    {
    9267      /* BS_WRITE gets the number of quoted backslashes at
    93         the end just before IN, and BACKSLASH gets nonzero
    94         if the next character is quoted.  */
    95       backslash = 0;
    96       bs_write = 0;
     68        the end just before IN, and BACKSLASH gets nonzero
     69        if the next character is quoted.  */
     70      unsigned int backslash = 0;
     71      unsigned int bs_write = 0;
    9772      for (p = in - 1; p >= line && *p == '\\'; --p)
    98         {
    99           if (backslash)
    100             ++bs_write;
    101           backslash = !backslash;
    102 
    103           /* It should be impossible to go back this far without exiting,
    104              but if we do, we can't get the right answer.  */
    105           if (in == out - 1)
    106             abort ();
    107         }
     73        {
     74          if (backslash)
     75            ++bs_write;
     76          backslash = !backslash;
     77
     78          /* It should be impossible to go back this far without exiting,
     79             but if we do, we can't get the right answer.  */
     80          if (in == out - 1)
     81            abort ();
     82        }
    10883
    10984      /* Output the appropriate number of backslashes.  */
    11085      while (bs_write-- > 0)
    111         *out++ = '\\';
     86        *out++ = '\\';
    11287
    11388      /* Skip the newline.  */
    11489      ++in;
    11590
    116       /* If the newline is escaped, discard following whitespace leaving just
    117          one space.  POSIX requires that each backslash/newline/following
    118          whitespace sequence be reduced to a single space.  */
    11991      if (backslash)
    120         {
    121           in = next_token (in);
    122           /* Removing this loop will fix Savannah bug #16670: do we want to? */
    123           while (out > line && isblank ((unsigned char)out[-1]))
    124             --out;
    125           *out++ = ' ';
    126         }
     92        {
     93          /* Backslash/newline handling:
     94             In traditional GNU make all trailing whitespace, consecutive
     95             backslash/newlines, and any leading non-newline whitespace on the
     96             next line is reduced to a single space.
     97             In POSIX, each backslash/newline and is replaced by a space.  */
     98          while (ISBLANK (*in))
     99            ++in;
     100          if (! posix_pedantic)
     101            while (out > line && ISBLANK (out[-1]))
     102              --out;
     103          *out++ = ' ';
     104        }
    127105      else
    128         /* If the newline isn't quoted, put it in the output.  */
    129         *out++ = '\n';
     106        /* If the newline isn't quoted, put it in the output.  */
     107        *out++ = '\n';
    130108
    131109      /* Now copy the following line to the output.
    132         Stop when we find backslashes followed by a newline.  */
     110        Stop when we find backslashes followed by a newline.  */
    133111      while (*in != '\0')
    134         if (*in == '\\')
    135           {
    136             p = in + 1;
    137             while (*p == '\\')
    138               ++p;
    139             if (*p == '\n')
    140               {
    141                 in = p;
    142                 break;
    143               }
    144             while (in < p)
    145               *out++ = *in++;
    146           }
    147         else
    148           *out++ = *in++;
     112        if (*in == '\\')
     113          {
     114            p = in + 1;
     115            while (*p == '\\')
     116              ++p;
     117            if (*p == '\n')
     118              {
     119                in = p;
     120                break;
     121              }
     122            while (in < p)
     123              *out++ = *in++;
     124          }
     125        else
     126          *out++ = *in++;
    149127    }
    150128
     
    168146
    169147const char *
    170 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
    171148concat (unsigned int num, ...)
    172 #else
    173 concat (num, va_alist)
    174      unsigned int num;
    175      va_dcl
    176 #endif
    177149{
    178150  static unsigned int rlen = 0;
    179151  static char *result = NULL;
    180   int ri = 0;
    181 
    182 #if USE_VARIADIC
     152  unsigned int ri = 0;
    183153  va_list args;
    184 #endif
    185 
    186   VA_START (args, num);
     154
     155  va_start (args, num);
    187156
    188157  while (num-- > 0)
    189158    {
    190159      const char *s = va_arg (args, const char *);
    191       unsigned int l = s ? strlen (s) : 0;
     160      unsigned int l = xstrlen (s);
    192161
    193162      if (l == 0)
     
    204173    }
    205174
    206   VA_END (args);
     175  va_end (args);
    207176
    208177  /* Get some more memory if we don't have enough space for the
     
    220189
    221190
    222 /* Print a message on stdout.  */
    223 
    224 void
    225 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
    226 message (int prefix, const char *fmt, ...)
    227 #else
    228 message (prefix, fmt, va_alist)
    229      int prefix;
    230      const char *fmt;
    231      va_dcl
    232 #endif
    233 {
    234 #if USE_VARIADIC
    235   va_list args;
    236 #endif
    237 
    238   log_working_directory (1);
    239 
    240   if (fmt != 0)
    241     {
    242       if (prefix)
    243         {
    244           if (makelevel == 0)
    245             printf ("%s: ", program);
    246           else
    247             printf ("%s[%u]: ", program, makelevel);
    248         }
    249       VA_START (args, fmt);
    250       VA_PRINTF (stdout, fmt, args);
    251       VA_END (args);
    252       putchar ('\n');
    253     }
    254 
    255   fflush (stdout);
    256 }
    257 
    258 /* Print an error message.  */
    259 
    260 void
    261 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
    262 error (const struct floc *flocp, const char *fmt, ...)
    263 #else
    264 error (flocp, fmt, va_alist)
    265      const struct floc *flocp;
    266      const char *fmt;
    267      va_dcl
    268 #endif
    269 {
    270 #if USE_VARIADIC
    271   va_list args;
    272 #endif
    273 
    274   log_working_directory (1);
    275 
    276   if (flocp && flocp->filenm)
    277     fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
    278   else if (makelevel == 0)
    279     fprintf (stderr, "%s: ", program);
    280   else
    281     fprintf (stderr, "%s[%u]: ", program, makelevel);
    282 
    283   VA_START(args, fmt);
    284   VA_PRINTF (stderr, fmt, args);
    285   VA_END (args);
    286 
    287   putc ('\n', stderr);
    288   fflush (stderr);
    289 }
    290 
    291 /* Print an error message and exit.  */
    292 
    293 void
    294 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
    295 fatal (const struct floc *flocp, const char *fmt, ...)
    296 #else
    297 fatal (flocp, fmt, va_alist)
    298      const struct floc *flocp;
    299      const char *fmt;
    300      va_dcl
    301 #endif
    302 {
    303 #if USE_VARIADIC
    304   va_list args;
    305 #endif
    306 
    307   log_working_directory (1);
    308 
    309   if (flocp && flocp->filenm)
    310     fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
    311   else if (makelevel == 0)
    312     fprintf (stderr, "%s: *** ", program);
    313   else
    314     fprintf (stderr, "%s[%u]: *** ", program, makelevel);
    315 
    316   VA_START(args, fmt);
    317   VA_PRINTF (stderr, fmt, args);
    318   VA_END (args);
    319 
    320   fputs (_(".  Stop.\n"), stderr);
    321 
    322   die (2);
    323 }
    324191
    325192#ifndef HAVE_STRERROR
    326 
    327 #undef  strerror
    328 
     193#undef  strerror
    329194char *
    330195strerror (int errnum)
     
    343208}
    344209#endif
    345 
    346 /* Print an error message from errno.  */
    347 
    348 void
    349 perror_with_name (const char *str, const char *name)
    350 {
    351   error (NILF, _("%s%s: %s"), str, name, strerror (errno));
    352 }
    353 
    354 /* Print an error message from errno and exit.  */
    355 
    356 void
    357 pfatal_with_name (const char *name)
    358 {
    359   fatal (NILF, _("%s: %s"), name, strerror (errno));
    360 
    361   /* NOTREACHED */
    362 }
    363210
    364211
     
    379226  void *result = malloc (size ? size : 1);
    380227  if (result == 0)
    381     fatal (NILF, _("virtual memory exhausted"));
     228    OUT_OF_MEM();
    382229  return result;
    383230}
     
    390237  void *result = calloc (size ? size : 1, 1);
    391238  if (result == 0)
    392     fatal (NILF, _("virtual memory exhausted"));
     239    OUT_OF_MEM();
    393240  return result;
    394241}
     
    405252  result = ptr ? realloc (ptr, size) : malloc (size);
    406253  if (result == 0)
    407     fatal (NILF, _("virtual memory exhausted"));
     254    OUT_OF_MEM();
    408255  return result;
    409256}
     
    422269
    423270  if (result == 0)
    424     fatal (NILF, _("virtual memory exhausted"));
     271    OUT_OF_MEM();
    425272
    426273#ifdef HAVE_STRDUP
     
    441288  result = strndup (str, length);
    442289  if (result == 0)
    443     fatal (NILF, _("virtual memory exhausted"));
     290    OUT_OF_MEM();
    444291#else
    445292  result = xmalloc (length + 1);
     
    476323end_of_token (const char *s)
    477324{
    478   while (*s != '\0' && !isblank ((unsigned char)*s))
    479     ++s;
     325  END_OF_TOKEN (s);
    480326  return (char *)s;
    481327}
    482 
    483 #ifdef WINDOWS32
    484 /*
    485  * Same as end_of_token, but take into account a stop character
    486  */
    487 char *
    488 end_of_token_w32 (const char *s, char stopchar)
    489 {
    490   const char *p = s;
    491   int backslash = 0;
    492 
    493   while (*p != '\0' && *p != stopchar
    494          && (backslash || !isblank ((unsigned char)*p)))
    495     {
    496       if (*p++ == '\\')
    497         {
    498           backslash = !backslash;
    499           while (*p == '\\')
    500             {
    501               backslash = !backslash;
    502               ++p;
    503             }
    504         }
    505       else
    506         backslash = 0;
    507     }
    508 
    509   return (char *)p;
    510 }
    511 #endif
    512328
    513329/* Return the address of the first nonwhitespace or null in the string S.  */
     
    516332next_token (const char *s)
    517333{
    518   while (isblank ((unsigned char)*s))
    519     ++s;
     334  NEXT_TOKEN (s);
    520335  return (char *)s;
    521336}
     
    542357
    543358
    544 /* Copy a chain of `struct dep'.  For 2nd expansion deps, dup the name.  */
     359/* Copy a chain of 'struct dep'.  For 2nd expansion deps, dup the name.  */
    545360
    546361struct dep *
     
    560375      c->next = 0;
    561376      if (firstnew == 0)
    562         firstnew = lastnew = c;
     377        firstnew = lastnew = c;
    563378      else
    564         lastnew = lastnew->next = c;
     379        lastnew = lastnew->next = c;
    565380
    566381      d = d->next;
     
    568383
    569384  return firstnew;
    570 }
    571 
    572 /* Free a chain of 'struct dep'.  */
    573 
    574 void
    575 free_dep_chain (struct dep *d)
    576 {
    577   while (d != 0)
    578     {
    579       struct dep *df = d;
    580       d = d->next;
    581       free_dep (df);
    582     }
    583385}
    584386
     
    593395      struct nameseq *t = ns;
    594396      ns = ns->next;
    595       free (t);
     397      free_ns (t);
    596398    }
    597399}
     
    600402
    601403#if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
    602 
    603404/* If we don't have strcasecmp() (from POSIX), or anything that can substitute
    604405   for it, define our own version.  */
     
    626427
    627428#if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
    628 
    629429/* If we don't have strncasecmp() (from POSIX), or anything that can
    630430   substitute for it, define our own version.  */
     
    654454
    655455
    656 #ifdef  GETLOADAVG_PRIVILEGED
     456#ifdef  GETLOADAVG_PRIVILEGED
    657457
    658458#ifdef POSIX
     
    667467#undef HAVE_SETREGID
    668468
    669 #else   /* Not POSIX.  */
     469#else   /* Not POSIX.  */
    670470
    671471/* Some POSIX.1 systems have the seteuid and setegid functions.  In a
     
    677477#undef HAVE_SETEGID
    678478
    679 #endif  /* POSIX.  */
    680 
    681 #ifndef HAVE_UNISTD_H
     479#endif  /* POSIX.  */
     480
     481#ifndef HAVE_UNISTD_H
    682482extern int getuid (), getgid (), geteuid (), getegid ();
    683483extern int setuid (), setgid ();
     
    685485extern int seteuid ();
    686486#else
    687 #ifdef  HAVE_SETREUID
     487#ifdef  HAVE_SETREUID
    688488extern int setreuid ();
    689 #endif  /* Have setreuid.  */
    690 #endif  /* Have seteuid.  */
     489#endif  /* Have setreuid.  */
     490#endif  /* Have seteuid.  */
    691491#ifdef HAVE_SETEGID
    692492extern int setegid ();
    693493#else
    694 #ifdef  HAVE_SETREGID
     494#ifdef  HAVE_SETREGID
    695495extern int setregid ();
    696 #endif  /* Have setregid.  */
    697 #endif  /* Have setegid.  */
    698 #endif  /* No <unistd.h>.  */
     496#endif  /* Have setregid.  */
     497#endif  /* Have setegid.  */
     498#endif  /* No <unistd.h>.  */
    699499
    700500/* Keep track of the user and group IDs for user- and make- access.  */
    701501static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
    702 #define access_inited   (user_uid != -1)
     502#define access_inited   (user_uid != -1)
    703503static enum { make, user } current_access;
    704504
     
    717517
    718518  fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
    719            flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
     519           flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
    720520           (unsigned long) getegid (), (unsigned long) getgid ());
    721521  fflush (stderr);
     
    743543}
    744544
    745 #endif  /* GETLOADAVG_PRIVILEGED */
     545#endif  /* GETLOADAVG_PRIVILEGED */
    746546
    747547/* Give the process appropriate permissions for access to
     
    750550user_access (void)
    751551{
    752 #ifdef  GETLOADAVG_PRIVILEGED
     552#ifdef  GETLOADAVG_PRIVILEGED
    753553
    754554  if (!access_inited)
     
    763563     which are the IDs of the process that exec'd make.  */
    764564
    765 #ifdef  HAVE_SETEUID
     565#ifdef  HAVE_SETEUID
    766566
    767567  /* Modern systems have the seteuid/setegid calls which set only the
     
    771571    pfatal_with_name ("user_access: seteuid");
    772572
    773 #else   /* Not HAVE_SETEUID.  */
    774 
    775 #ifndef HAVE_SETREUID
     573#else   /* Not HAVE_SETEUID.  */
     574
     575#ifndef HAVE_SETREUID
    776576
    777577  /* System V has only the setuid/setgid calls to set user/group IDs.
     
    786586    pfatal_with_name ("user_access: setuid");
    787587
    788 #else   /* HAVE_SETREUID.  */
     588#else   /* HAVE_SETREUID.  */
    789589
    790590  /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
     
    798598    pfatal_with_name ("user_access: setreuid");
    799599
    800 #endif  /* Not HAVE_SETREUID.  */
    801 #endif  /* HAVE_SETEUID.  */
    802 
    803 #ifdef  HAVE_SETEGID
     600#endif  /* Not HAVE_SETREUID.  */
     601#endif  /* HAVE_SETEUID.  */
     602
     603#ifdef  HAVE_SETEGID
    804604  if (setegid (user_gid) < 0)
    805605    pfatal_with_name ("user_access: setegid");
    806606#else
    807 #ifndef HAVE_SETREGID
     607#ifndef HAVE_SETREGID
    808608  if (setgid (user_gid) < 0)
    809609    pfatal_with_name ("user_access: setgid");
     
    818618  log_access (_("User access"));
    819619
    820 #endif  /* GETLOADAVG_PRIVILEGED */
     620#endif  /* GETLOADAVG_PRIVILEGED */
    821621}
    822622
     
    826626make_access (void)
    827627{
    828 #ifdef  GETLOADAVG_PRIVILEGED
     628#ifdef  GETLOADAVG_PRIVILEGED
    829629
    830630  if (!access_inited)
     
    836636  /* See comments in user_access, above.  */
    837637
    838 #ifdef  HAVE_SETEUID
     638#ifdef  HAVE_SETEUID
    839639  if (seteuid (make_uid) < 0)
    840640    pfatal_with_name ("make_access: seteuid");
    841641#else
    842 #ifndef HAVE_SETREUID
     642#ifndef HAVE_SETREUID
    843643  if (setuid (make_uid) < 0)
    844644    pfatal_with_name ("make_access: setuid");
     
    849649#endif
    850650
    851 #ifdef  HAVE_SETEGID
     651#ifdef  HAVE_SETEGID
    852652  if (setegid (make_gid) < 0)
    853653    pfatal_with_name ("make_access: setegid");
    854654#else
    855 #ifndef HAVE_SETREGID
     655#ifndef HAVE_SETREGID
    856656  if (setgid (make_gid) < 0)
    857657    pfatal_with_name ("make_access: setgid");
     
    866666  log_access (_("Make access"));
    867667
    868 #endif  /* GETLOADAVG_PRIVILEGED */
     668#endif  /* GETLOADAVG_PRIVILEGED */
    869669}
    870670
     
    874674child_access (void)
    875675{
    876 #ifdef  GETLOADAVG_PRIVILEGED
     676#ifdef  GETLOADAVG_PRIVILEGED
    877677
    878678  if (!access_inited)
     
    882682     They cannot be changed back to make's.  */
    883683
    884 #ifndef HAVE_SETREUID
     684#ifndef HAVE_SETREUID
    885685  if (setuid (user_uid) < 0)
    886686    pfatal_with_name ("child_access: setuid");
     
    890690#endif
    891691
    892 #ifndef HAVE_SETREGID
     692#ifndef HAVE_SETREGID
    893693  if (setgid (user_gid) < 0)
    894694    pfatal_with_name ("child_access: setgid");
     
    900700  log_access (_("Child access"));
    901701
    902 #endif  /* GETLOADAVG_PRIVILEGED */
    903 }
    904 
     702#endif  /* GETLOADAVG_PRIVILEGED */
     703}
    905704
    906705#ifdef NEED_GET_PATH_MAX
     
    914713      long int x = pathconf ("/", _PC_PATH_MAX);
    915714      if (x > 0)
    916         value = x;
     715        value = x;
    917716      else
    918         return MAXPATHLEN;
     717        return MAXPATHLEN;
    919718    }
    920719
     
    922721}
    923722#endif
    924 
    925 
    926 
    927 /* This code is stolen from gnulib.
    928    If/when we abandon the requirement to work with K&R compilers, we can
    929    remove this (and perhaps other parts of GNU make!) and migrate to using
    930    gnulib directly.
    931 
    932    This is called only through atexit(), which means die() has already been
    933    invoked.  So, call exit() here directly.  Apparently that works...?
    934 */
    935 
    936 /* Close standard output, exiting with status 'exit_failure' on failure.
    937    If a program writes *anything* to stdout, that program should close
    938    stdout and make sure that it succeeds before exiting.  Otherwise,
    939    suppose that you go to the extreme of checking the return status
    940    of every function that does an explicit write to stdout.  The last
    941    printf can succeed in writing to the internal stream buffer, and yet
    942    the fclose(stdout) could still fail (due e.g., to a disk full error)
    943    when it tries to write out that buffered data.  Thus, you would be
    944    left with an incomplete output file and the offending program would
    945    exit successfully.  Even calling fflush is not always sufficient,
    946    since some file systems (NFS and CODA) buffer written/flushed data
    947    until an actual close call.
    948 
    949    Besides, it's wasteful to check the return value from every call
    950    that writes to stdout -- just let the internal stream state record
    951    the failure.  That's what the ferror test is checking below.
    952 
    953    It's important to detect such failures and exit nonzero because many
    954    tools (most notably `make' and other build-management systems) depend
    955    on being able to detect failure in other tools via their exit status.  */
    956 
    957 void
    958 close_stdout (void)
    959 {
    960   int prev_fail = ferror (stdout);
    961   int fclose_fail = fclose (stdout);
    962 
    963   if (prev_fail || fclose_fail)
    964     {
    965       if (fclose_fail)
    966         error (NILF, _("write error: %s"), strerror (errno));
    967       else
    968         error (NILF, _("write error"));
    969       exit (EXIT_FAILURE);
    970     }
    971 }
Note: See TracChangeset for help on using the changeset viewer.