3 #include <sys/resource.h>
14 int parseline(const char *cmdline
, char **argv
);
15 void do_bgfg(char *cmdline
);
17 void eval (char *cmdline
) {
22 bg
= parseline(cmdline
, argv
);
23 if ((pid
= fork()) == 0) { /* child runs command */
24 if (execvp(argv
[0], argv
) < 0) {
25 perror("Command not found");
29 if (!bg
) { /* parent waits for fg job to terminate */
31 if (waitpid(pid
, &status
, 0) < 0) {
32 perror("waitfg: waitpid error");
35 else { /* otherwise, don’t wait for bg job */
36 printf("%d %s\n", pid
, cmdline
);
41 char cmdline
[MAXLINE
];
45 fgets(cmdline
, MAXLINE
, stdin
);
55 int parseline(const char *cmdline
, char **argv
)
57 static char array
[MAXLINE
]; /* holds local copy of command line */
58 char *buf
= array
; /* ptr that traverses command line */
59 char *delim
; /* points to first space delimiter */
60 int argc
; /* number of args */
61 int bg
; /* background job? */
64 buf
[strlen(buf
)-1] = ' '; /* replace trailing '\n' with space */
65 while (*buf
&& (*buf
== ' ')) /* ignore leading spaces */
68 /* Build the argv list */
72 delim
= strchr(buf
, '\'');
75 delim
= strchr(buf
, ' ');
82 while (*buf
&& (*buf
== ' ')) /* ignore spaces */
87 delim
= strchr(buf
, '\'');
90 delim
= strchr(buf
, ' ');
95 if (argc
== 0) /* ignore blank line */
98 /* should the job run in the background? */
99 if ((bg
= (*argv
[argc
-1] == '&')) != 0) {
104 void do_bgfg(char *cmdline
){