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 noc;  /* hack for bin package install -- see port pkg/INSTALL */
14 
h(home,uid,gid,mode)15 void h(home,uid,gid,mode)
16 char *home;
17 int uid;
18 int gid;
19 int mode;
20 {
21   if (mkdir(home,0700) == -1)
22     if (errno != error_exist)
23       strerr_die4sys(111,FATAL,"unable to mkdir ",home,": ");
24   if (chown(home,uid,gid) == -1)
25     strerr_die4sys(111,FATAL,"unable to chown ",home,": ");
26   if (chmod(home,mode) == -1)
27     strerr_die4sys(111,FATAL,"unable to chmod ",home,": ");
28 }
29 
d(home,subdir,uid,gid,mode)30 void d(home,subdir,uid,gid,mode)
31 char *home;
32 char *subdir;
33 int uid;
34 int gid;
35 int mode;
36 {
37   if (chdir(home) == -1)
38     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
39   if (mkdir(subdir,0700) == -1)
40     if (errno != error_exist)
41       strerr_die6sys(111,FATAL,"unable to mkdir ",home,"/",subdir,": ");
42   if (chown(subdir,uid,gid) == -1)
43     strerr_die6sys(111,FATAL,"unable to chown ",home,"/",subdir,": ");
44   if (chmod(subdir,mode) == -1)
45     strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",subdir,": ");
46 }
47 
p(home,fifo,uid,gid,mode)48 void p(home,fifo,uid,gid,mode)
49 char *home;
50 char *fifo;
51 int uid;
52 int gid;
53 int mode;
54 {
55   if (chdir(home) == -1)
56     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
57   if (fifo_make(fifo,0700) == -1)
58     if (errno != error_exist)
59       strerr_die6sys(111,FATAL,"unable to mkfifo ",home,"/",fifo,": ");
60   if (chown(fifo,uid,gid) == -1)
61     strerr_die6sys(111,FATAL,"unable to chown ",home,"/",fifo,": ");
62   if (chmod(fifo,mode) == -1)
63     strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",fifo,": ");
64 }
65 
66 char inbuf[SUBSTDIO_INSIZE];
67 char outbuf[SUBSTDIO_OUTSIZE];
68 substdio ssin;
69 substdio ssout;
70 
c(home,subdir,file,uid,gid,mode)71 void c(home,subdir,file,uid,gid,mode)
72 char *home;
73 char *subdir;
74 char *file;
75 int uid;
76 int gid;
77 int mode;
78 {
79  if (!noc) {
80   int fdin;
81   int fdout;
82 
83   if (fchdir(fdsourcedir) == -1)
84     strerr_die2sys(111,FATAL,"unable to switch back to source directory: ");
85 
86   fdin = open_read(file);
87   if (fdin == -1)
88     strerr_die4sys(111,FATAL,"unable to read ",file,": ");
89   substdio_fdbuf(&ssin,read,fdin,inbuf,sizeof inbuf);
90 
91   if (chdir(home) == -1)
92     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
93   if (chdir(subdir) == -1)
94     strerr_die6sys(111,FATAL,"unable to switch to ",home,"/",subdir,": ");
95 
96   fdout = open_trunc(file);
97   if (fdout == -1)
98     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
99   substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof outbuf);
100 
101   switch(substdio_copy(&ssout,&ssin)) {
102     case -2:
103       strerr_die4sys(111,FATAL,"unable to read ",file,": ");
104     case -3:
105       strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
106   }
107 
108   close(fdin);
109   if (substdio_flush(&ssout) == -1)
110     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
111   if (fsync(fdout) == -1)
112     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
113   if (close(fdout) == -1) /* NFS silliness */
114     strerr_die6sys(111,FATAL,"unable to write .../",subdir,"/",file,": ");
115 
116   if (chown(file,uid,gid) == -1)
117     strerr_die6sys(111,FATAL,"unable to chown .../",subdir,"/",file,": ");
118   if (chmod(file,mode) == -1)
119     strerr_die6sys(111,FATAL,"unable to chmod .../",subdir,"/",file,": ");
120  }
121 }
122 
z(home,file,len,uid,gid,mode)123 void z(home,file,len,uid,gid,mode)
124 char *home;
125 char *file;
126 int len;
127 int uid;
128 int gid;
129 int mode;
130 {
131   int fdout;
132 
133   if (chdir(home) == -1)
134     strerr_die4sys(111,FATAL,"unable to switch to ",home,": ");
135 
136   fdout = open_trunc(file);
137   if (fdout == -1)
138     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
139   substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof outbuf);
140 
141   while (len-- > 0)
142     if (substdio_put(&ssout,"",1) == -1)
143       strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
144 
145   if (substdio_flush(&ssout) == -1)
146     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
147   if (fsync(fdout) == -1)
148     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
149   if (close(fdout) == -1) /* NFS silliness */
150     strerr_die6sys(111,FATAL,"unable to write ",home,"/",file,": ");
151 
152   if (chown(file,uid,gid) == -1)
153     strerr_die6sys(111,FATAL,"unable to chown ",home,"/",file,": ");
154   if (chmod(file,mode) == -1)
155     strerr_die6sys(111,FATAL,"unable to chmod ",home,"/",file,": ");
156 }
157 
main(int argc,char ** argv)158 int main(int argc, char **argv)
159 {
160   noc=--argc;
161   fdsourcedir = open_read(".");
162   if (fdsourcedir == -1)
163     strerr_die2sys(111,FATAL,"unable to open current directory: ");
164 
165   umask(077);
166   hier();
167   _exit(0);
168 }
169