home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / fileutils-3.9-src.lha / src / amiga / fileutils-3.9 / lib / xgetcwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-12  |  2.1 KB  |  86 lines

  1. /* xgetcwd.c -- return current directory with unlimited length
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie, djm@gnu.ai.mit.edu. */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #if defined (CONFIG_BROKETS)
  22. /* We use <config.h> instead of "config.h" so that a compilation
  23.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  24.    (which it would do because it found this file in $srcdir).  */
  25. #include <config.h>
  26. #else
  27. #include "config.h"
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #ifndef errno
  34. extern int errno;
  35. #endif
  36. #include <sys/types.h>
  37. #include "pathmax.h"
  38.  
  39. #if !defined(_POSIX_VERSION) && !defined(HAVE_GETCWD)
  40. char *getwd ();
  41. #define getcwd(buf, max) getwd (buf)
  42. #else
  43. char *getcwd ();
  44. #endif
  45.  
  46. /* Amount to increase buffer size by in each try. */
  47. #define PATH_INCR 32
  48.  
  49. char *xmalloc ();
  50. char *xrealloc ();
  51. void free ();
  52.  
  53. /* Return the current directory, newly allocated, arbitrarily long.
  54.    Return NULL and set errno on error. */
  55.  
  56. char *
  57. xgetcwd ()
  58. {
  59.   char *cwd;
  60.   char *ret;
  61.   unsigned path_max;
  62.  
  63.   errno = 0;
  64.   path_max = (unsigned) PATH_MAX;
  65.   path_max += 2;        /* The getcwd docs say to do this. */
  66.  
  67.   cwd = xmalloc (path_max);
  68.  
  69.   errno = 0;
  70.   while ((ret = getcwd (cwd, path_max)) == NULL && errno == ERANGE)
  71.     {
  72.       path_max += PATH_INCR;
  73.       cwd = xrealloc (cwd, path_max);
  74.       errno = 0;
  75.     }
  76.  
  77.   if (ret == NULL)
  78.     {
  79.       int save_errno = errno;
  80.       free (cwd);
  81.       errno = save_errno;
  82.       return NULL;
  83.     }
  84.   return cwd;
  85. }
  86.