-bubsort
+cat
+cmp
+cp
echo
halt
+hex-dump
+ls
+mcp
+mkdir
+rm
+shell
+bubsort
insult
lineup
-ls
matmult
-mkdir
recursor
-shell
+*.d
# Test programs to compile, and a list of sources for each.
# To add a new test, put its name on the PROGS list
# and then add a name_SRC line that lists its source files.
-PROGS = bubsort echo halt insult lineup ls matmult mkdir recursor shell
-bubsort_SRC = bubsort.c
+PROGS = cat cmp cp echo halt hex-dump ls mcp mkdir rm shell \
+ bubsort insult lineup matmult recursor
+
+cat_SRC = cat.c
+cmp_SRC = cmp.c
+cp_SRC = cp.c
echo_SRC = echo.c
halt_SRC = halt.c
+hex-dump_SRC = hex-dump.c
+ls_SRC = ls.c
+mcp_SRC = mcp.c
+mkdir_SRC = mkdir.c
+rm_SRC = rm.c
+shell_SRC = shell.c
+
+bubsort_SRC = bubsort.c
insult_SRC = insult.c
lineup_SRC = lineup.c
matmult_SRC = matmult.c
recursor_SRC = recursor.c
-shell_SRC = shell.c
-ls_SRC = ls.c
-mkdir_SRC = mkdir.c
include $(SRCDIR)/Makefile.userprog
--- /dev/null
+/* cat.c
+
+ Prints files specified on command line to the console. */
+
+#include <stdio.h>
+#include <syscall.h>
+
+int
+main (int argc, char *argv[])
+{
+ int i;
+
+ for (i = 1; i < argc; i++)
+ {
+ int fd = open (argv[i]);
+ if (fd < 0)
+ {
+ printf ("%s: open failed\n", argv[i]);
+ continue;
+ }
+ for (;;)
+ {
+ char buffer[1024];
+ int bytes_read = read (fd, buffer, sizeof buffer);
+ if (bytes_read == 0)
+ break;
+ write (STDOUT_FILENO, buffer, bytes_read);
+ }
+ close (fd);
+ }
+ return 0;
+}
--- /dev/null
+/* cat.c
+
+ Compares two files. */
+
+#include <stdio.h>
+#include <syscall.h>
+
+int
+main (int argc, char *argv[])
+{
+ int fd[2];
+
+ if (argc != 3)
+ {
+ printf ("usage: cmp A B\n");
+ return 1;
+ }
+
+ /* Open files. */
+ fd[0] = open (argv[1]);
+ if (fd[0] < 0)
+ {
+ printf ("%s: open failed\n", argv[1]);
+ return 1;
+ }
+ fd[1] = open (argv[2]);
+ if (fd[1] < 0)
+ {
+ printf ("%s: open failed\n", argv[1]);
+ return 1;
+ }
+
+ /* Compare data. */
+ for (;;)
+ {
+ int pos;
+ char buffer[2][1024];
+ int bytes_read[2];
+ int min_read;
+ int i;
+
+ pos = tell (fd[0]);
+ bytes_read[0] = read (fd[0], buffer[0], sizeof buffer[0]);
+ bytes_read[1] = read (fd[1], buffer[1], sizeof buffer[1]);
+ min_read = bytes_read[0] < bytes_read[1] ? bytes_read[0] : bytes_read[1];
+ if (min_read == 0)
+ break;
+
+ for (i = 0; i < min_read; i++)
+ if (buffer[0][i] != buffer[1][i])
+ {
+ printf ("Byte %d is %02hhx ('%c') in %s but %02hhx ('%c') in %s\n",
+ pos + i,
+ buffer[0][i], buffer[0][i], argv[1],
+ buffer[1][i], buffer[1][i], argv[2]);
+ return 1;
+ }
+
+ if (min_read < bytes_read[1])
+ printf ("%s is shorter than %s\n", argv[1], argv[2]);
+ else if (min_read < bytes_read[0])
+ printf ("%s is shorter than %s\n", argv[2], argv[1]);
+ }
+
+ printf ("%s and %s are identical\n", argv[1], argv[2]);
+
+ return 0;
+}
--- /dev/null
+/* cat.c
+
+Copies one file to another. */
+
+#include <stdio.h>
+#include <syscall.h>
+
+int
+main (int argc, char *argv[])
+{
+ int in_fd, out_fd;
+
+ if (argc != 3)
+ {
+ printf ("usage: cp OLD NEW\n");
+ return 1;
+ }
+
+ /* Open input file. */
+ in_fd = open (argv[1]);
+ if (in_fd < 0)
+ {
+ printf ("%s: open failed\n", argv[1]);
+ return 1;
+ }
+
+ /* Create and open output file. */
+ if (!create (argv[2], filesize (in_fd)))
+ {
+ printf ("%s: create failed\n", argv[2]);
+ return 1;
+ }
+ out_fd = open (argv[2]);
+ if (out_fd < 0)
+ {
+ printf ("%s: open failed\n", argv[2]);
+ return 1;
+ }
+
+ /* Copy data. */
+ for (;;)
+ {
+ char buffer[1024];
+ int bytes_read = read (in_fd, buffer, sizeof buffer);
+ if (bytes_read == 0)
+ break;
+ if (write (out_fd, buffer, bytes_read) != bytes_read)
+ {
+ printf ("%s: write failed\n", argv[2]);
+ return 1;
+ }
+ }
+
+ return 0;
+}
--- /dev/null
+/* hex-dump.c
+
+ Prints files specified on command line to the console in hex. */
+
+#include <stdio.h>
+#include <syscall.h>
+
+int
+main (int argc, char *argv[])
+{
+ int i;
+
+ for (i = 1; i < argc; i++)
+ {
+ int fd = open (argv[i]);
+ if (fd < 0)
+ {
+ printf ("%s: open failed\n", argv[i]);
+ continue;
+ }
+ for (;;)
+ {
+ char buffer[1024];
+ int pos = tell (fd);
+ int bytes_read = read (fd, buffer, sizeof buffer);
+ if (bytes_read == 0)
+ break;
+ hex_dump (pos, buffer, bytes_read, true);
+ }
+ close (fd);
+ }
+ return 0;
+}
--- /dev/null
+/* cat.c
+
+ Copies one file to another, using mmap. */
+
+#include <stdio.h>
+#include <string.h>
+#include <syscall.h>
+
+int
+main (int argc, char *argv[])
+{
+ int in_fd, out_fd;
+ mapid_t in_map, out_map;
+ void *in_data = (void *) 0x10000000;
+ void *out_data = (void *) 0x20000000;
+ int size;
+
+ if (argc != 3)
+ {
+ printf ("usage: cp OLD NEW\n");
+ return 1;
+ }
+
+ /* Open input file. */
+ in_fd = open (argv[1]);
+ if (in_fd < 0)
+ {
+ printf ("%s: open failed\n", argv[1]);
+ return 1;
+ }
+ size = filesize (in_fd);
+
+ /* Create and open output file. */
+ if (!create (argv[2], size))
+ {
+ printf ("%s: create failed\n", argv[2]);
+ return 1;
+ }
+ out_fd = open (argv[2]);
+ if (out_fd < 0)
+ {
+ printf ("%s: open failed\n", argv[2]);
+ return 1;
+ }
+
+ /* Map files. */
+ in_map = mmap (in_fd, in_data);
+ if (in_map == MAP_FAILED)
+ {
+ printf ("%s: mmap failed\n", argv[1]);
+ return 1;
+ }
+ out_map = mmap (out_fd, out_data);
+ if (out_map == MAP_FAILED)
+ {
+ printf ("%s: mmap failed\n", argv[2]);
+ return 1;
+ }
+
+ /* Copy files. */
+ memcpy (out_data, in_data, size);
+
+ /* Unmap files (optional). */
+ munmap (in_map);
+ munmap (out_map);
+
+ return 0;
+}
--- /dev/null
+/* rm.c
+
+ Removes files specified on command line. */
+
+#include <stdio.h>
+#include <syscall.h>
+
+int
+main (int argc, char *argv[])
+{
+ int i;
+
+ for (i = 1; i < argc; i++)
+ if (!remove (argv[i]))
+ printf ("%s: remove failed\n", argv[i]);
+ return 0;
+}