11 void copy(char *from, char *to, int todir);
12 int copy1(int fdf, int fdt, char *from, char *to);
13 void worker(int fdf, int fdt, char *from, char *to);
15 void failure(void *, char *note);
21 main(int argc, char *argv[])
44 dirb = dirstat(argv[argc-1]);
45 if(dirb!=nil && (dirb->mode&DMDIR))
48 fprint(2, "fcp: %s not a directory\n", argv[argc-1]);
51 for(i=0; i<argc-1; i++)
52 copy(argv[i], argv[argc-1], todir);
58 fprint(2, "usage:\tfcp [-gux] fromfile tofile\n");
59 fprint(2, "\tfcp [-x] fromfile ... todir\n");
64 samefile(Dir *a, char *an, char *bn)
72 if(b->qid.type==a->qid.type)
73 if(b->qid.path==a->qid.path)
74 if(b->qid.vers==a->qid.vers)
77 fprint(2, "fcp: %s and %s are the same file\n", an, bn);
85 copy(char *from, char *to, int todir)
97 sprint(name, "%s/%s", to, elem);
101 if((dirb=dirstat(from))==nil){
102 fprint(2,"fcp: can't stat %s: %r\n", from);
108 fprint(2, "fcp: %s is a directory\n", from);
113 if(samefile(dirb, from, to)){
119 fdf=open(from, OREAD);
121 fprint(2, "fcp: can't open %s: %r\n", from);
126 fdt=create(to, OWRITE, mode);
128 fprint(2, "fcp: can't create %s: %r\n", to);
135 buflen = iounit(fdf);
139 if(copy1(fdf, fdt, from, to)==0 && (xflag || gflag || uflag)){
142 dirt.mtime = dirb->mtime;
143 dirt.mode = dirb->mode;
146 dirt.uid = dirb->uid;
148 dirt.gid = dirb->gid;
149 if(dirfwstat(fdt, &dirt) < 0)
150 fprint(2, "fcp: warning: can't wstat %s: %r\n", to);
158 copy1(int fdf, int fdt, char *from, char *to)
160 int i, n, rv, pid[Nwork];
165 for(i=0; i<Nwork; i++){
166 switch(pid[n] = rfork(RFPROC|RFMEM)){
169 worker(fdf, fdt, from, to);
178 fprint(2, "fcp: rfork: %r\n");
184 while((w = wait()) != nil){
190 postnote(PNPROC, pid[i], "failure");
198 worker(int fdf, int fdt, char *from, char *to)
207 fprint(2, "out of memory\n");
213 while(n = pread(fdf, bp, len, o)){
215 fprint(2, "reading %s at %lld: %r\n", from, o);
218 if(pwrite(fdt, bp, n, o) != n){
219 fprint(2, "writing %s: %r\n", to);
250 failure(void*, char *note)
252 if(strcmp(note, "failure") == 0)