summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-04-28 15:13:10 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-04-28 15:13:10 -0400
commit7a1eef12299f4b030bfb77d9bbb133d0ba2ee674 (patch)
tree61d7008b25e37799318db4030b86f85fc0059275 /src/commands
parentfix missing newline on single row output (diff)
downloadlazysphere-7a1eef12299f4b030bfb77d9bbb133d0ba2ee674.tar.gz
lazysphere-7a1eef12299f4b030bfb77d9bbb133d0ba2ee674.tar.bz2
lazysphere-7a1eef12299f4b030bfb77d9bbb133d0ba2ee674.zip
add tail
Diffstat (limited to '')
-rw-r--r--src/commands/cat.c11
-rw-r--r--src/commands/tail.c80
2 files changed, 91 insertions, 0 deletions
diff --git a/src/commands/cat.c b/src/commands/cat.c
index f5622e8..8e22706 100644
--- a/src/commands/cat.c
+++ b/src/commands/cat.c
@@ -1,7 +1,9 @@
#include "../command.h"
#include <ctype.h>
+#include <errno.h>
#include <string.h>
+#include <sys/stat.h>
struct Flags {
bool number_lines;
@@ -138,6 +140,15 @@ COMMAND(cat) {
continue;
} else {
files = true;
+ struct stat s;
+ if (stat(argv[i], &s) < 0) {
+ printf("error: failed to read %s: %s\n", argv[i], strerror(errno));
+ continue;
+ }
+ if (!S_ISREG(s.st_mode)) {
+ printf("error: %s is not a file\n", argv[i]);
+ continue;
+ }
FILE* in = get_file(argv[i], "r");
cat_file(in, flags);
fclose(in);
diff --git a/src/commands/tail.c b/src/commands/tail.c
new file mode 100644
index 0000000..04f7f42
--- /dev/null
+++ b/src/commands/tail.c
@@ -0,0 +1,80 @@
+#include "../command.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+
+static void tail_file(FILE* file) {
+ char* ring[10];
+ memset(ring, 0, sizeof(char*) * 10);
+
+ int ring_len[10];
+
+ int index = 0;
+ int size = 0;
+
+ int read;
+ size_t len = 0;
+ char* line = NULL;
+ while ((read = getline(&line, &len, file)) != -1) {
+
+ if (ring[index] != NULL) free(ring[index]);
+ ring[index] = line;
+ ring_len[index] = read;
+
+ index++;
+ index %= 10;
+ if (size < 10) size++;
+
+ line = NULL;
+ }
+
+ index += 10 - size;
+ index %= 10;
+
+ for (int i = 0; i < size; i++) {
+ fwrite(ring[index], ring_len[index], 1, stdout);
+ free(ring[index]);
+ index += 1;
+ index %= 10;
+ }
+
+ free(line);
+ fclose(file);
+}
+
+static void help() {
+ printf("Usage: tail [FILE]...\n\n");
+ printf("Print last 10 lines of FILEs (or stdin) to.\n");
+ printf("With more than one FILE, precede each with a filename header.\n");
+ exit(EXIT_SUCCESS);
+}
+
+COMMAND(tail) {
+ if (argc < 1) {
+ tail_file(stdin);
+ return EXIT_SUCCESS;
+ }
+
+ if (streql(argv[0], "--help")) help();
+
+ if (argc == 1) {
+ FILE* file = get_file(argv[0], "r");
+ tail_file(file);
+ return EXIT_SUCCESS;
+ }
+
+ for (int i = 0; i < argc; i++) {
+ FILE* file;
+ if (streql("-", argv[i])) {
+ file = stdin;
+ } else {
+ file = get_file_s(argv[i], "r");
+ if (file == NULL) continue;
+ }
+ printf("\n==> %s <==\n", argv[i]);
+ tail_file(file);
+ }
+
+ return EXIT_SUCCESS;
+}