mkfs_ext2: fix compile error
[oweals/busybox.git] / shell / ash.c
index 6ce621534d5302b55a4f045606cd1073c8ad8a94..481b84138dbed7da9286ca525934f34097d3fa08 100644 (file)
@@ -53,6 +53,8 @@
 #include "math.h"
 #if ENABLE_ASH_RANDOM_SUPPORT
 # include "random.h"
+#else
+# define CLEAR_RANDOM_T(rnd) ((void)0)
 #endif
 
 #if defined SINGLE_APPLET_MAIN
@@ -1155,6 +1157,49 @@ errmsg(int e, const char *em)
 
 /* ============ Memory allocation */
 
+#if 0
+/* I consider these wrappers nearly useless:
+ * ok, they return you to nearest exception handler, but
+ * how much memory do you leak in the process, making
+ * memory starvation worse?
+ */
+static void *
+ckrealloc(void * p, size_t nbytes)
+{
+       p = realloc(p, nbytes);
+       if (!p)
+               ash_msg_and_raise_error(bb_msg_memory_exhausted);
+       return p;
+}
+
+static void *
+ckmalloc(size_t nbytes)
+{
+       return ckrealloc(NULL, nbytes);
+}
+
+static void *
+ckzalloc(size_t nbytes)
+{
+       return memset(ckmalloc(nbytes), 0, nbytes);
+}
+
+static char *
+ckstrdup(const char *s)
+{
+       char *p = strdup(s);
+       if (!p)
+               ash_msg_and_raise_error(bb_msg_memory_exhausted);
+       return p;
+}
+#else
+/* Using bbox equivalents. They exit if out of memory */
+# define ckrealloc xrealloc
+# define ckmalloc  xmalloc
+# define ckzalloc  xzalloc
+# define ckstrdup  xstrdup
+#endif
+
 /*
  * It appears that grabstackstr() will barf with such alignments
  * because stalloc() will return a string allocated in a new stackblock.
@@ -1164,7 +1209,7 @@ enum {
        /* Most machines require the value returned from malloc to be aligned
         * in some way.  The following macro will get this right
         * on many machines.  */
-       SHELL_SIZE = sizeof(union {int i; char *cp; double d; }) - 1,
+       SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,
        /* Minimum size of a block */
        MINSIZE = SHELL_ALIGN(504),
 };
@@ -1210,43 +1255,10 @@ extern struct globals_memstack *const ash_ptr_to_globals_memstack;
        herefd = -1; \
 } while (0)
 
+
 #define stackblock()     ((void *)g_stacknxt)
 #define stackblocksize() g_stacknleft
 
-
-static void *
-ckrealloc(void * p, size_t nbytes)
-{
-       p = realloc(p, nbytes);
-       if (!p)
-               ash_msg_and_raise_error(bb_msg_memory_exhausted);
-       return p;
-}
-
-static void *
-ckmalloc(size_t nbytes)
-{
-       return ckrealloc(NULL, nbytes);
-}
-
-static void *
-ckzalloc(size_t nbytes)
-{
-       return memset(ckmalloc(nbytes), 0, nbytes);
-}
-
-/*
- * Make a copy of a string in safe storage.
- */
-static char *
-ckstrdup(const char *s)
-{
-       char *p = strdup(s);
-       if (!p)
-               ash_msg_and_raise_error(bb_msg_memory_exhausted);
-       return p;
-}
-
 /*
  * Parse trees for commands are allocated in lifo order, so we use a stack
  * to make this more efficient, and also to avoid all sorts of exception
@@ -4715,10 +4727,12 @@ forkshell(struct job *jp, union node *n, int mode)
                        freejob(jp);
                ash_msg_and_raise_error("can't fork");
        }
-       if (pid == 0)
+       if (pid == 0) {
+               CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */
                forkchild(jp, n, mode);
-       else
+       } else {
                forkparent(jp, n, mode, pid);
+       }
        return pid;
 }
 
@@ -7771,7 +7785,7 @@ static char *funcstring;        /* block to allocate strings from */
 #define EV_TESTED  02           /* exit status is checked; ignore -e flag */
 #define EV_BACKCMD 04           /* command executing within back quotes */
 
-static const short nodesize[N_NUMBER] = {
+static const uint8_t nodesize[N_NUMBER] = {
        [NCMD     ] = SHELL_ALIGN(sizeof(struct ncmd)),
        [NPIPE    ] = SHELL_ALIGN(sizeof(struct npipe)),
        [NREDIR   ] = SHELL_ALIGN(sizeof(struct nredir)),
@@ -10069,12 +10083,6 @@ setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 static void FAST_FUNC
 change_random(const char *value)
 {
-       /* Galois LFSR parameter */
-       /* Taps at 32 31 29 1: */
-       enum { MASK = 0x8000000b };
-       /* Another example - taps at 32 31 30 10: */
-       /* MASK = 0x00400007 */
-
        uint32_t t;
 
        if (value == NULL) {
@@ -13258,11 +13266,6 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
 #endif
        rootpid = getpid();
 
-#if ENABLE_ASH_RANDOM_SUPPORT
-       /* Can use monotonic_ns() for better randomness but for now it is
-        * not used anywhere else in busybox... so avoid bloat */
-       INIT_RANDOM_T(&random_gen, rootpid, monotonic_us());
-#endif
        init();
        setstackmark(&smark);
        procargs(argv);