-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiles.c
More file actions
480 lines (395 loc) · 11 KB
/
files.c
File metadata and controls
480 lines (395 loc) · 11 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "files.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <pwd.h>
#include <unistd.h>
bool createNode(tPos *p){
*p = malloc(sizeof(struct tNode)); //allocates memory for a new node
return *p != NULL; //if memory allocation was successful return true, else return false
}
bool createNodem(mPos *p){
*p = malloc(sizeof(struct mNode)); //allocates memory for a new node
return *p != NULL; //if memory allocation was successful return true, else return false
}
bool createNoded(dPos *p){
*p = malloc(sizeof(struct mNode)); //allocates memory for a new node
return *p != NULL; //if memory allocation was successful return true, else return false
}
void createfilelist (tfilelist *l){
*l = NULL;
}
bool isemptyfiles (tfilelist L){
return (L==NULL); // if the first element is null, it is empty
}
bool addfile(char *filename, int df, int mode, tfilelist *L) {
tPos q, p;
// Create a new node
if (!createNode(&q)) // if malloc failed
return false;
// Initialize the new node's data
strcpy(q->data.filename, filename);
q->data.descriptor = df;
q->data.mode = mode;
q->next = NULL;
// If the list is empty, insert at the front
if (*L == NULL) {
*L = q;
}
//Inserting at the end
else{
p = *L;
while(p->next != NULL && p->next->data.descriptor < df){
p=p->next;
}
q->next = p->next;
p->next = q;
}
return true;
}
tPos findfile(int df, tfilelist L) {
tPos p = L;
int i = 0;
while (p != NULL) {
if (p->data.descriptor == df) {
return p;
}
p = p->next;
i++;
}
return NULL;
}
void closefile (tPos p, tfilelist* L) {
tPos q;
if(p==*L) //delete the first
*L = (*L)->next;
else if (p->next == NULL) { // Deleting the last element
for (q = *L; q->next != p; q = q->next);
q->next = NULL;
} else { //deleting from the middle
q=p->next;
p->data = q->data;
p->next = q->next;
p=q; //get rid of the duplicated node
}
free(p);
}
bool initfilelist(tfilelist *L){
return (addfile("stdin", 0, fcntl(0,F_GETFL), L) && addfile("stdout", 1, fcntl(1,F_GETFL), L) && addfile("stderr", 2, fcntl(2,F_GETFL), L));
}
void printopenfiles(tfilelist l) {
tPos p;
if (!isemptyfiles(l)) {
for (p = l; p != NULL; p = p->next) {
printf("Name: %s\tDescriptor: %d\tMode: %d\n", p->data.filename, p->data.descriptor, p->data.mode);
}
} else printf("File list is empty");
}
void FreeFileList(tfilelist l) {
tPos p;
while (l != NULL) {
p = l;
l = l->next;
free(p);
}
}
// ----------------------------------------memory list------------------------------------------------
void creatememlist (tmemlist *m){
*m = NULL;
}
bool isemptymem (tmemlist m){
return m == NULL;
}
void printmemory(tmemlist m, char *method) {
mPos p;
printf("******Assigned blocks list for the process %d\n", getpid());
if (strcmp(method, "all") == 0) {
for (p = m; p != NULL; p = p->next) {
if (strcmp(p->data.method, "shared") == 0) {
// Special formatting for 'shared' method
printf("%p %4ld %s %s (key %d) ", p->data.adress, p->data.size, p->data.date, p->data.method, p->data.kdf);
} else {
printf("%p %4ld %s %s ", p->data.adress, p->data.size, p->data.date, p->data.method);
}
// Print additional fields if present
if (p->data.kdf != -1 && strcmp(p->data.method, "shared")) {
printf(" %d", p->data.kdf);
}
if (*p->data.filename != '\0') {
printf("%s", p->data.filename);
}
printf("\n");
}
} else {
for (p = m; p != NULL; p = p->next) {
if (strcmp(p->data.method, method) == 0) {
// Print the memory block for the selected method
if (strcmp(p->data.method, "shared") == 0) {
printf("%p %4ld %s %s (key %d) ", p->data.adress, p->data.size, p->data.date, p->data.method, p->data.kdf);
} else {
printf("%p %4ld %s %s ", p->data.adress, p->data.size, p->data.date, p->data.method);
}
if (p->data.kdf != -1 && strcmp(p->data.method, "shared")) {
// Print the KDF value if it's not -1
printf("%d ", p->data.kdf);
}
if (*p->data.filename != '\0') {
// Print filename if it's not empty
printf("%s ", p->data.filename);
}
}
printf("\n");
}
}
}
bool addmem(void *address, long size,char* date,char *method, char *filename, int kdf, tmemlist *m) {
mPos q, p;
if(address == NULL)
return false;
if (!createNodem(&q) || q == NULL) {
fprintf(stderr, "Error: Could not create node\n");
return false;
}
if (method != NULL) {
strncpy(q->data.method,method,MAX-1);
q->data.method[MAX - 1] = '\0';
} else {
q->data.method[0] = '\0';
}
if (date != NULL) {
strncpy(q->data.date, date, MAX - 1);
q->data.date[MAX - 1] = '\0';
} else {
q->data.date[0] = '\0';
}
if (filename != NULL) {
strncpy(q->data.filename, filename, MAX - 1);
q->data.filename[MAX - 1] = '\0';
} else {
q->data.filename[0] = '\0';
}
q->data.adress = address;
q->data.size = size;
q->data.kdf = kdf == 0 ? -1 : kdf;
q->next = NULL;
if (*m == NULL) {
*m = q;
return true;
}
p = *m;
while (p->next != NULL) {
p = p->next;
}
p->next = q;
return true;
}
mPos findmemad(void* adress,tmemlist *m) {
mPos p = *m;
while(p != NULL && p->data.adress != adress){
p = p->next;}
return p;
}
mPos findmemsz(int size,tmemlist *m){
mPos p = *m;
while(p != NULL && p->data.size != (long )size)
p = p->next;
return p;
}
mPos findmemsh(int key,tmemlist *m){
mPos p = *m;
while(p != NULL && p->data.kdf != key)
p = p->next;
return p;
}
mPos findmemfl(char *filename,tmemlist *m){
mPos p = *m;
while(p != NULL && strcmp(p->data.filename,filename))
p = p->next;
return p;
}
void closemem(mPos p, tmemlist *m) {
mPos q;
if (p == *m) { // Deleting the first node
*m = (*m)->next;
free(p);
} else {
for (q = *m; q != NULL && q->next != p; q = q->next);
if (q != NULL) {
q->next = p->next;
free(p);
}
}
}
void FreeMemList(tmemlist m){
mPos p,q;
p = m;
while(m!=NULL){
q = p;
p=p->next;
closemem(q,&m);
}
}
//-----------------------------------------process list----------------------------
void createproclist(tproclist *plist){
*plist = NULL;
}
bool isemptyproc(tproclist plist){
return plist == NULL;
}
bool addproc(int pid,char* date,char *comline, tproclist *plist){
pPos q = (pPos)malloc(sizeof(struct pNode)), p;
if (q == NULL) {
perror("Memory allocation failed");
return false;
}
q->data.PID = pid;
strncpy(q->data.date, date, sizeof(q->data.date) - 1);
q->data.state = ACTIVE;
strncpy(q->data.comline, comline, sizeof(q->data.comline) - 1);
q->next = NULL;
if (plist == NULL) {
*plist = q;
return true;
}
p = *plist;
while (p->next != NULL) {
p = p->next;
}
p->next = q;
return true;
}
pPos findproc(int pid, tproclist *plist){
pPos p = *plist;
while(p != NULL && p->data.PID != pid)
p = p->next;
return p;
}
void freeproclist(tproclist plist){
pPos q;
if(plist == NULL)
return;
while(plist->next != NULL){
q = plist;
free(q);
plist = plist->next;
}
free(plist);
}
char* getusr(int pid){
char path[256];
char line[256];
struct stat fileStat;
FILE *file;
int uid = -1;
snprintf(path,sizeof(path),"/proc/%d/status",pid);
file = fopen(path, "r");
if (file == NULL) {
perror("Error opening process status file");
return "****";
}
while (fgets(line, sizeof(line), file)) {
if (lstat(path, &fileStat) == -1) { //stat gives us information aboutr the file in question
return "error";
}
}
fclose(file);
if (uid == -1) {
printf("Could not find UID for process %d\n", pid);
return "****";
}
if (lstat(path, &fileStat) == -1) { //stat gives us information aboutr the file in question
return "error";
}
struct passwd *pws = getpwuid(fileStat.st_uid);//
if (pws == NULL) {
fprintf(stderr, "No user found for UID: %d\n", uid);
return strdup("UNKNOWN"); // Return a dynamically allocated fallback
}
else
return strdup(pws->pw_name);
}
void printproclist(tproclist plist){
pPos p;
if (!isemptyproc(plist)) {
for (p = plist; p != NULL; p = p->next) {
printf("%d %s p=%d %s %d %s %s\n", p->data.PID,getusr(p->data.PID),getpriority(PRIO_PROCESS,p->data.PID),p->data.date,p->data.state,p->data.comline,"command_name");
}
}
}
//-------------------Search list-------------
dPos SearchListFirst(tdirlist list){
return list;
}
dPos SearchListNext(dPos pos){
return pos->next;
}
void createdirlist(tdirlist *dlist){
*dlist = NULL;
}
bool isemptydir(tdirlist dlist){
return dlist == NULL;
}
bool adddir(tdirectory data, tdirlist *dlist) {
dPos q = (dPos)malloc(sizeof(*q));
if (q == NULL) {
return false;
}
q->data = data;
q->next = NULL;
if (*dlist == NULL) {
*dlist = q;
return true;
}
dPos p = *dlist;
while (p->next != NULL) {
p = p->next;
}
p->next = q;
return true;
}
dPos finddir(char* dir,tdirlist *dlist) {
dPos p = *dlist;
while (p != NULL && strcmp(p->data.dirname, dir))
p = p->next;
return p;
}
void printdir(tdirlist dlist){
dPos p;
if(!isemptydir(dlist)){
for (p = dlist; p != NULL; p = p->next) {
printf("%s", p->data.dirname);
}
}
}
bool removedir(dPos p,tdirlist *dlist) {
dPos q;
if (p == *dlist) { // Deleting the first node
*dlist = (*dlist)->next;
free(p);
return true;
} else {
for (q = *dlist; q != NULL && q->next != p; q = q->next);
if (q != NULL) {
q->next = p->next;
free(p);
return true;
}
}
return false;
}
void freedirlist(tdirlist dlist){
dPos q;
if(dlist == NULL)
return;
while(dlist->next != NULL){
q = dlist;
free(q);
dlist = dlist->next;
}
free(dlist);
}