-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.c
More file actions
125 lines (87 loc) · 2.73 KB
/
filter.c
File metadata and controls
125 lines (87 loc) · 2.73 KB
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include "includes/globals.h"
#include "includes/readLine.h"
int main(int argc, char** argv) {
// Chamada com o formato "filter colunaA operador colunaB", exemplo: "filter 2 > 4"
// Operadores possíveis: =, >=, <=, >, <, !=
if (argc < 4) {
return EXIT_FAILURE;
}
char* end;
long columnA = strtol(argv[1], &end, 10);
char* operator = argv[2];
long columnB = strtol(argv[3], &end, 10);
// strtol error handling (strtol retorna 0 quando há um erro)
if ( (columnA == 0 || columnB == 0) && errno != 0 ) {
fprintf(stderr, "(filter) Error reading arguments: %s\n", strerror(errno));
return EXIT_FAILURE;
} else if (columnA < 1 || columnB < 1) {
fprintf(stderr, "(filter) Error reading arguments (column value < 1): %ld %ld\n", columnA, columnB);
return EXIT_FAILURE;
}
// Ler linhas do stdin
char buffer[PIPE_BUF];
char* columnAValue = (char*) malloc(PIPE_BUF);
char* columnBValue = (char*) malloc(PIPE_BUF);
ssize_t i;
while ( (i = readLine(0, buffer, (long) PIPE_BUF)) > 0) {
// Obter valores das colunas
if (
getElementValue(buffer, columnA, columnAValue, (long) PIPE_BUF) < 1 ||
getElementValue(buffer, columnB, columnBValue, (long) PIPE_BUF) < 1
) {
fprintf(stderr, "(filter) Error obtaining column values (getElementValue returned 0)");
return EXIT_FAILURE;
}
long columnAValueLong;
long columnBValueLong;
char* end2;
columnAValueLong = strtol(columnAValue, &end2, 10);
columnBValueLong = strtol(columnBValue, &end2, 10);
if ( (columnAValueLong == 0 || columnBValueLong == 0) && errno != 0 ) {
fprintf(stderr, "(filter) Error converting column values to long (strtol returned 0 and set errno)");
return EXIT_FAILURE;
}
// Determinar operador a usar
int echoLine = 0;
if (strcmp(operator, "<") == 0) {
if (columnAValueLong < columnBValueLong) {
echoLine = 1;
}
} else if (strcmp(operator, "<=") == 0) {
if (columnAValueLong < columnBValueLong) {
echoLine = 1;
}
} else if (strcmp(operator, ">") == 0) {
if (columnAValueLong > columnBValueLong) {
echoLine = 1;
}
} else if (strcmp(operator, ">=") == 0) {
if (columnAValueLong >= columnBValueLong) {
echoLine = 1;
}
} else if (strcmp(operator, "!=") == 0) {
if (columnAValueLong != columnBValueLong) {
echoLine = 1;
}
} else if (strcmp(operator, "=") == 0) {
if (columnAValueLong == columnBValueLong) {
echoLine = 1;
}
}
if (echoLine) {
write(1, buffer, i);
}
}
if (i < 0) {
fprintf(stderr, "(filter) Error reading input: %s (read() returned %ld)\n", strerror(errno), i);
return EXIT_FAILURE;
} else {
return EXIT_SUCCESS;
}
}