J
jorntk
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
#define CMD_MAX 10
char *valid_cmds = " ls ps df ";
int main (void) {
char line_input[MAX], the_cmd[CMD_MAX];
char *new_args[CMD_MAX], *cp;
int i;
while(1) {
printf ("cmd> ");
if (gets(line_input ) != NULL) {
cp = line_input;
i = 0;
if ((new_args =strtok(cp, " " )) != NULL) {
sprintf(the_cmd, "%s ", new_args);
if((strstr(valid_cmds,the_cmd) - valid_cmds) % 4 == 1) {
do {
++i;
cp = NULL;
new_args = strtok(cp, " ");
} while (i < CMD_MAX -1 && new_args != NULL);
new_args =NULL;
switch (fork( )) {
case 0:
execvp(new_args[0], new_args);
perror("exec failure");
exit(1);
case -1:
perror("fork failure");
break;
default:
;
}
} else
printf("huh?\n");
}
}
}
}
the code above is taken from a unix system programming book, i compile
the code in red hat 9 and error message came out. can someone tell me
how to make the code work any help will be appreciated.
make -k shell
cc shell.c -o shell
/tmp/ccOmPIMu.o(.text+0x2f): In function `main':
: the `gets' function is dangerous and should not be used.
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
#define CMD_MAX 10
char *valid_cmds = " ls ps df ";
int main (void) {
char line_input[MAX], the_cmd[CMD_MAX];
char *new_args[CMD_MAX], *cp;
int i;
while(1) {
printf ("cmd> ");
if (gets(line_input ) != NULL) {
cp = line_input;
i = 0;
if ((new_args =strtok(cp, " " )) != NULL) {
sprintf(the_cmd, "%s ", new_args);
if((strstr(valid_cmds,the_cmd) - valid_cmds) % 4 == 1) {
do {
++i;
cp = NULL;
new_args = strtok(cp, " ");
} while (i < CMD_MAX -1 && new_args != NULL);
new_args =NULL;
switch (fork( )) {
case 0:
execvp(new_args[0], new_args);
perror("exec failure");
exit(1);
case -1:
perror("fork failure");
break;
default:
;
}
} else
printf("huh?\n");
}
}
}
}
the code above is taken from a unix system programming book, i compile
the code in red hat 9 and error message came out. can someone tell me
how to make the code work any help will be appreciated.
make -k shell
cc shell.c -o shell
/tmp/ccOmPIMu.o(.text+0x2f): In function `main':
: the `gets' function is dangerous and should not be used.