More doc patches from Matt Kraai <kraai@alumni.carnegiemellon.edu>.
[oweals/busybox.git] / utility.c
index 234955ea7a7bbcd4abfeeeb7e126f7738ad17645..e1e0f71a5ffa4f096c8d21741862664411f7b097 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -33,8 +33,8 @@
  || defined (BB_INSMOD)
 /* same conditions as recursiveAction */
 #define bb_need_name_too_long
-#define bb_need_memory_exhausted
 #endif
+#define bb_need_memory_exhausted
 #define BB_DECLARE_EXTERN
 #include "messages.c"
 
@@ -131,6 +131,16 @@ extern int get_kernel_revision(void)
 }
 #endif                                                 /* BB_INIT */
 
+
+
+#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
+#include <sys/syscall.h>
+#include <linux/unistd.h>
+_syscall1(int, sysinfo, struct sysinfo *, info);
+#endif                                                 /* BB_INIT */
+
+
+
 #if defined (BB_CP_MV) || defined (BB_DU)
 
 #define HASH_SIZE      311             /* Should be prime */
@@ -801,12 +811,13 @@ unsigned long my_getid(const char *filename, char *name, unsigned long id, unsig
 {
        FILE *file;
        char *rname, *start, *end, buf[128];
-       id_t rid;
+       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);
        }
 
@@ -891,7 +902,7 @@ unsigned long my_getpwnamegid(char *name)
 #endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */ 
 
 
-#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
+#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
 
 
 #include <linux/kd.h>
@@ -976,7 +987,7 @@ int get_console_fd(char *tty_name)
 }
 
 
-#endif                                                 /* BB_CHVT || BB_DEALLOCVT */
+#endif                                                 /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
 
 
 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
@@ -1055,7 +1066,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
@@ -1155,7 +1166,7 @@ extern int check_wildcard_match(const char *text, const char *pattern)
 
        return TRUE;
 }
-#endif                                                 /* BB_FIND */
+#endif                            /* BB_FIND || BB_INSMOD */
 
 
 
@@ -1317,7 +1328,7 @@ extern pid_t* findPidByName( char* pidName)
                fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
 
        /* Now search for a match */
-       for (i=1; i<pid_array[0] ; i++) {
+       for (i=1, j=0; i<pid_array[0] ; i++) {
                char* p;
                struct pid_info info;
 
@@ -1572,57 +1583,81 @@ extern int find_real_root_device_name(char* name)
 }
 #endif
 
-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)
+static const int GROWBY = 80; /* how large we will grow strings by */
+
+/* get_line_from_file() - This function reads an entire line from a text file
+ * up to a newline. It returns a malloc'ed char * which must be stored and
+ * free'ed  by the caller. */
+extern char *get_line_from_file(FILE *file)
 {
-    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;
+       int ch;
+       int idx = 0;
+       char *linebuf = NULL;
+       int linebufsz = 0;
+
+       while (1) {
+               ch = fgetc(file);
+               if (ch == EOF)
+                       break;
+               /* grow the line buffer as necessary */
+               if (idx > linebufsz-1)
+                       linebuf = realloc(linebuf, linebufsz += GROWBY);
+               linebuf[idx++] = (char)ch;
+               if ((char)ch == '\n')
+                       break;
+       }
+
+       if (idx == 0)
+               return NULL;
+
+       linebuf[idx] = 0;
+       return linebuf;
 }
 
-/* 
- * wrapper around recursive cstring_alloc 
- * it's the caller's responsibility to free the cstring
- */ 
-char *
-cstring_lineFromFile(FILE *f)
+#if defined BB_ECHO || defined BB_TR
+char process_escape_sequence(char **ptr)
 {
-    return cstring_alloc(f, 0);
+       char c;
+
+       switch (c = *(*ptr)++) {
+       case 'a':
+               c = '\a';
+               break;
+       case 'b':
+               c = '\b';
+               break;
+       case 'f':
+               c = '\f';
+               break;
+       case 'n':
+               c = '\n';
+               break;
+       case 't':
+               c = '\t';
+               break;
+       case 'v':
+               c = '\v';
+               break;
+       case '\\':
+               c = '\\';
+               break;
+       case '0': case '1': case '2': case '3':
+       case '4': case '5': case '6': case '7':
+               c -= '0';
+               if ('0' <= **ptr && **ptr <= '7') {
+                       c = c * 8 + (*(*ptr)++ - '0');
+                       if ('0' <= **ptr && **ptr <= '7')
+                               c = c * 8 + (*(*ptr)++ - '0');
+               }
+               break;
+       default:
+               (*ptr)--;
+               c = '\\';
+               break;
+       }
+       return c;
 }
+#endif
 
 /* END CODE */
 /*