Update internal.h to conditionally include asm/string.h
[oweals/busybox.git] / utility.c
index c8442f1bae01a40896293cde4efa6cc67e4b906f..b9c7a76cd30c4ab65c4b040a9bf375b33cdbd195 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -34,6 +34,7 @@
 /* same conditions as recursiveAction */
 #define bb_need_name_too_long
 #endif
+#define bb_need_memory_exhausted
 #define BB_DECLARE_EXTERN
 #include "messages.c"
 
@@ -234,15 +235,14 @@ int isDirectory(const char *fileName, const int followLinks, struct stat *statBu
 
 #if defined (BB_CP_MV)
 /*
- * Copy one file to another, while possibly preserving its modes, times,
- * and modes.  Returns TRUE if successful, or FALSE on a failure with an
- * error message output.  (Failure is not indicated if the attributes cannot
- * be set.)
- *  -Erik Andersen
+ * Copy one file to another, while possibly preserving its modes, times, and
+ * modes.  Returns TRUE if successful, or FALSE on a failure with an error
+ * message output.  (Failure is not indicated if attributes cannot be set.)
+ * -Erik Andersen
  */
 int
 copyFile(const char *srcName, const char *destName,
-                int setModes, int followLinks)
+                int setModes, int followLinks, int forceFlag)
 {
        int rfd;
        int wfd;
@@ -268,7 +268,8 @@ copyFile(const char *srcName, const char *destName,
        else
                status = lstat(destName, &dstStatBuf);
 
-       if (status < 0) {
+       if (status < 0 || forceFlag==TRUE) {
+               unlink(destName);
                dstStatBuf.st_ino = -1;
                dstStatBuf.st_dev = -1;
        }
@@ -306,10 +307,8 @@ copyFile(const char *srcName, const char *destName,
                }
 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
                if (setModes == TRUE) {
-                       if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
-                               perror(destName);
-                               return FALSE;
-                       }
+                       /* Try to set owner, but fail silently like GNU cp */
+                       lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
                }
 #endif
                return TRUE;
@@ -798,16 +797,17 @@ extern int parse_mode(const char *s, mode_t * theMode)
  * This uses buf as storage to hold things.
  * 
  */
-uid_t my_getid(const char *filename, char *name, uid_t id, gid_t *gid)
+unsigned long my_getid(const char *filename, char *name, unsigned long id, unsigned long *gid)
 {
        FILE *file;
        char *rname, *start, *end, buf[128];
-       id_t rid;
-       gid_t rgid = 0;
+       unsigned long rid;
+       unsigned long rgid = 0;
 
        file = fopen(filename, "r");
        if (file == NULL) {
-               perror(filename);
+               /* Do not complain.  It is ok for /etc/passwd and
+                * friends to be missing... */
                return (-1);
        }
 
@@ -831,13 +831,13 @@ uid_t my_getid(const char *filename, char *name, uid_t id, gid_t *gid)
 
                /* uid in passwd, gid in group */
                start = end + 1;
-               rid = (uid_t) strtol(start, &end, 10);
+               rid = (unsigned long) strtol(start, &end, 10);
                if (end == start)
                        continue;
 
                /* gid in passwd */
                start = end + 1;
-               rgid = (gid_t) strtol(start, &end, 10);
+               rgid = (unsigned long) strtol(start, &end, 10);
                
                if (name) {
                        if (0 == strcmp(rname, name)) {
@@ -858,33 +858,33 @@ uid_t my_getid(const char *filename, char *name, uid_t id, gid_t *gid)
 }
 
 /* returns a uid given a username */
-uid_t my_getpwnam(char *name)
+unsigned long my_getpwnam(char *name)
 {
        return my_getid("/etc/passwd", name, -1, NULL);
 }
 
 /* returns a gid given a group name */
-gid_t my_getgrnam(char *name)
+unsigned long my_getgrnam(char *name)
 {
        return my_getid("/etc/group", name, -1, NULL);
 }
 
 /* gets a username given a uid */
-void my_getpwuid(char *name, uid_t uid)
+void my_getpwuid(char *name, unsigned long uid)
 {
        my_getid("/etc/passwd", name, uid, NULL);
 }
 
 /* gets a groupname given a gid */
-void my_getgrgid(char *group, gid_t gid)
+void my_getgrgid(char *group, unsigned long gid)
 {
        my_getid("/etc/group", group, gid, NULL);
 }
 
 /* gets a gid given a user name */
-gid_t my_getpwnamegid(char *name)
+unsigned long my_getpwnamegid(char *name)
 {
-       gid_t gid;
+       unsigned long gid;
        my_getid("/etc/passwd", name, -1, &gid);
        return gid;
 }
@@ -1056,7 +1056,7 @@ extern int replace_match(char *haystack, char *needle, char *newNeedle,
 #endif                                                 /* ! BB_REGEXP && (BB_GREP || BB_SED) */
 
 
-#if defined BB_FIND
+#if defined BB_FIND || defined BB_INSMOD
 /*
  * Routine to see if a text string is matched by a wildcard pattern.
  * Returns TRUE if the text is matched, or FALSE if it is not matched
@@ -1156,7 +1156,7 @@ extern int check_wildcard_match(const char *text, const char *pattern)
 
        return TRUE;
 }
-#endif                                                 /* BB_FIND */
+#endif                            /* BB_FIND || BB_INSMOD */
 
 
 
@@ -1338,7 +1338,7 @@ extern pid_t* findPidByName( char* pidName)
                                && (strlen(pidName) == strlen(info.command_line))) {
                        pidList=realloc( pidList, sizeof(pid_t) * (j+2));
                        if (pidList==NULL)
-                               fatalError("out of memory\n");
+                               fatalError(memory_exhausted, "");
                        pidList[j++]=info.pid;
                }
        }
@@ -1411,7 +1411,7 @@ extern pid_t* findPidByName( char* pidName)
                                && (strlen(pidName) == strlen(p))) {
                        pidList=realloc( pidList, sizeof(pid_t) * (i+2));
                        if (pidList==NULL)
-                               fatalError("out of memory\n");
+                               fatalError(memory_exhausted, "");
                        pidList[i++]=strtol(next->d_name, NULL, 0);
                }
        }
@@ -1428,7 +1428,7 @@ extern void *xmalloc(size_t size)
        void *cp = malloc(size);
 
        if (cp == NULL)
-               fatalError("out of memory\n");
+               fatalError(memory_exhausted, "");
        return cp;
 }
 
@@ -1573,7 +1573,7 @@ extern int find_real_root_device_name(char* name)
 }
 #endif
 
-const unsigned int CSTRING_BUFFER_LENGTH = 128;
+const unsigned int CSTRING_BUFFER_LENGTH = 1024;
 /* recursive parser that returns cstrings of arbitrary length
  * from a FILE* 
  */
@@ -1583,12 +1583,12 @@ cstring_alloc(FILE* f, int depth)
     char *cstring;
     char buffer[CSTRING_BUFFER_LENGTH];
     int         target = CSTRING_BUFFER_LENGTH * depth;
-    int  i, len;
-    int  size;
+    int  c, i, len, size;
 
     /* fill buffer */
     i = 0;
-    while ((buffer[i] = fgetc(f)) != EOF) {
+       while ((c = fgetc(f)) != EOF) {
+               buffer[i] = (char) c;
                if (buffer[i++] == 0x0a) { break; }
                if (i == CSTRING_BUFFER_LENGTH) { break; }
     }