xref: /original-bsd/usr.bin/learn/learn/selsub.c (revision 92d3de31)
1 #ifndef lint
2 static char sccsid[] = "@(#)selsub.c	4.2	(Berkeley)	04/25/83";
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 selsub(argc,argv)
11 char *argv[];
12 {
13 	char ans1[100];
14 	static char ans2[30];
15 	static char dirname[20];
16 	static char subname[20];
17 	struct stat statbuf;
18 
19 	if (argc > 1 && argv[1][0] == '-') {
20 		direct = argv[1]+1;
21 		argc--;
22 		argv++;
23 	}
24 	if (chdir(direct) != 0) {
25 		perror(direct);
26 		fprintf(stderr, "Selsub:  couldn't cd to non-standard directory\n");
27 		exit(1);
28 	}
29 	sname = argc > 1 ? argv[1] : 0;
30 	if (argc > 2) {
31 		strcpy (level=ans2, argv[2]);
32 		if (strcmp(level, "-") == 0)	/* no lesson name is - */
33 			ask = 1;
34 		else
35 			again = 1;	/* treat as if "again lesson" */
36 	}
37 	else
38 		level = 0;
39 	if (argc > 3 )
40 		speed = atoi(argv[3]);
41 	if (!sname) {
42 		printf("These are the available courses -\n");
43 		list("Linfo");
44 		printf("If you want more information about the courses,\n");
45 		printf("or if you have never used 'learn' before,\n");
46 		printf("press RETURN; otherwise type the name of\n");
47 		printf("the course you want, followed by RETURN.\n");
48 		fflush(stdout);
49 		gets(sname=subname);
50 		if (sname[0] == '\0') {
51 			list("Xinfo");
52 			do {
53 				printf("\nWhich subject?  ");
54 				fflush(stdout);
55 				gets(sname=subname);
56 			} while (sname[0] == '\0');
57 		}
58 	}
59 	chknam(sname);
60 	stat(sname, &statbuf);
61 	total = statbuf.st_size / 16 - 2;	/* size/dirsize-(.+..) */
62 	if (!level) {
63 		printf("If you were in the middle of this subject\n");
64 		printf("and want to start where you left off, type\n");
65 		printf("the last lesson number the computer printed.\n");
66 		printf("If you don't know the number, type in a word\n");
67 		printf("you think might appear in the lesson you want,\n");
68 		printf("and I will look for the first lesson containing it.\n");
69 		printf("To start at the beginning, just hit RETURN.\n");
70 		fflush(stdout);
71 		gets(ans2);
72 		if (ans2[0]==0)
73 			strcpy(ans2,"0");
74 		level=ans2;
75 	}
76 
77 	/* make new directory for user to play in */
78 	if (chdir("/tmp") != 0) {
79 		perror("/tmp");
80 		fprintf(stderr, "Selsub:  couldn't cd to public directory\n");
81 		exit(1);
82 	}
83 	sprintf(dir=dirname, "pl%da", getpid());
84 	sprintf(ans1, "mkdir %s", dir);
85 	system(ans1);
86 	if (chdir(dir) < 0) {
87 		perror(dir);
88 		fprintf(stderr, "Selsub:  couldn't make play directory with %s.\nBye.\n", ans1);
89 		exit(1);
90 	}
91 	/* after this point, we have a working directory. */
92 	/* have to call wrapup to clean up */
93 	if (access(sprintf(ans1, "%s/%s/Init", direct, sname), 04)==0)
94 		if (system(sprintf(ans1, "%s/%s/Init %s", direct, sname, level)) != 0) {
95 			printf("Leaving learn.\n");
96 			wrapup(1);
97 		}
98 }
99 
100 chknam(name)
101 char *name;
102 {
103 	if (access(name, 05) < 0) {
104 		printf("Sorry, there is no subject or lesson named %s.\nBye.\n", name);
105 		exit(1);
106 	}
107 }
108