OSSP CVS Repository

ossp - Check-in [2524]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 2524
Date: 2002-Aug-23 17:33:18 (local)
2002-Aug-23 15:33:18 (UTC)
User:rse
Branch:
Comment: - Add new path_self(3) function for finding the path to the own program. - Add a better path_test program for testing more API functions
Tickets:
Inspections:
Files:
ossp-pkg/path/Makefile.in      1.3 -> 1.4     1 inserted, 1 deleted
ossp-pkg/path/path.h      1.3 -> 1.4     4 inserted, 1 deleted
ossp-pkg/path/path_self.c      added-> 1.1
ossp-pkg/path/path_test.c      1.2 -> 1.3     99 inserted, 24 deleted

ossp-pkg/path/Makefile.in 1.3 -> 1.4

--- Makefile.in  2002/08/03 21:00:14     1.3
+++ Makefile.in  2002/08/23 15:33:18     1.4
@@ -51,7 +51,7 @@
 
 LIB_NAME    = libpath.la
 LIB_OBJS    = path_abs2rel.lo path_rel2abs.lo path_resolve.lo \
-              path_dirname.lo path_basename.lo path_temp.lo path_util.lo
+              path_dirname.lo path_basename.lo path_temp.lo path_self.lo path_util.lo
 
 TST_NAME    = path_test
 TST_OBJS    = path_test.o


ossp-pkg/path/path.h 1.3 -> 1.4

--- path.h       2002/08/03 21:00:14     1.3
+++ path.h       2002/08/23 15:33:18     1.4
@@ -37,7 +37,9 @@
     PATH_ERR_USE,
     PATH_ERR_INT,
     PATH_ERR_SYS,
