Changes in uspace/app/bdsh/cmds/modules/mkfile/mkfile.c [8d2dd7f2:f77c1c9] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/mkfile/mkfile.c
r8d2dd7f2 rf77c1c9 31 31 #include <stdlib.h> 32 32 #include <stdint.h> 33 #include <str_error.h> 33 34 #include <dirent.h> 34 35 #include <macros.h> … … 85 86 * 86 87 * @param str String containing the size specification. 87 * @return Non-negative size in bytes on success, -1 on failure. 88 * @param rsize Place to store size in bytes 89 * @return EOK on success or error code 88 90 */ 89 static ssize_t read_size(const char *str)91 static int read_size(const char *str, size_t *rsize) 90 92 { 91 s size_t number, unit;93 size_t number, unit; 92 94 char *ep; 93 95 94 96 number = strtol(str, &ep, 10); 95 if (ep[0] == '\0') 96 return number; 97 if (ep[0] == '\0') { 98 *rsize = number; 99 return EOK; 100 } 97 101 98 102 if (ep[1] != '\0') 99 return -1;103 return EINVAL; 100 104 101 105 switch (tolower(ep[0])) { … … 103 107 case 'm': unit = 1024*1024; break; 104 108 case 'g': unit = 1024*1024*1024; break; 105 default: return -1; 106 } 107 108 return number * unit; 109 default: return EINVAL; 110 } 111 112 *rsize = number * unit; 113 return EOK; 109 114 } 110 115 … … 114 119 int c, opt_ind; 115 120 int fd; 116 ssize_t file_size; 117 ssize_t total_written; 118 ssize_t to_write, rc, rc2 = 0; 121 size_t file_size; 122 size_t total_written; 123 size_t to_write; 124 size_t nwritten; 125 int rc; 119 126 char *file_name; 120 127 void *buffer; … … 136 143 break; 137 144 case 's': 138 file_size = read_size(optarg);139 if ( file_size < 0) {145 rc = read_size(optarg, &file_size); 146 if (rc != EOK) { 140 147 printf("%s: Invalid file size specification.\n", 141 148 cmdname); … … 156 163 file_name = argv[optind]; 157 164 158 fd = vfs_lookup_open(file_name, WALK_REGULAR | WALK_MUST_CREATE, MODE_WRITE);159 if ( fd < 0) {165 rc = vfs_lookup_open(file_name, WALK_REGULAR | WALK_MUST_CREATE, MODE_WRITE, &fd); 166 if (rc != EOK) { 160 167 printf("%s: failed to create file %s.\n", cmdname, file_name); 161 168 return CMD_FAILURE; … … 166 173 167 174 pos = file_size - 1; 168 rc 2 = vfs_write(fd, &pos, &byte, sizeof(char));169 if (rc 2 < 0) {175 rc = vfs_write(fd, &pos, &byte, sizeof(char), &nwritten); 176 if (rc != EOK) { 170 177 vfs_put(fd); 171 178 goto error; … … 183 190 while (total_written < file_size) { 184 191 to_write = min(file_size - total_written, BUFFER_SIZE); 185 rc = vfs_write(fd, &pos, buffer, to_write );186 if (rc <= 0) {187 printf("%s: Error writing file (% d).\n", cmdname, errno);192 rc = vfs_write(fd, &pos, buffer, to_write, &nwritten); 193 if (rc != EOK) { 194 printf("%s: Error writing file (%s).\n", cmdname, str_error(errno)); 188 195 vfs_put(fd); 189 196 free(buffer); 190 197 return CMD_FAILURE; 191 198 } 192 total_written += rc;199 total_written += nwritten; 193 200 } 194 201 … … 200 207 return CMD_SUCCESS; 201 208 error: 202 printf("%s: Error writing file (% d).\n", cmdname, errno);209 printf("%s: Error writing file (%s).\n", cmdname, str_error(errno)); 203 210 return CMD_FAILURE; 204 211 }
Note:
See TracChangeset
for help on using the changeset viewer.