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(copy1(fdf, fds < 0 ? fdt : fds, from, to)==0){
131 if(fds >= 0 && write(fds, "", 0) < 0){
132 fprint(2, "cp: error writing %s: %r\n", to);
136 if(xflag || gflag || uflag){
139 dirt.mtime = dirb->mtime;
140 dirt.mode = dirb->mode;
143 dirt.uid = dirb->uid;
145 dirt.gid = dirb->gid;
146 if(dirfwstat(fdt, &dirt) < 0)
147 fprint(2, "cp: warning: can't wstat %s: %r\n", to);
162 copy1(int fdf, int fdt, char *from, char *to)
169 buf = malloc(buflen);
171 fprint(2, "cp: out of memory\n");
175 /* clear any residual error */
179 for(rcount=0;; rcount++) {
180 n = read(fdf, buf, buflen);
183 n1 = write(fdt, buf, n);
185 fprint(2, "cp: error writing %s: %r\n", to);
192 fprint(2, "cp: error reading %s: %r\n", from);