updates from John Kunze
[unix-history] / usr / src / usr.bin / learn / learn / start.c
CommitLineData
48330703 1#ifndef lint
0acfbe6a 2static char sccsid[] = "@(#)start.c 4.5 (Berkeley) %G%";
48330703
KM
3#endif not lint
4
5#include "stdio.h"
2b9e3de9 6#include "lrnref.h"
48330703 7#include <sys/types.h>
0acfbe6a 8#ifndef DIR
48330703 9#include <sys/dir.h>
0acfbe6a 10#endif
3bb0d2af 11
48330703
KM
12start(lesson)
13char *lesson;
14{
06f0a4af
BJ
15 struct direct dbuf;
16 register struct direct *ep = &dbuf; /* directory entry pointer */
17 int c, n;
48330703
KM
18 char where [100];
19
0acfbe6a 20#ifdef BSD4_2
06f0a4af
BJ
21 DIR *dp;
22#define OPENDIR(s) ((dp = opendir(s)) != NULL)
23#define DIRLOOP(s) for (s = readdir(dp); s != NULL; s = readdir(dp))
06f0a4af
BJ
24#define EPSTRLEN ep->d_namlen
25#define CLOSEDIR closedir(dp)
26#else
27 int f;
28#define OPENDIR(s) ((f = open(s, 0)) >= 0)
29#define DIRLOOP(s) while (read(f, s, sizeof *s) == sizeof *s)
30#define EPSTRLEN strlen(ep->d_name)
31#define CLOSEDIR close(f)
32#endif
33
0acfbe6a
JB
34 if (!OPENDIR(".")) { /* clean up play directory */
35 perror("Start: play directory");
36 wrapup(1);
37 }
06f0a4af
BJ
38 DIRLOOP(ep) {
39 if (ep->d_ino == 0)
40 continue;
41 n = EPSTRLEN;
42 if (ep->d_name[n-2] == '.' && ep->d_name[n-1] == 'c')
43 continue;
44 c = ep->d_name[0];
45 if (c>='a' && c<= 'z')
46 unlink(ep->d_name);
47 }
48 CLOSEDIR;
48330703
KM
49 if (ask)
50 return;
2b9e3de9 51 sprintf(where, "%s/%s/L%s", direct, sname, lesson);
48330703
KM
52 if (access(where, 04)==0) /* there is a file */
53 return;
2b9e3de9
KM
54 perror(where);
55 fprintf(stderr, "Start: no lesson %s\n",lesson);
48330703
KM
56 wrapup(1);
57}
58
59fcopy(new,old)
60char *new, *old;
61{
62 char b[BUFSIZ];
63 int n, fn, fo;
64 fn = creat(new, 0666);
65 fo = open(old,0);
66 if (fo<0) return;
67 if (fn<0) return;
68 while ( (n=read(fo, b, BUFSIZ)) > 0)
69 write(fn, b, n);
70 close(fn);
71 close(fo);
72}