diff options
| author | _ov4 <ov4@ov4.org> | 2026-08-13 11:15:11 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-08-13 11:15:11 +0000 |
| commit | 29843465a2621fc1dcb28ca5c4c6684c9f75930e (patch) | |
| tree | 9689642707fda287b0182cdd3d430d929a7da5f5 | |
| parent | ddbd21e8804071e794fb1264d2cfa095971870c5 (diff) | |
simple error check
| -rw-r--r-- | tsh.cc | 24 | ||||
| -rw-r--r-- | util.h | 11 |
2 files changed, 32 insertions, 3 deletions
@@ -19,6 +19,7 @@ using namespace std; using ov4::atomic_print; +using ov4::isnum; /* Misc manifest constants */ #define MAXLINE 1024 /* max line size */ @@ -339,11 +340,30 @@ void do_bgfg(char **argv) sigset_t prev; if (argv[1][0] == '%') // jid provided { - x = atoi(argv[1]); // eat % + if (!isnum(&argv[1][1])) + { + cerr << ((strcmp(argv[0], "fg") == 0) ? "fg" : "bg") << ": argument must be a PID or %jobid" << endl; + return; + } + x = atoi(&argv[1][1]); // eat % j = getjobjid(jobs, x); + if (j == nullptr) + { + cerr << argv[1] << ": No such job" << endl; + } } else { // pid - x = atoi(argv[0]); + if (!isnum(&argv[1][1])) + { + cerr << ((strcmp(argv[0], "fg") == 0) ? "fg" : "bg") << ": argument must be a PID or %jobid" << endl; + return; + } + x = atoi(&argv[1][0]); j = getjobpid(jobs, x); + if (j == nullptr) + { + cerr << argv[1] << ": No such process" << endl; + return; + } } block_all(&prev); @@ -19,6 +19,7 @@ namespace ov4 { void atomic_print(char *s); void atomic_print(int x); +bool isnum(char *s); void reverse(char str[], int length); char* itoa(int num, char* str, int base); @@ -27,7 +28,7 @@ bool DEBUG = true; // verbose mode, can be enabled on demand using something lik struct LogVoidify { void operator&(std::ostream&) const {} }; const int DECIMAL = 10; -const int LENGTH = 20; +const int LENGTH = 25; void atomic_print(char *s) { @@ -54,6 +55,14 @@ void reverse(char str[], int length) } } +bool isnum(char *s) +{ + for (int i = 0; s[i] != 0; i++) + if (!isdigit(s[i])) + return false; + return true; +} + char* itoa(int num, char* str, int base) { |
