-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltins.c
115 lines (96 loc) · 1.92 KB
/
builtins.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
// #include <time.h>
#include "builtins.h"
/*
* This file contains all bultin functions. Except, maybe, ls.
* Built-in functions include cd, pwd, exit.
*/
int shcd(char **argv, int argc) {
char *location;
if (argc == 1) {
location = "~";
}
else {
location = argv[1];
}
if (strcmp(location, "~") == 0) {
location = getenv("HOME");
}
if(chdir(location) == -1) {
perror("Could not cd into specified directory");
}
return 1;
}
int shpwd() {
char *cdir = (char*)malloc(PATH_MAX * sizeof(char*));
getcwd(cdir, PATH_MAX);
printf("%s ", cdir);
return 1;
}
int shexit() {
return 0;
}
// Basic echo. No support for double quoted shit
int shecho(char **argv, int argc) {
int i;
// char *temp;
for(i = 1; i < argc; i++) {
// temp = argv[i];
printf("%s", argv[i]);
}
printf("\n");
return 1;
}
int remind(char **argv, int argc) {
if(argc <= 2) {
printf("Usage: remindme time reminder\n");
return 1;
}
int time;
time = strtol(argv[1], (char**)NULL, 10);
char *text = (char*)malloc(4096*sizeof(char));
int i;
strcpy(text, argv[2]);
// printf("no\n");
for(i = 3; i < argc; i++) {
strcat(text, " ");
strcat(text, argv[i]);
}
strcat(text, "\0");
switch (fork()) {
case 0:
sleep(time);
printf("\n\nReminder: %s\n", text);
break;
}
return 1;
}
int shsetenv(char **argv, int argc) {
char* val;
// int ret;
if (argc != 2 && argc!= 3) {
printf("Usage: setenv var [value]\n");
return 1;
}
if (argc == 2) {
val = "";
}
else {
val = argv[2];
}
return setenv(argv[1], val, 1)==0?1:0;
}
int shunsetenv(char **argv, int argc) {
if (argc != 2) {
printf("Usage: unsetenv var\n");
return 1;
}
return unsetenv(argv[1])==0?1:0;
}