10 void copy(char *from, char *to, int todir);
11 int copy1(int fdf, int fdt, char *from, char *to);
14 main(int argc, char *argv[])
37 dirb = dirstat(argv[argc-1]);
38 if(dirb!=nil && (dirb->mode&DMDIR))
41 fprint(2, "cp: %s not a directory\n", argv[argc-1]);
44 for(i=0; i<argc-1; i++)
45 copy(argv[i], argv[argc-1], todir);
51 fprint(2, "usage:\tcp [-gux] fromfile tofile\n");
52 fprint(2, "\tcp [-x] fromfile ... todir\n");
57 samefile(Dir *a, char *an, char *bn)
65 if(b->qid.type==a->qid.type)
66 if(b->qid.path==a->qid.path)
67 if(b->qid.vers==a->qid.vers)
70 fprint(2, "cp: %s and %s are the same file\n", an, bn);
78 copy(char *from, char *to, int todir)
90 sprint(name, "%s/%s", to, elem);
94 if((dirb=dirstat(from))==nil){
95 fprint(2,"cp: can't stat %s: %r\n", from);
101 fprint(2, "cp: %s is a directory\n", from);
106 if(samefile(dirb, from, to)){
112 fdf=open(from, OREAD);
114 fprint(2, "cp: can't open %s: %r\n", from);
119 fdt=create(to, OWRITE, mode);
121 fprint(2, "cp: can't create %s: %r\n", to);
127 if(copy1(fdf, fdt, from, to)==0 && (xflag || gflag || uflag)){
130 dirt.mtime = dirb->mtime;
131 dirt.mode = dirb->mode;
134 dirt.uid = dirb->uid;
136 dirt.gid = dirb->gid;
137 if(dirfwstat(fdt, &dirt) < 0)
138 fprint(2, "cp: warning: can't wstat %s: %r\n", to);
146 copy1(int fdf, int fdt, char *from, char *to)
154 /* clear any residual error */
158 for(rcount=0;; rcount++) {
159 n = read(fdf, buf, DEFB);
162 n1 = write(fdt, buf, n);
164 fprint(2, "cp: error writing %s: %r\n", to);
171 fprint(2, "cp: error reading %s: %r\n", from);