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