xref: /original-bsd/usr.bin/learn/learn/start.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)start.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "stdio.h"
13 #include "lrnref.h"
14 #include <sys/types.h>
15 #include <dirent.h>
16 #include <unistd.h>
17 
18 lstart(lesson)
19 char *lesson;
20 {
21 	register struct dirent *ep;
22 	int c, n;
23 	char where [100];
24 	DIR *dp;
25 
26 	if ((dp = opendir(".")) == NULL) {	/* clean up play directory */
27 		perror("Start:  play directory");
28 		wrapup(1);
29 	}
30 	while ((ep = readdir(dp)) != NULL) {
31 		if (ep->d_ino == 0)
32 			continue;
33 		n = ep->d_namlen;
34 		if (n >= 2 && ep->d_name[n-2] == '.' && ep->d_name[n-1] == 'c')
35 			continue;
36 		c = ep->d_name[0];
37 		if (c>='a' && c<= 'z')
38 			unlink(ep->d_name);
39 	}
40 	(void) closedir(dp);
41 	if (ask)
42 		return;
43 	sprintf(where, "%s/%s/L%s", direct, sname, lesson);
44 	if (access(where, R_OK)==0)	/* there is a file */
45 		return;
46 	perror(where);
47 	fprintf(stderr, "Start:  no lesson %s\n",lesson);
48 	wrapup(1);
49 }
50 
51 fcopy(new,old)
52 char *new, *old;
53 {
54 	char b[BUFSIZ];
55 	int n, fn, fo;
56 	fn = creat(new, 0666);
57 	fo = open(old,0);
58 	if (fo<0) return;
59 	if (fn<0) return;
60 	while ( (n=read(fo, b, BUFSIZ)) > 0)
61 		write(fn, b, n);
62 	close(fn);
63 	close(fo);
64 }
65