|
|
1.1 ! root 1: #ifndef lint ! 2: static char sccsid[] = "@(#)selsub.c 4.3 (Berkeley) 5/15/86"; ! 3: #endif not lint ! 4: ! 5: #include "stdio.h" ! 6: #include "sys/types.h" ! 7: #include "sys/stat.h" ! 8: #include "lrnref.h" ! 9: ! 10: char learnrc[100]; ! 11: ! 12: selsub(argc,argv) ! 13: char *argv[]; ! 14: { ! 15: char ans1[100]; ! 16: static char ans2[40]; ! 17: static char dirname[40]; ! 18: static char subname[40]; ! 19: FILE *fp; ! 20: char *getenv(); ! 21: char *home; ! 22: ! 23: if (argc > 1 && argv[1][0] == '-') { ! 24: direct = argv[1]+1; ! 25: argc--; ! 26: argv++; ! 27: } ! 28: if (chdir(direct) != 0) { ! 29: perror(direct); ! 30: fprintf(stderr, "Selsub: couldn't cd to non-standard directory\n"); ! 31: exit(1); ! 32: } ! 33: sname = argc > 1 ? argv[1] : 0; ! 34: if (argc > 2) { ! 35: strcpy (level=ans2, argv[2]); ! 36: if (strcmp(level, "-") == 0) /* no lesson name is - */ ! 37: ask = 1; ! 38: else if (strcmp(level, "0") == 0) ! 39: level = 0; ! 40: else ! 41: again = 1; /* treat as if "again" lesson */ ! 42: } ! 43: else ! 44: level = 0; ! 45: if (argc > 3 ) ! 46: speed = atoi(argv[3]); ! 47: if ((home = getenv("HOME")) != NULL) { ! 48: sprintf(learnrc, "%s/.learnrc", home); ! 49: if ((fp=fopen(learnrc, "r")) != NULL) { ! 50: char xsub[40], xlev[40]; int xsp; ! 51: fscanf(fp, "%s %s %d", xsub, xlev, &xsp); ! 52: fclose(fp); ! 53: if (*xsub && *xlev && xsp >= 0 /* all read OK */ ! 54: && (argc == 2 && strcmp(sname, xsub) == 0 ! 55: || argc <= 1)) { ! 56: strcpy(sname = subname, xsub); ! 57: strcpy(level = ans2, xlev); ! 58: speed = xsp; ! 59: again = 1; ! 60: printf("[ Taking up where you left off last time: learn %s %s.\n", ! 61: sname, level); ! 62: printf("%s\n \"rm $HOME/.learnrc\", and re-enter with \"learn %s\". ]\n", ! 63: " To start this sequence over leave learn by typing \"bye\", then", ! 64: sname); ! 65: } ! 66: } ! 67: } ! 68: if (!sname) { ! 69: printf("These are the available courses -\n"); ! 70: list("Linfo"); ! 71: printf("If you want more information about the courses,\n"); ! 72: printf("or if you have never used 'learn' before,\n"); ! 73: printf("press RETURN; otherwise type the name of\n"); ! 74: printf("the course you want, followed by RETURN.\n"); ! 75: fflush(stdout); ! 76: gets(sname=subname); ! 77: if (sname[0] == '\0') { ! 78: list("Xinfo"); ! 79: do { ! 80: printf("\nWhich subject? "); ! 81: fflush(stdout); ! 82: gets(sname=subname); ! 83: } while (sname[0] == '\0'); ! 84: } ! 85: } ! 86: chknam(sname); ! 87: total = cntlessons(sname); ! 88: if (!level) { ! 89: printf("If you were in the middle of this subject\n"); ! 90: printf("and want to start where you left off, type\n"); ! 91: printf("the last lesson number the computer printed.\n"); ! 92: printf("If you don't know the number, type in a word\n"); ! 93: printf("you think might appear in the lesson you want,\n"); ! 94: printf("and I will look for the first lesson containing it.\n"); ! 95: printf("To start at the beginning, just hit RETURN.\n"); ! 96: fflush(stdout); ! 97: gets(ans2); ! 98: if (ans2[0]==0) ! 99: strcpy(ans2,"0"); ! 100: else ! 101: again = 1; ! 102: level=ans2; ! 103: getlesson(); ! 104: } ! 105: ! 106: /* make new directory for user to play in */ ! 107: if (chdir("/tmp") != 0) { ! 108: perror("/tmp"); ! 109: fprintf(stderr, "Selsub: couldn't cd to public directory\n"); ! 110: exit(1); ! 111: } ! 112: sprintf(dir=dirname, "pl%da", getpid()); ! 113: sprintf(ans1, "mkdir %s", dir); ! 114: system(ans1); ! 115: if (chdir(dir) < 0) { ! 116: perror(dir); ! 117: fprintf(stderr, "Selsub: couldn't make play directory with %s.\nBye.\n", ans1); ! 118: exit(1); ! 119: } ! 120: /* after this point, we have a working directory. */ ! 121: /* have to call wrapup to clean up */ ! 122: if (access(sprintf(ans1, "%s/%s/Init", direct, sname), 04)==0) ! 123: if (system(sprintf(ans1, "%s/%s/Init %s", direct, sname, level)) != 0) { ! 124: printf("Leaving learn.\n"); ! 125: wrapup(1); ! 126: } ! 127: } ! 128: ! 129: chknam(name) ! 130: char *name; ! 131: { ! 132: if (access(name, 05) < 0) { ! 133: printf("Sorry, there is no subject or lesson named %s.\nBye.\n", name); ! 134: exit(1); ! 135: } ! 136: } ! 137: ! 138: #ifndef DIR ! 139: #include <sys/dir.h> ! 140: #endif ! 141: ! 142: cntlessons(sname) /* return number of entries in lesson directory; */ ! 143: char *sname; /* approximate at best since I don't count L0, Init */ ! 144: { /* and lessons skipped by good students */ ! 145: #if BSD4_2 ! 146: struct direct dbuf; ! 147: register struct direct *ep = &dbuf; /* directory entry pointer */ ! 148: int n = 0; ! 149: DIR *dp; ! 150: ! 151: if ((dp = opendir(sname)) == NULL) { ! 152: perror(sname); ! 153: wrapup(1); ! 154: } ! 155: for (ep = readdir(dp); ep != NULL; ep = readdir(dp)) { ! 156: if (ep->d_ino != 0) ! 157: n++; ! 158: } ! 159: closedir(dp); ! 160: return n - 2; /* minus . and .. */ ! 161: #else ! 162: struct stat statbuf; ! 163: ! 164: stat(sname, &statbuf); ! 165: return statbuf.st_size / 16 - 2; ! 166: #endif ! 167: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.