/* Main entry point for cmd, accepts an array of arguments */ int cmd_cmp(char **argv) { int rc; unsigned int argc; int c, opt_ind; argc = cli_count_args(argv); for (c = 0, optind = 0, opt_ind = 0; c != -1;) { c = getopt_long(argc, argv, "hv", long_options, &opt_ind); switch (c) { case 'h': help_cmd_cmp(HELP_LONG); return CMD_SUCCESS; case 'v': printf("%s\n", CMP_VERSION); return CMD_SUCCESS; } } if (argc - optind != 2) { printf("%s - incorrect number of arguments. Try `%s --help'\n", cmdname, cmdname); return CMD_FAILURE; } rc = cmp_files(argv[optind], argv[optind + 1]); if (rc) return CMD_FAILURE; else return CMD_SUCCESS; }
/* Main entry point for mv, accepts an array of arguments */ int cmd_mv(char **argv) { unsigned int argc; int rc; argc = cli_count_args(argv); if (argc != 3) { printf("%s: invalid number of arguments.\n", cmdname); return CMD_FAILURE; } rc = rename(argv[1], argv[2]); if (rc != 0) { printf("Unable to rename %s to %s (error=%d)\n", argv[1], argv[2], errno); return CMD_FAILURE; } return CMD_SUCCESS; }
/* Main entry point for unmount, accepts an array of arguments */ int cmd_unmount(char **argv) { unsigned int argc; int rc; argc = cli_count_args(argv); if (argc != 2) { printf("%s: invalid number of arguments.\n", cmdname); return CMD_FAILURE; } rc = vfs_unmount(argv[1]); if (rc != EOK) { printf("Unable to unmount %s (rc=%d)\n", argv[1], rc); return CMD_FAILURE; } return CMD_SUCCESS; }
/* 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; }
/* Main entry point for rm, accepts an array of arguments */ int cmd_rm(char **argv) { unsigned int argc; unsigned int i, scope, ret = 0; int c, opt_ind; size_t len; char *buff = NULL; argc = cli_count_args(argv); if (argc < 2) { cli_error(CL_EFAIL, "%s: insufficient arguments. Try %s --help", cmdname, cmdname); return CMD_FAILURE; } if (!rm_start(&rm)) { cli_error(CL_ENOMEM, "%s: could not initialize", cmdname); rm_end(&rm); return CMD_FAILURE; } for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) { c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind); switch (c) { case 'h': help_cmd_rm(HELP_LONG); return CMD_SUCCESS; case 'v': printf("%s\n", RM_VERSION); return CMD_SUCCESS; case 'r': rm.recursive = 1; break; case 'f': rm.force = 1; break; case 's': rm.safe = 1; break; } } if ((unsigned) optind == argc) { cli_error(CL_EFAIL, "%s: insufficient arguments. Try %s --help", cmdname, cmdname); rm_end(&rm); return CMD_FAILURE; } i = optind; while (NULL != argv[i]) { len = str_size(argv[i]) + 2; buff = (char *) realloc(buff, len); if (buff == NULL) { printf("rm: out of memory\n"); ret = 1; break; } memset(buff, 0, len); snprintf(buff, len, "%s", argv[i]); scope = rm_scope(buff); switch (scope) { case RM_BOGUS: /* FIXME */ case RM_FILE: ret += rm_single(buff); break; case RM_DIR: if (! rm.recursive) { printf("%s is a directory, use -r to remove it.\n", buff); ret ++; } else { ret += rm_recursive(buff); } break; } i++; } if (NULL != buff) free(buff); rm_end(&rm); if (ret) return CMD_FAILURE; else return CMD_SUCCESS; }
int cmd_cp(char **argv) { unsigned int argc, verbose = 0; int buffer = 0, recursive = 0; int force = 0, interactive = 0; int c, opt_ind; int64_t ret; con = console_init(stdin, stdout); argc = cli_count_args(argv); for (c = 0, optreset = 1, optind = 0, opt_ind = 0; c != -1;) { c = getopt_long(argc, argv, "hvVfirb:", long_options, &opt_ind); switch (c) { case 'h': help_cmd_cp(1); return CMD_SUCCESS; case 'v': printf("%s\n", CP_VERSION); return CMD_SUCCESS; case 'V': verbose = 1; break; case 'f': interactive = 0; force = 1; break; case 'i': force = 0; interactive = 1; break; case 'r': recursive = 1; break; case 'b': if (-1 == (buffer = strtoint(optarg))) { printf("%s: Invalid buffer specification, " "(should be a number greater than zero)\n", cmdname); console_done(con); return CMD_FAILURE; } if (verbose) printf("Buffer = %d\n", buffer); break; } } if (buffer == 0) buffer = CP_DEFAULT_BUFLEN; argc -= optind; if (argc != 2) { printf("%s: invalid number of arguments. Try %s --help\n", cmdname, cmdname); console_done(con); return CMD_FAILURE; } ret = do_copy(argv[optind], argv[optind + 1], buffer, verbose, recursive, force, interactive); console_done(con); if (ret == 0) return CMD_SUCCESS; else return CMD_FAILURE; }