Update internal.h to conditionally include asm/string.h
[oweals/busybox.git] / utility.c
index 5899fe954370aa8d4fbf747e81991164593ba9bd..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"
 
@@ -796,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);
        }
 
@@ -829,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)) {
@@ -856,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;
 }
@@ -1054,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
@@ -1154,7 +1156,7 @@ extern int check_wildcard_match(const char *text, const char *pattern)
 
        return TRUE;
 }
-#endif                                                 /* BB_FIND */
+#endif                            /* BB_FIND || BB_INSMOD */
 
 
 
@@ -1336,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;
                }
        }
@@ -1409,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);
                }
        }
@@ -1426,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;
 }
 
@@ -1571,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* 
  */
@@ -1581,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; }
     }