-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressor.c
44 lines (37 loc) · 989 Bytes
/
compressor.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
//
// Created by andgel on 13/02/2020
//
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
bool compress(char *path)
{
struct stat s;
FILE *stream = fopen(path, "r");
char *buffer;
if (stat(path, &s) == -1 || !stream)
return perror(path), false;
if (s.st_size % 2 == 1)
return fprintf(stderr, "%s: %s\n", path, "File size mush be even"), fclose(stream), false;
buffer = malloc(s.st_size);
fread(buffer, 1, s.st_size, stream);
fclose(stream);
for (int i = 0; i < s.st_size / 2; i++)
buffer[i] = buffer[i * 2];
stream = fopen(path, "w");
if (!stream)
return perror(path), free(buffer), false;
fwrite(buffer, 1, s.st_size / 2, stream);
fclose(stream);
free(buffer);
return true;
}
int main(int argc, char **argv)
{
for (int i = 1; i < argc; i++)
if (!compress(argv[i]))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}