From 29843465a2621fc1dcb28ca5c4c6684c9f75930e Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 13 Aug 2026 11:15:11 +0000 Subject: simple error check --- tsh.cc | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'tsh.cc') diff --git a/tsh.cc b/tsh.cc index 6800e4d..ebf4d6a 100644 --- a/tsh.cc +++ b/tsh.cc @@ -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); -- cgit v1.3.1-4-g156e