ash: exec: Stricter pathopt parsing
[oweals/busybox.git] / shell / shell_common.c
index cc518d54b4642fadaf18ce6cf904f47113a361a5..12c4a073c4f68b1f9691c598ec62ddc4c7e37750 100644 (file)
 const char defifsvar[] ALIGN1 = "IFS= \t\n";
 const char defoptindvar[] ALIGN1 = "OPTIND=1";
 
-
-int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
-{
-       if (!s || !(isalpha(*s) || *s == '_'))
-               return 0;
-
-       do
-               s++;
-       while (isalnum(*s) || *s == '_');
-
-       return *s == terminator;
-}
-
 /* read builtin */
 
 /* Needs to be interruptible: shell must handle traps and shell-special signals
@@ -70,7 +57,7 @@ shell_builtin_read(struct builtin_read_params *params)
        argv = params->argv;
        pp = argv;
        while (*pp) {
-               if (!is_well_formed_var_name(*pp, '\0')) {
+               if (endofname(*pp)[0] != '\0') {
                        /* Mimic bash message */
                        bb_error_msg("read: '%s': not a valid identifier", *pp);
                        return (const char *)(uintptr_t)1;
@@ -335,53 +322,99 @@ shell_builtin_read(struct builtin_read_params *params)
 struct limits {
        uint8_t cmd;            /* RLIMIT_xxx fit into it */
        uint8_t factor_shift;   /* shift by to get rlim_{cur,max} values */
-       const char *name;
 };
 
 static const struct limits limits_tbl[] = {
-       { RLIMIT_CORE,          9,      "core file size (blocks)" }, // -c
-       { RLIMIT_DATA,          10,     "data seg size (kb)" },      // -d
-       { RLIMIT_NICE,          0,      "scheduling priority" },     // -e
-       { RLIMIT_FSIZE,         9,      "file size (blocks)" },      // -f
+       { RLIMIT_CORE,          9,      }, // -c
+       { RLIMIT_DATA,          10,     }, // -d
+       { RLIMIT_NICE,          0,      }, // -e
+       { RLIMIT_FSIZE,         9,      }, // -f
 #define LIMIT_F_IDX     3
+#ifdef RLIMIT_SIGPENDING
+       { RLIMIT_SIGPENDING,    0,      }, // -i
+#endif
 #ifdef RLIMIT_MEMLOCK
-       { RLIMIT_MEMLOCK,       10,     "max locked memory (kb)" },  // -l
+       { RLIMIT_MEMLOCK,       10,     }, // -l
 #endif
 #ifdef RLIMIT_RSS
-       { RLIMIT_RSS,           10,     "max memory size (kb)" },    // -m
+       { RLIMIT_RSS,           10,     }, // -m
 #endif
 #ifdef RLIMIT_NOFILE
-       { RLIMIT_NOFILE,        0,      "open files" },              // -n
+       { RLIMIT_NOFILE,        0,      }, // -n
+#endif
+#ifdef RLIMIT_MSGQUEUE
+       { RLIMIT_MSGQUEUE,      0,      }, // -q
 #endif
 #ifdef RLIMIT_RTPRIO
-       { RLIMIT_RTPRIO,        0,      "real-time priority" },      // -r
+       { RLIMIT_RTPRIO,        0,      }, // -r
 #endif
 #ifdef RLIMIT_STACK
-       { RLIMIT_STACK,         10,     "stack size (kb)" },         // -s
+       { RLIMIT_STACK,         10,     }, // -s
 #endif
 #ifdef RLIMIT_CPU
-       { RLIMIT_CPU,           0,      "cpu time (seconds)" },      // -t
+       { RLIMIT_CPU,           0,      }, // -t
 #endif
 #ifdef RLIMIT_NPROC
-       { RLIMIT_NPROC,         0,      "max user processes" },      // -u
+       { RLIMIT_NPROC,         0,      }, // -u
 #endif
 #ifdef RLIMIT_AS
-       { RLIMIT_AS,            10,     "virtual memory (kb)" },     // -v
+       { RLIMIT_AS,            10,     }, // -v
 #endif
 #ifdef RLIMIT_LOCKS
-       { RLIMIT_LOCKS,         0,      "file locks" },              // -x
+       { RLIMIT_LOCKS,         0,      }, // -x
 #endif
 };
-// bash also has these:
-//pending signals                 (-i) 61858   //RLIMIT_SIGPENDING
+// bash also shows:
 //pipe size            (512 bytes, -p) 8
