(root)/
make-4.4/
src/
w32/
pathstuff.c
       1  /* Path conversion for Windows pathnames.
       2  Copyright (C) 1996-2022 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 <https://www.gnu.org/licenses/>.  */
      16  
      17  #include "makeint.h"
      18  #include <string.h>
      19  #include <stdlib.h>
      20  #include "pathstuff.h"
      21  
      22  /*
      23   * Convert delimiter separated vpath to Canonical format.
      24   */
      25  char *
      26  convert_vpath_to_windows32(char *Path, char to_delim)
      27  {
      28      char *etok;            /* token separator for old Path */
      29  
      30          /*
      31           * Convert all spaces to delimiters. Note that pathnames which
      32           * contain blanks get trounced here. Use 8.3 format as a workaround.
      33           */
      34          for (etok = Path; etok && *etok; etok++)
      35                  if (ISBLANK ((unsigned char) *etok))
      36                          *etok = to_delim;
      37  
      38          return (convert_Path_to_windows32(Path, to_delim));
      39  }
      40  
      41  /*
      42   * Convert delimiter separated path to Canonical format.
      43   */
      44  char *
      45  convert_Path_to_windows32(char *Path, char to_delim)
      46  {
      47      char *etok;            /* token separator for old Path */
      48      char *p;            /* points to element of old Path */
      49  
      50      /* is this a multi-element Path ? */
      51      /* FIXME: Perhaps use ":;\"" in strpbrk to convert all quotes to
      52         delimiters as well, as a way to handle quoted directories in
      53         PATH?  */
      54      for (p = Path, etok = strpbrk(p, ":;");
      55           etok;
      56           etok = strpbrk(p, ":;"))
      57          if ((etok - p) == 1) {
      58              if (*(etok - 1) == ';' ||
      59                  *(etok - 1) == ':') {
      60                  etok[-1] = to_delim;
      61                  etok[0] = to_delim;
      62                  p = ++etok;
      63                  continue;    /* ignore empty bucket */
      64              } else if (!isalpha ((unsigned char) *p)) {
      65                  /* found one to count, handle things like '.' */
      66                  *etok = to_delim;
      67                  p = ++etok;
      68              } else if ((*etok == ':') && ((etok = strpbrk(etok+1, ":;")) != NULL)) {
      69                  /* found one to count, handle drive letter */
      70                  *etok = to_delim;
      71                  p = ++etok;
      72              } else
      73                  /* all finished, force abort */
      74                  p += strlen(p);
      75          } else if (*p == '"') { /* a quoted directory */
      76              for (p++; *p && *p != '"'; p++) /* skip quoted part */
      77                  ;
      78              etok = strpbrk(p, ":;");        /* find next delimiter */
      79              if (etok) {
      80                  *etok = to_delim;
      81                  p = ++etok;
      82              } else
      83                  p += strlen(p);
      84          } else {
      85              /* found another one, no drive letter */
      86              *etok = to_delim;
      87              p = ++etok;
      88          }
      89  
      90      return Path;
      91  }
      92  
      93  /*
      94   * Convert to forward slashes. Resolve to full pathname optionally
      95   */
      96  char *
      97  w32ify(const char *filename, int resolve)
      98  {
      99      static char w32_path[FILENAME_MAX];
     100      char *p;
     101  
     102      if (resolve)
     103        {
     104          char *fp = _fullpath (NULL, filename, sizeof (w32_path));
     105          strncpy (w32_path, fp, sizeof (w32_path) - 1);
     106          free (fp);
     107        }
     108      else
     109        strncpy(w32_path, filename, sizeof (w32_path) - 1);
     110  
     111      for (p = w32_path; p && *p; p++)
     112        if (*p == '\\')
     113          *p = '/';
     114  
     115      return w32_path;
     116  }
     117  
     118  char *
     119  getcwd_fs(char* buf, int len)
     120  {
     121          char *p = getcwd(buf, len);
     122  
     123          if (p) {
     124                  char *q = w32ify(buf, 0);
     125                  strncpy(buf, q, len);
     126          }
     127  
     128          return p;
     129  }
     130  
     131  #ifdef unused
     132  /*
     133   * Convert delimiter separated pathnames (e.g. PATH) or single file pathname
     134   * (e.g. c:/foo, c:\bar) to NutC format. If we are handed a string that
     135   * _NutPathToNutc() fails to convert, just return the path we were handed
     136   * and assume the caller will know what to do with it (It was probably
     137   * a mistake to try and convert it anyway due to some of the bizarre things
     138   * that might look like pathnames in makefiles).
     139   */
     140  char *
     141  convert_path_to_nutc(char *path)
     142  {
     143      int  count;            /* count of path elements */
     144      char *nutc_path;     /* new NutC path */
     145      int  nutc_path_len;    /* length of buffer to allocate for new path */
     146      char *pathp;        /* pointer to nutc_path used to build it */
     147      char *etok;            /* token separator for old path */
     148      char *p;            /* points to element of old path */
     149      char sep;            /* what flavor of separator used in old path */
     150      char *rval;
     151  
     152      /* is this a multi-element path ? */
     153      for (p = path, etok = strpbrk(p, ":;"), count = 0;
     154           etok;
     155           etok = strpbrk(p, ":;"))
     156          if ((etok - p) == 1) {
     157              if (*(etok - 1) == ';' ||
     158                  *(etok - 1) == ':') {
     159                  p = ++etok;
     160                  continue;    /* ignore empty bucket */
     161              } else if (etok = strpbrk(etok+1, ":;"))
     162                  /* found one to count, handle drive letter */
     163                  p = ++etok, count++;
     164              else
     165                  /* all finished, force abort */
     166                  p += strlen(p);
     167          } else
     168              /* found another one, no drive letter */
     169              p = ++etok, count++;
     170  
     171      if (count) {
     172          count++;    /* x1;x2;x3 <- need to count x3 */
     173  
     174          /*
     175           * Hazard a guess on how big the buffer needs to be.
     176           * We have to convert things like c:/foo to /c=/foo.
     177           */
     178          nutc_path_len = strlen(path) + (count*2) + 1;
     179          nutc_path = xmalloc(nutc_path_len);
     180          pathp = nutc_path;
     181          *pathp = '\0';
     182  
     183          /*
     184           * Loop through PATH and convert one element of the path at at
     185           * a time. Single file pathnames will fail this and fall
     186           * to the logic below loop.
     187           */
     188          for (p = path, etok = strpbrk(p, ":;");
     189               etok;
     190               etok = strpbrk(p, ":;")) {
     191  
     192              /* don't trip up on device specifiers or empty path slots */
     193              if ((etok - p) == 1)
     194                  if (*(etok - 1) == ';' ||
     195                      *(etok - 1) == ':') {
     196                      p = ++etok;
     197                      continue;
     198                  } else if ((etok = strpbrk(etok+1, ":;")) == NULL)
     199                      break;    /* thing found was a WINDOWS32 pathname */
     200  
     201              /* save separator */
     202              sep = *etok;
     203  
     204              /* terminate the current path element -- temporarily */
     205              *etok = '\0';
     206  
     207  #ifdef __NUTC__
     208              /* convert to NutC format */
     209              if (_NutPathToNutc(p, pathp, 0) == FALSE) {
     210                  free(nutc_path);
     211                  rval = savestring(path, strlen(path));
     212                  return rval;
     213              }
     214  #else
     215              *pathp++ = '/';
     216              *pathp++ = p[0];
     217              *pathp++ = '=';
     218              *pathp++ = '/';
     219              strcpy(pathp, &p[2]);
     220  #endif
     221  
     222              pathp += strlen(pathp);
     223              *pathp++ = ':';     /* use Unix style path separtor for new path */
     224              *pathp   = '\0'; /* make sure we are null terminaed */
     225  
     226              /* restore path separator */
     227              *etok = sep;
     228  
     229              /* point p to first char of next path element */
     230              p = ++etok;
     231  
     232          }
     233      } else {
     234          nutc_path_len = strlen(path) + 3;
     235          nutc_path = xmalloc(nutc_path_len);
     236          pathp = nutc_path;
     237          *pathp = '\0';
     238          p = path;
     239      }
     240  
     241      /*
     242        * OK, here we handle the last element in PATH (e.g. c of a;b;c)
     243       * or the path was a single filename and will be converted
     244       * here. Note, testing p here assures that we don't trip up
     245       * on paths like a;b; which have trailing delimiter followed by
     246       * nothing.
     247       */
     248      if (*p != '\0') {
     249  #ifdef __NUTC__
     250          if (_NutPathToNutc(p, pathp, 0) == FALSE) {
     251              free(nutc_path);
     252              rval = savestring(path, strlen(path));
     253              return rval;
     254          }
     255  #else
     256          *pathp++ = '/';
     257          *pathp++ = p[0];
     258          *pathp++ = '=';
     259          *pathp++ = '/';
     260          strcpy(pathp, &p[2]);
     261  #endif
     262      } else
     263          *(pathp-1) = '\0'; /* we're already done, don't leave trailing : */
     264  
     265      rval = savestring(nutc_path, strlen(nutc_path));
     266      free(nutc_path);
     267      return rval;
     268  }
     269  
     270  #endif