-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.c
79 lines (60 loc) · 1.21 KB
/
builtin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include "builtin.h"
#include "history.h"
#include "jobs.h"
void builtin_history() {
hist_print();
}
void builtin_pwd() {
#define CWD_BUFFER_SIZE 100
char buff[CWD_BUFFER_SIZE];
getcwd(buff, CWD_BUFFER_SIZE);
printf("%s", buff);
}
void builtin_cd(char *args[]) {
int x = chdir(args[1]);
if(x != 0) {
perror("Error in cd");
}
}
void builtin_exit() {
// pid_t parent_pid = getppid();
// kill(parent_pid, SIGTERM);
exit(0);
}
int builtin_match_and_run(char *program_name, char *args[]) {
if(strcmp(program_name, "history") == 0) {
builtin_history();
return 1;
}
if(strcmp(program_name, "pwd") == 0) {
builtin_pwd();
return 1;
}
if(strcmp(program_name, "cd") == 0) {
builtin_cd(args);
return 1;
}
if(strcmp(program_name, "exit") == 0) {
builtin_exit();
return 1;
}
if(strcmp(program_name, "jobs") == 0) {
print_jobs();
return 1;
}
if(strcmp(program_name, "fg") == 0) {
int job_number;
if(args[1] == 0 || sscanf(args[1], "%d", &job_number) == 0)
printf("Invalid argument to fd");
else
foreground_job(job_number);
return 1;
}
return 0;
}