/* Main entry point for cat, accepts an array of arguments */ int cmd_cat(char **argv) { unsigned int argc, i, ret = 0; size_t buffer = 0; int c, opt_ind; aoff64_t head = CAT_FULL_FILE, tail = CAT_FULL_FILE; bool hex = false; bool more = false; bool tailFirst = false; sysarg_t rows, cols; int rc; /* * reset global state * TODO: move to structure? */ paging_enabled = false; chars_remaining = 0; lines_remaining = 0; console_cols = 0; console_rows = 0; should_quit = false; console = console_init(stdin, stdout); number = false; lineno = 0; /* This enables printing of the first number. */ last_char_was_newline = true; argc = cli_count_args(argv); for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) { c = getopt_long(argc, argv, "xhvmH:t:b:s:n", long_options, &opt_ind); switch (c) { case 'h': help_cmd_cat(HELP_LONG); return CMD_SUCCESS; case 'v': printf("%s\n", CAT_VERSION); return CMD_SUCCESS; case 'H': if (!optarg || str_uint64_t(optarg, NULL, 10, false, &head) != EOK ) { puts("Invalid head size\n"); return CMD_FAILURE; } break; case 't': if (!optarg || str_uint64_t(optarg, NULL, 10, false, &tail) != EOK ) { puts("Invalid tail size\n"); return CMD_FAILURE; } if (head == CAT_FULL_FILE) tailFirst = true; break; case 'b': if (!optarg || str_size_t(optarg, NULL, 10, false, &buffer) != EOK ) { puts("Invalid buffer size\n"); return CMD_FAILURE; } break; case 'm': more = true; break; case 'x': hex = true; break; case 's': dash_represents_stdin = true; break; case 'n': number = true; break; } } argc -= optind; if (argc < 1) { printf("%s - incorrect number of arguments. Try `%s --help'\n", cmdname, cmdname); return CMD_FAILURE; } if (buffer < 4) buffer = CAT_DEFAULT_BUFLEN; if (more) { rc = console_get_size(console, &cols, &rows); if (rc != EOK) { printf("%s - cannot get console size\n", cmdname); return CMD_FAILURE; } console_cols = cols; console_rows = rows; paging_enabled = true; newpage(); } for (i = optind; argv[i] != NULL && !should_quit; i++) ret += cat_file(argv[i], buffer, hex, head, tail, tailFirst); if (ret) return CMD_FAILURE; else return CMD_SUCCESS; }
int main(int argc, char **argv) { int rc; char *image_name; char *device_name; category_id_t disk_cat; printf(NAME ": File-backed block device driver\n"); block_size = DEFAULT_BLOCK_SIZE; ++argv; --argc; while (*argv != NULL && (*argv)[0] == '-') { /* Option */ if (str_cmp(*argv, "-b") == 0) { if (argc < 2) { printf("Argument missing.\n"); print_usage(); return -1; } rc = str_size_t(argv[1], NULL, 10, true, &block_size); if (rc != EOK || block_size == 0) { printf("Invalid block size '%s'.\n", argv[1]); print_usage(); return -1; } ++argv; --argc; } else { printf("Invalid option '%s'.\n", *argv); print_usage(); return -1; } ++argv; --argc; } if (argc < 2) { printf("Missing arguments.\n"); print_usage(); return -1; } image_name = argv[0]; device_name = argv[1]; if (file_bd_init(image_name) != EOK) return -1; rc = loc_service_register(device_name, &service_id); if (rc != EOK) { printf("%s: Unable to register device '%s'.\n", NAME, device_name); return rc; } rc = loc_category_get_id("disk", &disk_cat, IPC_FLAG_BLOCKING); if (rc != EOK) { printf("%s: Failed resolving category 'disk'.\n", NAME); return rc; } rc = loc_service_add_to_cat(service_id, disk_cat); if (rc != EOK) { printf("%s: Failed adding %s to category.", NAME, device_name); return rc; } printf("%s: Accepting connections\n", NAME); task_retval(0); async_manager(); /* Not reached */ return 0; }
static char *get_suitable_filename(struct rfc2045 *r, const char *pfix, int ignore_filename) { const char *disposition_s; const char *disposition_name_s; const char *disposition_filename_s; char *filename_buf; const char *content_name_s; char *p, *q; char *dyn_disp_name=0; rfc2045_dispositioninfo(r, &disposition_s, &disposition_name_s, &disposition_filename_s); content_name_s=rfc2045_contentname(r); if (!disposition_filename_s || !*disposition_filename_s) disposition_filename_s=disposition_name_s; if (!disposition_filename_s || !*disposition_filename_s) disposition_filename_s=content_name_s; filename_buf=rfc2047_decode_simple(disposition_filename_s); if (!filename_buf) { perror("rfc2047_decode_simple"); exit(1); } if (strlen(filename_buf) > 32) { p=filename_buf; q=filename_buf + strlen(filename_buf)-32; while ( (*p++ = *q++) != 0) ; } disposition_filename_s=filename_buf; if (ignore_filename) { char numbuf[NUMBUFSIZE]; static size_t counter=0; const char *p=str_size_t(++counter, numbuf); dyn_disp_name=malloc(strlen(disposition_filename_s) + strlen(p)+2); if (!dyn_disp_name) { perror("malloc"); exit(1); } disposition_filename_s=strcat(strcat(strcpy( dyn_disp_name, p), "-"), disposition_filename_s); } else if (!disposition_filename_s || !*disposition_filename_s) { dyn_disp_name=tempname("."); disposition_filename_s=dyn_disp_name+2; /* Skip over ./ */ } p=malloc((pfix ? strlen(pfix):0)+strlen(disposition_filename_s)+1); if (!p) { perror("malloc"); exit(1); } *p=0; if (pfix) strcpy(p, pfix); q=p+strlen(p); for (strcpy(q, disposition_filename_s); *q; q++) if (!isalnum(*q) && *q != '.' && *q != '-') *q='_'; if (dyn_disp_name) free(dyn_disp_name); if (!pfix) { const char *content_type_s; const char *content_transfer_encoding_s; const char *charset_s; int c; static char filenamebuf[256]; char *t; FILE *tty; if ((tty=fopen("/dev/tty", "r+")) == 0) { perror("/dev/tty"); exit(1); } rfc2045_mimeinfo(r, &content_type_s, &content_transfer_encoding_s, &charset_s); fprintf (tty, "Extract %s? ", content_type_s); fflush(tty); c=getc(tty); if (c != '\n' && c != EOF) { int cc; while ((cc=getc(tty)) != '\n' && cc != EOF) ; } if (c != 'y' && c != 'Y') { free(p); fclose(tty); free(filename_buf); return (0); } fprintf (tty, "Filename [%s]: ", p); fgets(filenamebuf, sizeof(filenamebuf)-1, tty); fclose(tty); t=strchr(filenamebuf, '\n'); if (t) *t=0; else { fprintf(stderr, "Filename too long.\n"); exit(1); } if (filenamebuf[0]) { free(p); p=strdup(filenamebuf); if (!p) { perror("malloc"); exit(1); } } } free(filename_buf); return (p); }