Ok, this is a pretty major update to sh.c. I renamed all the variables and
[oweals/busybox.git] / utility.c
index 2f3486c13663456c1a7ee8258753ee71559dd0e9..794a9281a69f8b4d4e81b9ff34c44f27051f4abf 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -142,7 +142,7 @@ extern void perror_msg_and_die(const char *s, ...)
        exit(EXIT_FAILURE);
 }
 
-#if defined BB_INIT
+#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT
 /* 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> }
@@ -261,7 +261,7 @@ void reset_ino_dev_hashtable(void)
 
 #endif /* BB_CP_MV || BB_DU */
 
-#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_AR)
+#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
 /*
  * Return TRUE if a fileName is a directory.
  * Nonexistant files return FALSE.
@@ -536,7 +536,7 @@ const char *time_string(time_t timeVal)
 }
 #endif /* BB_TAR || BB_AR */
 
-#if defined BB_TAR || defined BB_CP_MV || defined BB_AR
+#if defined BB_AR || defined BB_CP_MV || defined BB_DD || defined BB_NC || defined BB_TAR
 /*
  * Write all of the supplied buffer out to a file.
  * This does multiple writes as necessary.
@@ -565,7 +565,7 @@ int full_write(int fd, const char *buf, int len)
 #endif /* BB_TAR || BB_CP_MV || BB_AR */
 
 
-#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV
+#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV || defined BB_DD
 /*
  * Read all of the supplied buffer from a file.
  * This does multiple reads as necessary.
@@ -611,7 +611,7 @@ int full_read(int fd, char *buf, int len)
  * location, and do something (something specified
  * by the fileAction and dirAction function pointers).
  *
- * Unfortunatly, while nftw(3) could replace this and reduce 
+ * Unfortunately, while nftw(3) could replace this and reduce 
  * code size a bit, nftw() wasn't supported before GNU libc 2.1, 
  * and so isn't sufficiently portable to take over since glibc2.1
  * is so stinking huge.
@@ -722,7 +722,7 @@ int recursive_action(const char *fileName,
 
 
 
-#if defined (BB_TAR) || defined (BB_MKDIR) || defined (BB_AR)
+#if defined (BB_TAR) || defined (BB_MKDIR)
 /*
  * Attempt to create the directories along the specified path, except for
  * the final component.  The mode is given for the final directory only,
@@ -866,7 +866,7 @@ extern int parse_mode(const char *s, mode_t * theMode)
 
 #if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
  || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
- || defined BB_LOGNAME || defined BB_WHOAMI
+ || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
 
 /* This parses entries in /etc/passwd and /etc/group.  This is desirable
  * for BusyBox, since we want to avoid using the glibc NSS stuff, which
@@ -932,6 +932,7 @@ unsigned long my_getid(const char *filename, char *name, long id, long *gid)
                }
                if (id != -1 && id == rid) {
                        strncpy(name, rname, 8);
+                       name[8]='\0';
                        if (gid) *gid = rgid;
                        fclose(file);
                        return (TRUE);
@@ -956,12 +957,14 @@ long my_getgrnam(char *name)
 /* gets a username given a uid */
 void my_getpwuid(char *name, long uid)
 {
+       name[0] = '\0';
        my_getid("/etc/passwd", name, uid, NULL);
 }
 
 /* gets a groupname given a gid */
 void my_getgrgid(char *group, long gid)
 {
+       group[0] = '\0';
        my_getid("/etc/group", group, gid, NULL);
 }
 
@@ -1310,11 +1313,11 @@ extern pid_t* find_pid_by_name( char* pidName)
        /* open device */ 
        fd = open(device, O_RDONLY);
        if (fd < 0)
-               error_msg_and_die( "open failed for `%s': %s\n", device, strerror (errno));
+               perror_msg_and_die( "open failed for `%s'", device);
 
        /* Find out how many processes there are */
        if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) 
