-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconst.c
More file actions
58 lines (39 loc) · 1.12 KB
/
const.c
File metadata and controls
58 lines (39 loc) · 1.12 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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include "includes/globals.h"
#include "includes/readLine.h"
/* int open(const char *path, int oflag [, mode]);
ssize_t read(int fildes, void *buf, size_t nbyte);
ssize_t write(int fildes, const void *buf, size_t nbyte);
int close(int fildes); */
// argv[1] eh o 10
// minibuf e a:3:x:4
int main (int argc, char **argv) {
if (argc < 2) {
return EXIT_FAILURE;
}
char *input = argv[1];
char *minibuf = malloc(PIPE_BUF);
ssize_t bufferSize = PIPE_BUF - 1 - strlen(input); // Só podemos ler enquanto couber o que queremos adicionar à linha
ssize_t i;
while ( (i = readLine(0, minibuf, bufferSize)) > 0) {
minibuf[i - 1] = ':';
minibuf[i] = '\0';
strcat(minibuf, input);
ssize_t newLength = i + strlen(input);
minibuf[newLength] = '\n';
minibuf[newLength + 1] = '\0';
newLength++;
write(1, minibuf, newLength);
}
if (i < 0) {
fprintf(stderr, "(const) Error reading input: %s (readLine() returned %ld)\n", strerror(errno), i);
return EXIT_FAILURE;
} else {
return EXIT_SUCCESS;
}
}