-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.c
145 lines (134 loc) · 3.46 KB
/
student.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
144
145
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <dirent.h>
bool IsOccupied();
void viewAll();
void download(char filename[]);
void view_on_terminal(char filename[]);
void acquireLock();
void releaseLock();
char shared_location[] = "shared folder/";
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("View the available list of case studies : viewall\n");
printf("View the content of a file on the terminal : view <filename>\n");
printf("Download an available case study : download <filename>\n");
return 0;
}
if (argc == 2) //? Single argument provided
{
printf("Single argument provided \n");
printf("argument -> %s \n", argv[1]);
if (strcmp(argv[1], "viewall") == 0) //! Viewall function
{
puts("In viewall");
if(IsOccupied())
{
puts("Cant access directory ! Someone is using it \n");
}
else{
acquireLock();
viewAll();
releaseLock();
}
}
}
else if (argc == 3) //? Two argument provided
{
printf("Two argument provided \n");
printf("argument -> %s , %s \n", argv[1], argv[2]);
if (strcmp(argv[1], "download") == 0) //! Download function
{
puts("In download");
if(IsOccupied())
{
puts("Cant access directory Someone is using it \n");
}
else{
acquireLock();
download(argv[2]);
releaseLock();
}
}
if (strcmp(argv[1], "view") == 0) //! View function
{
puts("In view <filename> ");
if(IsOccupied())
{
puts("Cant access directory Someone is using it \n");
}
else{
acquireLock();
view_on_terminal(argv[2]);
releaseLock();
}
}
}
}
bool IsOccupied(){
FILE *file_pointer;
char msg[20];
file_pointer = fopen("shared folder/lock", "r");
fscanf(file_pointer, "%s", msg);
printf("Lock status -> %s \n", msg);
fclose(file_pointer);
if (strcmp(msg, "occupied") == 0){
return true;
}
else if (strcmp(msg, "available") == 0){
return false;
}
else{
printf("lock file corrupted \n");
return true;
}
}
void viewAll(){
DIR *d;
struct dirent *dir;
char path[1000] = "shared folder/contents/";
d = opendir(path);
char full_path[1000];
if (d){
while ((dir = readdir(d)) != NULL){
//Condition to check regular file.
if (dir->d_type == DT_REG){
full_path[0] = '\0';
strcat(full_path, dir->d_name);
printf("%s\n", full_path);
}
}
closedir(d);
}
}
void view_on_terminal(char filename[])
{
char path[] = "shared folder/contents/";
strcat(path , filename);
execl("/bin/cat" ,"cat" , path , (char *)0);
}
void download(char filename[])
{
printf("Downloading file -> %s \n", filename);
char path[] = "shared folder/contents/";
strcat(path , filename);
execl("/bin/cp", "-i", "-p",path , filename, (char *)0);
printf("Downloaded file -> %s \n", filename);
}
void acquireLock(){
FILE *file_pointer1;
file_pointer1 = fopen("shared folder/lock", "w");
fprintf(file_pointer1,"%s","occupied");
printf("Lock acquired \n");
fclose(file_pointer1);
}
void releaseLock(){
FILE *file_pointer2;
file_pointer2 = fopen("shared folder/lock", "w");
fprintf(file_pointer2,"%s","available");
printf("Lock released \n");
fclose(file_pointer2);
}