Mercurial > hg > toybox
comparison toys/cp.c @ 565:44abf4d901f3
Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 14 Apr 2012 22:30:41 -0500 |
parents | 878b94b32866 |
children | 9802b2afbce8 |
comparison
equal
deleted
inserted
replaced
564:9530899eee51 | 565:44abf4d901f3 |
---|---|
8 * | 8 * |
9 * "R+ra+d+p+r" | 9 * "R+ra+d+p+r" |
10 USE_CP(NEWTOY(cp, "<2vslrR+rdpa+d+p+rHLPif", TOYFLAG_BIN)) | 10 USE_CP(NEWTOY(cp, "<2vslrR+rdpa+d+p+rHLPif", TOYFLAG_BIN)) |
11 | 11 |
12 config CP | 12 config CP |
13 bool "cp" | 13 bool "cp (broken by dirtree changes)" |
14 default y | 14 default n |
15 help | 15 help |
16 usage: cp -fiprdal SOURCE... DEST | 16 usage: cp -fiprdal SOURCE... DEST |
17 | 17 |
18 Copy files from SOURCE to DEST. If more than one SOURCE, DEST must | 18 Copy files from SOURCE to DEST. If more than one SOURCE, DEST must |
19 be a directory. | 19 be a directory. |
126 xclose(fdout); | 126 xclose(fdout); |
127 } | 127 } |
128 | 128 |
129 // Callback from dirtree_read() for each file/directory under a source dir. | 129 // Callback from dirtree_read() for each file/directory under a source dir. |
130 | 130 |
131 int cp_node(char *path, struct dirtree *node) | 131 int cp_node(struct dirtree *node) |
132 { | 132 { |
133 char *path = dirtree_path(node, 0); // TODO: use openat() instead | |
133 char *s = path+strlen(path); | 134 char *s = path+strlen(path); |
134 struct dirtree *n; | 135 struct dirtree *n; |
135 | 136 |
136 // Find appropriate chunk of path for destination. | 137 // Find appropriate chunk of path for destination. |
137 | 138 |
146 if (s != path) s++; | 147 if (s != path) s++; |
147 | 148 |
148 s = xmsprintf("%s/%s", TT.destname, s); | 149 s = xmsprintf("%s/%s", TT.destname, s); |
149 cp_file(path, s, &(node->st)); | 150 cp_file(path, s, &(node->st)); |
150 free(s); | 151 free(s); |
152 free(path); // redo this whole darn function. | |
151 | 153 |
152 return 0; | 154 return 0; |
153 } | 155 } |
154 | 156 |
155 void cp_main(void) | 157 void cp_main(void) |
207 cp_file(src, dst, &st); | 209 cp_file(src, dst, &st); |
208 | 210 |
209 TT.keep_symlinks++; | 211 TT.keep_symlinks++; |
210 strncpy(toybuf, src, sizeof(toybuf)-1); | 212 strncpy(toybuf, src, sizeof(toybuf)-1); |
211 toybuf[sizeof(toybuf)-1]=0; | 213 toybuf[sizeof(toybuf)-1]=0; |
212 dirtree_read(toybuf, NULL, cp_node); | 214 dirtree_read(toybuf, cp_node); |
213 } else error_msg("Skipped dir '%s'", src); | 215 } else error_msg("Skipped dir '%s'", src); |
214 } else cp_file(src, dst, &st); | 216 } else cp_file(src, dst, &st); |
215 if (TT.destisdir) free(dst); | 217 if (TT.destisdir) free(dst); |
216 } | 218 } |
217 | 219 |