+ in the interest of robustness, I added
[oweals/busybox.git] / utility.c
index c779cc6badb588c2f57f65278a2fd9ee761c0ffc..42b8dc1e96f28222ec4d7de2ed77fac9ed17b58b 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -89,7 +89,7 @@ extern void usage(const char *usage)
        exit FALSE;
 }
 
-extern void errorMsg(char *s, ...)
+extern void errorMsg(const char *s, ...)
 {
        va_list p;
 
@@ -100,7 +100,7 @@ extern void errorMsg(char *s, ...)
        fflush(stderr);
 }
 
-extern void fatalError(char *s, ...)
+extern void fatalError(const char *s, ...)
 {
        va_list p;
 
@@ -112,12 +112,12 @@ extern void fatalError(char *s, ...)
        exit( FALSE);
 }
 
-#if defined (BB_INIT) || defined (BB_PS)
+#if defined BB_INIT
 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
  * so, for example,  to check if the kernel is greater than 2.2.11:
- *     if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
+ *     if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
  */
-int get_kernel_revision()
+extern int get_kernel_revision(void)
 {
        struct utsname name;
        int major = 0, minor = 0, patch = 0;
@@ -129,7 +129,7 @@ int get_kernel_revision()
        sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
        return major * 65536 + minor * 256 + patch;
 }
-#endif                                                 /* BB_INIT || BB_PS */
+#endif                                                 /* BB_INIT */
 
 #if defined (BB_CP_MV) || defined (BB_DU)
 
