summaryrefslogtreecommitdiffstats
path: root/src/examples/lineup.c
diff options
context:
space:
mode:
authorFelipe Boeira <felipe.boeira@liu.se>2019-01-08 18:39:03 +0100
committerFelipe Boeira <felipe.boeira@liu.se>2019-01-08 18:39:03 +0100
commitd4522b8e9854178473adcea0fbb84f23f6e744bd (patch)
treefbcf620617c5023154eba3f965b3a982daa64a47 /src/examples/lineup.c
downloadpintos-d4522b8e9854178473adcea0fbb84f23f6e744bd.tar.gz
Initial commit
Diffstat (limited to 'src/examples/lineup.c')
-rw-r--r--src/examples/lineup.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/examples/lineup.c b/src/examples/lineup.c
new file mode 100644
index 0000000..60402d0
--- /dev/null
+++ b/src/examples/lineup.c
@@ -0,0 +1,46 @@
+/* lineup.c
+
+ Converts a file to uppercase in-place.
+
+ Incidentally, another way to do this while avoiding the seeks
+ would be to open the input file, then remove() it and reopen
+ it under another handle. Because of Unix deletion semantics
+ this works fine. */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <syscall.h>
+
+int
+main (int argc, char *argv[])
+{
+ char buf[1024];
+ int handle;
+
+ if (argc != 2)
+ exit (1);
+
+ handle = open (argv[1]);
+ if (handle < 0)
+ exit (2);
+
+ for (;;)
+ {
+ int n, i;
+
+ n = read (handle, buf, sizeof buf);
+ if (n <= 0)
+ break;
+
+ for (i = 0; i < n; i++)
+ buf[i] = toupper ((unsigned char) buf[i]);
+
+ seek (handle, tell (handle) - n);
+ if (write (handle, buf, n) != n)
+ printf ("write failed\n");
+ }
+
+ close (handle);
+
+ return EXIT_SUCCESS;
+}