forked from baskerville/sutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuq.c
More file actions
26 lines (24 loc) · 625 Bytes
/
uq.c
File metadata and controls
26 lines (24 loc) · 625 Bytes
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
/* Filter duplicate lines from standard input */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
int main(void) {
char cur[BUFSIZ] = {0};
char last[BUFSIZ] = {0};
int nr = 1, nw = 1;
while (nr > 0 && nw == nr) {
if ((nr = read(STDIN_FILENO, cur, sizeof(cur))) > 0) {
cur[nr] = 0;
if (strcmp(cur, last) != 0) {
nw = write(STDOUT_FILENO, cur, nr);
strcpy(last, cur);
}
}
}
if (nr > 0 && nw <= 0)
return EXIT_FAILURE;
else
return EXIT_SUCCESS;
}