-    PATH_ERR_EXS
+    PATH_ERR_MEM,
+    PATH_ERR_EXS,
+    PATH_ERR_NFD
 } path_rc_t;
 
 typedef enum {
@@ -51,6 +53,7 @@
 char *path_dirname  (char *, size_t, const char *);
 char *path_basename (char *, size_t, const char *);
 path_rc_t path_temp(path_temp_t id, const char *tmpl, char **res_ptr, size_t *res_size, int *res_fd);
+path_rc_t path_self (char *res_buf, size_t res_size, const char *argv0);
 
 #endif /* __PATH_H__ */
 


ossp-pkg/path/path_self.c -> 1.1

*** /dev/null    Mon Apr 29 11:45:35 2024
--- -    Mon Apr 29 11:48:31 2024
***************
*** 0 ****
--- 1,113 ----
+ /*
+ **  OSSP path - Filesystem Path Manipulation
+ **  Copyright (c) 2002 Ralf S. Engelschall <rse@engelschall.com>
+ **  Copyright (c) 2002 The OSSP Project <http://www.ossp.org/>
+ **  Copyright (c) 2002 Cable & Wireless Deutschland <http://www.cw.com/de/>
+ **
+ **  This file is part of OSSP path, a filesystem path manipulation library
+ **  which can be found at http://www.ossp.org/pkg/lib/path/.
+ **
+ **  Permission to use, copy, modify, and distribute this software for
+ **  any purpose with or without fee is hereby granted, provided that
+ **  the above copyright notice and this permission notice appear in all
+ **  copies.
+ **
+ **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+ **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ **  SUCH DAMAGE.
+ **
+ **  path_self.c: resolve path to current program
+ */
+ 
+ #include <stdlib.h>
+ #include <string.h>
+ #include <sys/types.h>
+ #include <sys/stat.h>
+ #include <unistd.h>
+ 
+ #include "path.h"
+ 
+ #define PATH_DEFAULT "/bin:/sbin:/usr/bin:/usr/sbin:."
+ 
+ path_rc_t path_self(char *res_buf, size_t res_size, const char *argv0)
+ {
+     char *path;
+     size_t l;
+     struct stat sb;
+     char *cpB, *cpE;
+     size_t argv0_len;
+ 
+     if (res_buf == NULL || res_size == 0 || argv0 == NULL)
+         return PATH_ERR_ARG;
+ 
+     /* determine length of argv[0] */
+     argv0_len = strlen(argv0);
+ 
+     /* short-circuit if argv[0] already contains an absolute path */
+     if (argv0[0] == '/') {
+         if (argv0_len > res_size-1)
+             return PATH_ERR_MEM;
+         memcpy(res_buf, argv0, argv0_len);
+         res_buf[argv0_len] = '\0';
+         if (path_resolve(res_buf, res_size, res_buf) == NULL)
+             return PATH_ERR_SYS;
+         return PATH_OK;
+     }
+ 
+     /* short-circuit if argv[0] already contains a relative path */
+     if (argv0[0] == '.') {
+         if (getcwd(res_buf, res_size) == NULL)
+             return PATH_ERR_SYS;
+         l = strlen(res_buf);
+         if (res_buf[l-1] == '/')
+             l--;
+         if (l+1+argv0_len+1 > res_size)
+             return PATH_ERR_MEM;
+         res_buf[l++] = '/';
+         memcpy(res_buf+l, argv0, argv0_len);
+         res_buf[l+argv0_len] = '\0';
+         if (path_resolve(res_buf, res_size, res_buf) == NULL)
+             return PATH_ERR_SYS;
+         return PATH_OK;
+     }
+ 
+     /* else search argv[0] in $PATH */
+     if ((path = getenv("PATH")) == NULL)
+         path = PATH_DEFAULT;
+     cpE = path;
+     cpB = cpE;
+     while (*cpE != '\0') {
+         if ((cpE = strchr(cpB, ':')) == NULL)
+             cpE = strchr(cpB, '\0');
+         if ((l = cpE-cpB) > 0) {
+             if (cpB[l-1] == '/')
+                 l--;
+             if (l+1+argv0_len+1 <= res_size) {
+                 memcpy(res_buf, cpB, l);
+                 res_buf[l++] = '/';
+                 memcpy(res_buf+l, argv0, argv0_len);
+                 res_buf[l+argv0_len] = '\0';
+                 if (stat(res_buf, &sb) == 0) {
+                     if (path_resolve(res_buf, res_size, res_buf) == NULL)
+                         return PATH_ERR_SYS;
+                     return PATH_OK;
+                 }
+             }
+         }
+         if (*cpE == '\0')
+             break;
+         cpB = cpE+1;
+     }
+ 
+     return PATH_ERR_NFD;
+ }
+ 


ossp-pkg/path/path_test.c 1.2 -> 1.3

--- path_test.c  2002/08/03 10:23:41     1.2
+++ path_test.c  2002/08/23 15:33:18     1.3
@@ -37,33 +37,108 @@
 
 #include "path.h"
 
+static void usage(const char *argv0, const char *error)
+{
+    if (error != NULL)
+        fprintf(stderr, "error: %s\n", error);
+    fprintf(stderr, "usage: %s abs2rel <path> [<base>]\n", argv0);
+    fprintf(stderr, "usage: %s rel2abs <path> [<base>]\n", argv0);
+    fprintf(stderr, "usage: %s resolve <path>\n", argv0);
+    fprintf(stderr, "usage: %s dirname <path>\n", argv0);
+    fprintf(stderr, "usage: %s basename <path>\n", argv0);
+    fprintf(stderr, "usage: %s temp dir|file [<template>]\n", argv0);
+    fprintf(stderr, "usage: %s self\n", argv0);
+    if (error != NULL)
+        exit(1);
+    else
+        exit(0);
+}
+
 int main(int argc, char *argv[])
 {
-        char result[MAXPATHLEN];
-        char cwd[MAXPATHLEN];
+    char res[MAXPATHLEN];
+    char cwd[MAXPATHLEN];
     char *rv;
 
-        if (argc < 3) {
-                fprintf(stderr, "usage: test abs2rel|rel2abs path [base]\n");
-                exit(1);
-        }
-        if (argc == 3) {
-                if (getcwd(cwd, MAXPATHLEN) == NULL) {
-                        fprintf(stderr, "cannot get current directory.\n");
-                        exit(1);
-                }
-        } else
-                strcpy(cwd, argv[3]);
-        
-    if (strcmp(argv[1], "abs2rel") == 0)
-            rv = path_abs2rel(result, MAXPATHLEN, argv[2], cwd);
-    else                                                  
-            rv = path_rel2abs(result, MAXPATHLEN, argv[2], cwd);
-        
-    if (rv != NULL)
-                printf("%s\n", result);
-        else
-                printf("ERROR\n");
-        exit(0);
+    if ((argc >= 3 && argc <= 4) && strcmp(argv[1], "abs2rel") == 0) {
+        if (argc == 3) {
+            if (getcwd(cwd, MAXPATHLEN) == NULL) {
+                fprintf(stderr, "error: cannot get current directory\n");
+                exit(1);
+            }
+        } else
+            strcpy(cwd, argv[3]);
+        rv = path_abs2rel(res, sizeof(res), argv[2], cwd);
+        if (rv != NULL)
+            printf("%s\n", res);
+        else
+            printf("ERROR\n");
+    }
+    else if ((argc >= 3 && argc <= 4) && strcmp(argv[1], "rel2abs") == 0) {
+        if (argc == 3) {
+            if (getcwd(cwd, MAXPATHLEN) == NULL) {
+                fprintf(stderr, "error: cannot get current directory\n");
+                exit(1);
+            }
+        } else
+            strcpy(cwd, argv[3]);
+        rv = path_rel2abs(res, sizeof(res), argv[2], cwd);
+        if (rv != NULL)
+            printf("%s\n", res);
+        else
+            printf("ERROR\n");
+    }
+    else if (argc == 3 && strcmp(argv[1], "resolve") == 0) {
+        rv = path_resolve(res, sizeof(res), argv[2]);
+        if (rv != NULL)
+            printf("%s\n", res);
+        else
+            printf("ERROR\n");
+    }
+    else if (argc == 3 && strcmp(argv[1], "dirname") == 0) {
+        rv = path_dirname(res, sizeof(res), argv[2]);
+        if (rv != NULL)
+            printf("%s\n", res);
+        else
+            printf("ERROR\n");
+    }
+    else if (argc == 3 && strcmp(argv[1], "basename") == 0) {
+        rv = path_basename(res, sizeof(res), argv[2]);
+        if (rv != NULL)
+            printf("%s\n", res);
+        else
+            printf("ERROR\n");
+    }
+    else if ((argc >= 3 && argc <= 4) && strcmp(argv[1], "temp") == 0) {
+        path_temp_t id = 0;
+        char *tmpl;
+        char *result;
+        size_t result_len;
+        if (strcmp(argv[2], "dir") == 0)
+            id = PATH_TEMP_DIR;
+        else if (strcmp(argv[2], "file") == 0)
+            id = PATH_TEMP_FILE;
+        else
+            usage(argv[0], "invalid temp id");
+        tmpl = NULL;
+        if (argc == 4)
+            tmpl = argv[3];
+        result = res;
+        if (path_temp(id, tmpl, &result, &result_len, NULL) != PATH_OK)
+            fprintf(stderr, "ERROR\n");
+        else {
+            fprintf(stderr, "%s\n", res);
+            unlink(res);
+        }
+    }
+    else if (argc == 2 && strcmp(argv[1], "self") == 0) {
+        if (path_self(res, sizeof(res), argv[0]) != PATH_OK)
+            fprintf(stderr, "ERROR\n");
+        else
+            fprintf(stderr, "%s\n", res);
+    }
+    else
+        usage(argv[0], "invalid usage");
+    exit(0);
 }
 

CVSTrac 2.0.1