Only remove directories when removing recursively.
[oweals/busybox.git] / utility.c
index c682bf05d14ebf63af683e7cf81493e08e02c260..8c3c5e5fad4c10a6a29274b1685a30ab876e4ffa 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -37,6 +37,7 @@
 #define bb_need_name_too_long
 #endif
 #define bb_need_memory_exhausted
+#define bb_need_full_version
 #define BB_DECLARE_EXTERN
 #include "messages.c"
 
@@ -49,7 +50,6 @@
 #include <utime.h>
 #include <unistd.h>
 #include <ctype.h>
-#include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/utsname.h>               /* for uname(2) */
 
 
 
 #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
-#  if defined BB_FEATURE_USE_PROCFS
-const char mtab_file[] = "/proc/mounts";
-#  else
-#    if defined BB_MTAB
+#  if defined BB_MTAB
 const char mtab_file[] = "/etc/mtab";
+#  else
+#    if defined BB_FEATURE_USE_PROCFS
+const char mtab_file[] = "/proc/mounts";
 #    else
 #      if defined BB_FEATURE_USE_DEVPS_PATCH
 const char mtab_file[] = "/dev/mtab";
@@ -84,8 +84,7 @@ const char mtab_file[] = "/dev/mtab";
 
 extern void usage(const char *usage)
 {
-       fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
-                       BB_VER, BB_BT);
+       fprintf(stderr, "%s\n\n", full_version);
        fprintf(stderr, "Usage: %s\n", usage);
        exit FALSE;
 }
@@ -154,6 +153,13 @@ extern _syscall5(int, mount, const char *, special_file, const char *, dir,
                const char *, fstype, unsigned long int, rwflag, const void *, data);
 #endif
 
+#if defined BB_INSMOD || defined BB_LSMOD
+#ifndef __NR_query_module
+#define __NR_query_module     167
+#endif
+_syscall5(int, query_module, const char *, name, int, which,
+               void *, buf, size_t, bufsize, size_t*, ret);
+#endif
 
 
 #if defined (BB_CP_MV) || defined (BB_DU)
@@ -258,6 +264,29 @@ int isDirectory(const char *fileName, const int followLinks, struct stat *statBu
 }
 #endif
 
+#if defined (BB_AR) || defined BB_CP_MV
+/*
+ * Copy readSize bytes between two file descriptors
+ */
+int copySubFile(int srcFd, int dstFd, size_t remaining)
+{
+        size_t size;
+        char buffer[BUFSIZ];
+
+        while (remaining > 0) {
+                if (remaining > BUFSIZ)
+                        size = BUFSIZ;
+                else
+                        size = remaining;
+                if (fullWrite(dstFd, buffer, fullRead(srcFd, buffer, size)) < size)
+                        return(FALSE);
+                remaining -= size;
+        }
+        return (TRUE);
+}
+#endif
+
+
 #if defined (BB_CP_MV)
 /*
  * Copy one file to another, while possibly preserving its modes, times, and
@@ -271,9 +300,7 @@ copyFile(const char *srcName, const char *destName,
 {
        int rfd;
        int wfd;
-       int rcc;
        int status;
-       char buf[BUF_SIZE];
        struct stat srcStatBuf;
        struct stat dstStatBuf;
        struct utimbuf times;
@@ -358,8 +385,7 @@ copyFile(const char *srcName, const char *destName,
                        return FALSE;
                }
 
-               wfd =
-                       open(destName, O_WRONLY | O_CREAT | O_TRUNC,
+               wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
                                 srcStatBuf.st_mode);
                if (wfd < 0) {
                        perror(destName);
@@ -367,14 +393,9 @@ copyFile(const char *srcName, const char *destName,
                        return FALSE;
                }
 
-               while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
-                       if (fullWrite(wfd, buf, rcc) < 0)
-                               goto error_exit;
-               }
-               if (rcc < 0) {
-                       goto error_exit;
-               }
-
+               if (copySubFile(rfd, wfd, srcStatBuf.st_size)==FALSE)
+                       goto error_exit;        
+               
                close(rfd);
                if (close(wfd) < 0) {
                        return FALSE;
@@ -412,7 +433,7 @@ copyFile(const char *srcName, const char *destName,
 
 
 
-#if defined BB_TAR || defined BB_LS
+#if defined BB_TAR || defined BB_LS ||defined BB_AR
 
 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
 #define TYPECHAR(mode)  ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
@@ -483,7 +504,7 @@ const char *timeString(time_t timeVal)
 
        return buf;
 }
-#endif                                                 /* BB_TAR */
+#endif /* BB_TAR || BB_AR */
 
 #if defined BB_TAR || defined BB_CP_MV || defined BB_AR
 /*
@@ -511,10 +532,10 @@ int fullWrite(int fd, const char *buf, int len)
 
        return total;
 }
-#endif                                                 /* BB_TAR || BB_CP_MV */
+#endif /* BB_TAR || BB_CP_MV || BB_AR */
 
 
-#if defined BB_TAR || defined BB_TAIL || defined BB_AR
+#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV
 /*
  * Read all of the supplied buffer from a file.
  * This does multiple reads as necessary.
@@ -544,7 +565,7 @@ int fullRead(int fd, char *buf, int len)
 
        return total;
 }
-#endif                                                 /* BB_TAR || BB_TAIL */
+#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
 
 
 #if defined (BB_CHMOD_CHOWN_CHGRP) \
