-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlle.c
104 lines (82 loc) · 2.12 KB
/
handlle.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
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int errAtoi(const char *str) {
char *endptr;
int result = strtol(str, &endptr, 10);
if (*endptr != '\0') {
return -1;
}
return result;
}
void printError(const char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
int myExit(ShellInfo *info) {
if (info->argv[1]) {
int exitCheck = errAtoi(info->argv[1]);
if (exitCheck == -1) {
info->status = 2;
printError("Illegal number");
fprintf(stderr, "%s\n", info->argv[1]);
return 1;
}
info->err_num = exitCheck;
return -2;
}
info->err_num = -1;
return -2;
}
int myCd(ShellInfo *info) {
char *s, *dir, buffer[1024];
int chdirRet;
s = getcwd(buffer, 1024);
if (!s) {
printError("getcwd failure");
}
if (!info->argv[1]) {
dir = getenv("HOME");
if (!dir) {
chdirRet = chdir((dir = getenv("PWD")) ? dir : "/");
} else {
chdirRet = chdir(dir);
}
} else if (strcmp(info->argv[1], "-") == 0) {
if (!getenv("OLDPWD")) {
printf("%s\n", s);
return 1;
}
printf("%s\n", getenv("OLDPWD"));
chdirRet = chdir((dir = getenv("OLDPWD")) ? dir : "/");
} else {
chdirRet = chdir(info->argv[1]);
}
if (chdirRet == -1) {
printError("can't cd to");
fprintf(stderr, "%s\n", info->argv[1]);
} else {
setenv("OLDPWD", getenv("PWD"), 1);
setenv("PWD", getcwd(buffer, 1024), 1);
}
return 0;
}
int myHelp(ShellInfo *info) {
char **argArray;
argArray = info->argv;
printf("help call works. Function not yet implemented\n");
if (*argArray) {
printf("%s\n", *argArray);
}
return 0;
}
int main() {
ShellInfo info;
char *arguments[] = { "command", "argument", NULL };
info.argv = arguments;
printf("myExit status: %d\n", myExit(&info));
printf("myCd status: %d\n", myCd(&info));
printf("myHelp status: %d\n", myHelp(&info));
return 0;
}