-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeleteStudent.c
More file actions
73 lines (69 loc) · 2.2 KB
/
Copy pathDeleteStudent.c
File metadata and controls
73 lines (69 loc) · 2.2 KB
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
//DELETE STUDENT
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define MAX 256
void main(int argc, char* argv[]) {
char directory[MAX], command[MAX];
int exitCode;
pid_t pid;
if (argc != 2) {
write(2, "DeleteStudent: missing parameters!\n", strlen("DeleteStudent: missing parameters!\n"));
exit(2); //an error but not going to close the whole shell
}
//first going to check the permissions of the student's directory.
memset(directory, '\0', MAX);
strcpy(directory, "./student/");
strcat(directory, argv[1]);
//in case there are no permissions
chmod(directory, 0777);
//checking if there's a schedule file in the directory of the student
strcat(directory, "/Schedule.txt");
if (access(directory, F_OK) == 0) { //if exists
//calling the rm command from bin
pid = fork();
if (pid < 0) { //fork error
perror("Fork failure.\n");
exit(-1);
}
else if (pid == 0) { //child process
execlp("/bin/rm", "bin/rm", "-r", directory, NULL);
perror("Remove execution failure.\n");
exit(-1);
}
else { //parend process
waitpid(pid, &exitCode, 0);
}
}
//checking if there's a blocked file in the directory of the student
memset(directory, '\0', MAX);
strcpy(directory, "./student/");
strcat(directory, argv[1]);
strcat(directory, "/Blocked.txt");
if (access(directory, F_OK) == 0) { //if exists
//calling the rm command from bin
pid = fork();
if (pid < 0) { //fork error
perror("Fork failure.\n");
exit(-1);
}
else if (pid == 0) { //child process
execlp("/bin/rm", "bin/rm", "-r", directory, NULL);
perror("Remove execution failure.\n");
exit(-1);
}
else { //parend process
waitpid(pid, &exitCode, 0);
}
}
//now removing the directory
memset(directory, '\0', MAX);
strcpy(directory, "./student/");
strcat(directory, argv[1]);
if (rmdir(directory) == -1) {
write(2, "Removing directory error.\n", strlen("Removing directory error.\n"));
exit(-1);
}
}