home-etc: src/core.c, src/realpath.c, src/test.c - major fixes at ...
siefca
siefca at pld-linux.org
Thu May 18 13:53:23 CEST 2006
Author: siefca Date: Thu May 18 11:53:22 2006 GMT
Module: home-etc Tag: HEAD
---- Log message:
- major fixes at canonize_path.c
---- Files affected:
home-etc/src:
core.c (1.22 -> 1.23) , realpath.c (1.4 -> 1.5) , test.c (1.12 -> 1.13)
---- Diffs:
================================================================
Index: home-etc/src/core.c
diff -u home-etc/src/core.c:1.22 home-etc/src/core.c:1.23
--- home-etc/src/core.c:1.22 Thu May 18 01:41:23 2006
+++ home-etc/src/core.c Thu May 18 13:53:17 2006
@@ -47,6 +47,27 @@
/*********************************************************************/
+inline static void fix_trailslash(char *p, char trailslash)
+{
+ size_t s;
+
+ s = strlen(p);
+ if (s > 0)
+ {
+ if(trailslash && (*(p+s-1) != '/') && s < sizeof(p)-2)
+ {
+ *(p+s) = '/';
+ *(p+s+1) = '\0';
+ }
+ if(!trailslash && (*(p+s-1) == '/'))
+ *(p+s-1) = '\0';
+ }
+
+ return;
+}
+
+/*********************************************************************/
+
inline static int absolutize_dir(char *path, size_t s)
{
/* change dir to path */
@@ -67,7 +88,6 @@
char *canonize_path(const char *path, char use_env, char expand_tilde)
{
char trailslash = 0;
- int counter = 256;
size_t s;
char *p = NULL;
char *q = NULL;
@@ -156,38 +176,53 @@
/* remember the original size */
s = strlen(pbuff);
+ /* do we have pure resolvable path which is a dir? */
+ if (chdir(pbuff) != -1)
+ {
+ chdir(prev);
+ /* make it absolute and return fast */
+ if(absolutize_dir(pbuff, sizeof(pbuff)-1) == -1)
+ return NULL;
+ /* add or remove trailing slash as memorized before */
+ fix_trailslash(pbuff, trailslash);
+ return pbuff;
+ }
+
/* travel from last slash to first and try to enter the dir */
/* to split path into 2 pieces: resolvable and unresolvable */
q = NULL;
- while((q = strrchr(pbuff, (int)'/')) && counter--
- && chdir(pbuff) == -1)
+ while((q = strrchr(pbuff, (int)'/')) && chdir(pbuff) == -1)
*q = '\0';
- /* do we have strange traversal loops? */
- if(counter <= 0)
+ /* do we have pure unresolvable path? */
+ if (q == NULL)
+ p = pbuff;
+ else
{
- chdir(prev);
- return NULL;
+ q += strlen(q); /* point q to last char */
+ p = q;
}
- /* do we have pure resolvable path? */
- if (q == NULL || q >= pbuff+s)
- q = pbuff+s; /* point q to \0 string */
- else
- q += strlen(q); /* point q to last char */
-
- /* q pointer now keeps the borderline between resolvable */
- /* and unresolvable part of the pathname */
-
/* rebirth our paths by eliminating zeroes from prev. oper. */
- p = q;
while(p < pbuff+s)
{
if(*p == '\0')
- *p = '/';
+ *p = '/';
p++;
}
+ /* do we have pure unresolvable path? */
+ if (q == NULL)
+ {
+ /* add or remove trailing slash as memorized before */
+ fix_trailslash(pbuff, trailslash);
+ /* return fast */
+ return pbuff;
+ }
+
+ /* q pointer now keeps the borderline between resolvable */
+ /* and unresolvable part of the pathname */
+
/* keep unresolvable part of the path in intbuf */
if(q && *q != '\0')
{
@@ -222,17 +257,7 @@
}
/* add or remove trailing slash as memorized before */
- s = strlen(pbuff);
- if (s > 0)
- {
- if(trailslash && (*(pbuff+s-1) != '/') && s < sizeof(pbuff)-2)
- {
- *(pbuff+s) = '/';
- *(pbuff+s+1) = '\0';
- }
- if(!trailslash && (*(pbuff+s-1) == '/'))
- *(pbuff+s-1) = '\0';
- }
+ fix_trailslash(pbuff, trailslash);
/* go back to the directory we were and return pointer to buffer */
chdir(prev);
================================================================
Index: home-etc/src/realpath.c
diff -u home-etc/src/realpath.c:1.4 home-etc/src/realpath.c:1.5
--- home-etc/src/realpath.c:1.4 Thu May 18 02:09:08 2006
+++ home-etc/src/realpath.c Thu May 18 13:53:17 2006
@@ -10,6 +10,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
+
#include <home_etc.h>
extern int errno;
================================================================
Index: home-etc/src/test.c
diff -u home-etc/src/test.c:1.12 home-etc/src/test.c:1.13
--- home-etc/src/test.c:1.12 Wed May 17 16:28:45 2006
+++ home-etc/src/test.c Thu May 18 13:53:17 2006
@@ -13,7 +13,8 @@
"/home/users/siefca/.muttrc", "/home/users/siefca/.muttrc/xxx",
"/home/users/siefca/.a", "/home/../home/users/siefca/hmm",
"/home/../home/users/siefca/hmm/", "/home/users/siefca/.muttrc/",
- "tmp/.muttrc", "tmp/.muttrc/", "~/.muttrc", "~/muttrc/", "//.muttrc", "~noexpand", "~", "~/", "/////"
+ "tmp/.muttrc", "tmp/.muttrc/", "~/.muttrc", "~/muttrc/", "//.muttrc", "~noexpand",
+ "~", "~/", "/////", "/x"
};
home_etc_expand_tilde(1);
================================================================
---- CVS-web:
http://cvs.pld-linux.org/home-etc/src/core.c?r1=1.22&r2=1.23&f=u
http://cvs.pld-linux.org/home-etc/src/realpath.c?r1=1.4&r2=1.5&f=u
http://cvs.pld-linux.org/home-etc/src/test.c?r1=1.12&r2=1.13&f=u
More information about the pld-cvs-commit
mailing list