This is for an assignment.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
int run=1;
while (run) {
int perror;
pid_t pid;
char in[1536];
char para[512]="/bin/";
char *argv[512];
printf("%s..: %s","\x1B[31m","\x1B[37m");
fgets(in,1530,stdin);
char *com = strtok (in," \t\r\n");
char *c2=com;
strcat (para,com);
strcat (para,"\0");
int i=0;
while (com != NULL){
com = strtok (NULL, " \n\r\t");
if (com!=NULL){
argv[i]=com;
i++;
}
}
argv[i]="\0";
if (strcmp(c2, "exit") == 0|strcmp(c2, "quit") == 0){
run=0;
}
else{
if ((pid=fork())==0){ //Child
execvp(para,argv);
}
else{ //Parent
waitpid(pid,&perror,0);
}
}
}
return 0;
}
Commands like ls
and pwd
work perfectly without arguments, but when I try to use arguments the first argument is ignored. Sample outputs below.
$ make
$ ./A1T2
..: ls
A1T2 main.c main.c~ main.c++~ main.c.old Makefile Makefile~
..: pwd
/home/kevin/Documents/COS-222/Assignments/Assignment-1/Task-2
..: mkdir one
one: cannot create directory ‘’: No such file or directory
..: mkdir one two
one: cannot create directory ‘’: No such file or directory
..: ls
: cannot access : No such file or directory
two:
..: exit
kevin@Kevin-MATE:~/Documents/COS-222/Assignments/Assignment-1/Task-2$ make
./A1T2
..: ls
A1T2 main.c main.c~ main.c++~ main.c.old Makefile Makefile~ two
..: echo hello world
world
..: exit
$