Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <linux/limits.h>
- #include <ctype.h>
- #include <unistd.h>
- void run(char *filename, char *cmd);
- int main() {
- char input[ARG_MAX], cmd[ARG_MAX];
- while (1) {
- printf("$ ");
- fgets(input, ARG_MAX-1, stdin);
- // "ls"
- // "ls -la"
- // "kill -9 pid"
- // Read the command from the input
- int i;
- for (i = 0; i < ARG_MAX; i++) {
- if (i == 0 && input[i] == '\n')
- cmd[i] = '\0';
- else if (isspace(input[i])) {
- break;
- }
- else
- cmd[i] = input[i];
- }
- if (!strcmp("ls", cmd)) {
- run("/bin/ls", "ls");
- }
- else if (!strcmp("pwd", cmd)) {
- run("/bin/pwd", "pwd");
- }
- else if (!strcmp("ps", cmd)) {
- run("/bin/ps", "ps");
- }
- else if (!strcmp("kill", cmd)) {
- run("/bin/kill", "kill");
- }
- else {
- run(cmd, cmd);
- }
- }
- return 0;
- }
- void run(char *filename, char *cmd) {
- pid_t child = fork();
- if (child == 0)
- execlp(filename, cmd, NULL);
- else
- wait(child);
- return;
- }
Add Comment
Please, Sign In to add comment