From: Ben Pfaff Date: Tue, 23 May 2006 16:33:22 +0000 (+0000) Subject: Add EXIT_FAILURE, EXIT_SUCCESS to syscall.h, X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0db48366884f3d6418217b926c4febc1bc2a3c13;p=pintos-anon Add EXIT_FAILURE, EXIT_SUCCESS to syscall.h, and use them in the example programs. Add comments to syscall.h. --- diff --git a/src/examples/cat.c b/src/examples/cat.c index 6ebf781..c8d229d 100644 --- a/src/examples/cat.c +++ b/src/examples/cat.c @@ -8,6 +8,7 @@ int main (int argc, char *argv[]) { + bool success = true; int i; for (i = 1; i < argc; i++) @@ -16,6 +17,7 @@ main (int argc, char *argv[]) if (fd < 0) { printf ("%s: open failed\n", argv[i]); + success = false; continue; } for (;;) @@ -28,5 +30,5 @@ main (int argc, char *argv[]) } close (fd); } - return 0; + return success ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/examples/cmp.c b/src/examples/cmp.c index a616a7e..94b406d 100644 --- a/src/examples/cmp.c +++ b/src/examples/cmp.c @@ -13,7 +13,7 @@ main (int argc, char *argv[]) if (argc != 3) { printf ("usage: cmp A B\n"); - return 1; + return EXIT_FAILURE; } /* Open files. */ @@ -21,13 +21,13 @@ main (int argc, char *argv[]) if (fd[0] < 0) { printf ("%s: open failed\n", argv[1]); - return 1; + return EXIT_FAILURE; } fd[1] = open (argv[2]); if (fd[1] < 0) { printf ("%s: open failed\n", argv[1]); - return 1; + return EXIT_FAILURE; } /* Compare data. */ @@ -53,7 +53,7 @@ main (int argc, char *argv[]) pos + i, buffer[0][i], buffer[0][i], argv[1], buffer[1][i], buffer[1][i], argv[2]); - return 1; + return EXIT_FAILURE; } if (min_read < bytes_read[1]) @@ -64,5 +64,5 @@ main (int argc, char *argv[]) printf ("%s and %s are identical\n", argv[1], argv[2]); - return 0; + return EXIT_SUCCESS; } diff --git a/src/examples/cp.c b/src/examples/cp.c index 0c8c1a5..86a5cd7 100644 --- a/src/examples/cp.c +++ b/src/examples/cp.c @@ -13,7 +13,7 @@ main (int argc, char *argv[]) if (argc != 3) { printf ("usage: cp OLD NEW\n"); - return 1; + return EXIT_FAILURE; } /* Open input file. */ @@ -21,20 +21,20 @@ main (int argc, char *argv[]) if (in_fd < 0) { printf ("%s: open failed\n", argv[1]); - return 1; + return EXIT_FAILURE; } /* Create and open output file. */ if (!create (argv[2], filesize (in_fd))) { printf ("%s: create failed\n", argv[2]); - return 1; + return EXIT_FAILURE; } out_fd = open (argv[2]); if (out_fd < 0) { printf ("%s: open failed\n", argv[2]); - return 1; + return EXIT_FAILURE; } /* Copy data. */ @@ -47,9 +47,9 @@ main (int argc, char *argv[]) if (write (out_fd, buffer, bytes_read) != bytes_read) { printf ("%s: write failed\n", argv[2]); - return 1; + return EXIT_FAILURE; } } - return 0; + return EXIT_SUCCESS; } diff --git a/src/examples/echo.c b/src/examples/echo.c index c0ecd00..1b136f2 100644 --- a/src/examples/echo.c +++ b/src/examples/echo.c @@ -1,4 +1,5 @@ #include +#include int main (int argc, char **argv) @@ -9,5 +10,5 @@ main (int argc, char **argv) printf ("%s ", argv[i]); printf ("\n"); - return 0; + return EXIT_SUCCESS; } diff --git a/src/examples/hex-dump.c b/src/examples/hex-dump.c index 713d973..ee313f2 100644 --- a/src/examples/hex-dump.c +++ b/src/examples/hex-dump.c @@ -8,6 +8,7 @@ int main (int argc, char *argv[]) { + bool success = true; int i; for (i = 1; i < argc; i++) @@ -16,6 +17,7 @@ main (int argc, char *argv[]) if (fd < 0) { printf ("%s: open failed\n", argv[i]); + success = false; continue; } for (;;) @@ -29,5 +31,5 @@ main (int argc, char *argv[]) } close (fd); } - return 0; + return success ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/examples/insult.c b/src/examples/insult.c index 80acc6e..98c4e6a 100644 --- a/src/examples/insult.c +++ b/src/examples/insult.c @@ -319,7 +319,7 @@ main (int argc, char *argv[]) if (handle < 0) { printf ("%s: open failed\n", argv[i]); - return -1; + return EXIT_FAILURE; } } else @@ -341,7 +341,7 @@ main (int argc, char *argv[]) if (file_flag) close (handle); - return 0; + return EXIT_SUCCESS; } void diff --git a/src/examples/lineup.c b/src/examples/lineup.c index 44636e7..60402d0 100644 --- a/src/examples/lineup.c +++ b/src/examples/lineup.c @@ -42,5 +42,5 @@ main (int argc, char *argv[]) close (handle); - return 0; + return EXIT_SUCCESS; } diff --git a/src/examples/ls.c b/src/examples/ls.c index d927bc1..fbe27a1 100644 --- a/src/examples/ls.c +++ b/src/examples/ls.c @@ -12,14 +12,14 @@ #include #include -static void +static bool list_dir (const char *dir, bool verbose) { int dir_fd = open (dir); if (dir_fd == -1) { printf ("%s: not found\n", dir); - return; + return false; } if (isdir (dir_fd)) @@ -60,13 +60,16 @@ list_dir (const char *dir, bool verbose) } else printf ("%s: not a directory\n", dir); - close (dir_fd); + close (dir_fd); + return true; } int main (int argc, char *argv[]) { + bool success = true; bool verbose = false; + if (argc > 1 && !strcmp (argv[1], "-l")) { verbose = true; @@ -75,12 +78,13 @@ main (int argc, char *argv[]) } if (argc <= 1) - list_dir (".", verbose); + success = list_dir (".", verbose); else { int i; for (i = 1; i < argc; i++) - list_dir (argv[i], verbose); + if (!list_dir (argv[i], verbose)) + success = false; } - return 0; + return success ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/examples/mcat.c b/src/examples/mcat.c index 4d98db5..7b39760 100644 --- a/src/examples/mcat.c +++ b/src/examples/mcat.c @@ -23,7 +23,7 @@ main (int argc, char *argv[]) if (fd < 0) { printf ("%s: open failed\n", argv[i]); - return 1; + return EXIT_FAILURE; } size = filesize (fd); @@ -32,7 +32,7 @@ main (int argc, char *argv[]) if (map == MAP_FAILED) { printf ("%s: mmap failed\n", argv[i]); - return 1; + return EXIT_FAILURE; } /* Write file to console. */ @@ -41,5 +41,5 @@ main (int argc, char *argv[]) /* Unmap files (optional). */ munmap (map); } - return 0; + return EXIT_SUCCESS; } diff --git a/src/examples/mcp.c b/src/examples/mcp.c index c99aeb9..6091dc8 100644 --- a/src/examples/mcp.c +++ b/src/examples/mcp.c @@ -18,7 +18,7 @@ main (int argc, char *argv[]) if (argc != 3) { printf ("usage: cp OLD NEW\n"); - return 1; + return EXIT_FAILURE; } /* Open input file. */ @@ -26,7 +26,7 @@ main (int argc, char *argv[]) if (in_fd < 0) { printf ("%s: open failed\n", argv[1]); - return 1; + return EXIT_FAILURE; } size = filesize (in_fd); @@ -34,13 +34,13 @@ main (int argc, char *argv[]) if (!create (argv[2], size)) { printf ("%s: create failed\n", argv[2]); - return 1; + return EXIT_FAILURE; } out_fd = open (argv[2]); if (out_fd < 0) { printf ("%s: open failed\n", argv[2]); - return 1; + return EXIT_FAILURE; } /* Map files. */ @@ -48,13 +48,13 @@ main (int argc, char *argv[]) if (in_map == MAP_FAILED) { printf ("%s: mmap failed\n", argv[1]); - return 1; + return EXIT_FAILURE; } out_map = mmap (out_fd, out_data); if (out_map == MAP_FAILED) { printf ("%s: mmap failed\n", argv[2]); - return 1; + return EXIT_FAILURE; } /* Copy files. */ @@ -64,5 +64,5 @@ main (int argc, char *argv[]) munmap (in_map); munmap (out_map); - return 0; + return EXIT_SUCCESS; } diff --git a/src/examples/mkdir.c b/src/examples/mkdir.c index ae6913c..7ddbc3f 100644 --- a/src/examples/mkdir.c +++ b/src/examples/mkdir.c @@ -8,11 +8,17 @@ int main (int argc, char *argv[]) { - if (argc != 2) - PANIC ("usage: %s DIRECTORY\n", argv[0]); + if (argc != 2) + { + printf ("usage: %s DIRECTORY\n", argv[0]); + return EXIT_FAILURE; + } - if (!mkdir (argv[1])) - PANIC ("%s: mkdir failed\n", argv[1]); - - return 0; + if (!mkdir (argv[1])) + { + printf ("%s: mkdir failed\n", argv[1]); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; } diff --git a/src/examples/pwd.c b/src/examples/pwd.c index b2441f5..d2305cf 100644 --- a/src/examples/pwd.c +++ b/src/examples/pwd.c @@ -16,12 +16,12 @@ main (void) if (getcwd (cwd, sizeof cwd)) { printf ("%s\n", cwd); - return 0; + return EXIT_SUCCESS; } else { printf ("error\n"); - return 1; + return EXIT_FAILURE; } } diff --git a/src/examples/rm.c b/src/examples/rm.c index 2f1ce73..0db7f7b 100644 --- a/src/examples/rm.c +++ b/src/examples/rm.c @@ -8,10 +8,14 @@ int main (int argc, char *argv[]) { + bool success = true; int i; for (i = 1; i < argc; i++) - if (!remove (argv[i])) - printf ("%s: remove failed\n", argv[i]); - return 0; + if (!remove (argv[i])) + { + printf ("%s: remove failed\n", argv[i]); + success = false; + } + return success ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/examples/shell.c b/src/examples/shell.c index 0aaafbb..916d252 100644 --- a/src/examples/shell.c +++ b/src/examples/shell.c @@ -41,7 +41,7 @@ main (void) } printf ("Shell exiting."); - return 0; + return EXIT_SUCCESS; } /* Reads a line of input from the user into LINE, which has room diff --git a/src/lib/user/syscall.h b/src/lib/user/syscall.h index d9487d3..8a9e0c0 100644 --- a/src/lib/user/syscall.h +++ b/src/lib/user/syscall.h @@ -4,14 +4,22 @@ #include #include +/* Process identifier. */ typedef int pid_t; #define PID_ERROR ((pid_t) -1) +/* Map region identifier. */ typedef int mapid_t; #define MAP_FAILED ((mapid_t) -1) +/* Maximum characters in a filename written by readdir(). */ #define READDIR_MAX_LEN 14 +/* Typical return values from main() and arguments to exit(). */ +#define EXIT_SUCCESS 0 /* Successful execution. */ +#define EXIT_FAILURE 1 /* Unsuccessful execution. */ + +/* Projects 2 and later. */ void halt (void) NO_RETURN; void exit (int status) NO_RETURN; pid_t exec (const char *file); @@ -25,8 +33,12 @@ int write (int fd, const void *buffer, unsigned length); void seek (int fd, unsigned position); unsigned tell (int fd); void close (int fd); + +/* Project 3 and optionally project 4. */ mapid_t mmap (int fd, void *addr); void munmap (mapid_t); + +/* Project 4 only. */ bool chdir (const char *dir); bool mkdir (const char *dir); bool readdir (int fd, char name[READDIR_MAX_LEN + 1]);