Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/cmds/modules/cat/cat.c

    ra33706e r9d58539  
    5252#define CAT_VERSION "0.0.1"
    5353#define CAT_DEFAULT_BUFLEN 1024
    54 #define CAT_FULL_FILE 0
    55 
     54
     55static const char *cat_oops = "That option is not yet supported\n";
    5656static const char *hexchars = "0123456789abcdef";
    5757
     
    6262static sysarg_t console_rows = 0;
    6363static bool should_quit = false;
    64 static bool dash_represents_stdin = false;
    6564
    6665static console_ctrl_t *console = NULL;
     
    7473        { "more", no_argument, 0, 'm' },
    7574        { "hex", no_argument, 0, 'x' },
    76         { "stdin", no_argument, 0, 's' },
    7775        { 0, 0, 0, 0 }
    7876};
     
    9593                "  -m, --more       Pause after each screen full\n"
    9694                "  -x, --hex        Print bytes as hex values\n"
    97                 "  -s  --stdin      Treat `-' in file list as standard input\n"
    9895                "Currently, %s is under development, some options don't work.\n",
    9996                cmdname, cmdname);
     
    166163}
    167164
    168 static unsigned int cat_file(const char *fname, size_t blen, bool hex,
    169     off64_t head, off64_t tail, bool tail_first)
     165static unsigned int cat_file(const char *fname, size_t blen, bool hex)
    170166{
    171167        int fd, bytes = 0, count = 0, reads = 0;
    172168        char *buff = NULL;
    173169        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);
    186173        if (fd < 0) {
    187174                printf("Unable to open %s\n", fname);
     
    196183        }
    197184
    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         } else
    217                 length = head;
    218 
    219185        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);
    235187                if (bytes > 0) {
     188                        count += bytes;
    236189                        buff[bytes] = '\0';
    237190                        offset = 0;
     
    240193                                        paged_char(hexchars[((uint8_t)buff[i])/16]);
    241194                                        paged_char(hexchars[((uint8_t)buff[i])%16]);
    242                                         paged_char(((count+i+1) & 0xf) == 0 ? '\n' : ' ');
    243195                                }
    244196                                else {
     
    247199                                                /* Reached end of string */
    248200                                                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;
    255201                                        }
    256202                                        paged_char(c);
     
    258204                               
    259205                        }
    260                         count += bytes;
    261206                        reads++;
    262207                }
    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);
    268209
    269210        close(fd);
     
    282223int cmd_cat(char **argv)
    283224{
    284         unsigned int argc, i, ret = 0;
    285         size_t buffer = 0;
     225        unsigned int argc, i, ret = 0, buffer = 0;
    286226        int c, opt_ind;
    287         aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE;
    288227        bool hex = false;
    289228        bool more = false;
    290         bool tailFirst = false;
    291229        sysarg_t rows, cols;
    292230        int rc;
     
    307245
    308246        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);
    310248                switch (c) {
    311249                case 'h':
     
    316254                        return CMD_SUCCESS;
    317255                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;
    323258                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;
    331261                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);
    336263                        break;
    337264                case 'm':
     
    341268                        hex = true;
    342269                        break;
    343                 case 's':
    344                         dash_represents_stdin = true;
    345                         break;
    346270                }
    347271        }
     
    355279        }
    356280
    357         if (buffer < 4)
     281        if (buffer <= 0)
    358282                buffer = CAT_DEFAULT_BUFLEN;
    359283       
     
    371295
    372296        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);
    374298
    375299        if (ret)
Note: See TracChangeset for help on using the changeset viewer.