home-etc: src/core.c, src/core.h, src/includes.h, src/test.c - add...

siefca siefca at pld-linux.org
Thu May 18 15:49:41 CEST 2006


Author: siefca                       Date: Thu May 18 13:49:41 2006 GMT
Module: home-etc                      Tag: HEAD
---- Log message:
- added stat() support instead of chdir() where possible
- added tests for path_canonize
- cosmetics

---- Files affected:
home-etc/src:
   core.c (1.24 -> 1.25) , core.h (1.12 -> 1.13) , includes.h (1.4 -> 1.5) , test.c (1.13 -> 1.14) 

---- Diffs:

================================================================
Index: home-etc/src/core.c
diff -u home-etc/src/core.c:1.24 home-etc/src/core.c:1.25
--- home-etc/src/core.c:1.24	Thu May 18 14:31:35 2006
+++ home-etc/src/core.c	Thu May 18 15:49:36 2006
@@ -96,6 +96,13 @@
 
 inline static int absolutize_dir(char *path, size_t s)
 {
+  char prev[MAXPATHLEN];
+
+  /* memorize CWD */
+  prev[MAXPATHLEN-1] = '\0';
+  if (! getcwd(prev, sizeof(prev)))
+    return -1;
+
   /* change dir to path */
   if (chdir(path) == -1)
     return -1;
@@ -103,9 +110,11 @@
   /* get the result */
   if (! getcwd(path, s))
     {
+      chdir(prev);
       return -1;
     }
 
+  chdir(prev);
   return 0;
 }
 
@@ -121,12 +130,6 @@
   static char pbuff[MAXPATHLEN+2];
   char buff[MAXPATHLEN];
   char intbuf[MAXPATHLEN];
-  char prev[MAXPATHLEN];
-
-  /* memorize CWD */
-  prev[MAXPATHLEN-1] = '\0';
-  if (! getcwd(prev, sizeof(prev)))
-    return NULL;
 
   /* memorize trailing slash */
   s = strlen(path);
@@ -142,10 +145,7 @@
       home_d = obtain_home_dir(use_env);
       if (home_d == NULL ||
           strlen(home_d) + strlen(path) > sizeof(buff)-2)
-        {
-	  chdir(prev);
-          return NULL;
-        }
+        return NULL;
     }
 
   /* if we have leading tilde-slash or just tilde */
