os161-1.99
 All Data Structures
driver.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 #include <errno.h>
00037 #include <err.h>
00038 
00039 #include "config.h"
00040 #include "test.h"
00041 
00042 ////////////////////////////////////////////////////////////
00043 
00044 static
00045 int
00046 finderror(int rv, int error)
00047 {
00048         if (rv==-1) {
00049                 return error;
00050         }
00051         else {
00052                 return 0;
00053         }
00054 }
00055 
00056 void
00057 report_survival(int rv, int error, const char *desc)
00058 {
00059         /* allow any error as long as we survive */
00060         errno = finderror(rv, error);
00061         warn("passed: %s", desc);
00062 }
00063 
00064 void
00065 report_test(int rv, int error, int right_error, const char *desc)
00066 {
00067         int goterror = finderror(rv, error);
00068 
00069         if (goterror == right_error) {
00070                 warnx("passed: %s", desc);
00071         }
00072         else if (goterror == EUNIMP || goterror == ENOSYS) {
00073                 warnx("------: %s (unimplemented)", desc);
00074         }
00075         else {
00076                 errno = goterror;
00077                 warn("FAILURE: %s", desc);
00078         }
00079 }
00080 
00081 void
00082 report_test2(int rv, int error, int okerr1, int okerr2, const char *desc)
00083 {
00084         int goterror = finderror(rv, error);
00085         if (goterror == okerr1 || goterror == okerr2) {
00086                 warnx("passed: %s", desc);
00087         }
00088         else if (goterror == EUNIMP || goterror == ENOSYS) {
00089                 warnx("------: %s (unimplemented)", desc);
00090         }
00091         else {
00092                 errno = goterror;
00093                 warn("FAILURE: %s", desc);
00094         }
00095 }
00096 
00097 ////////////////////////////////////////////////////////////
00098 
00099 int
00100 open_testfile(const char *string)
00101 {
00102         int fd, rv;
00103         size_t len;
00104 
00105         fd = open(TESTFILE, O_RDWR|O_CREAT|O_TRUNC);
00106         if (fd<0) {
00107                 warn("UH-OH: creating %s: failed", TESTFILE);
00108                 return -1;
00109         }
00110 
00111         if (string) {
00112                 len = strlen(string);
00113                 rv = write(fd, string, len);
00114                 if (rv<0) {
00115                         warn("UH-OH: write to %s failed", TESTFILE);
00116                         close(fd);
00117                         remove(TESTFILE);
00118                         return -1;
00119                 }
00120                 if ((unsigned)rv != len) {
00121                         warnx("UH-OH: write to %s got short count", TESTFILE);
00122                         close(fd);
00123                         remove(TESTFILE);
00124                         return -1;
00125                 }
00126                 rv = lseek(fd, 0, SEEK_SET);
00127                 if (rv<0) {
00128                         warn("UH-OH: rewind of %s failed", TESTFILE);
00129                         close(fd);
00130                         remove(TESTFILE);
00131                         return -1;
00132                 }
00133         }
00134         return fd;
00135 }
00136 
00137 int
00138 create_testfile(void)
00139 {
00140         int fd, rv;
00141 
00142         fd = open_testfile(NULL);
00143         if (fd<0) {
00144                 return -1;
00145         }
00146 
00147         rv = close(fd);
00148         if (rv<0) {
00149                 warn("UH-OH: closing %s failed", TESTFILE);
00150                 return -1;
00151         }
00152 
00153         return 0;
00154 }
00155 
00156 int
00157 create_testdir(void)
00158 {
00159         int rv;
00160         rv = mkdir(TESTDIR, 0775);
00161         if (rv<0) {
00162                 warn("UH-OH: mkdir %s failed", TESTDIR);
00163                 return -1;
00164         }
00165         return 0;
00166 }
00167 
00168 int
00169 create_testlink(void)
00170 {
00171         int rv;
00172         rv = symlink("blahblah", TESTLINK);
00173         if (rv<0) {
00174                 warn("UH-OH: making symlink %s failed", TESTLINK);
00175                 return -1;
00176         }
00177         return 0;
00178 }
00179 
00180 ////////////////////////////////////////////////////////////
00181 
00182 static
00183 struct {
00184         int ch;
00185         int asst;
00186         const char *name;
00187         void (*f)(void);
00188 } ops[] = {
00189         { 'a', 2, "execv",              test_execv },
00190         { 'b', 2, "waitpid",            test_waitpid },
00191         { 'c', 2, "open",               test_open },
00192         { 'd', 2, "read",               test_read },
00193         { 'e', 2, "write",              test_write },
00194         { 'f', 2, "close",              test_close },
00195         { 'g', 0, "reboot",             test_reboot },
00196         { 'h', 3, "sbrk",               test_sbrk },
00197         { 'i', 5, "ioctl",              test_ioctl },
00198         { 'j', 2, "lseek",              test_lseek },
00199         { 'k', 4, "fsync",              test_fsync },
00200         { 'l', 4, "ftruncate",          test_ftruncate },
00201         { 'm', 4, "fstat",              test_fstat },
00202         { 'n', 4, "remove",             test_remove },
00203         { 'o', 4, "rename",             test_rename },
00204         { 'p', 5, "link",               test_link },
00205         { 'q', 4, "mkdir",              test_mkdir },
00206         { 'r', 4, "rmdir",              test_rmdir },
00207         { 's', 2, "chdir",              test_chdir },
00208         { 't', 4, "getdirentry",        test_getdirentry },
00209         { 'u', 5, "symlink",            test_symlink },
00210         { 'v', 5, "readlink",           test_readlink },
00211         { 'w', 2, "dup2",               test_dup2 },
00212         { 'x', 5, "pipe",               test_pipe },
00213         { 'y', 5, "__time",             test_time },
00214         { 'z', 2, "__getcwd",           test_getcwd },
00215         { '{', 5, "stat",               test_stat },
00216         { '|', 5, "lstat",              test_lstat },
00217         { 0, 0, NULL, NULL }
00218 };
00219 
00220 #define LOWEST  'a'
00221 #define HIGHEST '|'
00222 
00223 static
00224 void
00225 menu(void)
00226 {
00227         int i;
00228         for (i=0; ops[i].name; i++) {
00229                 printf("[%c] %-24s", ops[i].ch, ops[i].name);
00230                 if (i%2==1) {
00231                         printf("\n");
00232                 }
00233         }
00234         if (i%2==1) {
00235                 printf("\n");
00236         }
00237         printf("[1] %-24s", "asst1");
00238         printf("[2] %-24s\n", "asst2");
00239         printf("[3] %-24s", "asst3");
00240         printf("[4] %-24s\n", "asst4");
00241         printf("[*] %-24s", "all");
00242         printf("[!] %-24s\n", "quit");
00243 }
00244 
00245 static
00246 void
00247 runit(int op)
00248 {
00249         int i, k;
00250 
00251         if (op=='!') {
00252                 exit(0);
00253         }
00254 
00255         if (op=='?') {
00256                 menu();
00257                 return;
00258         }
00259 
00260         if (op=='*') {
00261                 for (i=0; ops[i].name; i++) {
00262                         printf("[%s]\n", ops[i].name);
00263                         ops[i].f();
00264                 }
00265                 return;
00266         }
00267 
00268         if (op>='1' && op <= '4') {
00269                 k = op-'0';
00270                 for (i=0; ops[i].name; i++) {
00271                         if (ops[i].asst <= k) {
00272                                 printf("[%s]\n", ops[i].name);
00273                                 ops[i].f();
00274                         }
00275                 }
00276                 return;
00277         }
00278 
00279         if (op < LOWEST || op > HIGHEST) {
00280                 printf("Invalid request %c\n", op);
00281                 return;
00282         }
00283 
00284         ops[op-'a'].f();
00285 }
00286         
00287 int
00288 main(int argc, char **argv)
00289 {
00290         int op, i, j;
00291 
00292         printf("[%c-%c, 1-4, *, ?=menu, !=quit]\n", LOWEST, HIGHEST);
00293 
00294         if (argc > 1) {
00295                 for (i=1; i<argc; i++) {
00296                         for (j=0; argv[i][j]; j++) {
00297                                 printf("Choose: %c\n",
00298                                        argv[i][j]);
00299                                 runit(argv[i][j]);
00300                         }
00301                 }
00302         }
00303         else {
00304                 menu();
00305                 while (1) {
00306                         printf("Choose: ");
00307                         op = getchar();
00308                         if (op==EOF) {
00309                                 break;
00310                         }
00311                         printf("%c\n", op);
00312                         runit(op);
00313                 }
00314         }
00315 
00316         return 0;
00317 }
 All Data Structures