diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-14 21:43:02 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-14 21:43:02 -0400 |
commit | e735ad6710a82604a206ad29f6cbcdd7dc4b024c (patch) | |
tree | 5fe8eebbb7a2bff20bb71bd2368955356979e0a2 /command | |
parent | sync (diff) | |
download | lazysphere-e735ad6710a82604a206ad29f6cbcdd7dc4b024c.tar.gz lazysphere-e735ad6710a82604a206ad29f6cbcdd7dc4b024c.tar.bz2 lazysphere-e735ad6710a82604a206ad29f6cbcdd7dc4b024c.zip |
refactor and add su
Diffstat (limited to 'command')
-rw-r--r-- | command/chmod.c | 10 | ||||
-rw-r--r-- | command/chown.c | 7 | ||||
-rw-r--r-- | command/cp.c | 20 | ||||
-rw-r--r-- | command/dd.c | 2 | ||||
-rw-r--r-- | command/ed.c | 26 | ||||
-rw-r--r-- | command/groups.c | 9 | ||||
-rw-r--r-- | command/id.c | 9 | ||||
-rw-r--r-- | command/ls.c | 16 | ||||
-rw-r--r-- | command/mkdir.c | 4 | ||||
-rw-r--r-- | command/mv.c | 4 | ||||
-rw-r--r-- | command/rm.c | 6 | ||||
-rw-r--r-- | command/su.c | 104 | ||||
-rw-r--r-- | command/sync.c | 5 | ||||
-rw-r--r-- | command/tail.c | 6 | ||||
-rw-r--r-- | command/tee.c | 2 | ||||
-rw-r--r-- | command/xargs.c | 11 |
16 files changed, 168 insertions, 73 deletions
diff --git a/command/chmod.c b/command/chmod.c index 9e2972e..ef4ec3d 100644 --- a/command/chmod.c +++ b/command/chmod.c @@ -1,4 +1,3 @@ -#include "args.h" #include "command.h" #include "lslib.h" @@ -7,7 +6,6 @@ #include <pwd.h> #include <unistd.h> #include <dirent.h> -#include <errno.h> #include <string.h> enum method { @@ -27,7 +25,7 @@ static struct { static void help (void) { printf("Usage: chmod [-Rcvf] MODE[,MODE]... FILE...\n\n"); - printf("MODE is octal number (bit pattern sstrwxrwxrwx) or [ugoa]{+|-|=}[rwxXst]\n\n"); + printf("MODE is octal number (bit pattern sstrwxrwxrwx) or {+|-|=}[rwxXst]\n\n"); printf("\t-R\tRecurse\n"); printf("\t-c\tList changed files\n"); printf("\t-v\tVerbose\n"); @@ -73,7 +71,7 @@ static void chmod_file(char* path) { if (lstat(get_path_buffer(), &s) < 0) { if (!flags.quiet) { - error_s("cannot stat '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot stat '%s'", get_path_buffer()); } pop_path_buffer(save); return; @@ -89,7 +87,7 @@ static void chmod_file(char* path) { if (chmod(get_path_buffer(), mode) < 0) { if (!flags.quiet) { - error_s("cannot chmod '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot chmod '%s'", get_path_buffer()); } pop_path_buffer(save); return; @@ -110,7 +108,7 @@ static void chmod_file(char* path) { d = opendir(get_path_buffer()); if (d == NULL) { if (!flags.quiet) { - error_s("cannot open dir '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot open dir '%s'", get_path_buffer()); } pop_path_buffer(save); return; diff --git a/command/chown.c b/command/chown.c index 164e3b4..dcb6941 100644 --- a/command/chown.c +++ b/command/chown.c @@ -6,7 +6,6 @@ #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> -#include <errno.h> #include <string.h> #include <sys/types.h> #include <pwd.h> @@ -60,7 +59,7 @@ static void chown_file(char* path) { if (chown(get_path_buffer(), flags.uid, flags.gid) < 0) { if (!flags.quiet) { - error_s("cannot chown '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot chown '%s'", get_path_buffer()); } pop_path_buffer(save); return; @@ -75,7 +74,7 @@ static void chown_file(char* path) { if (lstat(get_path_buffer(), &s) < 0) { if (!flags.quiet) { - error_s("cannot stat '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot stat '%s'", get_path_buffer()); } pop_path_buffer(save); return; @@ -89,7 +88,7 @@ static void chown_file(char* path) { d = opendir(get_path_buffer()); if (d == NULL) { if (!flags.quiet) { - error_s("cannot open dir '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot open dir '%s'", get_path_buffer()); } pop_path_buffer(save); return; diff --git a/command/cp.c b/command/cp.c index ca80f69..47a4ff0 100644 --- a/command/cp.c +++ b/command/cp.c @@ -2,12 +2,12 @@ #include "lslib.h" #include <dirent.h> -#include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> +#include <errno.h> #include <unistd.h> static struct { @@ -81,7 +81,7 @@ static bool copy_file(char* from, char* to) { static bool symlink_file(char* from, char* to) { if (symlink(from, to) < 0) { - error_s("failed to symlink '%s': %s", from, strerror(errno)); + error_s("failed to symlink '%s'", from); return false; } else if (flags.verbose) { output("symlinked '%s'", from); @@ -91,7 +91,7 @@ static bool symlink_file(char* from, char* to) { static bool hardlink_file(char* from, char* to) { if (link(from, to) < 0) { - error_s("failed to hardlink '%s': %s", from, strerror(errno)); + error_s("failed to hardlink '%s'", from); return false; } else if (flags.verbose) { output("hardlinked '%s'", from); @@ -117,12 +117,12 @@ static void run_copy(struct stat* s) { if (!flags.preserve) return; if (chmod(to, s->st_mode) < 0) { - error_s("cannot chmod '%s': %s", to, strerror(errno)); + error_s("cannot chmod '%s'", to); return; } if (chown(to, s->st_uid, s->st_gid) < 0) { - error_s("cannot chown '%s': %s", to, strerror(errno)); + error_s("cannot chown '%s'", to); return; } } @@ -140,14 +140,14 @@ static void cp_directory(struct stat* s) { } if (mkdir(get_path_buffer_2(), s->st_mode) < 0 && errno != EEXIST) { - error_s("cannot create directory '%s': %s", get_path_buffer_2(), strerror(errno)); + error_s("cannot create directory '%s'", get_path_buffer_2()); return; } d = opendir(get_path_buffer()); if (d == NULL) { - error_s("cannot open directory '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot open directory '%s'", get_path_buffer()); return; } @@ -183,7 +183,7 @@ static void cp_file(char* path) { struct stat s; if (lstat(get_path_buffer(), &s) < 0) { pop_path_buffer(save); - error_s("cannot stat '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot stat '%s'", get_path_buffer()); return; } @@ -218,7 +218,7 @@ COMMAND(cp) { if (argc - start == 2) { struct stat s; if (lstat(argv[start], &s) < 0) { - error("cannot stat '%s': %s", argv[start], strerror(errno)); + error("cannot stat '%s'", argv[start]); } push_path_buffer(argv[argc-2]); push_path_buffer_2(argv[argc-1]); @@ -234,7 +234,7 @@ COMMAND(cp) { push_path_buffer_2(argv[argc-1]); if (lstat(get_path_buffer_2(), &s) < 0) { - error("target: '%s': %s", get_path_buffer_2(), strerror(errno)); + error("target: '%s'", get_path_buffer_2()); } for (i = start; i < argc - 1; i++) { diff --git a/command/dd.c b/command/dd.c index 67f8be3..1fb81c5 100644 --- a/command/dd.c +++ b/command/dd.c @@ -48,7 +48,7 @@ COMMAND(dd) { } } - buffer = malloc(bs); + buffer = xalloc(bs); while ((read = fread(buffer, 1, bs, in_file)) != 0) { fwrite(buffer, 1, read, out_file); diff --git a/command/ed.c b/command/ed.c index 472473f..8e8eae1 100644 --- a/command/ed.c +++ b/command/ed.c @@ -76,7 +76,7 @@ static bool parse_regex(char** end, struct LineAddress* address, enum RegexDirec cap = 8; siz = 0; - buf = malloc(cap * sizeof(long int)); + buf = xalloc(cap * sizeof(long int)); i = (dir == ALL ? 0 : line_current); until = (dir == BEFORE ? 0 : line_count - 1); @@ -88,7 +88,7 @@ static bool parse_regex(char** end, struct LineAddress* address, enum RegexDirec } if (cap == siz) { cap *= 2; - buf = realloc(buf, cap * sizeof(long int)); + buf = xrealloc(buf, cap * sizeof(long int)); } buf[siz] = i; siz++; @@ -112,7 +112,7 @@ static void free_address (struct LineAddress address) { static void expand_buffer(long int** buf, unsigned long* cap, unsigned long* size) { if (*cap == *size) { *cap *= 2; - *buf = realloc(*buf, sizeof(long int) * *cap); + *buf = xrealloc(*buf, sizeof(long int) * *cap); } } @@ -127,7 +127,7 @@ static bool parse_regex_lines(char** end, struct LineAddress* address) { cap = 8; siz = 0; - buf = malloc(cap * sizeof(long int)); + buf = xalloc(cap * sizeof(long int)); addr = *address; if (addr.type == INDEX) { @@ -310,7 +310,7 @@ static void load_empty(void) { free_data(false); line_capacity = 8; - lines = malloc(sizeof(char*) * line_capacity); + lines = xalloc(sizeof(char*) * line_capacity); line_count = 0; line_current = 0; @@ -320,7 +320,7 @@ static void load_empty(void) { static void get_input(FILE* file, char*** buffer, unsigned long* capacity, unsigned long* size) { unsigned long cap = 8; unsigned long siz = 0; - char** buf = malloc(sizeof(char*) * cap); + char** buf = xalloc(sizeof(char*) * cap); char* line = NULL; size_t offset = 0; @@ -329,7 +329,7 @@ static void get_input(FILE* file, char*** buffer, unsigned long* capacity, unsig while (getline(&line, &offset, file) != -1) { if (cap == siz) { cap *= 2; - buf = realloc(buf, sizeof(char*) * cap); + buf = xrealloc(buf, sizeof(char*) * cap); } buf[siz] = line; siz++; @@ -400,7 +400,7 @@ static void expand(unsigned long count) { if (count < line_capacity) return; line_capacity *= 2; if (count > line_capacity) line_capacity = count; - lines = realloc(lines, line_capacity * sizeof(char*)); + lines = xrealloc(lines, line_capacity * sizeof(char*)); } static void append_lines(unsigned long index, char** new, unsigned long new_len) { @@ -495,9 +495,9 @@ static bool get_file_name(char** filename) { len--; } if (default_filename != NULL) { - default_filename = realloc(default_filename, len + 1); + default_filename = xrealloc(default_filename, len + 1); } else { - default_filename = malloc(len + 1); + default_filename = xalloc(len + 1); } memcpy(default_filename, *filename, len + 1); *filename = default_filename; @@ -561,7 +561,7 @@ static void read_file(char* filename) { capacity = 8; size = 0; - buf = malloc(capacity * sizeof(char*)); + buf = xalloc(capacity * sizeof(char*)); get_input(file, &buf, &capacity, &size); if (size < 1) { @@ -584,7 +584,7 @@ static void expand_string(char** buf, int* capacity, int* size, char* text, int if (*size + len >= *capacity) { *capacity *= 2; if (*capacity < *size + len) *capacity = *size + len; - *buf = realloc(*buf, *capacity); + *buf = xrealloc(*buf, *capacity); } memcpy(*buf + *size, text, len); *size += len; @@ -593,7 +593,7 @@ static void expand_string(char** buf, int* capacity, int* size, char* text, int static int substute_string(long int index, long int matches, re_t regex, char* sub, int sub_len) { int capacity = 8; int size = 0; - char* buf = malloc(sizeof(char) * capacity); + char* buf = xalloc(sizeof(char) * capacity); long int left; int offset = 0; diff --git a/command/groups.c b/command/groups.c index cb950be..f95ee80 100644 --- a/command/groups.c +++ b/command/groups.c @@ -1,13 +1,12 @@ -#include "args.h" #include "command.h" #include "lslib.h" -#include <errno.h> #include <grp.h> #include <pwd.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <errno.h> static void help (void) { printf("Usage: groups [USER]\n\n"); @@ -34,14 +33,14 @@ COMMAND(groups) { if (errno == 0) { error("user not found"); } else { - error("failed to fetch groups: %s", strerror(errno)); + error("failed to fetch groups"); } } ngroups = 0; getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups); - groups = malloc(sizeof(gid_t) * ngroups); + groups = xalloc(sizeof(gid_t) * ngroups); getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); for (i = 0; i < ngroups; i++){ @@ -49,7 +48,7 @@ COMMAND(groups) { if(gr == NULL) { free(groups); - error("failed to fetch groups: %s", strerror(errno)); + error("failed to fetch groups"); } printf("%s ",gr->gr_name); diff --git a/command/id.c b/command/id.c index 3a63989..3d3d19e 100644 --- a/command/id.c +++ b/command/id.c @@ -1,13 +1,12 @@ -#include "args.h" #include "command.h" #include "lslib.h" -#include <errno.h> #include <grp.h> #include <pwd.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <errno.h> static void help (void) { printf("Usage: id [USER]\n\n"); @@ -35,7 +34,7 @@ COMMAND(user_id) { if (errno == 0) { error("user not found"); } else { - error("failed to fetch groups: %s", strerror(errno)); + error("failed to fetch groups"); } } @@ -44,7 +43,7 @@ COMMAND(user_id) { ngroups = 0; getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups); - groups = malloc(sizeof(gid_t) * ngroups); + groups = xalloc(sizeof(gid_t) * ngroups); getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); gid = pw->pw_gid; @@ -60,7 +59,7 @@ COMMAND(user_id) { struct group* gr = getgrgid(groups[i]); if(gr == NULL) { free(groups); - error("failed to fetch groups: %s", strerror(errno)); + error("failed to fetch groups"); } printf("%d(%s)", gr->gr_gid, gr->gr_name); diff --git a/command/ls.c b/command/ls.c index bbfedfb..d4d19c1 100644 --- a/command/ls.c +++ b/command/ls.c @@ -66,9 +66,9 @@ static DIR* get_directory(char* path) { DIR* d = opendir(path); if (d == NULL) { if (errno == ENOTDIR) { - error_s("`%s` is a a file\n", path); + error_s("`%s` is a a file", path); } else { - error_s("failed to open directory '%s': %s\n", path, strerror(errno)); + error_s("failed to open directory '%s'", path); } } return d; @@ -90,7 +90,7 @@ static bool get_file_info(const char* file_name, struct FileInfo* info) { save = push_path_buffer(file_name); if (lstat(get_path_buffer(), &s) < 0) { - error_s("failed to read file '%s': %s\n", get_path_buffer(), strerror(errno)); + error_s("failed to read file '%s'", get_path_buffer()); pop_path_buffer(save); return false; } @@ -199,7 +199,7 @@ static bool get_file_info(const char* file_name, struct FileInfo* info) { info->type = ty; file_len = strlen(file_name) + 1; - info->name = malloc(file_len); + info->name = xalloc(file_len); memcpy(info->name, file_name, file_len); print_file_size(s.st_size, info->size); @@ -340,7 +340,7 @@ static void push_file( if (*size == *capacity) { *capacity *= 2; - *files = realloc(*files, sizeof(struct FileInfo) * *capacity); + *files = xrealloc(*files, sizeof(struct FileInfo) * *capacity); } user_len = strlen(finfo.usr->pw_name); @@ -382,7 +382,7 @@ static void recurse_directory(char* dir_name) { capacity = 8; size = 0; - files = malloc(sizeof(struct FileInfo) * capacity); + files = xalloc(sizeof(struct FileInfo) * capacity); memset(&info, 0, sizeof(struct FileListInfo)); while((file = readdir(d)) != NULL) { @@ -434,7 +434,7 @@ static void list_directory(char* path) { capacity = 8; size = 0; - files = malloc(sizeof(struct FileInfo) * capacity); + files = xalloc(sizeof(struct FileInfo) * capacity); memset(&info, 0, sizeof(struct FileListInfo)); while ((file = readdir(d)) != NULL) { @@ -466,7 +466,7 @@ static void list_file_args(int start, int argc, char** argv) { capacity = 8; size = 0; - files = malloc(sizeof(struct FileInfo) * capacity); + files = xalloc(sizeof(struct FileInfo) * capacity); memset(&info, 0, sizeof(struct FileListInfo)); for (i = start; i < argc; i++) { diff --git a/command/mkdir.c b/command/mkdir.c index 0d3950d..e6d6268 100644 --- a/command/mkdir.c +++ b/command/mkdir.c @@ -38,7 +38,7 @@ static bool mkdir_parents(char* path) { if (path[i] != '/') continue; path[i] = '\0'; if (mkdir(path, flags.mode) < 0 && errno != EEXIST) { - error_s("failed to create directory '%s': %s", path, strerror(errno)); + error_s("failed to create directory '%s'", path); return false; }; path[i] = '/'; @@ -62,7 +62,7 @@ COMMAND(makedir) { continue; } if (mkdir(argv[i], flags.mode) < 0) { - error_s("failed to create directory '%s': %s", argv[i], strerror(errno)); + error_s("failed to create directory '%s'", argv[i]); } } diff --git a/command/mv.c b/command/mv.c index adce2b7..8f8f0c7 100644 --- a/command/mv.c +++ b/command/mv.c @@ -64,7 +64,7 @@ static void mv_dir(bool exists) { } if (rename(get_path_buffer(), get_path_buffer_2()) < 0) { - error_s("cannot move '%s': %s", get_path_buffer(), strerror(errno)); + error_s("cannot move '%s'", get_path_buffer()); } else if (flags.verbose) { output("moved '%s'", get_path_buffer()); } @@ -91,7 +91,7 @@ COMMAND(mv) { dest = true; if (lstat(get_path_buffer_2(), &s) < 0 && argc - start > 2) { dest = false; - error("cannot stat '%s': %s", get_path_buffer_2(), strerror(errno)); + error("cannot stat '%s'", get_path_buffer_2()); } if (dest && flags.refuse_if_dir) { diff --git a/command/rm.c b/command/rm.c index 81a956a..9834a40 100644 --- a/command/rm.c +++ b/command/rm.c @@ -52,7 +52,7 @@ static bool rm_dir (void) { d = opendir(get_path_buffer()); if (d == NULL) { - error_s("failed to stat '%s': %s\n", get_path_buffer(), strerror(errno)); + error_s("failed to stat '%s'", get_path_buffer()); return false; } @@ -71,7 +71,7 @@ static void rm_file(char* path) { struct stat s; if (lstat(get_path_buffer(), &s) < 0) { pop_path_buffer(save); - error_s("failed to stat '%s': %s\n", get_path_buffer(), strerror(errno)); + error_s("failed to stat '%s'", get_path_buffer()); return; } @@ -102,7 +102,7 @@ static void rm_file(char* path) { } if (remove(get_path_buffer()) < 0) { - error_s("failed to delete '%s': %s\n", get_path_buffer(), strerror(errno)); + error_s("failed to delete '%s'", get_path_buffer()); } else if (flags.verbose) { output("deleted '%s'\n", get_path_buffer()); } diff --git a/command/su.c b/command/su.c new file mode 100644 index 0000000..942f37e --- /dev/null +++ b/command/su.c @@ -0,0 +1,104 @@ +#include "command.h" +#include "lslib.h" + +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static struct { + bool clear_env; + bool update_env; + char* shell; + char* user; +} flags; + +static bool restricted_shell(const char *shell) { + char *line; + int result = true; + + while ((line = getusershell()) != NULL) { + if (strcmp(line, shell) == 0) { + result = false; + break; + } + } + endusershell(); + return result; +} + +static void help (void) { + printf("Usage: su [-lmp] [-s SH] [-] [USER [FILE ARGS]] \n\n"); + printf("Run shell under USER (by default, root)\n\n"); + printf("\t-,-l\tClear environment, go to home dir, run shell as login shell\n"); + printf("\t-p,-m\tDo not set new $HOME, $SHELL, $USER, $LOGNAME\n"); + printf("\t-s SH Shell\tto use instead of user's default\n"); +} + +static int short_arg (char c, char* next) { + switch (c) { + case 'l': + flags.clear_env = true; + break; + case 'p': + case 'm': + flags.update_env = false; + break; + case 's': + check_arg(next); + flags.shell = next; + return ARG_USED; + default: + return ARG_INVALID; + + } + return ARG_UNUSED; +} + +COMMAND (su) { + + int start, res; + uid_t cur_uid; + struct passwd* pw; + + flags.clear_env = false; + flags.update_env = true; + flags.shell = NULL; + flags.user = "root"; + + start = parse_args(argc, argv, help, short_arg, NULL); + + if (start < argc && streql(argv[start], "-")) { + flags.clear_env = true; + start++; + } + + if (start < argc) + flags.user = argv[start++]; + + pw = xgetpwnam(flags.user); + cur_uid = getuid(); + + if (cur_uid != 0) { + res = prompt_password(pw, "Password: "); + } else { + res = PASSWORD_VALID; + } + + if (res != PASSWORD_VALID) { + error("invalid password"); + } + + if (flags.shell && cur_uid != 0 && pw->pw_shell && restricted_shell(pw->pw_shell)) { + error_s("using restricted shell"); + flags.shell = NULL; + } + + if (!flags.shell) + flags.shell = pw->pw_shell; + + change_identity(pw); + setup_environment(flags.shell, flags.update_env, flags.clear_env, pw); + exec_shell(flags.shell, flags.clear_env, (const char**) &argv[start]); + + return EXIT_FAILURE; /* */ +} diff --git a/command/sync.c b/command/sync.c index 797d7a5..89ff86f 100644 --- a/command/sync.c +++ b/command/sync.c @@ -1,12 +1,9 @@ -#include "args.h" #include "command.h" #include "lslib.h" -#define _X_OPEN_SOURCE 500 #include <stdlib.h> #include <unistd.h> #include <fcntl.h> -#include <errno.h> #include <string.h> static struct { @@ -58,7 +55,7 @@ static bool sync_file(char* path) { } else { ret = flags.sync_data ? fdatasync(fd) : fsync(fd); if (ret < 0) { - error_s("failed to sync '%s': %s", path, strerror(errno)); + error_s("failed to sync '%s'", path); } } diff --git a/command/tail.c b/command/tail.c index 8137eca..64f4608 100644 --- a/command/tail.c +++ b/command/tail.c @@ -23,10 +23,10 @@ static size_t tail_file_lines(FILE* file, unsigned int count, size_t skip) { size_t len; char* line; - ring = malloc(sizeof(char*) * count); + ring = xalloc(sizeof(char*) * count); memset(ring, 0, sizeof(char*) * count); - ring_len = malloc(sizeof(int) * count); + ring_len = xalloc(sizeof(int) * count); index = 0; size = 0; @@ -74,7 +74,7 @@ static size_t tail_file_chars(FILE* file, unsigned int count, size_t skip) { unsigned int size, i; int read, c; - ring = malloc(sizeof(char) * count); + ring = xalloc(sizeof(char) * count); memset(ring, 0, count); index = 0; diff --git a/command/tee.c b/command/tee.c index 364a13d..b9ff974 100644 --- a/command/tee.c +++ b/command/tee.c @@ -71,7 +71,7 @@ COMMAND(tee_cmd) { return EXIT_SUCCESS; } - files = malloc(sizeof(FILE*) * (argc - start)); + files = xalloc(sizeof(FILE*) * (argc - start)); for (i = start; i < argc; i++) { FILE* file = get_file(argv[i], flags.append ? "a" : "w"); diff --git a/command/xargs.c b/command/xargs.c index 2a41460..47e0f0b 100644 --- a/command/xargs.c +++ b/command/xargs.c @@ -1,7 +1,6 @@ #include "command.h" #include "lslib.h" -#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -76,7 +75,7 @@ char* read_next(FILE* file, int arg_count) { size = 0; capacity = 8; - buf = malloc(sizeof(char) * capacity); + buf = xalloc(sizeof(char) * capacity); while(c = getc(file), true) { if (c == EOF && size == 0) { @@ -86,7 +85,7 @@ char* read_next(FILE* file, int arg_count) { if (size == capacity) { capacity *= 2; - buf = realloc(buf, sizeof(char) * capacity); + buf = xrealloc(buf, sizeof(char) * capacity); } if (c == '\0' || c == EOF || (!flags.null_seperated && c == '\n')) { @@ -104,7 +103,7 @@ void read_args(FILE* file, char*** args, int* size, int* capacity) { while (arg = read_next(file, read), true) { if (*size == *capacity) { *capacity *= 2; - *args = realloc(*args, sizeof(char*) * *capacity); + *args = xrealloc(*args, sizeof(char*) * *capacity); } (*args)[(*size)++] = arg; read++; @@ -146,7 +145,7 @@ COMMAND(xargs) { size = arg_on_stack_count + 1; capacity = size + 8; - args = malloc(sizeof(char*) * capacity); + args = xalloc(sizeof(char*) * capacity); args[0] = command; memcpy(&args[1], &argv[arg_start], arg_on_stack_count * sizeof(char*)); read_args(flags.file, &args, &size, &capacity); @@ -178,7 +177,7 @@ COMMAND(xargs) { } if (execvp(command, args) == -1) { - error("error: failed to execute command: %s", strerror(errno)); + error("failed to execute command"); } cleanup: |