aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tsh.cc24
-rw-r--r--util.h11
2 files changed, 32 insertions, 3 deletions
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);
diff --git a/util.h b/util.h
index 072f65e..1477d3b 100644
--- a/util.h
+++ b/util.h
@@ -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)
{