/* * I'm doing "clever" things below to account for the fact that the boolean * values in C differ from the exit statuses, but they're both ints and they * look similar (zero and one), so cleverness is possible. I'm not sure * whether this is a good idea. */ #include #include #include #include #include int main(int argc, char **argv) { extern int fexists(char *file), usage(); if (argc == 3 && strcmp(argv[1], "-f") == 0) return(fexists(argv[2])); else if (argc != 4) return(usage()); else if (strcmp(argv[2], "-lt") == 0) return(!(atoi(argv[1]) < atoi(argv[3]))); else if (strcmp(argv[2], "-eq") == 0) return(!(atoi(argv[1]) == atoi(argv[3]))); else if (strcmp(argv[2], "!=") == 0) return(!strcmp(argv[1], argv[3])); else if (strcmp(argv[2], "=") == 0) return(!!strcmp(argv[1], argv[3])); else return(usage()); } int fexists(char *file) { struct stat statbuf; return(lstat(file, &statbuf) < 0 || !S_ISREG(statbuf.st_mode)); } int usage() { fprintf(stderr, "usage: test { -f file | item1 {-lt|-gt|=|!=} item2 }\n"); return(2); /* for the exam, it's ok if you used exit status of 1 here */ }