Changes in uspace/app/bdsh/cmds/modules/cat/cat.c [cc9f314:7c014d1] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
rcc9f314 r7c014d1 52 52 #define CAT_VERSION "0.0.1" 53 53 #define CAT_DEFAULT_BUFLEN 1024 54 #define CAT_FULL_FILE 0 55 54 55 static const char *cat_oops = "That option is not yet supported\n"; 56 56 static const char *hexchars = "0123456789abcdef"; 57 57 … … 163 163 } 164 164 165 static unsigned int cat_file(const char *fname, size_t blen, bool hex, 166 off64_t head, off64_t tail, bool tail_first) 165 static unsigned int cat_file(const char *fname, size_t blen, bool hex) 167 166 { 168 167 int fd, bytes = 0, count = 0, reads = 0; 169 168 char *buff = NULL; 170 169 int i; 171 size_t offset = 0, copied_bytes = 0; 172 off64_t file_size = 0, length = 0; 170 size_t offset = 0; 173 171 174 172 fd = open(fname, O_RDONLY); … … 185 183 } 186 184 187 if (tail != CAT_FULL_FILE) {188 file_size = lseek(fd, 0, SEEK_END);189 if (head == CAT_FULL_FILE) {190 head = file_size;191 length = tail;192 } else if (tail_first) {193 length = head;194 } else {195 if (tail > head)196 tail = head;197 length = tail;198 }199 200 if (tail_first) {201 lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);202 } else {203 lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);204 }205 } else206 length = head;207 208 185 do { 209 bytes = read(fd, buff + copied_bytes, ( 210 (length != CAT_FULL_FILE && length - (off64_t)count <= (off64_t)(blen - copied_bytes)) ? 211 (size_t)(length - count) : 212 (blen - copied_bytes) ) ); 213 bytes += copied_bytes; 214 copied_bytes = 0; 215 186 bytes = read(fd, buff, blen); 216 187 if (bytes > 0) { 188 count += bytes; 217 189 buff[bytes] = '\0'; 218 190 offset = 0; … … 221 193 paged_char(hexchars[((uint8_t)buff[i])/16]); 222 194 paged_char(hexchars[((uint8_t)buff[i])%16]); 223 paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');224 195 } 225 196 else { … … 228 199 /* Reached end of string */ 229 200 break; 230 } else if (c == U_SPECIAL && offset + 2 >= (size_t)bytes) {231 /* If an extended character is cut off due to the size of the buffer,232 we will copy it over to the next buffer so it can be read correctly. */233 copied_bytes = bytes - offset + 1;234 memcpy(buff, buff + offset - 1, copied_bytes);235 break;236 201 } 237 202 paged_char(c); … … 239 204 240 205 } 241 count += bytes;242 206 reads++; 243 207 } 244 } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE));208 } while (bytes > 0 && !should_quit); 245 209 246 210 close(fd); … … 259 223 int cmd_cat(char **argv) 260 224 { 261 unsigned int argc, i, ret = 0; 262 size_t buffer = 0; 225 unsigned int argc, i, ret = 0, buffer = 0; 263 226 int c, opt_ind; 264 aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE;265 227 bool hex = false; 266 228 bool more = false; 267 bool tailFirst = false;268 229 sysarg_t rows, cols; 269 230 int rc; … … 293 254 return CMD_SUCCESS; 294 255 case 'H': 295 if (!optarg || str_uint64_t(optarg, NULL, 10, false, &head) != EOK ) { 296 puts("Invalid head size\n"); 297 return CMD_FAILURE; 298 } 299 break; 256 printf("%s", cat_oops); 257 return CMD_FAILURE; 300 258 case 't': 301 if (!optarg || str_uint64_t(optarg, NULL, 10, false, &tail) != EOK ) { 302 puts("Invalid tail size\n"); 303 return CMD_FAILURE; 304 } 305 if (head == CAT_FULL_FILE) 306 tailFirst = true; 307 break; 259 printf("%s", cat_oops); 260 return CMD_FAILURE; 308 261 case 'b': 309 if (!optarg || str_size_t(optarg, NULL, 10, false, &buffer) != EOK ) { 310 puts("Invalid buffer size\n"); 311 return CMD_FAILURE; 312 } 262 printf("%s", cat_oops); 313 263 break; 314 264 case 'm': … … 329 279 } 330 280 331 if (buffer < 4)281 if (buffer <= 0) 332 282 buffer = CAT_DEFAULT_BUFLEN; 333 283 … … 345 295 346 296 for (i = optind; argv[i] != NULL && !should_quit; i++) 347 ret += cat_file(argv[i], buffer, hex , head, tail, tailFirst);297 ret += cat_file(argv[i], buffer, hex); 348 298 349 299 if (ret)
Note:
See TracChangeset
for help on using the changeset viewer.