-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeze.c
143 lines (140 loc) · 4.8 KB
/
Freeze.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
//FREEZE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#define MAX 256
#define ID 9
void main(int argc, char* argv[]) {
int i, itsAnId = 1, stdFile, blocked;
char directory[MAX], id[ID + 1], buffer[MAX], ch;
if (argc != 2) {
write(2, "Freeze: missing parameters!\n", strlen("Freeze: missing parameters!\n"));
exit(2); //an error but not going to close the whole shell
}
//first checking if the given argument is an id number
if (strlen(argv[1]) == 9) {
for (i = 0; i < ID; i++) {
if (argv[1][i] < '0' || argv[1][i] > '9') {
itsAnId = 0;
break;
}
}
}
else //can't be an idea with less than 9 digits.
itsAnId = 0;
//if itsAnId = 1 >> argv[1] holds the ID number of the student
if (itsAnId == 1) {
strcpy(id, argv[1]);
}
else {
//if not >> looking for the username in the students file
if ((stdFile = open("std_pass.txt", O_RDONLY)) == -1) {
write(2, "Opening students file failure.\n", strlen("Opening students file failure.\n"));
exit(-1);
}
//moving to the second line (the first line is the title of the table - not relevant)
while (read(stdFile, &ch, 1) == 1)
{
if (ch == '\n')
break;
}
//now moving to the student's username >> must exist cause tried to log into stdShell
while (1) {
memset(buffer, '\0', MAX);
if (read(stdFile, buffer, strlen(argv[1])) == -1) {
write(2, "Reading students file failure.\n", strlen("Reading students file failure.\n"));
exit(-1);
}
if (strcmp(buffer, argv[1]) == 0)
break;
else {
//moving to the next line
while (1) {
if (read(stdFile, &ch, 1) == -1) {
write(2, "Reading students file failure.\n", strlen("Reading students file failure.\n"));
exit(-1);
}
if (ch == '\n')
break;
}
}
}
//moving to the ID column and reading the ID
i = 0; //counting two columns, from username to password (1), and than from password to id number (2)
while (1) {
if (read(stdFile, &ch, 1) == -1) {
perror("Reading students file failure.\n");
exit(-1);
}
if (ch == ' ') //between columns
continue;
else {
i++;
if (i == 2)
break;
while (1) { //if i < 2, moving to the next column
if (read(stdFile, &ch, 1) == -1) {
perror("Reading students file failure.\n");
exit(-1);
}
if (ch != ' ')
continue;
else
break;
}
}
}
//now reading the id
lseek(stdFile, -1, 1);
memset(id, '\0', MAX);
if (read(stdFile, id, 9) == -1) {
perror("Reading students file failure.\n");
exit(-1);
}
id[10] = '\0';
close(stdFile);
}
//now getting the directory path of the student
strcpy(directory, "./student/");
strcat(directory, id);
//checking if the student already blocked
if (chmod(directory, 0777) == -1) {
write(2, "Changing mode failure.\n", strlen("Changing mode failure.\n"));
exit(-1);
}
strcat(directory, "/Blocked.txt");
if (access(directory, F_OK) == 0) { //student is already blocked
memset(directory, '\0', MAX);
strcpy(directory, "./student/");
strcat(directory, id);
if (chmod(directory, 0000) == -1) {
write(2, "Changing mode failure.\n", strlen("Changing mode failure.\n"));
exit(-1);
}
exit(0);
}
else { //adding Blocked file to the directory
if ((blocked = open(directory, O_WRONLY | O_CREAT, 0777)) == -1) {
write(2, "Opening blocked file failure.\n", strlen("Opening blocked file failure.\n"));
exit(-1);
}
write(blocked, "Blocked", 7);
close(blocked);
//changing the permission mode of the student's path
memset(directory, '\0', MAX);
strcpy(directory, "./student/");
strcat(directory, id);
if (chmod(directory, 0000) == -1) {
write(2, "Changing mode failure.\n", strlen("Changing mode failure.\n"));
exit(-1);
}
else {
exit(0);
}
}
}