59 lines
2.1 KiB
C
59 lines
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
/**
|
|
* Uso: paste [OPÇÃO]... [ARQUIVO]...
|
|
* Escreve linhas constituídas das linhas sequencialmente correspondentes de
|
|
* cada ARQUIVO, separadas por tabulações, para a saída padrão.
|
|
*
|
|
* Se ARQUIVO não for especificado ou for -, lê a entrada padrão.
|
|
*
|
|
* Argumentos obrigatórios para opções longas também o são para opções curtas.
|
|
* -d, --delimiters=LISTA reutiliza caracteres da LISTA em vez de tabulações
|
|
* -s, --serial cola um arquivo por vez em de todos em paralelo
|
|
* -z, --zero-terminated delimitador de linha é NULO, não nova linha
|
|
* --help mostra esta ajuda e sai
|
|
* --version mostra informação da versão e sai
|
|
*
|
|
* Página de ajuda do GNU coreutils: <https://www.gnu.org/software/coreutils/>
|
|
* Relate erros de tradução para <https://translationproject.org/team/pt_BR.html>
|
|
* Documentação completa em <https://www.gnu.org/software/coreutils/paste>
|
|
* ou disponível localmente via: info "(coreutils) paste invocation"
|
|
*/
|
|
|
|
int main(int argc, char **argv) {
|
|
int i;
|
|
FILE **files = malloc((argc - 1) * sizeof(FILE *));
|
|
char *eofcur = malloc(argc * sizeof(char *));
|
|
char *eofend = malloc(argc * sizeof(char *));
|
|
for (i = 0; i < argc-1; i++) {
|
|
if (strcmp(argv[i+1], "-") == 0)
|
|
files[i] = stdin;
|
|
else
|
|
files[i] = fopen(argv[i+1], "r");
|
|
eofcur[i] = '0';
|
|
eofend[i] = '1';
|
|
}
|
|
char sep = '\t';
|
|
char buffer[BUFSIZ];
|
|
while (strcmp(eofcur, eofend) != 0) {
|
|
for (i = 0; i < argc-1; i++) {
|
|
if (fgets(buffer, BUFSIZ, files[i]) == NULL) {
|
|
strcpy(buffer, "\n");
|
|
// buffer[0] = '\n';
|
|
// buffer[1] = '\0';
|
|
eofcur[i] = '1';
|
|
if (strcmp(eofcur, eofend) == 0)
|
|
continue;
|
|
}
|
|
if (i < argc-2)
|
|
buffer[strcspn(buffer, "\n")] = sep;
|
|
printf("%s", buffer);
|
|
}
|
|
}
|
|
for (i = 0; i < argc-1; i++) {
|
|
fclose(files[i]);
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|