1 #ifndef lint 2 static char sccsid[] = "@(#)getlesson.c 4.3 (Berkeley) 05/10/89"; 3 #endif not lint 4 5 #include "stdio.h" 6 #include "lrnref.h" 7 #include "pathnames.h" 8 9 char * 10 getlesson() 11 { 12 register char *p; 13 char ans[80], line[200]; 14 int isnum, found, fd[2]; 15 FILE *fp; 16 17 sprintf(ans, "%s/%s/L%s", direct, sname, level); 18 if (access(ans, 04) == 0) /* there is a file */ 19 return(level); 20 isnum = 1; 21 for (p=level; *p; p++) /* accept: (digit|dot)*anychar */ 22 if (*p != '.' && (*p < '0' || *p > '9') && *(p+1) != '\0') 23 isnum = 0; 24 if (isnum) { 25 strcpy(line, level); 26 p = level; 27 while (*p != '.' && *p >= '0' && *p <= '9') 28 p++; 29 *p = '\0'; 30 strcat(level, ".1a"); 31 sprintf(ans, "%s/%s/L%s", direct, sname, level); 32 if (access(ans, 04) == 0) { /* there is a file */ 33 printf("There is no lesson %s; trying lesson %s instead.\n\n", line, level); 34 return(level); 35 } 36 printf("There is no lesson %s.\n", line); 37 return(0); 38 } 39 /* fgrep through lessons for one containing the string in 'level' */ 40 pipe(fd); 41 if (fork() == 0) { 42 close(fd[0]); 43 dup2(fd[1], 1); 44 sprintf(ans,"cd %s/%s ; fgrep '%s' L?.* L??.* L???.*", direct, sname, level); 45 execl(_PATH_CSHELL, "csh", "-cf", ans, 0); 46 perror(_PATH_CSHELL); 47 fprintf(stderr, "Getlesson: can't do %s\n", ans); 48 } 49 close(fd[1]); 50 fp = fdopen(fd[0], "r"); 51 found = 0; 52 while (fgets(line, 200, fp) != NULL) { 53 for (p=line; *p != ':'; p++) ; 54 p++; 55 if (*p == '#') 56 continue; 57 else { 58 found = 1; 59 break; 60 } 61 } 62 /*fclose(fp);*/ 63 if (found) { 64 *--p = '\0'; 65 strcpy(level, &line[1]); 66 sprintf(ans, "%s/%s/L%s", direct, sname, level); 67 if (access(ans, 04) == 0) { /* there is a file */ 68 printf("Trying lesson %s.\n\n", level); 69 return(level); 70 } 71 } 72 printf("There is no lesson containing \"%s\".\n", level); 73 return(0); 74 } 75