home-etc: src/core.c, src/home_etc.c, src/test.c - major fixes and...
siefca
siefca at pld-linux.org
Wed May 17 16:28:50 CEST 2006
Author: siefca Date: Wed May 17 14:28:50 2006 GMT
Module: home-etc Tag: HEAD
---- Log message:
- major fixes and addons related to trailing slashes
- a few new tests in test.c
---- Files affected:
home-etc/src:
core.c (1.19 -> 1.20) , home_etc.c (1.14 -> 1.15) , test.c (1.11 -> 1.12)
---- Diffs:
================================================================
Index: home-etc/src/core.c
diff -u home-etc/src/core.c:1.19 home-etc/src/core.c:1.20
--- home-etc/src/core.c:1.19 Wed May 17 11:13:00 2006
+++ home-etc/src/core.c Wed May 17 16:28:45 2006
@@ -90,8 +90,21 @@
bzero(buff, sizeof(buff));
bzero(pbuff, sizeof(pbuff));
- /* if we have leading tilde-slash */
- if (expand_tilde && *path == '~' && *(path+1) == '/')
+ /* if we have just tilde */
+ if (expand_tilde && *path == '~' && *(path+1) == '\0')
+ {
+ home_d = obtain_home_dir(use_env);
+ if (home_d == NULL ||
+ strlen(home_d) + strlen(path) > sizeof(buff)-2)
+ {
+ chdir(prev);
+ return NULL;
+ }
+ }
+
+ /* if we have leading tilde-slash or just tilde */
+ if (expand_tilde && *path == '~' &&
+ (*(path+1) == '/' || *(path+1) == '\0') )
{
home_d = obtain_home_dir(use_env);
if (home_d == NULL ||
@@ -101,7 +114,10 @@
return NULL;
}
strcpy(buff, home_d); /* strcpy checked */
- strcat(buff, path+1); /* strcat checked */
+ if (*(path+1) != '\0')
+ strcat(buff, path+1); /* strcat checked */
+ else
+ trailslash = 0;
}
else /* or just copy path into buffer */
{
@@ -215,7 +231,7 @@
*(pbuff+s+1) = '\0';
}
if(!trailslash && (*(pbuff+s-1) == '/'))
- *(pbuff+s) = '\0';
+ *(pbuff+s-1) = '\0';
}
/* go back to the directory we were and return pointer to buffer */
================================================================
Index: home-etc/src/home_etc.c
diff -u home-etc/src/home_etc.c:1.14 home-etc/src/home_etc.c:1.15
--- home-etc/src/home_etc.c:1.14 Wed May 17 03:50:04 2006
+++ home-etc/src/home_etc.c Wed May 17 16:28:45 2006
@@ -90,6 +90,7 @@
const char *home_etc_path(const char *path, char use_env)
{
+ char trailslash = 0;
static char dirbuf[MAXPATHLEN];
char pathbuf[MAXPATHLEN];
char home_dir[MAXPATHLEN];
@@ -106,10 +107,16 @@
if (strlen(path) > sizeof(pathbuf)-2)
return NULL;
+ /* memorize trailing slash */
+ if (s > 0 && *(path+s-1) == '/')
+ trailslash = 1;
+
+ /* clean the buffers */
bzero(home_dir, sizeof(home_dir));
bzero(pathbuf, sizeof(pathbuf));
bzero(dirbuf, sizeof(dirbuf));
+
/* absolutize home directory name */
strncpy(home_dir, p, sizeof(home_dir)-1);
p = canonize_path(home_dir, use_env, 0);
@@ -138,6 +145,19 @@
strcat(dirbuf, "/"); /* slash */
if (*p != '\0')
strcat(dirbuf, p); /* rest of the dir */
+
+ /* add or remove trailing slash as memorized before */
+ s = strlen(dirbuf);
+ if (s > 0)
+ {
+ if(trailslash && (*(dirbuf+s-1) != '/') && s < sizeof(dirbuf)-2)
+ {
+ *(dirbuf+s) = '/';
+ *(dirbuf+s+1) = '\0';
+ }
+ if(!trailslash && (*(dirbuf+s-1) == '/'))
+ *(dirbuf+s-1) = '\0';
+ }
return dirbuf;
}
================================================================
Index: home-etc/src/test.c
diff -u home-etc/src/test.c:1.11 home-etc/src/test.c:1.12
--- home-etc/src/test.c:1.11 Wed May 17 03:50:04 2006
+++ home-etc/src/test.c Wed May 17 16:28:45 2006
@@ -82,6 +82,40 @@
_HE(a[x]));
}
+ printf("\n----------------------------\n"
+ "wrapper hard-core tests\n"
+ "----------------------------\n");
+
+ printf("press any key...\n");
+ (void) getc(stdin);
+
+ for (x = 0; x <= MAXPATHLEN+8; x++)
+ b[x] = '/';
+ b[x] = '\0';
+
+ printf(" path:\t\t%s\n result:\t%s\n----------------------------\n",
+ b, _HE(b));
+
+ printf("press any key...\n");
+ (void) getc(stdin);
+
+ for (x = 0; x <= MAXPATHLEN+8; x++)
+ b[x] = 'a';
+ b[x] = '\0';
+
+ printf(" path:\t\t%s\n result:\t%s\n----------------------------\n",
+ b, _HE(b));
+
+ printf("press any key...\n");
+ (void) getc(stdin);
+
+ for (x = 0; x <= MAXPATHLEN-128; x++)
+ b[x] = 'b';
+ b[x] = '\0';
+
+ printf(" path:\t\t%s\n result:\t%s\n----------------------------\n",
+ b, _HE(b));
+
printf("press any key...\n");
(void) getc(stdin);
================================================================
---- CVS-web:
http://cvs.pld-linux.org/home-etc/src/core.c?r1=1.19&r2=1.20&f=u
http://cvs.pld-linux.org/home-etc/src/home_etc.c?r1=1.14&r2=1.15&f=u
http://cvs.pld-linux.org/home-etc/src/test.c?r1=1.11&r2=1.12&f=u
More information about the pld-cvs-commit
mailing list