11 void copy(char *from, char *to, int todir);
12 int copy1(int fdf, int fdt, char *from, char *to);
15 main(int argc, char *argv[])
38 dirb = dirstat(argv[argc-1]);
39 if(dirb!=nil && (dirb->mode&DMDIR))
42 fprint(2, "cp: %s not a directory\n", argv[argc-1]);
45 for(i=0; i<argc-1; i++)
46 copy(argv[i], argv[argc-1], todir);
52 fprint(2, "usage:\tcp [-gux] fromfile tofile\n");
53 fprint(2, "\tcp [-x] fromfile ... todir\n");
58 samefile(Dir *a, char *an, char *bn)
66 if(b->qid.type==a->qid.type)
67 if(b->qid.path==a->qid.path)
68 if(b->qid.vers==a->qid.vers)
71 fprint(2, "cp: %s and %s are the same file\n", an, bn);
79 copy(char *from, char *to, int todir)
83 int fdf, fdt, fds, mode;
92 name = smprint("%s/%s", to, elem);
97 if((dirb=dirstat(from))==nil){
98 fprint(2,"cp: can't stat %s: %r\n", from);
104 fprint(2, "cp: %s is a directory\n", from);
108 if(samefile(dirb, from, to)){
113 fdf=open(from, OREAD);
115 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);
126 buflen = iounit(fdf);
130 if(dirb->length/2 > buflen){
133 snprint(nam, sizeof nam, "/fd/%dstream", fdf);
134 fds = open(nam, OREAD);
139 snprint(nam, sizeof nam, "/fd/%dstream", fdt);
140 fds = open(nam, OWRITE);
143 if(copy1(fdf, fds < 0 ? fdt : fds, from, to)==0){
144 if(fds >= 0 && write(fds, "", 0) < 0){
145 fprint(2, "cp: error writing %s: %r\n", to);
149 if(xflag || gflag || uflag){
152 dirt.mtime = dirb->mtime;
153 dirt.mode = dirb->mode;
156 dirt.uid = dirb->uid;
158 dirt.gid = dirb->gid;
159 if(dirfwstat(fdt, &dirt) < 0)
160 fprint(2, "cp: warning: can't wstat %s: %r\n", to);
175 copy1(int fdf, int fdt, char *from, char *to)
182 buf = malloc(buflen);
184 fprint(2, "cp: out of memory\n");
188 /* clear any residual error */
192 for(rcount=0;; rcount++) {
193 n = read(fdf, buf, buflen);
196 n1 = write(fdt, buf, n);
198 fprint(2, "cp: error writing %s: %r\n", to);
205 fprint(2, "cp: error reading %s: %r\n", from);