Changes in uspace/app/bdsh/cmds/modules/cat/cat.c [a33706e:9d58539] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
ra33706e r9d58539 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 … … 62 62 static sysarg_t console_rows = 0; 63 63 static bool should_quit = false; 64 static bool dash_represents_stdin = false;65 64 66 65 static console_ctrl_t *console = NULL; … … 74 73 { "more", no_argument, 0, 'm' }, 75 74 { "hex", no_argument, 0, 'x' }, 76 { "stdin", no_argument, 0, 's' },77 75 { 0, 0, 0, 0 } 78 76 }; … … 95 93 " -m, --more Pause after each screen full\n" 96 94 " -x, --hex Print bytes as hex values\n" 97 " -s --stdin Treat `-' in file list as standard input\n"98 95 "Currently, %s is under development, some options don't work.\n", 99 96 cmdname, cmdname); … … 166 163 } 167 164 168 static unsigned int cat_file(const char *fname, size_t blen, bool hex, 169 off64_t head, off64_t tail, bool tail_first) 165 static unsigned int cat_file(const char *fname, size_t blen, bool hex) 170 166 { 171 167 int fd, bytes = 0, count = 0, reads = 0; 172 168 char *buff = NULL; 173 169 int i; 174 size_t offset = 0, copied_bytes = 0; 175 off64_t file_size = 0, length = 0; 176 177 bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0); 178 179 if (reading_stdin) { 180 fd = fileno(stdin); 181 /* Allow storing the whole UTF-8 character. */ 182 blen = STR_BOUNDS(1); 183 } else { 184 fd = open(fname, O_RDONLY); 185 } 170 size_t offset = 0; 171 172 fd = open(fname, O_RDONLY); 186 173 if (fd < 0) { 187 174 printf("Unable to open %s\n", fname); … … 196 183 } 197 184 198 if (tail != CAT_FULL_FILE) {199 file_size = lseek(fd, 0, SEEK_END);200 if (head == CAT_FULL_FILE) {201 head = file_size;202 length = tail;203 } else if (tail_first) {204 length = head;205 } else {206 if (tail > head)207 tail = head;208 length = tail;209 }210 211 if (tail_first) {212 lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);213 } else {214 lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);215 }216 } else217 length = head;218 219 185 do { 220 size_t bytes_to_read; 221 if (reading_stdin) { 222 bytes_to_read = 1; 223 } else { 224 if ((length != CAT_FULL_FILE) 225 && (length - (off64_t)count <= (off64_t)(blen - copied_bytes))) { 226 bytes_to_read = (size_t) (length - count); 227 } else { 228 bytes_to_read = blen - copied_bytes; 229 } 230 } 231 bytes = read(fd, buff + copied_bytes, bytes_to_read); 232 bytes += copied_bytes; 233 copied_bytes = 0; 234 186 bytes = read(fd, buff, blen); 235 187 if (bytes > 0) { 188 count += bytes; 236 189 buff[bytes] = '\0'; 237 190 offset = 0; … … 240 193 paged_char(hexchars[((uint8_t)buff[i])/16]); 241 194 paged_char(hexchars[((uint8_t)buff[i])%16]); 242 paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');243 195 } 244 196 else { … … 247 199 /* Reached end of string */ 248 200 break; 249 } else if (c == U_SPECIAL && offset + 2 >= (size_t)bytes) {250 /* If an extended character is cut off due to the size of the buffer,251 we will copy it over to the next buffer so it can be read correctly. */252 copied_bytes = bytes - offset + 1;253 memcpy(buff, buff + offset - 1, copied_bytes);254 break;255 201 } 256 202 paged_char(c); … … 258 204 259 205 } 260 count += bytes;261 206 reads++; 262 207 } 263 264 if (reading_stdin) { 265 fflush(stdout); 266 } 267 } while (bytes > 0 && !should_quit && (count < length || length == CAT_FULL_FILE)); 208 } while (bytes > 0 && !should_quit); 268 209 269 210 close(fd); … … 282 223 int cmd_cat(char **argv) 283 224 { 284 unsigned int argc, i, ret = 0; 285 size_t buffer = 0; 225 unsigned int argc, i, ret = 0, buffer = 0; 286 226 int c, opt_ind; 287 aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE;288 227 bool hex = false; 289 228 bool more = false; 290 bool tailFirst = false;291 229 sysarg_t rows, cols; 292 230 int rc; … … 307 245 308 246 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 309 c = getopt_long(argc, argv, "xhvmH:t:b: s", long_options, &opt_ind);247 c = getopt_long(argc, argv, "xhvmH:t:b:", long_options, &opt_ind); 310 248 switch (c) { 311 249 case 'h': … … 316 254 return CMD_SUCCESS; 317 255 case 'H': 318 if (!optarg || str_uint64_t(optarg, NULL, 10, false, &head) != EOK ) { 319 puts("Invalid head size\n"); 320 return CMD_FAILURE; 321 } 322 break; 256 printf("%s", cat_oops); 257 return CMD_FAILURE; 323 258 case 't': 324 if (!optarg || str_uint64_t(optarg, NULL, 10, false, &tail) != EOK ) { 325 puts("Invalid tail size\n"); 326 return CMD_FAILURE; 327 } 328 if (head == CAT_FULL_FILE) 329 tailFirst = true; 330 break; 259 printf("%s", cat_oops); 260 return CMD_FAILURE; 331 261 case 'b': 332 if (!optarg || str_size_t(optarg, NULL, 10, false, &buffer) != EOK ) { 333 puts("Invalid buffer size\n"); 334 return CMD_FAILURE; 335 } 262 printf("%s", cat_oops); 336 263 break; 337 264 case 'm': … … 341 268 hex = true; 342 269 break; 343 case 's':344 dash_represents_stdin = true;345 break;346 270 } 347 271 } … … 355 279 } 356 280 357 if (buffer < 4)281 if (buffer <= 0) 358 282 buffer = CAT_DEFAULT_BUFLEN; 359 283 … … 371 295 372 296 for (i = optind; argv[i] != NULL && !should_quit; i++) 373 ret += cat_file(argv[i], buffer, hex , head, tail, tailFirst);297 ret += cat_file(argv[i], buffer, hex); 374 298 375 299 if (ret)
Note:
See TracChangeset
for help on using the changeset viewer.