chiark / gitweb /
Import fastforward 0.51
[fastforward] / install.c
1 #include "substdio.h"
2 #include "strerr.h"
3 #include "error.h"
4 #include "open.h"
5 #include "readwrite.h"
6 #include "exit.h"
7
8 extern void hier();
9
10 #define FATAL "install: fatal: "
11
12 int fdsourcedir = -1;
13
14 void h(home,uid,gid,mode)
15 char *home;
16 int uid;
17 int gid;
18 int mode;
19 {
20   if (mkdir(home,0700) == -1)
21     if (errno != error_exist)
22       strerr_die4sys(111,FATAL,"unable to mkdir ",home,": ");
23   if (chown(home,uid,gid) == -1)
24     strerr_die4sys(111,FATAL,"unable to chown ",home,": ");
25   if (chmod(home,mode) == -1)
26     strerr_die4sys(111,FATAL,"unable to chmod ",home,": ");
27 }
28
29 void d(home,subdir,uid,gid,mode)
30 char *home;
31 char *subdir;
32 int uid;
33 int gid;
34 int mode;
35 {
36   if (chdir(home) == -1)
37     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
38   if (mkdir(subdir,0700) == -1)
39     if (errno != error_exist)
40       strerr_die6sys(111,FATAL,"unable to mkdir ",home,"/",subdir,": ");
41   if (chown(subdir,uid,gid) == -1)
42     strerr_die6sys(111,FATAL,"unable to chown ",home,"/",subdir,": ");
43   if (chmod(subdir,mode) == -1)
44     strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",subdir,": ");
45 }
46
47 char inbuf[SUBSTDIO_INSIZE];
48 char outbuf[SUBSTDIO_OUTSIZE];
49 substdio ssin;
50 substdio ssout;
51
52 void c(home,subdir,file,uid,gid,mode)
53 char *home;
54 char *subdir;
55 char *file;
56 int uid;
57 int gid;
58 int mode;
59 {
60   int fdin;
61   int fdout;
62
63   if (fchdir(fdsourcedir) == -1)
64     strerr_die2sys(111,FATAL,"unable to switch back to source directory: ");
65
66   fdin = open_read(file);
67   if (fdin == -1)
68     strerr_die4sys(111,FATAL,"unable to read ",file,": ");
69   substdio_fdbuf(&ssin,read,fdin,inbuf,sizeof inbuf);
70
71   if (chdir(home) == -1)
72     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
73   if (chdir(subdir) == -1)
74     strerr_die6sys(111,FATAL,"unable to switch to ",home,"/",subdir,": ");
75
76   fdout = open_trunc(file);
77   if (fdout == -1)
78     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
79   substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof outbuf);
80
81   switch(substdio_copy(&ssout,&ssin)) {
82     case -2:
83       strerr_die4sys(111,FATAL,"unable to read ",file,": ");
84     case -3:
85       strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
86   }
87
88   close(fdin);
89   if (substdio_flush(&ssout) == -1)
90     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
91   if (fsync(fdout) == -1)
92     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
93   if (close(fdout) == -1) /* NFS silliness */
94     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
95
96   if (chown(file,uid,gid) == -1)
97     strerr_die6sys(111,FATAL,"unable to chown .../",subdir,"/",file,": ");
98   if (chmod(file,mode) == -1)
99     strerr_die6sys(111,FATAL,"unable to chmod .../",subdir,"/",file,": ");
100 }
101
102 void main()
103 {
104   fdsourcedir = open_read(".");
105   if (fdsourcedir == -1)
106     strerr_die2sys(111,FATAL,"unable to open current directory: ");
107
108   umask(077);
109   hier();
110   _exit(0);
111 }