-               error_msg_and_die( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
+               perror_msg_and_die( "\nDEVPS_GET_PID_LIST");
        
        /* Allocate some memory -- grab a few extras just in case 
         * some new processes start up while we wait. The kernel will
@@ -1325,7 +1328,7 @@ extern pid_t* find_pid_by_name( char* pidName)
 
        /* Now grab the pid list */
        if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) 
-               error_msg_and_die( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
+               perror_msg_and_die( "\nDEVPS_GET_PID_LIST");
 
        /* Now search for a match */
        for (i=1, j=0; i<pid_array[0] ; i++) {
@@ -1334,7 +1337,7 @@ extern pid_t* find_pid_by_name( char* pidName)
 
            info.pid = pid_array[i];
            if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
-                       error_msg_and_die( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
+                       perror_msg_and_die( "\nDEVPS_GET_PID_INFO");
 
                /* Make sure we only match on the process name */
                p=info.command_line+1;
@@ -1358,7 +1361,7 @@ extern pid_t* find_pid_by_name( char* pidName)
 
        /* close device */
        if (close (fd) != 0) 
-               error_msg_and_die( "close failed for `%s': %s\n",device, strerror (errno));
+               perror_msg_and_die( "close failed for `%s'", device);
 
        return pidList;
 }
@@ -1384,7 +1387,7 @@ extern pid_t* find_pid_by_name( char* pidName)
 
        dir = opendir("/proc");
        if (!dir)
-               error_msg_and_die( "Cannot open /proc: %s\n", strerror (errno));
+               perror_msg_and_die( "Cannot open /proc");
        
        while ((next = readdir(dir)) != NULL) {
                FILE *status;
@@ -1444,7 +1447,7 @@ extern void *xcalloc(size_t nmemb, size_t size)
 }
 #endif
 
-#if defined BB_FEATURE_NFSMOUNT || defined BB_SH || defined BB_LS
+#if defined BB_FEATURE_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET
 # ifndef DMALLOC
 extern char * xstrdup (const char *s) {
        char *t;
@@ -1467,7 +1470,7 @@ extern char * xstrndup (const char *s, int n) {
        char *t;
 
        if (s == NULL)
-               error_msg_and_die("xstrndup bug");
+               error_msg_and_die("xstrndup bug\n");
 
        t = xmalloc(n+1);
        strncpy(t,s,n);
@@ -1730,14 +1733,16 @@ char *get_last_path_component(char *path)
        char *s=path+strlen(path)-1;
 
        /* strip trailing slashes */
-       while (s && *s == '/') {
+       while (s != path && *s == '/') {
                *s-- = '\0';
        }
 
        /* find last component */
        s = strrchr(path, '/');
-       if (s==NULL) return path;
-       else return s+1;
+       if (s == NULL || s[1] == '\0')
+               return path;
+       else
+               return s+1;
 }
 #endif
 
@@ -1759,7 +1764,7 @@ FILE *wfopen(const char *path, const char *mode)
 {
        FILE *fp;
        if ((fp = fopen(path, mode)) == NULL) {
-               error_msg("%s: %s\n", path, strerror(errno));
+               perror_msg("%s", path);
                errno = 0;
        }
        return fp;
@@ -1773,7 +1778,7 @@ FILE *xfopen(const char *path, const char *mode)
 {
        FILE *fp;
        if ((fp = fopen(path, mode)) == NULL)
-               error_msg_and_die("%s: %s\n", path, strerror(errno));
+               perror_msg_and_die("%s", path);
        return fp;
 }
 #endif
@@ -1786,6 +1791,19 @@ int applet_name_compare(const void *x, const void *y)
        return strcmp(applet1->name, applet2->name);
 }
 
+#if defined BB_NC
+ssize_t safe_read(int fd, void *buf, size_t count)
+{
+       ssize_t n;
+
+       do {
+               n = read(fd, buf, count);
+       } while (n < 0 && errno == EINTR);
+
+       return n;
+}
+#endif
+
 /* END CODE */
 /*
 Local Variables: