Move pathname normalization to libcaptive captive_path_normalize().
authorshort <>
Mon, 18 Aug 2003 14:09:38 +0000 (14:09 +0000)
committershort <>
Mon, 18 Aug 2003 14:09:38 +0000 (14:09 +0000)
src/client/cmdline/cmd_cd.c
src/libcaptive/client/lib.c
src/libcaptive/include/captive/Makefile.am
src/libcaptive/include/captive/client.h [new file with mode: 0644]

index 84ff15f..9f85e8d 100644 (file)
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <captive/client-directory.h>
 #include <stdio.h>
+#include <captive/client.h>
 
 #include "cmd_cd.h"    /* self */
 #include "main.h"
@@ -60,57 +61,22 @@ G_CONST_RETURN gchar *cmdline_path_from_cwd(const gchar *relative)
 {
 static gchar *statics[STATICS_NUM];
 static int staticsi=0;
-gchar *r,*r_end,*s,*d;
+gchar *r,*s;
 
        /* 'relative' may be NULL for the '.' meaning */
 
        if (!cmdline_cwd || (relative && *relative==G_DIR_SEPARATOR)) { /* bootstrap or absolute */
                g_assert(g_path_is_absolute(relative));
-               r=g_strdup(relative);
+               s=g_strdup(relative);
                }
        else if (!relative)
-               r=g_strdup(cmdline_cwd);
+               s=g_strdup(cmdline_cwd);
        else
-               r=g_build_filename(cmdline_cwd,relative,NULL);
-       g_assert(g_path_is_absolute(r));
-
-       /* coalesce '/'es */
-       for (d=s=r;*s;s++) {
-               if (*s==G_DIR_SEPARATOR && d>r && d[-1]==G_DIR_SEPARATOR)
-                       continue;
-               *d++=*s;
-               }
-       g_assert(d>r);
-       if (d>(r+1) && d[-1]==G_DIR_SEPARATOR)
-               d--;
-       *d++=G_DIR_SEPARATOR;
-       r_end=d;
-
-       /* 'r' is NOT NULL-terminated here! */
-
-       for (d=s=r+1;s<r_end;) {
-               if (!strncmp(s-1,"/./",3)) {
-                       s+=2;
-                       continue;
-                       }
-               if (!strncmp(s-1,"/../",4)) {
-                       s+=3;
-                       g_assert(d[-1]==G_DIR_SEPARATOR);
-                       if (d>r+1) {
-                               do {
-                                       d--;
-                                       } while (d[-1]!=G_DIR_SEPARATOR);
-                               }
-                       continue;
-                       }
-               *d++=*s++;
-               }
-       g_assert(d[-1]==G_DIR_SEPARATOR);       /* trailing '/' */
-       if (d>r+1)      /* leave at least "/" */
-               d--;
-       *d='\0';
+               s=g_build_filename(cmdline_cwd,relative,NULL);
+       g_assert(g_path_is_absolute(s));
 
-       g_assert(g_path_is_absolute(r));
+       r=captive_path_normalize(s);
+       g_free(s);
 
        g_free(statics[staticsi]);
        statics[staticsi++]=r;
index 3847cde..da2d5d7 100644 (file)
 #include "captive/unicode.h"
 #include <libgnomevfs/gnome-vfs-utils.h>       /* for gnome_vfs_unescape_string() */
 #include "captive/options.h"
+#include <glib/gstrfuncs.h>
 
 
+/* Returns: g_malloc() allocated absolute pathname string */
+gchar *captive_path_normalize(const gchar *raw_pathname)
+{
+gchar *r,*r_end,*s,*d;
+
+       g_return_val_if_fail(raw_pathname!=NULL,NULL);
+       g_return_val_if_fail(g_path_is_absolute(raw_pathname),NULL);
+
+       r=g_strdup(raw_pathname);
+
+       /* coalesce '/'es */
+       for (d=s=r;*s;s++) {
+               if (*s==G_DIR_SEPARATOR && d>r && d[-1]==G_DIR_SEPARATOR)
+                       continue;
+               *d++=*s;
+               }
+       g_assert(d>r);
+       if (d>(r+1) && d[-1]==G_DIR_SEPARATOR)
+               d--;
+       *d++=G_DIR_SEPARATOR;
+       r_end=d;
+
+       /* 'r' is NOT NULL-terminated here! */
+
+       for (d=s=r+1;s<r_end;) {
+               if (!strncmp(s-1,"/./",3)) {
+                       s+=2;
+                       continue;
+                       }
+               if (!strncmp(s-1,"/../",4)) {
+                       s+=3;
+                       g_assert(d[-1]==G_DIR_SEPARATOR);
+                       if (d>r+1) {
+                               do {
+                                       d--;
+                                       } while (d[-1]!=G_DIR_SEPARATOR);
+                               }
+                       continue;
+                       }
+               *d++=*s++;
+               }
+       g_assert(d[-1]==G_DIR_SEPARATOR);       /* trailing '/' */
+       if (d>r+1)      /* leave at least "/" */
+               d--;
+       *d='\0';
+
+       g_assert(g_path_is_absolute(r));
+
+       return r;
+}
+
 /* function will leave g_malloc()ed 'ObjectAttributes->ObjectName'!
  */
 GnomeVFSResult captive_ObjectAttributes_init(const gchar *pathname,OBJECT_ATTRIBUTES *ObjectAttributes)
index fd98d11..060b9c0 100644 (file)
@@ -24,6 +24,7 @@ pkginclude_HEADERS+= \
                client-directory.h \
                client-file.h \
                client-vfs.h \
+               client.h \
                config2.h \
                ldr.h \
                ldr_exports.h \
diff --git a/src/libcaptive/include/captive/client.h b/src/libcaptive/include/captive/client.h
new file mode 100644 (file)
index 0000000..363d5cd
--- /dev/null
@@ -0,0 +1,34 @@
+/* $Id$
+ * Include file with client utility functions of libcaptive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program 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 General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _CAPTIVE_CLIENT_H
+#define _CAPTIVE_CLIENT_H 1
+
+
+#include <glib/gtypes.h>
+
+
+G_BEGIN_DECLS
+
+gchar *captive_path_normalize(const gchar *raw_pathname);
+
+G_END_DECLS
+
+
+#endif /* _CAPTIVE_CLIENT_H */