From 009b2b4793bdd1105d9dbe75fe008192ea63f12b Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 21 Jun 2005 00:41:29 +0000 Subject: [PATCH] Add some more examples. --- src/examples/.cvsignore | 15 ++++++--- src/examples/Makefile | 19 +++++++++--- src/examples/cat.c | 32 +++++++++++++++++++ src/examples/cmp.c | 68 +++++++++++++++++++++++++++++++++++++++++ src/examples/cp.c | 55 +++++++++++++++++++++++++++++++++ src/examples/hex-dump.c | 33 ++++++++++++++++++++ src/examples/mcp.c | 68 +++++++++++++++++++++++++++++++++++++++++ src/examples/rm.c | 17 +++++++++++ 8 files changed, 298 insertions(+), 9 deletions(-) create mode 100644 src/examples/cat.c create mode 100644 src/examples/cmp.c create mode 100644 src/examples/cp.c create mode 100644 src/examples/hex-dump.c create mode 100644 src/examples/mcp.c create mode 100644 src/examples/rm.c diff --git a/src/examples/.cvsignore b/src/examples/.cvsignore index d5c74d7..28574b0 100644 --- a/src/examples/.cvsignore +++ b/src/examples/.cvsignore @@ -1,10 +1,17 @@ -bubsort +cat +cmp +cp echo halt +hex-dump +ls +mcp +mkdir +rm +shell +bubsort insult lineup -ls matmult -mkdir recursor -shell +*.d diff --git a/src/examples/Makefile b/src/examples/Makefile index ae1df0f..a7bba67 100644 --- a/src/examples/Makefile +++ b/src/examples/Makefile @@ -3,16 +3,25 @@ SRCDIR = .. # 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 diff --git a/src/examples/cat.c b/src/examples/cat.c new file mode 100644 index 0000000..6ebf781 --- /dev/null +++ b/src/examples/cat.c @@ -0,0 +1,32 @@ +/* cat.c + + Prints files specified on command line to the console. */ + +#include +#include + +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; +} diff --git a/src/examples/cmp.c b/src/examples/cmp.c new file mode 100644 index 0000000..a616a7e --- /dev/null +++ b/src/examples/cmp.c @@ -0,0 +1,68 @@ +/* cat.c + + Compares two files. */ + +#include +#include + +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; +} diff --git a/src/examples/cp.c b/src/examples/cp.c new file mode 100644 index 0000000..0c8c1a5 --- /dev/null +++ b/src/examples/cp.c @@ -0,0 +1,55 @@ +/* cat.c + +Copies one file to another. */ + +#include +#include + +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; +} diff --git a/src/examples/hex-dump.c b/src/examples/hex-dump.c new file mode 100644 index 0000000..713d973 --- /dev/null +++ b/src/examples/hex-dump.c @@ -0,0 +1,33 @@ +/* hex-dump.c + + Prints files specified on command line to the console in hex. */ + +#include +#include + +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; +} diff --git a/src/examples/mcp.c b/src/examples/mcp.c new file mode 100644 index 0000000..a53677a --- /dev/null +++ b/src/examples/mcp.c @@ -0,0 +1,68 @@ +/* cat.c + + Copies one file to another, using mmap. */ + +#include +#include +#include + +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; +} diff --git a/src/examples/rm.c b/src/examples/rm.c new file mode 100644 index 0000000..2f1ce73 --- /dev/null +++ b/src/examples/rm.c @@ -0,0 +1,17 @@ +/* rm.c + + Removes files specified on command line. */ + +#include +#include + +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; +} -- 2.30.2