]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sysdeps/mach/hurd/getcwd.c
Add script to update copyright notices and reformat some to facilitate its use.
[thirdparty/glibc.git] / sysdeps / mach / hurd / getcwd.c
index 02699a7e84faa431942ae39d25d636c3516eca1a..90757ad5ea34b1b286ef96ff7e70a97be8e00f71 100644 (file)
@@ -1,20 +1,19 @@
-/* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
-This file is part of the GNU C Library.
+/* Copyright (C) 1991-2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
 
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
-The GNU C Library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
 
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
 #include <sys/types.h>
@@ -29,28 +28,37 @@ Cambridge, MA 02139, USA.  */
 #include <fcntl.h>
 
 
-/* Get the pathname of the current working directory, and put it in SIZE
-   bytes of BUF.  Returns NULL if the directory couldn't be determined or
-   SIZE was too small.  If successful, returns BUF.  In GNU, if BUF is
-   NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
-   unless SIZE <= 0, in which case it is as big as necessary.  */
+/* Get the canonical absolute name of the given directory port, and put it
+   in SIZE bytes of BUF.  Returns NULL if the directory couldn't be
+   determined or SIZE was too small.  If successful, returns BUF.  In GNU,
+   if BUF is NULL, an array is allocated with `malloc'; the array is SIZE
+   bytes long, unless SIZE <= 0, in which case it is as big as necessary.
+   If our root directory cannot be reached, the result will not begin with
+   a slash to indicate that it is relative to some unknown root directory.  */
 
 char *