@@ -641,7 +662,7 @@ int recursiveAction(const char *fileName,
                        status =
                                recursiveAction(nextFile, TRUE, followLinks, depthFirst,
                                                                fileAction, dirAction, userData);
-                       if (status < 0) {
+                       if (status == FALSE) {
                                closedir(dir);
                                return FALSE;
                        }
@@ -706,7 +727,7 @@ extern int createPath(const char *name, int mode)
 
 
 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
- || defined (BB_MKFIFO) || defined (BB_MKNOD)
+ || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
 /* [ugoa]{+|-|=}[rwxst] */
 
 
@@ -722,6 +743,9 @@ extern int parse_mode(const char *s, mode_t * theMode)
        char type;
        char c;
 
+       if (s==NULL)
+               return (FALSE);
+
        do {
                for (;;) {
                        switch (c = *s++) {
@@ -1012,82 +1036,6 @@ int get_console_fd(char *tty_name)
 #endif                                                 /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
 
 
-#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
-
-/* Do a case insensitive strstr() */
-char *stristr(char *haystack, const char *needle)
-{
-       int len = strlen(needle);
-
-       while (*haystack) {
-               if (!strncasecmp(haystack, needle, len))
-                       break;
-               haystack++;
-       }
-
-       if (!(*haystack))
-               haystack = NULL;
-
-       return haystack;
-}
-
-/* This tries to find a needle in a haystack, but does so by
- * only trying to match literal strings (look 'ma, no regexps!)
- * This is short, sweet, and carries _very_ little baggage,
- * unlike its beefier cousin in regexp.c
- *  -Erik Andersen
- */
-extern int find_match(char *haystack, char *needle, int ignoreCase)
-{
-
-       if (ignoreCase == FALSE)
-               haystack = strstr(haystack, needle);
-       else
-               haystack = stristr(haystack, needle);
-       if (haystack == NULL)
-               return FALSE;
-       return TRUE;
-}
-
-
-/* This performs substitutions after a string match has been found.  */
-extern int replace_match(char *haystack, char *needle, char *newNeedle,
-                                                int ignoreCase)
-{
-       int foundOne = 0;
-       char *where, *slider, *slider1, *oldhayStack;
-
-       if (ignoreCase == FALSE)
-               where = strstr(haystack, needle);
-       else
-               where = stristr(haystack, needle);
-
-       if (strcmp(needle, newNeedle) == 0)
-               return FALSE;
-
-       oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
-       while (where != NULL) {
-               foundOne++;
-               strcpy(oldhayStack, haystack);
-               for (slider = haystack, slider1 = oldhayStack; slider != where;
-                        slider++, slider1++);
-               *slider = 0;
-               haystack = strcat(haystack, newNeedle);
-               slider1 += strlen(needle);
-               haystack = strcat(haystack, slider1);
-               where = strstr(slider, needle);
-       }
-       free(oldhayStack);
-
-       if (foundOne > 0)
-               return TRUE;
-       else
-               return FALSE;
-}
-
-#endif                                                 /* ! BB_REGEXP && (BB_GREP || BB_SED) */
-
-
 #if defined BB_FIND || defined BB_INSMOD
 /*
  * Routine to see if a text string is matched by a wildcard pattern.
@@ -1286,7 +1234,7 @@ extern long getNum(const char *cp)
 #endif                                                 /* BB_DD || BB_TAIL */
 
 
-#if defined BB_INIT || defined BB_SYSLOGD || defined BB_AR
+#if defined BB_INIT || defined BB_SYSLOGD 
 /* try to open up the specified device */
 extern int device_open(char *device, int mode)
 {
@@ -1342,7 +1290,7 @@ extern pid_t* findPidByName( char* pidName)
         * some new processes start up while we wait. The kernel will
         * just ignore any extras if we give it too many, and will trunc.
         * the list if we give it too few.  */
-       pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
+       pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
        pid_array[0] = num_pids+10;
 
        /* Now grab the pid list */
@@ -1368,9 +1316,7 @@ extern pid_t* findPidByName( char* pidName)
 
                if ((strstr(info.command_line, pidName) != NULL)
                                && (strlen(pidName) == strlen(info.command_line))) {
-                       pidList=realloc( pidList, sizeof(pid_t) * (j+2));
-                       if (pidList==NULL)
-                               fatalError(memory_exhausted);
+                       pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
                        pidList[j++]=info.pid;
                }
        }
@@ -1414,14 +1360,12 @@ extern pid_t* findPidByName( char* pidName)
                FILE *status;
                char filename[256];
                char buffer[256];
-               char* p;
 
                /* If it isn't a number, we don't want it */
                if (!isdigit(*next->d_name))
                        continue;
 
-               /* Now open the status file */
-               sprintf(filename, "/proc/%s/status", next->d_name);
+               sprintf(filename, "/proc/%s/cmdline", next->d_name);
                status = fopen(filename, "r");
                if (!status) {
                        continue;
@@ -1429,24 +1373,12 @@ extern pid_t* findPidByName( char* pidName)
                fgets(buffer, 256, status);
                fclose(status);
 
-               /* Make sure we only match on the process name */
-               p=buffer+5; /* Skip the name */
-               while ((p)++) {
-                       if (*p==0 || *p=='\n') {
-                               *p='\0';
-                               break;
-                       }
-               }
-               p=buffer+6; /* Skip the "Name:\t" */
-
-               if ((strstr(p, pidName) != NULL)
-                               && (strlen(pidName) == strlen(p))) {
-                       pidList=realloc( pidList, sizeof(pid_t) * (i+2));
-                       if (pidList==NULL)
-                               fatalError(memory_exhausted);
+               if (strstr(get_last_path_component(buffer), pidName) != NULL) {
+                       pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
                        pidList[i++]=strtol(next->d_name, NULL, 0);
                }
        }
+
        if (pidList)
                pidList[i]=0;
        return pidList;
@@ -1454,17 +1386,36 @@ extern pid_t* findPidByName( char* pidName)
 #endif                                                 /* BB_FEATURE_USE_DEVPS_PATCH */
 #endif                                                 /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
 
+#ifndef DMALLOC
 /* this should really be farmed out to libbusybox.a */
 extern void *xmalloc(size_t size)
 {
-       void *cp = malloc(size);
+       void *ptr = malloc(size);
 
-       if (cp == NULL)
+       if (!ptr)
                fatalError(memory_exhausted);
-       return cp;
+       return ptr;
 }
 
-#if defined BB_FEATURE_NFSMOUNT
+extern void *xrealloc(void *old, size_t size)
+{
+       void *ptr = realloc(old, size);
+       if (!ptr)
+               fatalError(memory_exhausted);
+       return ptr;
+}
+
+extern void *xcalloc(size_t nmemb, size_t size)
+{
+       void *ptr = calloc(nmemb, size);
+       if (!ptr)
+               fatalError(memory_exhausted);
+       return ptr;
+}
+#endif
+
+#if defined BB_FEATURE_NFSMOUNT || defined BB_SH || defined BB_LS
+# ifndef DMALLOC
 extern char * xstrdup (const char *s) {
        char *t;
 
@@ -1478,7 +1429,10 @@ extern char * xstrdup (const char *s) {
 
        return t;
 }
+# endif
+#endif
 
+#if defined BB_FEATURE_NFSMOUNT
 extern char * xstrndup (const char *s, int n) {
        char *t;
 
@@ -1617,7 +1571,7 @@ extern int find_real_root_device_name(char* name)
                if (strcmp(entry->d_name, "..") == 0)
                        continue;
 
-               sprintf( fileName, "/dev/%s", entry->d_name);
+               snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
 
                if (stat(fileName, &statBuf) != 0)
                        continue;
@@ -1654,7 +1608,7 @@ extern char *get_line_from_file(FILE *file)
                        break;
                /* grow the line buffer as necessary */
                if (idx > linebufsz-2)
-                       linebuf = realloc(linebuf, linebufsz += GROWBY);
+                       linebuf = xrealloc(linebuf, linebufsz += GROWBY);
                linebuf[idx++] = (char)ch;
                if ((char)ch == '\n')
                        break;
@@ -1667,6 +1621,29 @@ extern char *get_line_from_file(FILE *file)
        return linebuf;
 }
 
+#if defined BB_CAT || defined BB_LSMOD
+extern void print_file(FILE *file)
+{
+       int c;
+
+       while ((c = getc(file)) != EOF)
+               putc(c, stdout);
+       fclose(file);
+       fflush(stdout);
+}
+
+extern int print_file_by_name(char *filename)
+{
+       FILE *file;
+       file = fopen(filename, "r");
+       if (file == NULL) {
+               return FALSE;
+       }
+       print_file(file);
+       return TRUE;
+}
+#endif /* BB_CAT || BB_LSMOD */
+
 #if defined BB_ECHO || defined BB_TR
 char process_escape_sequence(char **ptr)
 {
@@ -1712,7 +1689,7 @@ char process_escape_sequence(char **ptr)
 }
 #endif
 
-#if defined BB_BASENAME || defined BB_LN
+#if defined BB_BASENAME || defined BB_LN || defined BB_SH
 char *get_last_path_component(char *path)
 {
        char *s=path+strlen(path)-1;