commit dd4d24115238100107963f7071b070a2a1393072 Author: NRZ Code Date: Sat Jun 21 13:58:24 2025 -0300 Initial commit diff --git a/paste.c b/paste.c new file mode 100644 index 0000000..f5eaf11 --- /dev/null +++ b/paste.c @@ -0,0 +1,59 @@ +#include +#include +#include +/** + * 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: + * Relate erros de tradução para + * Documentação completa em + * 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; +} +