-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.h
230 lines (196 loc) · 4.38 KB
/
file.h
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
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// function declarations
void create_file(char *file_name);
void read_file(char *file_name);
void write_file(char *file_name);
void info_file(char *file_name);
// driver code
void file(char *file_name, char *mode)
{
if (strcmp(mode, "create") == 0)
{
create_file(file_name);
}
else if (strcmp(mode, "write") == 0)
{
write_file(file_name);
}
else if (strcmp(mode, "read") == 0)
{
read_file(file_name);
}
else if (strcmp(mode, "info") == 0)
{
printf("\nInfo of the file:");
info_file(file_name);
}
else
{
printf("Wrong Mode Selected");
}
}
// function definitions
void create_file(char *file_name)
{
int fd;
int access_check = access(file_name, F_OK);
if (access_check == 0)
{
printf("\nFile with the name: %s already exists. \nPlease try a different name or use this one in future.", file_name);
return;
}
int cr = creat(file_name, O_RDWR | S_IRWXU | S_IRWXG | S_IRWXO);
if (cr < 0)
{
printf("\nError in creating the file %s.", file_name);
return;
}
else
{
printf("\nFile %s created successfully!!!", file_name);
}
close(cr);
}
void write_file(char *file_name)
{
int fd;
int access_check = access(file_name, F_OK);
if (access_check != 0)
{
printf("\nFile %s doesn't exists, please create one before.", file_name);
return;
}
access_check = access(file_name, W_OK);
if (access_check != 0)
{
printf("\nFile %s doesn't have write access.", file_name);
return;
}
printf("\nEnter the option in which you want to write to the file:");
printf("\n[1] Truncate");
printf("\n[2] Append");
printf("\n[3] Overwrite(Default)\n");
int choice;
int flag = O_WRONLY;
scanf("%d", &choice);
if (choice == 1)
flag = flag | O_TRUNC;
else if (choice == 2)
flag = flag | O_APPEND;
else if (choice == 3)
flag = O_WRONLY;
else
{
printf("\nEnter correct choice");
printf("\nUsing Default option");
}
fd = open(file_name, flag);
if (fd < 0)
{
printf("\nError opening the file %s in write mode", file_name);
return;
}
int nw;
printf("\nEnter the contents to be written in the file (Press # to stop writing)\n");
char c;
scanf("%c", &c);
while (c != '#')
{
nw = write(fd, &c, 1);
scanf("%c", &c);
if (nw <= 0)
{
printf("%s", "\nError! While writing the file");
close(fd);
return;
}
}
close(fd);
}
void read_file(char *file_name)
{
int fd;
int access_check = access(file_name, F_OK);
if (access_check != 0)
{
printf("\nFile %s doesn't exists, please create one before", file_name);
return;
}
access_check = access(file_name, R_OK);
if (access_check != 0)
{
printf("\nFile %s doesn't have read access", file_name);
return;
}
fd = open(file_name, O_RDONLY);
if (fd < 0)
{
printf("\nError opening the file %s in read mode.", file_name);
return;
}
int offset = 0;
int lk = 0;
printf("\nEnter the offset from which to read the file(Press 0 to read from start): ");
scanf("%d", &offset);
lk = lseek(fd, offset, SEEK_CUR);
// printf("Output of lseek %d", lk);
if (lk == -1)
{
printf("\nError!! While jumping the offset %d.", offset);
printf("\nReading from starting of the file");
lk = lseek(fd, 0, SEEK_SET);
}
char c;
int nr;
printf("\nContents of the file:\n");
do
{
nr = read(fd, &c, 1);
if (nr < 0)
{
printf("\nFile either ended or encountered an error.");
close(fd);
return;
}
printf("%c", c);
} while (nr > 0);
close(fd);
}
void info_file(char *file_name)
{
struct stat stats;
struct tm dt;
if (stat(file_name, &stats) == 0)
{
// File permissions
printf("\nFile access: ");
if (stats.st_mode & R_OK)
printf("read ");
if (stats.st_mode & W_OK)
printf("write ");
if (stats.st_mode & X_OK)
printf("execute");
// File size
printf("\nFile size: %ld", stats.st_size);
// Get file creation time in seconds and
// convert seconds to date and time format
dt = *(gmtime(&stats.st_ctime));
printf("\nCreated on: %d-%d-%d %d:%d:%d", dt.tm_mday, dt.tm_mon, dt.tm_year + 1900,
dt.tm_hour, dt.tm_min, dt.tm_sec);
// File modification time
dt = *(gmtime(&stats.st_mtime));
printf("\nModified on: %d-%d-%d %d:%d:%d", dt.tm_mday, dt.tm_mon, dt.tm_year + 1900,
dt.tm_hour, dt.tm_min, dt.tm_sec);
}
else
{
printf("Unable to get file properties.\n");
printf("Please check whether '%s' file exists.\n", file_name);
}
}