-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.c
176 lines (139 loc) · 3.1 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "headers.h"
int builtin_cd(char **args, int arg_count)
{
if (arg_count == 1)
{
strcpy(args[arg_count++], "~");
}
else if (arg_count > 2)
{
fprintf(stderr, "Too many arguments\n");
return EXIT_FAILURE;
}
if (strcmp(args[1], "~") == 0)
{
if (chdir(getenv("HOME")) != 0)
{
perror("chdir() error");
return EXIT_FAILURE;
}
}
else
{
if (chdir(args[1]) != 0)
{
perror("chdir() failed");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int builtin_pwd(char **args, int arg_count)
{
if (arg_count != 1)
{
fprintf(stderr, "Too many arguments\n");
return EXIT_FAILURE;
}
size_t alloc_sz = BUFFER_SIZE * sizeof(char);
char *cwd = (char *)malloc(alloc_sz);
if (cwd == NULL)
{
fprintf(stderr, "Error allocating memory");
return EXIT_FAILURE;
}
if (getcwd(cwd, alloc_sz) != NULL)
{
printf("%s\n", cwd);
}
else
{
perror("getcwd() error");
free(cwd);
return EXIT_FAILURE;
}
free(cwd);
return EXIT_SUCCESS;
}
int builtin_echo(char **args, int arg_count)
{
if (arg_count == 1)
{
printf("\n");
}
else
{
for (int i = 1; i < arg_count; ++i)
{
printf("%s ", args[i]);
}
printf("\n");
}
return EXIT_SUCCESS;
}
int builtin_pinfo(char **args, int arg_count)
{
if (arg_count > 2)
{
fprintf(stderr, "Too many arguments");
return EXIT_FAILURE;
}
int pid;
if (arg_count == 2)
{
long num = 0;
char *end = NULL;
num = strtol(args[1], &end, 10);
if (end == args[1])
{
fprintf(stderr, "Not an integer argument\n");
return EXIT_FAILURE;
}
if ((num > INT_MAX) || (num < 0))
{
fprintf(stderr, "Number out of range\n");
return EXIT_FAILURE;
}
pid = (int)num;
}
else
{
pid = getpid();
}
char *path = (char *)malloc(BUFFER_SIZE);
if (path == NULL)
{
fprintf(stderr, "Error allocating memory");
return EXIT_FAILURE;
}
sprintf(path, "/proc/%d/stat", pid);
FILE *fp;
fp = fopen(path, "r");
if (fp == NULL)
{
perror("fopen() error");
return EXIT_FAILURE;
}
free(path);
char *exe = (char *)malloc(BUFFER_SIZE);
if (exe == NULL)
{
fprintf(stderr, "Error allocating memory");
return EXIT_FAILURE;
}
char state;
long unsigned int v_size;
fscanf(fp, "%*d %s %c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*u %lu",
exe, &state, &v_size);
fclose(fp);
printf("pid ---> %d\n", pid);
printf("Process Status ---> %c\n", state);
printf("Memory ---> %lu\n", v_size);
printf("Executable path ---> %s\n", exe);
free(exe);
return EXIT_SUCCESS;
}
int quit(char **args, int arg_count)
{
exit(EXIT_SUCCESS);
}