-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths6.c
142 lines (132 loc) · 4.55 KB
/
s6.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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
typedef struct {
char numeFisier[BUFSIZ], drepturiUser[BUFSIZ], drepturiGrup[BUFSIZ], drepturiAltii[BUFSIZ];
int dimensiune, inaltime, lungime, userId, nrLegaturi;
time_t timpUltimaModificare;
} ReqStats;
static void getBMPFileStats(int in, char* filename, ReqStats* reqStats)
{
struct stat inStat;
int buffer;
/// Copy filename
strcpy(reqStats->numeFisier, filename);
/// Dimensiune
lseek(in, 18, SEEK_SET);
read(in, &buffer, 4);
reqStats->dimensiune = buffer;
/// Lungime
read(in, &buffer, 4);
reqStats->lungime = buffer;
/// Inaltime
read(in, &buffer, 4);
reqStats->inaltime = buffer;
/// Get other stats with fstat
fstat(in, &inStat);
reqStats->nrLegaturi = inStat.st_nlink;
reqStats->userId = inStat.st_uid;
reqStats->timpUltimaModificare = inStat.st_mtime;
/// Is dir
strcpy(reqStats->drepturiUser, S_ISDIR(inStat.st_mode) ? "d" : "-");
/// User rights
strcat(reqStats->drepturiUser, (inStat.st_mode & S_IRUSR) ? "r" : "-");
strcat(reqStats->drepturiUser, (inStat.st_mode & S_IWUSR) ? "w" : "-");
strcat(reqStats->drepturiUser, (inStat.st_mode & S_IXUSR) ? "x" : "-");
/// Group rights
strcat(reqStats->drepturiGrup, (inStat.st_mode & S_IRGRP) ? "r" : "-");
strcat(reqStats->drepturiGrup, (inStat.st_mode & S_IWGRP) ? "w" : "-");
strcat(reqStats->drepturiGrup, (inStat.st_mode & S_IXGRP) ? "x" : "-");
/// Others rights
strcat(reqStats->drepturiAltii, (inStat.st_mode & S_IROTH) ? "r" : "-");
strcat(reqStats->drepturiAltii, (inStat.st_mode & S_IWOTH) ? "w" : "-");
strcat(reqStats->drepturiAltii, (inStat.st_mode & S_IXOTH) ? "x" : "-");
return;
}
static int printfBMPFileStats(int out, ReqStats stats)
{
char buffer[BUFSIZ];
/// Nume fisier
sprintf(buffer, "Nume fisier: %s.\n", stats.numeFisier);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Dimensiune
sprintf(buffer, "Dimensiune: %d.\n", stats.dimensiune);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Lungime
sprintf(buffer, "Lungime: %d.\n", stats.lungime);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Inaltime
sprintf(buffer, "Inaltime: %d.\n", stats.inaltime);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// User ID
sprintf(buffer, "User ID: %d.\n", stats.userId);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Numar legaturi
sprintf(buffer, "Numar legaturi: %d.\n", stats.nrLegaturi);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Last modified time
sprintf(buffer, "Timpul ultimei modificari: %li.\n", stats.timpUltimaModificare);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Drepturi de acces user
sprintf(buffer, "Drepturi de acces user: %s.\n", stats.drepturiUser);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Drepturi de acces grup
sprintf(buffer, "Drepturi de acces grup: %s.\n", stats.drepturiGrup);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
/// Drepturi de acces altii
sprintf(buffer, "Drepturi de acces altii: %s.\n", stats.drepturiAltii);
write(out, buffer, strlen(buffer));
strcpy(buffer, "");
return 0;
}
int main(int argc, char **argv)
{
/// Check args
if (argc != 2)
{
fprintf(stderr, "Usage: %s <input_file>.\n", argv[0]);
exit(EXIT_FAILURE);
}
if (strstr(argv[1], ".bmp") == NULL)
{
fprintf(stderr, "Given argument does not point to a .bmp file : %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
fprintf(stdout, "Given file path : %s.\n", argv[1]);
/// Open required files
/// Input bmp file
int input_fd = open(argv[1], O_RDONLY);
if (input_fd == -1)
{
fprintf(stderr, "Could not open input file.\n");
exit(EXIT_FAILURE);
}
/// Output txt file
int output_fd = open("statistica.txt", O_WRONLY);
if (output_fd == -1)
{
fprintf(stderr, "Could not open output file.\n");
exit(EXIT_FAILURE);
}
/// Get stats
ReqStats reqStats;
getBMPFileStats(input_fd, argv[1], &reqStats);
printfBMPFileStats(output_fd, reqStats);
close(input_fd);
close(output_fd);
return 0;
}