@@ -542,9 +542,12 @@ int fullRead(int fd, char *buf, int len)
 int recursiveAction(const char *fileName,
                                        int recurse, int followLinks, int depthFirst,
                                        int (*fileAction) (const char *fileName,
-                                                                          struct stat * statbuf),
+                                                                          struct stat * statbuf,
+                                                                          void* userData),
                                        int (*dirAction) (const char *fileName,
-                                                                         struct stat * statbuf))
+                                                                         struct stat * statbuf,
+                                                                         void* userData),
+                                       void* userData)
 {
        int status;
        struct stat statbuf;
@@ -569,13 +572,13 @@ int recursiveAction(const char *fileName,
                if (fileAction == NULL)
                        return TRUE;
                else
-                       return fileAction(fileName, &statbuf);
+                       return fileAction(fileName, &statbuf, userData);
        }
 
        if (recurse == FALSE) {
                if (S_ISDIR(statbuf.st_mode)) {
                        if (dirAction != NULL)
-                               return (dirAction(fileName, &statbuf));
+                               return (dirAction(fileName, &statbuf, userData));
                        else
                                return TRUE;
                }
@@ -590,7 +593,7 @@ int recursiveAction(const char *fileName,
                        return FALSE;
                }
                if (dirAction != NULL && depthFirst == FALSE) {
-                       status = dirAction(fileName, &statbuf);
+                       status = dirAction(fileName, &statbuf, userData);
                        if (status == FALSE) {
                                perror(fileName);
                                return FALSE;
@@ -610,7 +613,7 @@ int recursiveAction(const char *fileName,
                        sprintf(nextFile, "%s/%s", fileName, next->d_name);
                        status =
                                recursiveAction(nextFile, TRUE, followLinks, depthFirst,
-                                                               fileAction, dirAction);
+                                                               fileAction, dirAction, userData);
                        if (status < 0) {
                                closedir(dir);
                                return FALSE;
@@ -622,7 +625,7 @@ int recursiveAction(const char *fileName,
                        return FALSE;
                }
                if (dirAction != NULL && depthFirst == TRUE) {
-                       status = dirAction(fileName, &statbuf);
+                       status = dirAction(fileName, &statbuf, userData);
                        if (status == FALSE) {
                                perror(fileName);
                                return FALSE;
@@ -632,7 +635,7 @@ int recursiveAction(const char *fileName,
                if (fileAction == NULL)
                        return TRUE;
                else
-                       return fileAction(fileName, &statbuf);
+                       return fileAction(fileName, &statbuf, userData);
        }
        return TRUE;
 }
@@ -780,7 +783,7 @@ extern int parse_mode(const char *s, mode_t * theMode)
 
 
 
-#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
+#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR 
 
 /* Use this to avoid needing the glibc NSS stuff 
  * This uses storage buf to hold things.
@@ -855,7 +858,7 @@ void my_getgrgid(char *group, gid_t gid)
 }
 
 
-#endif                                                 /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
+#endif                                                 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR */
 
 
 
@@ -1256,17 +1259,15 @@ extern int device_open(char *device, int mode)
  *  This finds the pid of the specified process,
  *  by using the /dev/ps device driver.
  *
- *  [return]
- *  0      failure
- *  pid            when the pid is found.
+ *  Returns a list of all matching PIDs
  */
-extern pid_t findPidByName( char* pidName)
+extern pid_t* findPidByName( char* pidName)
 {
-       int fd, i;
+       int fd, i, j;
        char device[] = "/dev/ps";
-       pid_t thePid = 0;
        pid_t num_pids;
        pid_t* pid_array = NULL;
+       pid_t* pidList=NULL;
 
        /* open device */ 
        fd = open(device, O_RDONLY);
@@ -1297,10 +1298,13 @@ extern pid_t findPidByName( char* pidName)
                        fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
 
                if ((strstr(info.command_line, pidName) != NULL)) {
-                       thePid = info.pid;
-                       break;
+                       pidList=realloc( pidList, sizeof(pid_t) * (j+2));
+                       if (pidList==NULL)
+                               fatalError("out of memory\n");
+                       pidList[j++]=info.pid;
                }
        }
+       pidList[j]=0;
 
        /* Free memory */
        free( pid_array);
@@ -1309,7 +1313,7 @@ extern pid_t findPidByName( char* pidName)
        if (close (fd) != 0) 
                fatalError( "close failed for `%s': %s\n",device, strerror (errno));
 
-       return thePid;
+       return pidList;
 }
 #else          /* BB_FEATURE_USE_DEVPS_PATCH */
 #if ! defined BB_FEATURE_USE_PROCFS
@@ -1322,14 +1326,14 @@ extern pid_t findPidByName( char* pidName)
  *  Currently, it's implemented by rummaging through 
  *  the proc filesystem.
  *
- *  [return]
- *  0      failure
- *  pid            when the pid is found.
+ *  Returns a list of all matching PIDs
  */
-extern pid_t findPidByName( char* pidName)
+extern pid_t* findPidByName( char* pidName)
 {
        DIR *dir;
        struct dirent *next;
+       pid_t* pidList=NULL;
+       int i=0;
 
        dir = opendir("/proc");
        if (!dir)
@@ -1344,7 +1348,7 @@ extern pid_t findPidByName( char* pidName)
                if (!isdigit(*next->d_name))
                        continue;
 
-               /* Now open the command line file */
+               /* Now open the status file */
                sprintf(filename, "/proc/%s/status", next->d_name);
                status = fopen(filename, "r");
                if (!status) {
@@ -1354,10 +1358,14 @@ extern pid_t findPidByName( char* pidName)
                fclose(status);
 
                if ((strstr(buffer, pidName) != NULL)) {
-                       return strtol(next->d_name, NULL, 0);
+                       pidList=realloc( pidList, sizeof(pid_t) * (i+2));
+                       if (pidList==NULL)
+                               fatalError("out of memory\n");
+                       pidList[i++]=strtol(next->d_name, NULL, 0);
                }
        }
-       return 0;
+       pidList[i]=0;
+       return pidList;
 }
 #endif                                                 /* BB_FEATURE_USE_DEVPS_PATCH */
 #endif                                                 /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
@@ -1368,7 +1376,7 @@ extern void *xmalloc(size_t size)
        void *cp = malloc(size);
 
        if (cp == NULL)
-               fatalError("out of memory");
+               fatalError("out of memory\n");
        return cp;
 }
 
@@ -1513,19 +1521,57 @@ extern int find_real_root_device_name(char* name)
 }
 #endif
 
-
-#if defined BB_MTAB
-#define whine_if_fstab_is_missing() {}
-#else
-extern void whine_if_fstab_is_missing()
+const unsigned int CSTRING_BUFFER_LENGTH = 128;
+/* recursive parser that returns cstrings of arbitrary length
+ * from a FILE* 
+ */
+static char *
+cstring_alloc(FILE* f, int depth)
 {
-       struct stat statBuf;
+    char *cstring;
+    char buffer[CSTRING_BUFFER_LENGTH];
+    int         target = CSTRING_BUFFER_LENGTH * depth;
+    int  i, len;
+    int  size;
+
+    /* fill buffer */
+    i = 0;
+    while ((buffer[i] = fgetc(f)) != EOF) {
+               if (buffer[i++] == 0x0a) { break; }
+               if (i == CSTRING_BUFFER_LENGTH) { break; }
+    }
+    len = i;
+
+    /* recurse or malloc? */
+    if (len == CSTRING_BUFFER_LENGTH) {
+               cstring = cstring_alloc(f, (depth + 1));
+    } else {
+               /* [special case] EOF */
+               if ((depth | len) == 0) { return NULL; }
+
+               /* malloc */
+               size = target + len + 1;
+               cstring = malloc(size);
+               if (!cstring) { return NULL; }
+               cstring[size - 1] = 0;
+    }
+
+    /* copy buffer */
+    if (cstring) {
+               memcpy(&cstring[target], buffer, len);
+    }
+    return cstring;
+}
 
-       if (stat("/etc/fstab", &statBuf) < 0)
-               fprintf(stderr,
-                               "/etc/fstab file missing -- install one to name /dev/root.\n\n");
+/* 
+ * wrapper around recursive cstring_alloc 
+ * it's the caller's responsibility to free the cstring
+ */ 
+char *
+cstring_lineFromFile(FILE *f)
+{
+    return cstring_alloc(f, 0);
 }
-#endif
 
 /* END CODE */
 /*