This commit is contained in:
Your Name 2023-11-24 11:38:47 +03:00
parent 1eb916455b
commit a96e514c6a
4 changed files with 14 additions and 31 deletions

1
TODO
View File

@ -3,7 +3,6 @@ With "micro-" prefix
*Todo:
**dmesg (portable)
**head
**cat
tail
expr
uniq

View File

@ -1,7 +1,7 @@
#ifndef _CONFIG_H
#define _CONFIG_H
/* (tee wc) */
/* (cat tee wc) */
#define BUF_SIZE 4096
/* Random source (shred) */

View File

@ -3,50 +3,34 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
unsigned int n_flag;
#include "config.h"
int cat(const char *path) {
FILE *fp = NULL;
int fd = STDIN_FILENO;
if (!strcmp(path, "-"))
fp = stdin;
if (strcmp(path, "-"))
fd = open(path, O_RDONLY);
else
fp = fopen(path, "r");
if (fp == NULL) {
if (fd < 0) {
fprintf(stderr, "cat: %s: %s\n", path, strerror(errno));
return 1;
}
char *buf = NULL;
size_t len = 0;
char buf[BUF_SIZE + 1];
off_t len = 0;
while ((len = read(fd, buf, sizeof(buf))) > 0)
write(STDOUT_FILENO, buf, len);
size_t line = 0;
while (getline(&buf, &len, fp) != EOF) {
line++;
if (n_flag)
printf("\t%zu\t%s", line, buf);
else
printf("%s", buf);
}
free(buf);
close(fd);
return 0;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "n")) != -1) {
while ((opt = getopt(argc, argv, "")) != -1) {
switch (opt) {
case 'n':
n_flag = 1;
break;
default:
printf("cat [file1 file2]\n\t[-n number all output lines]\n");
printf("cat [file1 file2]\n");
return 0;
}
}

View File

@ -21,7 +21,7 @@ int main(int argc, char **argv) {
break;
default:
printf("truncate [file]\n\t[-c Do not create files] [-s=SIZE]\n");
printf("truncate [file]\n\t[-c Do not create files] [-s SIZE]\n");
return 0;
}
}