-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
96 lines (91 loc) · 1.99 KB
/
utils.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
#include "utils.h"
#include <string.h>
#include <fcntl.h>
int get_flags(const char *mode)
{
int flags=0;
// deschide fisierul pentru citire. Esueaza daca nu exista
if (strcmp(mode, "r") == 0)
{
flags=O_RDONLY;
}
//deschide fisierul pentru scriere. Daca nu exista, creeaza. Daca exista, truncheaza la 0
if (strcmp(mode, "w") == 0)
{
flags=O_WRONLY|O_CREAT|O_TRUNC;
}
//deschide in append(scriere la final).Daca nu exista,creeaza
if (strcmp(mode, "a") == 0)
{
flags=O_WRONLY|O_CREAT|O_APPEND;
}
//deschide pentru citire si scriere. Esueaza daca nu exista
if (strcmp(mode, "r+") == 0)
{
flags=O_RDWR;
}
//deschide pentru citire si scriere. Daca nu exista,creeaza. Daca exista, truncheaza la 0
if (strcmp(mode, "w+") == 0)
{
flags=O_RDWR|O_CREAT|O_TRUNC;
}
//deschide in append + read. Daca nu exista,creeaza
if (strcmp(mode, "a+") == 0)
{
flags=O_RDWR|O_CREAT|O_APPEND;
}
return flags;
}
int get_filemode(const char* mode)
{
int filemode=-1;
if((strcmp(mode, "w") == 0)||(strcmp(mode, "a") == 0)||(strcmp(mode, "w+") == 0)||(strcmp(mode, "a+") == 0))
{
filemode=0644; //permisiunile implicite
}
return filemode;
}
OPENMODE get_open_mode(const char* mode)
{
if (strcmp(mode, "r") == 0)
{
return r;
}
if (strcmp(mode, "w") == 0)
{
return w;
}
if (strcmp(mode, "a") == 0)
{
return a;
}
if (strcmp(mode, "r+") == 0)
{
return rplus;
}
if (strcmp(mode, "w+") == 0)
{
return wplus;
}
if (strcmp(mode, "a+") == 0)
{
return aplus;
}
return unset;
}
int is_read_flag_on(OPENMODE mode)
{
if(mode==r || mode==rplus || mode==aplus||mode==wplus)
{
return 1;
}
return 0;
}
int is_write_flag_on(OPENMODE mode)
{
if(mode==w || mode==wplus || mode==aplus||mode==a||mode==rplus)
{
return 1;
}
return 0;
}