Browse Source

Import some apps from mainline/core that at least compile (fgrep may not work due to stdio brokenness)

K. Lange 2 years ago
parent
commit
b89ce3f7eb
15 changed files with 447 additions and 6 deletions
  1. 19 0
      apps/clear.c
  2. 74 0
      apps/cp.c
  3. 42 0
      apps/fgrep.c
  4. 25 0
      apps/mkdir.c
  5. 1 1
      apps/nslookup.c
  6. 1 5
      apps/readelf.c
  7. 29 0
      apps/readlink.c
  8. 18 0
      apps/reboot.c
  9. 32 0
      apps/sleep.c
  10. 73 0
      apps/stat.c
  11. 18 0
      apps/sysfunc.c
  12. 28 0
      apps/touch.c
  13. 61 0
      apps/which.c
  14. 22 0
      apps/yes.c
  15. 4 0
      base/usr/include/stdlib.h

+ 19 - 0
apps/clear.c

@@ -0,0 +1,19 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013 K. Lange
+ */
+/*
+ * clear
+ *
+ * Clears the terminal.
+ * This is a very dumb version and probably only works
+ * within the toaruOS terminal, but it might also work
+ * with an xterm or similar.
+ */
+#include <stdio.h>
+
+int main(int argc, char ** argv) {
+	printf("\033[H\033[2J");
+	fflush(stdout);
+	return 0;
+}

+ 74 - 0
apps/cp.c

@@ -0,0 +1,74 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013 K. Lange
+ * Copyright (C) 2013 Tyler Bindon
+ */
+/*
+ * cp
+ */
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <stdio.h>
+
+#define CHUNK_SIZE 4096
+
+int main(int argc, char ** argv) {
+
+	FILE * fd;
+	FILE * fout;
+	if (argc < 3) {
+		fprintf(stderr, "usage: %s [source] [destination]\n", argv[0]);
+		return 1;
+	}
+	fd = fopen(argv[1], "r");
+	if (!fd) {
+		fprintf(stderr, "%s: %s: no such file or directory\n", argv[0], argv[1]);
+		return 1;
+	}
+
+	struct stat statbuf;
+	stat(argv[2], &statbuf);
+	if (S_ISDIR(statbuf.st_mode)) {
+		char *filename = strrchr(argv[1], '/');
+		if (!filename) {
+			filename = argv[1];
+		}
+
+		char *target_path = malloc((strlen(argv[2]) + strlen(filename) + 2) * sizeof(char));
+		sprintf(target_path, "%s/%s", argv[2], filename );
+		fout = fopen( target_path, "w" );
+
+		free(target_path);
+	} else {
+		fout = fopen( argv[2], "w" );
+	}
+
+	size_t length;
+
+	fseek(fd, 0, SEEK_END);
+	length = ftell(fd);
+	fseek(fd, 0, SEEK_SET);
+
+	char buf[CHUNK_SIZE];
+	while (length > CHUNK_SIZE) {
+		fread( buf, 1, CHUNK_SIZE, fd);
+		fwrite(buf, 1, CHUNK_SIZE, fout);
+		length -= CHUNK_SIZE;
+	}
+	if (length > 0) {
+		fread( buf, 1, length, fd);
+		fwrite(buf, 1, length, fout);
+	}
+
+	fclose(fd);
+	fclose(fout);
+
+	return 0;
+}
+
+/*
+ * vim:tabstop=4
+ * vim:noexpandtab
+ * vim:shiftwidth=4
+ */

+ 42 - 0
apps/fgrep.c

@@ -0,0 +1,42 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2014-2018 K. Lange
+ */
+/*
+ * Dump grep
+ */
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+
+#define LINE_SIZE 4096
+
+int main(int argc, char ** argv) {
+	if (argc < 2) {
+		fprintf(stderr, "usage: %s thing-to-grep-for\n", argv[0]);
+		return 1;
+	}
+
+	char * needle = argv[1];
+	char buf[LINE_SIZE];
+	int ret = 1;
+
+	while (fgets(buf, LINE_SIZE, stdin)) {
+		char * found = strstr(buf, needle);
+		if (found) {
+			*found = '\0';
+			found += strlen(needle);
+			fprintf(stdout, "%s\033[1;31m%s\033[0m%s", buf, needle, found);
+			ret = 0;
+		}
+	}
+
+	return ret;
+}
+
+/*
+ * vim:tabstop=4
+ * vim:noexpandtab
+ * vim:shiftwidth=4
+ */

+ 25 - 0
apps/mkdir.c

@@ -0,0 +1,25 @@
+/* vim:tabstop=4 shiftwidth=4 noexpandtab
+ * This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013-2014 K. Lange
+ *
+ * mkdir
+ *
+ * Create a directory.
+ */
+#include <stdio.h>
+#include <stdint.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+int main(int argc, char ** argv) {
+	if (argc < 2) {
+		fprintf(stderr, "%s: expected argument\n", argv[0]);
+		return 1;
+	}
+
+	mkdir(argv[1], 0x00);
+
+	return 0;
+}

