summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile18
-rwxr-xr-xbin/catbin0 -> 15728 bytes
-rwxr-xr-xbin/catplan9bin0 -> 12888 bytes
-rwxr-xr-xbin/cpbin0 -> 13024 bytes
-rwxr-xr-xbin/echobin0 -> 11912 bytes
-rwxr-xr-xbin/headbin0 -> 12720 bytes
-rwxr-xr-xbin/tacbin0 -> 13224 bytes
-rwxr-xr-xbin/tailbin0 -> 13288 bytes
-rwxr-xr-xbin/touchbin0 -> 12328 bytes
-rw-r--r--do.txt28
-rw-r--r--src/cat.c45
-rw-r--r--src/cp.c28
-rw-r--r--src/echo.c11
-rw-r--r--src/head.c49
-rw-r--r--src/tac.c58
-rw-r--r--src/tail.c59
-rw-r--r--src/touch.c28
-rw-r--r--test27
-rw-r--r--todo.txt2
19 files changed, 353 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ddc169b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+CC = tcc
+CFLAGS = -Wall -g
+#CC = gcc
+#CFLAGS = -Wall -Wextra
+LDFLAGS =
+SRC = src
+BIN = bin
+
+SRCS=$(wildcard $(SRC)/*.c)
+BINS=$(SRCS:$(SRC)/%.c=%)
+
+all: $(BINS)
+
+%: $(SRC)/%.c
+ $(CC) $(CFLAGS) -o $(BIN)/$@ $<
+
+clean:
+ rm -r $(BIN)/*
diff --git a/bin/cat b/bin/cat
new file mode 100755
index 0000000..95c49db
--- /dev/null
+++ b/bin/cat
Binary files differ
diff --git a/bin/catplan9 b/bin/catplan9
new file mode 100755
index 0000000..35d66a3
--- /dev/null
+++ b/bin/catplan9
Binary files differ
diff --git a/bin/cp b/bin/cp
new file mode 100755
index 0000000..e5b3bf6
--- /dev/null
+++ b/bin/cp
Binary files differ
diff --git a/bin/echo b/bin/echo
new file mode 100755
index 0000000..1344bcf
--- /dev/null
+++ b/bin/echo
Binary files differ
diff --git a/bin/head b/bin/head
new file mode 100755
index 0000000..08aef70
--- /dev/null
+++ b/bin/head
Binary files differ
diff --git a/bin/tac b/bin/tac
new file mode 100755
index 0000000..dc2dda2
--- /dev/null
+++ b/bin/tac
Binary files differ
diff --git a/bin/tail b/bin/tail
new file mode 100755
index 0000000..b5f2a26
--- /dev/null
+++ b/bin/tail
Binary files differ
diff --git a/bin/touch b/bin/touch
new file mode 100755
index 0000000..69f0d0b
--- /dev/null
+++ b/bin/touch
Binary files differ
diff --git a/do.txt b/do.txt
new file mode 100644
index 0000000..a8ababa
--- /dev/null
+++ b/do.txt
@@ -0,0 +1,28 @@
+cp
+dd
+df
+dir
+ln
+ls
+mkdir
+mv
+rm
+rmdir
+base32
+base64
+wc
+arch
+basename
+date
+du
+false
+pwd
+true
+whoami
+yes
+less
+more
+ascii
+awk
+grep
+cd
diff --git a/src/cat.c b/src/cat.c
new file mode 100644
index 0000000..b89cba6
--- /dev/null
+++ b/src/cat.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define PROGRAM_NAME "cat"
+
+struct stat statbuf;
+
+int main(int argc, char* argv[]) {
+ FILE *fileptr = NULL;
+ char buf[8192];
+ char ch;
+
+ if (argc < 2) {
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ printf("%s", buf);
+ }
+
+ return 0;
+ }
+
+ if (access(argv[1], F_OK) != 0) {
+ printf("%s: %s: No such file or directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ if (stat(argv[1], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
+ printf("%s: %s is a directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ fileptr = fopen(argv[1], "r");
+
+ if (fileptr == NULL){
+ perror("cat");
+ return 1;
+ }
+
+ while ((ch = fgetc(fileptr)) != EOF) {
+ putchar(ch);
+ }
+
+ fclose(fileptr);
+ return 0;
+}
diff --git a/src/cp.c b/src/cp.c
new file mode 100644
index 0000000..573677d
--- /dev/null
+++ b/src/cp.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+
+int main(int argc, char* argv[]) {
+ FILE *input, *output;
+ char ch;
+
+ if (argv[2] == NULL) {
+ printf("specify a destination\n");
+ return 1;
+ }
+
+ input = fopen(argv[1], "r");
+ output = fopen(argv[2], "a");
+
+ if (input == NULL || output == NULL) {
+ printf("failed to open the file dingus\n");
+ return 1;
+ } else {
+ while ((ch = fgetc(input)) != EOF) {
+ fputc(ch, output);
+ }
+ }
+
+ fclose(input);
+ fclose(output);
+
+ return 0;
+}
diff --git a/src/echo.c b/src/echo.c
new file mode 100644
index 0000000..ad853f1
--- /dev/null
+++ b/src/echo.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+int main(int argc, char *argv[]) {
+ for (int i=1; i < argc; i++) {
+ printf("%s ", argv[i]);
+ }
+
+ printf("\n");
+
+ return 0;
+}
diff --git a/src/head.c b/src/head.c
new file mode 100644
index 0000000..d669545
--- /dev/null
+++ b/src/head.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define PROGRAM_NAME "head"
+
+struct stat statbuf;
+
+int main(int argc, char* argv[]) {
+ FILE *fileptr = NULL;
+ char buf[8192];
+ char ch;
+ int lines = 0;
+
+ if (argc < 2) {
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ printf("%s", buf);
+ }
+
+ return 0;
+ }
+
+ if (access(argv[1], F_OK) != 0) {
+ printf("%s: %s: No such file or directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ if (stat(argv[1], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
+ printf("%s: %s is a directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ fileptr = fopen(argv[1], "r");
+
+ if (fileptr == NULL) {
+ perror("head");
+ return 1;
+ }
+
+ while (((ch = fgetc(fileptr)) != EOF) && (lines < 10)) {
+ if (ch == '\n') {
+ lines++;
+ }
+ putchar(ch);
+ }
+
+ fclose(fileptr);
+ return 0;
+}
diff --git a/src/tac.c b/src/tac.c
new file mode 100644
index 0000000..3f7caf6
--- /dev/null
+++ b/src/tac.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define PROGRAM_NAME "tac"
+
+struct stat statbuf;
+
+int main(int argc, char* argv[]) {
+ FILE *fileptr = NULL;
+ char buf[8192];
+ char ch;
+ int size = 0;
+
+ if (argc < 2) {
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ printf("%s", buf);
+ }
+
+ return 0;
+ }
+
+ if (access(argv[1], F_OK) != 0) {
+ printf("%s: %s: No such file or directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ if (stat(argv[1], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
+ printf("%s: %s is a directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ fileptr = fopen(argv[1], "r");
+
+ if (fileptr == NULL) {
+ perror("tac");
+ return 1;
+ }
+
+ while ((ch = fgetc(fileptr)) != EOF) {
+ size++;
+ }
+
+ size = size - 2;
+ rewind(fileptr);
+
+ while (size >= 0) {
+ fseek(fileptr, size, 0);
+ ch = fgetc(fileptr);
+ putchar(ch);
+ size--;
+ }
+
+ printf("\n");
+
+ fclose(fileptr);
+ return 0;
+}
diff --git a/src/tail.c b/src/tail.c
new file mode 100644
index 0000000..e34b90f
--- /dev/null
+++ b/src/tail.c
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define PROGRAM_NAME "tail"
+
+struct stat statbuf;
+
+int main(int argc, char* argv[]) {
+ FILE *fileptr = NULL;
+ char buf[8192];
+ char ch;
+ int size = 0, lines = 0;
+
+ if (argc < 2) {
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ printf("%s", buf);
+ }
+
+ return 0;
+ }
+
+ if (access(argv[1], F_OK) != 0) {
+ printf("%s: %s: No such file or directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ if (stat(argv[1], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
+ printf("%s: %s is a directory.\n", PROGRAM_NAME, argv[1]);
+ return 1;
+ }
+
+ fileptr = fopen(argv[1], "r");
+
+ if (fileptr == NULL) {
+ printf("ermmmm can't open this\n");
+ return 1;
+ }
+
+ while ((ch = fgetc(fileptr)) != EOF) {
+ size++;
+ }
+
+ size = size - 2;
+ rewind(fileptr);
+
+ while (size >= 0 && lines < 10) {
+ fseek(fileptr, size, 0);
+ ch = fgetc(fileptr);
+ if (ch == '\n') {
+ lines++;
+ }
+ putchar(ch);
+ size--;
+ }
+
+ fclose(fileptr);
+ return 0;
+}
diff --git a/src/touch.c b/src/touch.c
new file mode 100644
index 0000000..6cbde27
--- /dev/null
+++ b/src/touch.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <sys/time.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#define PROGRAM_NAME "touch"
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ fprintf(stderr, "%s: missing file operand\n", PROGRAM_NAME);
+ return 1;
+ }
+
+ for (int i = 1; i < argc; i++) {
+ if (utimes(argv[i], NULL) != 0) {
+ int file = open(argv[i], O_CREAT | O_WRONLY, 0644);
+
+ if (file == -1) {
+ perror("touch");
+ continue;
+ }
+
+ close(file);
+ }
+ }
+
+ return 0;
+}
diff --git a/test b/test
new file mode 100644
index 0000000..c0da8f4
--- /dev/null
+++ b/test
@@ -0,0 +1,27 @@
+jajf;daj fa;df
+aifa
+ fjksa]fk
+as
+fl
+'a lf
+afjsaif fpjas fposa fhpasihfidhidahpfa
+'s
+f
+saf
+sa
+fs
+af
+afsagagda
+fjsafak
+fg
+ad
+g
+ag
+a
+g
+dagdagagda
+g
+gda
+gda
+gadhfsahhhfsh
+ hSF HF H FH FS H
diff --git a/todo.txt b/todo.txt
new file mode 100644
index 0000000..a3197d3
--- /dev/null
+++ b/todo.txt
@@ -0,0 +1,2 @@
+tail + head:
+implement variable lines