-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin2.c
92 lines (80 loc) · 1.57 KB
/
builtin2.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
#include "headers.h"
int rm_func(char **argv)
{
if (argv[1] == NULL)
{
printf("rm: missing operand\n");
}
else
{
int val = remove(argv[1]);
if(val!=0)
{
printf("rm: cannot remove '%s': No such file \n",argv[1]);
}
}
return 1;
}
int rmdir_func(char **argv)
{
if (argv[1] == NULL)
{
printf("rmdir: missing operand\n");
}
else
{
int val = rmdir(argv[1]);
if(val!=0)
{
printf("rmdir: cannot remove '%s': No such directory\n",argv[1]);
}
}
return 1;
}
int pwd_func(char **argv)
{
char *temp= malloc(500 * sizeof(char));
if (temp != NULL)
{
if (getcwd(temp, 1024) != NULL)
{
printf("%s\n",temp);
}
else
{
printf("Failed to get the current working directory.\n");
}
free(temp);
}
else
{
printf("Memory allocation error.\n");
}
return 1;
}
int head_func(char **argv)
{
if (argv[1] == NULL)
{
printf("head: missing operand\n");
}
else
{
FILE *file = fopen(argv[1], "r");
if(file == NULL)
{
printf("Error in opening the file\n");
}
else
{
char line[256];
int count = 0;
while (fgets(line, 256, file) && count < 10)
{
printf("%s", line);
count++;
}
}
}
return 1;
}