+ 1 - 1
apps/nslookup.c

@@ -1,7 +1,7 @@
 /* vim: ts=4 sw=4 noexpandtab
  * This file is part of ToaruOS and is released under the terms
  * of the NCSA / University of Illinois License - see LICENSE.md
- * Copyright (C) 2016 Kevin Lange
+ * Copyright (C) 2016-2018 K. Lange
  *
  * nslookup - perform nameserver lookups
  *

+ 1 - 5
apps/readelf.c

@@ -1,10 +1,6 @@
 /* This file is part of ToaruOS and is released under the terms
  * of the NCSA / University of Illinois License - see LICENSE.md
- * Copyright (C) 2013-2014 Kevin Lange
- */
-/*
- * ToAruOS Miniature ELF Reader
- * (C) 2011 Kevin Lange
+ * Copyright (C) 2013-2018 K Lange
  */
 
 #include <stdio.h>

+ 29 - 0
apps/readlink.c

@@ -0,0 +1,29 @@
+/* vim: tabstop=4 shiftwidth=4 noexpandtab
+ * This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2015 Mike Gerow
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define MAX_LINK_SIZE 4096
+
+static char usage[] =
+"Usage: %s LINK\n";
+
+int main(int argc, char * argv[]) {
+	if (argc != 2) {
+		fprintf(stderr, usage, argv[0]);
+		exit(EXIT_FAILURE);
+	}
+	char * name = argv[1];
+
+	char buf[MAX_LINK_SIZE];
+	if (readlink(name, buf, sizeof(buf)) < 0) {
+		//perror("link");
+		exit(EXIT_FAILURE);
+	}
+	fprintf(stdout, "%s\n", buf);
+	exit(EXIT_SUCCESS);
+}

+ 18 - 0
apps/reboot.c

@@ -0,0 +1,18 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013-2014 K. Lange
+ */
+/*
+ * reboot
+ *
+ * Reboot the system.
+ */
+#include <stdio.h>
+#include <syscall.h>
+
+int main(int argc, char ** argv) {
+	if (syscall_reboot() < 0) {
+		printf("%s: permission denied\n", argv[0]);
+	}
+	return 1;
+}

+ 32 - 0
apps/sleep.c

@@ -0,0 +1,32 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013 K. Lange
+ */
+/*
+ * Sleep
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <syscall.h>
+#include <string.h>
+
+int main(int argc, char ** argv) {
+	int ret = 0;
+
+	char * arg = strdup(argv[1]);
+
+	float time = atof(arg);
+
+	unsigned int seconds = (unsigned int)time;
+	unsigned int subsecs = (unsigned int)((time - (float)seconds) * 100);
+
+	ret = syscall_nanosleep(seconds, subsecs);
+
+	return ret;
+}
+
+/*
+ * vim:tabstop=4
+ * vim:noexpandtab
+ * vim:shiftwidth=4
+ */

+ 73 - 0
apps/stat.c

@@ -0,0 +1,73 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013 K. Lange
+ */
+/*
+ * stat
+ *
+ * Displays information on a file's inode.
+ */
+#include <stdio.h>
+#include <sys/stat.h>
+#include <syscall.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <sys/time.h>
+
+int main(int argc, char ** argv) {
+	int dereference = 0;
+	if (argc < 2) {
+		fprintf(stderr,"%s: expected argument\n", argv[0]);
+		return 1;
+	}
+
+	char * file = argv[1];
+
+	if (argc > 2) {
+		if (!strcmp(argv[1],"-L")) {
+			dereference = 1;
+		}
+		file = argv[2];
+	}
+
+	struct stat _stat;
+	if (dereference) {
+		if (stat(file, &_stat) < 0) return 1;
+	} else {
+		if (lstat(file, &_stat) < 0) return 1;
+	}
+
+	printf("0x%x bytes\n", (unsigned int)_stat.st_size);
+
+	if (S_ISDIR(_stat.st_mode)) {
+		printf("Is a directory.\n");
+	} else if (S_ISFIFO(_stat.st_mode)) {
+		printf("Is a pipe.\n");
+	} else if (S_ISLNK(_stat.st_mode)) {
+		printf("Is a symlink.\n");
+	} else if (_stat.st_mode & 0111) {
+		printf("Is executable.\n");
+	}
+
+	struct stat * f = &_stat;
+
+	printf("st_dev   0x%x %d\n", (unsigned int)f->st_dev   , (unsigned int)sizeof(f->st_dev  ));
+	printf("st_ino   0x%x %d\n", (unsigned int)f->st_ino   , (unsigned int)sizeof(f->st_ino  ));
+	printf("st_mode  0x%x %d\n", (unsigned int)f->st_mode  , (unsigned int)sizeof(f->st_mode  ));
+	printf("st_nlink 0x%x %d\n", (unsigned int)f->st_nlink , (unsigned int)sizeof(f->st_nlink ));
+	printf("st_uid   0x%x %d\n", (unsigned int)f->st_uid   , (unsigned int)sizeof(f->st_uid   ));
+	printf("st_gid   0x%x %d\n", (unsigned int)f->st_gid   , (unsigned int)sizeof(f->st_gid   ));
+	printf("st_rdev  0x%x %d\n", (unsigned int)f->st_rdev  , (unsigned int)sizeof(f->st_rdev  ));
+	printf("st_size  0x%x %d\n", (unsigned int)f->st_size  , (unsigned int)sizeof(f->st_size  ));
+
+	printf("0x%x\n", (unsigned int)((uint32_t *)f)[0]);
+
+	return 0;
+}
+
+/*
+ * vim:tabstop=4
+ * vim:noexpandtab
+ * vim:shiftwidth=4
+ */

