home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / posix / getcwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-28  |  5.4 KB  |  228 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library 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 GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <limits.h>
  22. #include <stddef.h>
  23. #include <dirent.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30.  
  31. #ifdef __linux__
  32. #define d_namlen d_reclen
  33. #define d_fileno d_ino
  34. #define STAT(x,y) lstat((x),(y))
  35. #else
  36. #define STAT(x,y) stat((x),(y))
  37. #endif
  38.  
  39. /* Get the pathname of the current working directory,
  40.    and put it in SIZE bytes of BUF.  Returns NULL if the
  41.    directory couldn't be determined or SIZE was too small.
  42.    If successful, returns BUF.  In GNU, if BUF is NULL,
  43.    an array is allocated with `malloc'; the array is SIZE
  44.    bytes long, unless SIZE <= 0, in which case it is as
  45.    big as necessary.  */
  46. char *
  47. DEFUN(getcwd, (buf, size), char *buf AND size_t size)
  48. {
  49.   static CONST char dots[]
  50.     = "../../../../../../../../../../../../../../../../../../../../../../../\
  51. ../../../../../../../../../../../../../../../../../../../../../../../../../../\
  52. ../../../../../../../../../../../../../../../../../../../../../../../../../..";
  53.   CONST char *dotp, *dotlist;
  54.   size_t dotsize;
  55.   dev_t rootdev, thisdev;
  56.   ino_t rootino, thisino;
  57.   char *path;
  58.   register char *pathp;
  59.   struct stat st;
  60.  
  61. if (size == 0)
  62.   {
  63.     if (buf != NULL)
  64.       {
  65.     errno = EINVAL;
  66.     return NULL;
  67.       }
  68.  
  69. #ifndef PATH_MAX
  70. #define    PATH_MAX 1024
  71. #endif
  72.     size = PATH_MAX + 1;
  73.   }
  74.  
  75.   if (buf != NULL)
  76.     path = buf;
  77.   else
  78.     {
  79.       path = malloc (size);
  80.       if (path == NULL)
  81.     return NULL;
  82.     }
  83.  
  84.   pathp = path + size;
  85.   *--pathp = '\0';
  86.  
  87.   if (STAT (".", &st) < 0)
  88.     return NULL;
  89.   thisdev = st.st_dev;
  90.   thisino = st.st_ino;
  91.  
  92.   if (STAT ("/", &st) < 0)
  93.     return NULL;
  94.   rootdev = st.st_dev;
  95.   rootino = st.st_ino;
  96.  
  97.   dotsize = sizeof (dots) - 1;
  98.   dotp = &dots[sizeof (dots)];
  99.   dotlist = dots;
  100.   while (!(thisdev == rootdev && thisino == rootino))
  101.     {
  102.       register DIR *dirstream;
  103.       register struct dirent *d;
  104.       dev_t dotdev;
  105.       ino_t dotino;
  106.       char mount_point;
  107.  
  108.       /* Look at the parent directory.  */
  109.       if (dotp == dotlist)
  110.     {
  111.       /* My, what a deep directory tree you have, Grandma.  */
  112.       char *new;
  113.       if (dotlist == dots)
  114.         {
  115.           new = malloc (dotsize * 2 + 1);
  116.           if (new == NULL)
  117.         return NULL;
  118.           memcpy (new, dots, dotsize);
  119.         }
  120.       else
  121.         {
  122.           new = realloc ((PTR) dotlist, dotsize * 2 + 1);
  123.           if (new == NULL)
  124.         goto lose;
  125.         }
  126.       memcpy (&new[dotsize], new, dotsize);
  127.       dotp = &new[dotsize];
  128.       dotsize *= 2;
  129.       new[dotsize] = '\0';
  130.       dotlist = new;
  131.     }
  132.  
  133.       dotp -= 3;
  134.  
  135.       /* Figure out if this directory is a mount point.  */
  136.       if (STAT (dotp, &st) < 0)
  137.     goto lose;
  138.       dotdev = st.st_dev;
  139.       dotino = st.st_ino;
  140.       mount_point = dotdev != thisdev;
  141.  
  142.       /* Search for the last directory.  */
  143.       dirstream = opendir (dotp);
  144.       if (dirstream == NULL)
  145.     goto lose;
  146.       while ((d = readdir (dirstream)) != NULL)
  147.     {
  148.       if (d->d_name[0] == '.' &&
  149.           (d->d_namlen == 1 || (d->d_namlen == 2 && d->d_name[1] == '.')))
  150.         continue;
  151.       if (mount_point || d->d_fileno == thisino)
  152.         {
  153.           char *name = __alloca (dotlist + dotsize - dotp +
  154.                      1 + d->d_namlen + 1);
  155.           memcpy (name, dotp, dotlist + dotsize - dotp);
  156.           name[dotlist + dotsize - dotp] = '/';
  157.           memcpy (&name[dotlist + dotsize - dotp + 1],
  158.               d->d_name, d->d_namlen + 1);
  159.           if (STAT (name, &st) < 0)
  160.         {
  161.           int save = errno;
  162.           (void) closedir (dirstream);
  163.           errno = save;
  164.           goto lose;
  165.         }
  166.           if (st.st_dev == thisdev && st.st_ino == thisino)
  167.         break;
  168.         }
  169.     }
  170.       if (d == NULL)
  171.     {
  172.       int save = errno;
  173.       (void) closedir (dirstream);
  174.       errno = save;
  175.       goto lose;
  176.     }
  177.       else
  178.     {
  179.       if (pathp - path < d->d_namlen + 1)
  180.         {
  181.           if (buf != NULL)
  182.         {
  183.           (void) closedir (dirstream);
  184.           errno = ERANGE;
  185.           goto lose;
  186.         }
  187.           else
  188.         {
  189.           size *= 2;
  190.           buf = realloc (path, size);
  191.           if (buf == NULL)
  192.             {
  193.               (void) closedir (dirstream);
  194.               free (path);
  195.               errno = ENOMEM; /* closedir might have changed it.  */
  196.               goto lose;
  197.             }
  198.           pathp = &buf[pathp - path];
  199.           path = buf;
  200.         }
  201.         }
  202.       pathp -= d->d_namlen;
  203.       (void) memcpy (pathp, d->d_name, d->d_namlen);
  204.       (void) closedir (dirstream);
  205.       *--pathp = '/';
  206.     }
  207.  
  208.       thisdev = dotdev;
  209.       thisino = dotino;
  210.     }
  211.  
  212.   if (pathp == &path[size - 1])
  213.     *--pathp = '/';
  214.  
  215.   if (dotlist != dots)
  216.     free ((PTR) dotlist);
  217.  
  218.   return memmove (path, pathp, path + size - pathp);
  219.  
  220.  lose:
  221.   if (dotlist != dots)
  222.     free ((PTR) dotlist);
  223.   /* XXX need to free path iff it was mallocated here and for some other
  224.      early returns.  Should handle all cleanups (of path, dots and
  225.      dirstream) by jumping here.  */
  226.   return NULL;
  227. }
  228.