@@ -153,12 +153,8 @@
       (*(path+1) == '/' || *(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 (home_d == NULL || strlen(home_d) + strlen(path) > sizeof(buff)-2)
+        return NULL;
       strcpy(buff, home_d);      /* strcpy checked */
       if (*(path+1) != '\0')
         strcat(buff, path+1);    /* strcat checked */
@@ -168,10 +164,7 @@
     else /* or just copy path into buffer */
     {
       if (strlen(path) > sizeof(buff)-2)
-        {
-	  chdir(prev);
-	  return NULL;
-        }
+        return NULL;
       strcpy(buff, path); /* strcpy checked */
     }
 
@@ -181,10 +174,7 @@
       intbuf[sizeof(intbuf)-1] = '\0';
       if (!getcwd(intbuf, (sizeof(intbuf) - 2)) ||
 	   strlen(intbuf) + strlen(buff) > sizeof(pbuff) - 2)
-        {
-	  chdir(prev);
-	  return NULL;
-	}
+	return NULL;
       strcpy(pbuff, intbuf);/* strcpy checked */
       strcat(pbuff, "/");   /* strcat checked */
       strcat(pbuff, buff);  /* strcat checked */
@@ -192,10 +182,7 @@
   else /* or if we have absolute pathname */
     {
       if (strlen(buff) > sizeof(pbuff)-2)
-	{
-	  chdir(prev);
-	  return NULL;
-	}
+	return NULL;
       strcpy(pbuff, buff);   /* strcpy checked */
     }
 
@@ -203,9 +190,8 @@
   s = strlen(pbuff);
 
   /* do we have pure resolvable path which is a dir? */
-  if (chdir(pbuff) != -1)
+  if (isdir(pbuff))
   {
-      chdir(prev);
       /* make it absolute and return fast */
       if(absolutize_dir(pbuff, sizeof(pbuff)-1) == -1)
 	return NULL;
@@ -219,7 +205,7 @@
   /* 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)'/')) && chdir(pbuff) == -1)
+  while((q = strrchr(pbuff, (int)'/')) && !isdir(pbuff))
     *q = '\0';
 
   /* do we have pure unresolvable path?   */
@@ -256,10 +242,7 @@
   if(q && *q != '\0')
     {
       if(strlen(q) > sizeof(intbuf)-2)
-        {
-          chdir(prev);
-          return NULL;
-        }
+        return NULL;
       strcpy(intbuf, q); /* strcpy checked */
       *q = '\0';         /* keep split */
     }
@@ -268,20 +251,14 @@
 
   /* make the buffered, resolvable path absolute */
   if(absolutize_dir(pbuff, sizeof(pbuff)-1) == -1)
-    {
-      chdir(prev);
-      return NULL;
-    }
+    return NULL;
 
   /* do we have unresolvable part of path? */
   if(intbuf && *intbuf != '\0')
     {
       /* attach the unresolvable part to the absolutized part */
       if(strlen(pbuff)+strlen(intbuf) > sizeof(pbuff)-2)
-        {
-          chdir(prev);
-          return NULL;
-        }
+        return NULL;
       strcat(pbuff, intbuf);   /* strcat checked */
     }
 
@@ -292,8 +269,6 @@
   /* add or remove trailing slash as memorized before */
   fix_trailslash(pbuff, sizeof(pbuff), trailslash);
 
-  /* go back to the directory we were and return pointer to buffer */
-  chdir(prev);
   return pbuff;
 }
 

================================================================
Index: home-etc/src/core.h
diff -u home-etc/src/core.h:1.12 home-etc/src/core.h:1.13
--- home-etc/src/core.h:1.12	Thu May 18 01:41:23 2006
+++ home-etc/src/core.h	Thu May 18 15:49:36 2006
@@ -7,8 +7,7 @@
  *
  */
 
-#include <sys/param.h>
-#include <sys/types.h>
+#include "includes.h"
 
 #ifndef	__CORE_H
 #define	__CORE_H
@@ -16,8 +15,6 @@
 #define	ENV_VAR		"HOME_ETC"
 #define	HELPER_FILENAME	".home_etc"
 
-extern int errno;
-
 const char *obtain_home_dir(char use_env);
 const char *get_home_etc_core(char use_env);
 const char *home_etc_path_core(const char *path, char use_env);
@@ -27,16 +24,11 @@
 
 inline static int isdir(const char *path)
 {
-  char prev[MAXPATHLEN];
-
-  prev[MAXPATHLEN-1] = '\0';
-  if (! getcwd(prev, sizeof(prev)))
-    return 0;
+  struct stat statbuf;
 
-  if (chdir(path) == -1 && errno == ENOTDIR)
+  if (stat(path, &statbuf) == -1 || !(S_ISDIR(statbuf.st_mode)))
     return 0;
 
-  chdir(prev);
   return 1;
 }
 

================================================================
Index: home-etc/src/includes.h
diff -u home-etc/src/includes.h:1.4 home-etc/src/includes.h:1.5
--- home-etc/src/includes.h:1.4	Thu May 18 01:41:23 2006
+++ home-etc/src/includes.h	Thu May 18 15:49:36 2006
@@ -76,6 +76,10 @@
 # include <pwd.h>
 #endif
 
+#if HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+
 #if HAVE_ERRNO_H
 # include <errno.h>
 #endif

================================================================
Index: home-etc/src/test.c
diff -u home-etc/src/test.c:1.13 home-etc/src/test.c:1.14
--- home-etc/src/test.c:1.13	Thu May 18 13:53:17 2006
+++ home-etc/src/test.c	Thu May 18 15:49:36 2006
@@ -1,6 +1,5 @@
 #include <stdio.h>
 #include <sys/types.h>
-
 #include <sys/param.h>
 
 #include "home_etc.h"
@@ -19,7 +18,14 @@
 
   home_etc_expand_tilde(1);
 
-  printf("\nHOME_ETC:\t%s\n\n", get_home_etc(1));
+  getcwd(b, sizeof(b) - 1);
+  b[MAXPATHLEN-1] = '\0';
+  printf("\nHOME_ETC:\t%s\n", get_home_etc(1));
+  printf("current directory: %s\n\n", b);
+
+  printf("press any key...\n");
+  (void) getc(stdin);
+
 
   for (x = 0; x < sizeof(a) / sizeof(char *); x++)
     {
@@ -121,7 +127,69 @@
   (void) getc(stdin);
 
   printf("_HEdir: %s\n", _HEdir);
-  printf("_HEndir: %s\n", _HEndir);
+  printf("_HEndir: %s\n\n", _HEndir);
+
+  printf("\n----------------------------\n"
+	 "path_canonize tests\n"
+	 "----------------------------\n");
+
+  printf("press any key...\n");
+  (void) getc(stdin);
+
+  for (x = 0; x < sizeof(a) / sizeof(char *); x++)
+    {
+      printf(" path:\t\t%s\n result:\t%s\n----------------------------\n",
+	     a[x],
+	     path_canonize(a[x],1));
+    }
+
+  printf("press any key...\n");
+  (void) getc(stdin);
+
+  printf("\n----------------------------\n"
+	 "path_canonize 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,
+	 path_canonize(b,1));
+
+  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,
+	 path_canonize(b,1));
+
+  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,
+	 path_canonize(b,1));
+
+  printf("press any key...\n");
+  (void) getc(stdin);
+
+  getcwd(b, sizeof(b) - 1);
+  b[MAXPATHLEN-1] = '\0';
+  printf("\nHOME_ETC:\t%s\n", get_home_etc(1));
+  printf("current directory: %s\n\n", b);
 
   printf("press any key...\n");
   (void) getc(stdin);
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/home-etc/src/core.c?r1=1.24&r2=1.25&f=u
    http://cvs.pld-linux.org/home-etc/src/core.h?r1=1.12&r2=1.13&f=u
    http://cvs.pld-linux.org/home-etc/src/includes.h?r1=1.4&r2=1.5&f=u
    http://cvs.pld-linux.org/home-etc/src/test.c?r1=1.13&r2=1.14&f=u



More information about the pld-cvs-commit mailing list