+ 18 - 0
apps/sysfunc.c

@@ -0,0 +1,18 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013 K. Lange
+ */
+/* vim:tabstop=4 shiftwidth=4 noexpandtab
+ *
+ * sysfunc
+ *
+ * Executes an "extended system function" which
+ * is basically just a super-syscall.
+ */
+#include <syscall.h>
+#include <stdlib.h>
+
+int main(int argc, char ** argv) {
+	if (argc < 2) return 1;
+	return syscall_system_function(atoi(argv[1]), &argv[2]);
+}

+ 28 - 0
apps/touch.c

@@ -0,0 +1,28 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013 K. Lange
+ */
+/*
+ * touch
+ *
+ * Creates a file or updates its last-modified date.
+ * (in theory)
+ */
+#include <stdio.h>
+#include <string.h>
+
+int main(int argc, char * argv[]) {
+	if (argc < 2) {
+		fprintf(stderr, "%s: argument expected\n", argv[0]);
+		return 1;
+	}
+
+	FILE * f = fopen(argv[1], "a");
+	if (!f) {
+		//perror(argv[0]);
+		return 1;
+	}
+	fclose(f);
+
+	return 0;
+}

+ 61 - 0
apps/which.c

@@ -0,0 +1,61 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013-2014 Kevin Lange
+ *
+ * which
+ *
+ * Searches through PATH to find an executable.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#define DEFAULT_PATH ".:/bin:/usr/bin"
+
+int main(int argc, char * argv[]) {
+
+	if (argc < 2) {
+		return 1;
+	}
+
+	if (strstr(argv[1], "/")) {
+		struct stat t;
+		if (!stat(argv[1], &t)) {
+			if ((t.st_mode & 0111)) {
+				printf("%s\n", argv[1]);
+				return 0;
+			}
+		}
+	} else {
+		char * file = argv[1];
+		char * path = getenv("PATH");
+		if (!path) {
+			path = DEFAULT_PATH;
+		}
+
+		char * xpath = strdup(path);
+		char * p, * last;
+		for ((p = strtok_r(xpath, ":", &last)); p; p = strtok_r(NULL, ":", &last)) {
+			int r;
+			struct stat stat_buf;
+			char * exe = malloc(strlen(p) + strlen(file) + 2);
+			strcpy(exe, p);
+			strcat(exe, "/");
+			strcat(exe, file);
+
+			r = stat(exe, &stat_buf);
+			if (r != 0) {
+				continue;
+			}
+			if (!(stat_buf.st_mode & 0111)) {
+				continue; /* XXX not technically correct; need to test perms */
+			}
+			printf("%s\n", exe);
+			return 0;
+		}
+		free(xpath);
+		return 1;
+	}
+	return 1;
+}

+ 22 - 0
apps/yes.c

@@ -0,0 +1,22 @@
+/* This file is part of ToaruOS and is released under the terms
+ * of the NCSA / University of Illinois License - see LICENSE.md
+ * Copyright (C) 2013 K Lange
+ */
+/* vim: tabstop=4 shiftwidth=4 noexpandtab
+ *
+ * yes
+ *
+ * Continually prints its first argument, followed by a newline.
+ */
+#include <stdio.h>
+
+int main(int argc, char * argv[]) {
+	char * yes_string = "y";
+	if (argc > 1) {
+		yes_string = argv[1];
+	}
+	while (1) {
+		printf("%s\n", yes_string);
+	}
+	return 0;
+}

+ 4 - 0
base/usr/include/stdlib.h

@@ -20,5 +20,9 @@ extern int putenv(char * name);
 extern int setenv(const char *name, const char *value, int overwrite);
 
 extern double atof(const char * nptr);
+extern int atoi(const char * nptr);
+
+#define EXIT_SUCCESS 0
+#define EXIT_FAILURE 1
 
 #define NULL 0