0% found this document useful (0 votes)
7 views

assignment 1 systems

جامعة

Uploaded by

itztozx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

assignment 1 systems

جامعة

Uploaded by

itztozx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

#define MAX_INPUT 1024


char *arg_list[150];

void tokenizeCommand(char *str) {


int i = 0;
const char delim[] = " ";
arg_list[i] = strtok(str, delim);
while (arg_list[i] != NULL) {
arg_list[++i] = strtok(NULL, delim);
}
}

int main(void) {
char input[MAX_INPUT];

printf("Welcome to Simple Shell! Type 'exit' to quit.\n");

while (1) {
printf("\n> ");
if (fgets(input, MAX_INPUT, stdin) == NULL) {
perror("Error reading input");
continue;
}

input[strcspn(input, "\n")] = '\0';

if (strcmp(input, "exit") == 0) {
printf("Goodbye!\n");
break;
}

tokenizeCommand(input);

pid_t pid = fork();


if (pid < 0) {
perror("Fork failed");
continue;
} else if (pid == 0) {
if (execvp(arg_list[0], arg_list) == -1) {
perror("Command not found");
}
exit(EXIT_FAILURE);
} else {
int status;
waitpid(pid, &status, 0);

if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {


printf("Child process exited with status %d\n",
WEXITSTATUS(status));
}
}
}
return 0;
}

You might also like