Whamcloud - gitweb
build: fix compile warnings on OSX
[tools/e2fsprogs.git] / lib / ext2fs / get_pathname.c
index 591af6d..3320994 100644 (file)
@@ -1,71 +1,94 @@
 /*
  * get_pathname.c --- do directry/inode -> name translation
- * 
- * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
- * under the terms of the GNU Public License.
+ *
+ * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
+ *
+ * %Begin-Header%
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
+ * %End-Header%
  */
 
+/*
+ *
+ *     ext2fs_get_pathname(fs, dir, ino, name)
+ *
+ *     This function translates takes two inode numbers into a
+ *     string, placing the result in <name>.  <dir> is the containing
+ *     directory inode, and <ino> is the inode number itself.  If
+ *     <ino> is zero, then ext2fs_get_pathname will return pathname
+ *     of the the directory <dir>.
+ *
+ */
+
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
+#if HAVE_UNISTD_H
 #include <unistd.h>
-#include <stdlib.h>
-#include <linux/fs.h>
-#include <linux/ext2_fs.h>
+#endif
 
+#include "ext2_fs.h"
 #include "ext2fs.h"
 
 struct get_pathname_struct {
-       int             search_ino;
-       int             parent;
+       ext2_ino_t      search_ino;
+       ext2_ino_t      parent;
        char            *name;
        errcode_t       errcode;
 };
 
+#ifdef __TURBOC__
+ #pragma argsused
+#endif
 static int get_pathname_proc(struct ext2_dir_entry *dirent,
-                            int        offset,
-                            int        blocksize,
-                            char       *buf,
-                            void       *private)
+                            int        offset EXT2FS_ATTR((unused)),
+                            int        blocksize EXT2FS_ATTR((unused)),
+                            char       *buf EXT2FS_ATTR((unused)),
+                            void       *priv_data)
 {
        struct get_pathname_struct      *gp;
+       errcode_t                       retval;
 
-       gp = (struct get_pathname_struct *) private;
+       gp = (struct get_pathname_struct *) priv_data;
 
-       if ((dirent->name_len == 2) &&
+       if (((dirent->name_len & 0xFF) == 2) &&
            !strncmp(dirent->name, "..", 2))
                gp->parent = dirent->inode;
        if (dirent->inode == gp->search_ino) {
-               gp->name = malloc(dirent->name_len + 1);
-               if (!gp->name) {
-                       gp->errcode = ENOMEM;
+               retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1,
+                                       &gp->name);
+               if (retval) {
+                       gp->errcode = retval;
                        return DIRENT_ABORT;
                }
-               strncpy(gp->name, dirent->name, dirent->name_len);
-               gp->name[dirent->name_len] = '\0';
+               strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF));
+               gp->name[dirent->name_len & 0xFF] = '\0';
                return DIRENT_ABORT;
        }
        return 0;
 }
 
-static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
-                                        int maxdepth, char *buf, char **name)
+static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ext2_ino_t dir,
+                                        ext2_ino_t ino, int maxdepth,
+                                        char *buf, char **name)
 {
        struct get_pathname_struct gp;
-       char    *parent_name, *ret;
+       char    *parent_name = 0, *ret;
        errcode_t       retval;
 
        if (dir == ino) {
-               *name = malloc(2);
-               if (!*name)
-                       return ENOMEM;
+               retval = ext2fs_get_mem(2, name);
+               if (retval)
+                       return retval;
                strcpy(*name, (dir == EXT2_ROOT_INO) ? "/" : ".");
                return 0;
        }
 
        if (!dir || (maxdepth < 0)) {
-               *name = malloc(4);
-               if (!*name)
-                       return ENOMEM;
+               retval = ext2fs_get_mem(4, name);
+               if (retval)
+                       return retval;
                strcpy(*name, "...");
                return 0;
        }
@@ -74,9 +97,21 @@ static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
        gp.parent = 0;
        gp.name = 0;
        gp.errcode = 0;
-       
+
        retval = ext2fs_dir_iterate(fs, dir, 0, buf, get_pathname_proc, &gp);
-       if (retval)
+       if (retval == EXT2_ET_NO_DIRECTORY) {
+               char buf[32];
+
+               if (ino)
+                       snprintf(buf, sizeof(buf), "<%u>/<%u>", dir, ino);
+               else
+                       snprintf(buf, sizeof(buf), "<%u>", dir);
+               retval = ext2fs_get_mem(strlen(buf)+1, name);
+               if (retval)
+                       goto cleanup;
+               strcpy(*name, buf);
+               return 0;
+       } else if (retval)
                goto cleanup;
        if (gp.errcode) {
                retval = gp.errcode;
@@ -91,12 +126,15 @@ static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
                *name = parent_name;
                return 0;
        }
-       
-       ret = malloc(strlen(parent_name)+strlen(gp.name)+2);
-       if (!ret) {
-               retval = ENOMEM;
+
+       if (gp.name)
+               retval = ext2fs_get_mem(strlen(parent_name)+strlen(gp.name)+2,
+                                       &ret);
+       else
+               retval = ext2fs_get_mem(strlen(parent_name)+5, &ret);
+       if (retval)
                goto cleanup;
-       }
+
        ret[0] = 0;
        if (parent_name[1])
                strcat(ret, parent_name);
@@ -106,28 +144,29 @@ static errcode_t ext2fs_get_pathname_int(ext2_filsys fs, ino_t dir, ino_t ino,
        else
                strcat(ret, "???");
        *name = ret;
-       free(parent_name);
        retval = 0;
-       
+
 cleanup:
-       if (gp.name)
-               free(gp.name);
+       ext2fs_free_mem(&parent_name);
+       ext2fs_free_mem(&gp.name);
        return retval;
 }
 
-errcode_t ext2fs_get_pathname(ext2_filsys fs, ino_t dir, ino_t ino,
+errcode_t ext2fs_get_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino,
                              char **name)
 {
        char    *buf;
        errcode_t       retval;
 
-       buf = malloc(fs->blocksize);
-       if (!buf)
-               return ENOMEM;
+       EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
+
+       retval = ext2fs_get_mem(fs->blocksize, &buf);
+       if (retval)
+               return retval;
        if (dir == ino)
                ino = 0;
        retval = ext2fs_get_pathname_int(fs, dir, ino, 32, buf, name);
-       free(buf);
+       ext2fs_free_mem(&buf);
        return retval;
-       
+
 }