-__getcwd (char *buf, size_t size)
+_hurd_canonicalize_directory_name_internal (file_t thisdir,
+                                           char *buf,
+                                           size_t size)
 {
   error_t err;
-  dev_t rootdev, thisdev;
-  ino_t rootino, thisino;
+  mach_port_t rootid, thisid, rootdevid, thisdevid;
+  ino64_t rootino, thisino;
   char *file_name;
   register char *file_namep;
-  struct stat st;
   file_t parent;
   char *dirbuf = NULL;
   unsigned int dirbufsize = 0;
+  const size_t orig_size = size;
 
   inline void cleanup (void)
     {
-      __mach_port_deallocate (__mach_task_self (), parent);
+      if (parent != thisdir)
+       __mach_port_deallocate (__mach_task_self (), parent);
+
+      __mach_port_deallocate (__mach_task_self (), thisid);
+      __mach_port_deallocate (__mach_task_self (), thisdevid);
+      __mach_port_deallocate (__mach_task_self (), rootid);
 
       if (dirbuf != NULL)
        __vm_deallocate (__mach_task_self (),
@@ -58,7 +66,7 @@ __getcwd (char *buf, size_t size)
     }
 
 
-  if (size == 0)
+  if (size <= 0)
     {
       if (buf != NULL)
        {
@@ -81,42 +89,35 @@ __getcwd (char *buf, size_t size)
   file_namep = file_name + size;
   *--file_namep = '\0';
 
-  /* Get a port to our root directory and stat it.  */
+  /* Get a port to our root directory and get its identity.  */
 
-  if (err = __USEPORT (CRDIR, __io_stat (port, &st)))
+  if (err = __USEPORT (CRDIR, __io_identity (port,
+                                            &rootid, &rootdevid, &rootino)))
     return __hurd_fail (err), NULL;
-  rootdev = st.st_dev;
-  rootino = st.st_ino;
+  __mach_port_deallocate (__mach_task_self (), rootdevid);
 
-  /* Get a port to our current working directory and stat it.  */
+  /* Stat the port to the directory of interest.  */
 
-  if (err = __USEPORT (CWDIR, __mach_port_mod_refs (__mach_task_self (),
-                                                   (parent = port),
-                                                   MACH_PORT_RIGHT_SEND,
-                                                   1)))
-    return __hurd_fail (err), NULL;
-  if (err = __io_stat (parent, &st))
+  if (err = __io_identity (thisdir, &thisid, &thisdevid, &thisino))
     {
-      cleanup ();
+      __mach_port_deallocate (__mach_task_self (), rootid);
       return __hurd_fail (err), NULL;
     }
 
-  thisdev = st.st_dev;
-  thisino = st.st_ino;
-
-  while (!(thisdev == rootdev && thisino == rootino))
+  parent = thisdir;
+  while (thisid != rootid)
     {
       /* PARENT is a port to the directory we are currently on;
-        THISDEV and THISINO are its device and node numbers.
-        Look in its parent (..) for a file with the same numbers.  */
+        THISID, THISDEV, and THISINO are its identity.
+        Look in its parent (..) for a file with the same file number.  */
 
-      struct dirent *d;
-      dev_t dotdev;
-      ino_t dotino;
+      struct dirent64 *d;
+      mach_port_t dotid, dotdevid;
+      ino64_t dotino;
       int mount_point;
       file_t newp;
       char *dirdata;
-      unsigned int dirdatasize;
+      size_t dirdatasize;
       int direntry, nentries;
 
 
@@ -124,15 +125,23 @@ __getcwd (char *buf, size_t size)
       newp = __file_name_lookup_under (parent, "..", O_READ, 0);
       if (newp == MACH_PORT_NULL)
        goto lose;
-      __mach_port_deallocate (__mach_task_self (), parent);
+      if (parent != thisdir)
+       __mach_port_deallocate (__mach_task_self (), parent);
       parent = newp;
 
-      /* Figure out if this directory is a mount point.  */
-      if (err = __io_stat (parent, &st))
+      /* Get this directory's identity and figure out if it's a mount
+         point.  */
+      if (err = __io_identity (parent, &dotid, &dotdevid, &dotino))
        goto errlose;
-      dotdev = st.st_dev;
-      dotino = st.st_ino;
-      mount_point = dotdev != thisdev;
+      mount_point = dotdevid != thisdevid;
+
+      if (thisid == dotid)
+       {
+         /* `..' == `.' but it is not our root directory.  */
+         __mach_port_deallocate (__mach_task_self (), dotid);
+         __mach_port_deallocate (__mach_task_self (), dotdevid);
+         break;
+       }
 
       /* Search for the last directory.  */
       direntry = 0;
@@ -165,7 +174,7 @@ __getcwd (char *buf, size_t size)
          offset = 0;
          while (offset < dirdatasize)
            {
-             d = (struct dirent *) &dirdata[offset];
+             d = (struct dirent64 *) &dirdata[offset];
              offset += d->d_reclen;
 
              /* Ignore `.' and `..'.  */
@@ -178,27 +187,36 @@ __getcwd (char *buf, size_t size)
                {
                  file_t try = __file_name_lookup_under (parent, d->d_name,
                                                         O_NOLINK, 0);
+                 file_t id, devid;
+                 ino64_t fileno;
                  if (try == MACH_PORT_NULL)
                    goto lose;
-                 err = __io_stat (try, &st);
+                 err = __io_identity (try, &id, &devid, &fileno);
                  __mach_port_deallocate (__mach_task_self (), try);
                  if (err)
-                   goto errlose;
-                 if (st.st_dev == thisdev && st.st_ino == thisino)
+                   goto inner_errlose;
+                 __mach_port_deallocate (__mach_task_self (), id);
+                 __mach_port_deallocate (__mach_task_self (), devid);
+                 if (id == thisid)
                    goto found;
                }
            }
        }
 
       if (err)
-       goto errlose;
+       {
+       inner_errlose:          /* Goto ERRLOSE: after cleaning up.  */
+         __mach_port_deallocate (__mach_task_self (), dotid);
+         __mach_port_deallocate (__mach_task_self (), dotdevid);
+         goto errlose;
+       }
       else if (nentries == 0)
        {
          /* We got to the end of the directory without finding anything!
             We are in a directory that has been unlinked, or something is
             broken.  */
          err = ENOENT;
-         goto errlose;
+         goto inner_errlose;
        }
       else
       found:
@@ -207,7 +225,7 @@ __getcwd (char *buf, size_t size)
 
          if (file_namep - file_name < d->d_namlen + 1)
            {
-             if (buf != NULL)
+             if (orig_size > 0)
                {
                  errno = ERANGE;
                  return NULL;
@@ -221,8 +239,12 @@ __getcwd (char *buf, size_t size)
                      free (file_name);
                      return NULL;
                    }
-                 file_namep = &buf[file_namep - file_name];
+                 file_namep = &buf[file_namep - file_name + size / 2];
                  file_name = buf;
+                 /* Move current contents up to the end of the buffer.
+                    This is guaranteed to be non-overlapping.  */
+                 memcpy (file_namep, file_namep - size / 2,
+                         file_name + size - file_namep);
                }
            }
          file_namep -= d->d_namlen;
@@ -232,7 +254,10 @@ __getcwd (char *buf, size_t size)
 
       /* The next iteration will find the name of the directory we
         just searched through.  */
-      thisdev = dotdev;
+      __mach_port_deallocate (__mach_task_self (), thisid);
+      __mach_port_deallocate (__mach_task_self (), thisdevid);
+      thisid = dotid;
+      thisdevid = dotdevid;
       thisino = dotino;
     }
 
@@ -241,6 +266,11 @@ __getcwd (char *buf, size_t size)
        So the root is our current directory.  */
     *--file_namep = '/';
 
+  if (thisid != rootid)
+    /* We did not get to our root directory. The returned name should
+       not begin with a slash.  */
+    ++file_namep;
+
   memmove (file_name, file_namep, file_name + size - file_namep);
   cleanup ();
   return file_name;
@@ -252,4 +282,41 @@ __getcwd (char *buf, size_t size)
   cleanup ();
   return NULL;
 }
+
+char *
+__canonicalize_directory_name_internal (thisdir, buf, size)
+     const char *thisdir;
+     char *buf;
+     size_t size;
+{
+  char *result;
+  file_t port = __file_name_lookup (thisdir, 0, 0);
+  if (port == MACH_PORT_NULL)
+    return NULL;
+  result = _hurd_canonicalize_directory_name_internal (port, buf, size);
+  __mach_port_deallocate (__mach_task_self (), port);
+  return result;
+}
+\f
+/* Get the pathname of the current working directory, and put it in SIZE
+   bytes of BUF.  Returns NULL if the directory couldn't be determined or
+   SIZE was too small.  If successful, returns BUF.  In GNU, if BUF is
+   NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
+   unless SIZE <= 0, in which case it is as big as necessary.  */
+char *
+__getcwd (char *buf, size_t size)
+{
+  char *cwd =
+    __USEPORT (CWDIR,
+              _hurd_canonicalize_directory_name_internal (port,
+                                                          buf, size));
+  if (cwd && cwd[0] != '/')
+    {
+      /* `cwd' is an unknown root directory.  */
+      if (buf == NULL)
+         free (cwd);
+      return __hurd_fail (EGRATUITOUS), NULL;
+    }
+  return cwd;
+}
 weak_alias (__getcwd, getcwd)