Changeset 8df7a1c in mainline
- Timestamp:
- 2008-11-29T12:29:00Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 890793b
- Parents:
- a3b5831
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cp/cp.c
ra3b5831 r8df7a1c 35 35 #include <string.h> 36 36 #include <fcntl.h> 37 #include <assert.h> 37 38 #include "config.h" 38 39 #include "util.h" … … 74 75 int fd1, fd2, bytes = 0; 75 76 off_t total = 0; 76 int64_t copied = -1;77 int64_t copied = 0; 77 78 char *buff = NULL; 78 79 … … 87 88 if (-1 == (fd2 = open(dest, O_CREAT))) { 88 89 printf("Unable to open destination file %s\n", dest); 90 close(fd1); 89 91 return copied; 90 92 } … … 97 99 lseek(fd1, 0, SEEK_SET); 98 100 99 if (NULL == (buff = (char *) malloc(blen + 1))) {101 if (NULL == (buff = (char *) malloc(blen))) { 100 102 printf("Unable to allocate enough memory to read %s\n", 101 src); 103 src); 104 copied = -1; 102 105 goto out; 103 106 } 104 107 105 do { 106 if (-1 == (bytes = read(fd1, buff, blen))) 107 break; 108 /* We read a terminating NULL */ 109 if (0 == bytes) { 110 copied ++; 111 break; 112 } 108 for (;;) { 109 ssize_t res; 110 111 bytes = read(fd1, buff, blen); 112 if (bytes <= 0) 113 break; 113 114 copied += bytes; 114 write(fd2, buff, blen); 115 } while (bytes > 0); 116 117 if (bytes == -1) { 118 printf("Error copying %s\n", src); 115 res = bytes; 116 do { 117 /* 118 * Theoretically, it may not be enough to call write() 119 * only once. Also the previous read() may have 120 * returned less data than requested. 121 */ 122 bytes = write(fd2, buff, res); 123 if (bytes < 0) 124 goto err; 125 res -= bytes; 126 } while (res > 0); 127 assert(res == 0); 128 } 129 130 if (bytes < 0) { 131 err: 132 printf("Error copying %s, (%d)\n", src, bytes); 119 133 copied = bytes; 120 goto out;121 134 } 122 135 … … 131 144 void help_cmd_cp(unsigned int level) 132 145 { 146 static char helpfmt[] = 147 "Usage: %s [options] <source> <dest>\n" 148 "Options: (* indicates not yet implemented)\n" 149 " -h, --help A short option summary\n" 150 " -v, --version Print version information and exit\n" 151 "* -V, --verbose Be annoyingly noisy about what's being done\n" 152 "* -f, --force Do not complain when <dest> exists\n" 153 "* -r, --recursive Copy entire directories\n" 154 " -b, --buffer ## Set the read buffer size to ##\n" 155 "Currently, %s is under development, some options may not work.\n"; 133 156 if (level == HELP_SHORT) { 134 157 printf("`%s' copies files and directories\n", cmdname); 135 158 } else { 136 159 help_cmd_cp(HELP_SHORT); 137 printf( 138 "Usage: %s [options] <source> <dest>\n" 139 "Options: (* indicates not yet implemented)\n" 140 " -h, --help A short option summary\n" 141 " -v, --version Print version information and exit\n" 142 "* -V, --verbose Be annoyingly noisy about what's being done\n" 143 "* -f, --force Do not complain when <dest> exists\n" 144 "* -r, --recursive Copy entire directories\n" 145 " -b, --buffer ## Set the read buffer size to ##\n" 146 "Currently, %s is under development, some options might not work.\n", 147 cmdname, cmdname); 160 printf(helpfmt, cmdname, cmdname); 148 161 } 149 162 … … 178 191 if (-1 == (buffer = strtoint(optarg))) { 179 192 printf("%s: Invalid buffer specification, " 180 181 193 "(should be a number greater than zero)\n", 194 cmdname); 182 195 return CMD_FAILURE; 183 196 } … … 195 208 if (argc != 2) { 196 209 printf("%s: invalid number of arguments. Try %s --help\n", 197 210 cmdname, cmdname); 198 211 return CMD_FAILURE; 199 212 } … … 204 217 printf("%d bytes copied\n", ret); 205 218 206 if (ret <= 0)219 if (ret >= 0) 207 220 return CMD_SUCCESS; 208 221 else
Note:
See TracChangeset
for help on using the changeset viewer.