-//POSIX message queues     (bytes, -q) 819200  //RLIMIT_MSGQUEUE
+
+static const char limits_help[] ALIGN1 =
+       "core file size (blocks)"          // -c
+       "\0""data seg size (kb)"           // -d
+       "\0""scheduling priority"          // -e
+       "\0""file size (blocks)"           // -f
+#ifdef RLIMIT_SIGPENDING
+       "\0""pending signals"              // -i
+#endif
+#ifdef RLIMIT_MEMLOCK
+       "\0""max locked memory (kb)"       // -l
+#endif
+#ifdef RLIMIT_RSS
+       "\0""max memory size (kb)"         // -m
+#endif
+#ifdef RLIMIT_NOFILE
+       "\0""open files"                   // -n
+#endif
+#ifdef RLIMIT_MSGQUEUE
+       "\0""POSIX message queues (bytes)" // -q
+#endif
+#ifdef RLIMIT_RTPRIO
+       "\0""real-time priority"           // -r
+#endif
+#ifdef RLIMIT_STACK
+       "\0""stack size (kb)"              // -s
+#endif
+#ifdef RLIMIT_CPU
+       "\0""cpu time (seconds)"           // -t
+#endif
+#ifdef RLIMIT_NPROC
+       "\0""max user processes"           // -u
+#endif
+#ifdef RLIMIT_AS
+       "\0""virtual memory (kb)"          // -v
+#endif
+#ifdef RLIMIT_LOCKS
+       "\0""file locks"                   // -x
+#endif
+;
 
 static const char limit_chars[] ALIGN1 =
                        "c"
                        "d"
                        "e"
                        "f"
+#ifdef RLIMIT_SIGPENDING
+                       "i"
+#endif
 #ifdef RLIMIT_MEMLOCK
                        "l"
 #endif
@@ -391,6 +424,9 @@ static const char limit_chars[] ALIGN1 =
 #ifdef RLIMIT_NOFILE
                        "n"
 #endif
+#ifdef RLIMIT_MSGQUEUE
+                       "q"
+#endif
 #ifdef RLIMIT_RTPRIO
                        "r"
 #endif
@@ -417,6 +453,9 @@ static const char ulimit_opt_string[] ALIGN1 = "-HSa"
                        "d::"
                        "e::"
                        "f::"
+#ifdef RLIMIT_SIGPENDING
+                       "i::"
+#endif
 #ifdef RLIMIT_MEMLOCK
                        "l::"
 #endif
@@ -426,6 +465,9 @@ static const char ulimit_opt_string[] ALIGN1 = "-HSa"
 #ifdef RLIMIT_NOFILE
                        "n::"
 #endif
+#ifdef RLIMIT_MSGQUEUE
+                       "q::"
+#endif
 #ifdef RLIMIT_RTPRIO
                        "r::"
 #endif
@@ -555,10 +597,12 @@ shell_builtin_ulimit(char **argv)
        if (!(opts & (OPT_hard | OPT_soft)))
                opts |= (OPT_hard | OPT_soft);
        if (opts & OPT_all) {
+               const char *help = limits_help;
                for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) {
                        getrlimit(limits_tbl[i].cmd, &limit);
-                       printf("%-32s(-%c) ", limits_tbl[i].name, limit_chars[i]);
+                       printf("%-32s(-%c) ", help, limit_chars[i]);
                        printlim(opts, &limit, &limits_tbl[i]);
+                       help += strlen(help) + 1;
                }
                return EXIT_SUCCESS;
        }
@@ -589,7 +633,7 @@ shell_builtin_ulimit(char **argv)
                getrlimit(limits_tbl[i].cmd, &limit);
                if (!val_str) {
                        if (opt_cnt > 1)
-                               printf("%-32s(-%c) ", limits_tbl[i].name, limit_chars[i]);
+                               printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]);
                        printlim(opts, &limit, &limits_tbl[i]);
                } else {
                        rlim_t val = RLIM_INFINITY;
@@ -616,7 +660,7 @@ shell_builtin_ulimit(char **argv)
                                limit.rlim_cur = val;
 //bb_error_msg("setrlimit(%d, %lld, %lld)", limits_tbl[i].cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
                        if (setrlimit(limits_tbl[i].cmd, &limit) < 0) {
-                               bb_perror_msg("error setting limit");
+                               bb_simple_perror_msg("error setting limit");
                                return EXIT_FAILURE;
                        }
                }