(root)/
gettext-0.22.4/
gnulib-local/
lib/
xgetcwd.c
       1  /* xgetcwd.c -- return current directory with unlimited length
       2     Copyright (C) 1992, 1996, 2000, 2003, 2005-2006, 2011, 2020 Free
       3     Software Foundation, Inc.
       4  
       5     This program is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation; either version 3 of the License, or
       8     (at your option) any later version.
       9  
      10     This program is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
      19  
      20  #include <config.h>
      21  
      22  /* Specification.  */
      23  #include "xgetcwd.h"
      24  
      25  #include <stdlib.h>
      26  #include <stdio.h>
      27  #include <errno.h>
      28  #include <sys/types.h>
      29  #include <unistd.h>
      30  
      31  #include "pathmax.h"
      32  
      33  /* In this file, PATH_MAX is the size of an initial memory allocation.  */
      34  #ifndef PATH_MAX
      35  # define PATH_MAX 8192
      36  #endif
      37  
      38  #if HAVE_GETCWD
      39  # ifdef VMS
      40     /* We want the directory in Unix syntax, not in VMS syntax.  */
      41  #  define getcwd(Buf, Max) (getcwd) (Buf, Max, 0)
      42  # else
      43  char *getcwd ();
      44  # endif
      45  #else
      46  char *getwd ();
      47  # define getcwd(Buf, Max) getwd (Buf)
      48  #endif
      49  
      50  #include "xalloc.h"
      51  
      52  /* Return the current directory, newly allocated, arbitrarily long.
      53     Return NULL and set errno on error. */
      54  
      55  char *
      56  xgetcwd (void)
      57  {
      58    char *ret;
      59    unsigned path_max;
      60    char buf[1024];
      61  
      62    errno = 0;
      63    ret = getcwd (buf, sizeof (buf));
      64    if (ret != NULL)
      65      return xstrdup (buf);
      66    if (errno != ERANGE)
      67      return NULL;
      68  
      69    path_max = (unsigned) PATH_MAX;
      70    path_max += 2;                /* The getcwd docs say to do this. */
      71  
      72    for (;;)
      73      {
      74        char *cwd = XNMALLOC (path_max, char);
      75  
      76        errno = 0;
      77        ret = getcwd (cwd, path_max);
      78        if (ret != NULL)
      79          return ret;
      80        if (errno != ERANGE)
      81          {
      82            int save_errno = errno;
      83            free (cwd);
      84            errno = save_errno;
      85            return NULL;
      86          }
      87  
      88        free (cwd);
      89  
      90        path_max += path_max / 16;
      91        path_max += 32;
      92      }
      93  }