diff --git a/build-box.sh b/build-box.sh new file mode 100755 index 0000000..65d1e33 --- /dev/null +++ b/build-box.sh @@ -0,0 +1,50 @@ +#!/bin/sh +CFLAGS="-s -Os -Wextra -Wall -pedantic" +CC="cc" + +chmod +x ./libmu/build-libmu.sh +./libmu/build-libmu.sh + +cat > mutils.c << EOF +#include +#include + +$(for i in $(ls src); do + f=$(basename $i .c) + echo "int "$f"_main(int argc, char **argv);" +done) + +struct cmd { + int (*func)(int argc, char **argv); + const char *str; +} cmds[] = { +$(for i in $(ls src); do + f=$(basename $i .c) + + #EDIT SOURCES + cat src/$i | sed "s/main(/"$f"_main(/g" > bin/$i + + #MAKE STRUCTURE + echo -e "\t{"$f"_main, \"$f\"}," +done) +}; + +int main(int argc, char **argv) { + if (argc > 1) { + argc--; + argv++; + + for (size_t i = 0; i < sizeof(cmds) / sizeof(struct cmd); i++) + if (!strcmp(argv[0], cmds[i].str)) + return cmds[i].func(argc, argv); + } + + for (size_t i = 0; i < sizeof(cmds) / sizeof(struct cmd); i++) + printf("%s ", cmds[i].str); + + putchar('\n'); + return 0; +} +EOF + +echo $CFLAGS | xargs $CC mutils.c bin/*.c obj/*.o -Iconfigs -Ilibmu -omutils diff --git a/build.sh b/build.sh index eeb9809..72ff08b 100755 --- a/build.sh +++ b/build.sh @@ -1,8 +1,11 @@ #!/bin/sh -CFLAGS=-s -Os -Wextra -Wall -pedantic -CC=cc +CFLAGS="-s -Os -Wextra -Wall -pedantic" +CC="cc" + +chmod +x ./libmu/build-libmu.sh +./libmu/build-libmu.sh for i in $(ls src); do echo "CC $i" - $CC $CFLAGS -Iconfigs -Iinclude src/$i -o bin/$(basename $i .c) + echo $CFLAGS | xargs $CC -Iconfigs -Ilibmu src/$i -o bin/$(basename $i .c) obj/*.o done diff --git a/clean.sh b/clean.sh new file mode 100755 index 0000000..7881c8e --- /dev/null +++ b/clean.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +rm -f obj/* bin/* mutils.c mutils diff --git a/configs/config.h b/configs/config.h index 5959897..112d499 100644 --- a/configs/config.h +++ b/configs/config.h @@ -16,8 +16,8 @@ #ifdef _PW_CHECK_H /* Pw_check. Salt for crypt() */ - int MU_SALT_ENC[] = {'s', 'a', 'l', 't'}; - char MU_SALT_BUF[sizeof(MU_SALT_ENC) + 1]; + static int MU_SALT_ENC[] = {'s', 'a', 'l', 't'}; + static char MU_SALT_BUF[sizeof(MU_SALT_ENC) + 1]; #endif #ifdef _MOUNT_C diff --git a/include/mu_strlen.h b/include/mu_strlen.h deleted file mode 100644 index ee6463b..0000000 --- a/include/mu_strlen.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef _UTF8_STRLEN -#define _UTF8_STRLEN - -static size_t mu_strlen(const char *s) { - size_t i = 0; - - while (*s++) - i += (*s & 0xC0) != 0x80; - - return i; -} - -#endif diff --git a/libmu/build-libmu.sh b/libmu/build-libmu.sh new file mode 100755 index 0000000..00b5b67 --- /dev/null +++ b/libmu/build-libmu.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +cd obj +cc ../libmu/*.c -I ../configs -I ../libmu -c diff --git a/include/duration.h b/libmu/duration.c similarity index 72% rename from include/duration.h rename to libmu/duration.c index ac09e4d..af68748 100644 --- a/include/duration.h +++ b/libmu/duration.c @@ -1,9 +1,7 @@ -#ifndef _DURATION_H -#define _DURATION_H - +#include #include -static unsigned long long parse_uint(const char *str) { +unsigned long long parse_uint(const char *str) { char *p = NULL; unsigned long long res = strtoull(str, &p, 0); @@ -32,7 +30,7 @@ static unsigned long long parse_uint(const char *str) { return res; } -static unsigned long long mu_parse_duration(const char *arg) { +unsigned long long mu_parse_duration(const char *arg) { if (strchr(arg, '.')) { /* TODO */ } @@ -44,5 +42,3 @@ static unsigned long long mu_parse_duration(const char *arg) { return 0; } - -#endif diff --git a/libmu/duration.h b/libmu/duration.h new file mode 100644 index 0000000..063aa81 --- /dev/null +++ b/libmu/duration.h @@ -0,0 +1,7 @@ +#ifndef _DURATION_H +#define _DURATION_H + +unsigned long long parse_uint(const char *str); +unsigned long long mu_parse_duration(const char *arg); + +#endif diff --git a/include/get_stat.h b/libmu/get_stat.c similarity index 58% rename from include/get_stat.h rename to libmu/get_stat.c index 258f9fa..1085922 100644 --- a/include/get_stat.h +++ b/libmu/get_stat.c @@ -1,12 +1,9 @@ -#ifndef _GET_STAT_H -#define _GET_STAT_H - #include -#include #include -#include +#include +#include "get_stat.h" -static int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path) { +int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path) { if (stat(path, stat_path)) { if (prog_name != NULL) fprintf(stderr, "%s: %s: %s\n", prog_name, path, strerror(errno)); @@ -17,7 +14,7 @@ static int mu_get_stat(const char *prog_name, const char *path, struct stat *sta return 0; } -static int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path) { +int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path) { if (lstat(path, stat_path)) { if (prog_name != NULL) fprintf(stderr, "%s: %s: %s\n", prog_name, path, strerror(errno)); @@ -28,12 +25,10 @@ static int mu_get_lstat(const char *prog_name, const char *path, struct stat *st return 0; } -static int mu_get_stats(const char *prog_name, int flag, const char *path, struct stat *stat_path) { +int mu_get_stats(const char *prog_name, int flag, const char *path, struct stat *stat_path) { if (flag) return mu_get_lstat(prog_name, path, stat_path); else return mu_get_stat(prog_name, path, stat_path); } - -#endif diff --git a/libmu/get_stat.h b/libmu/get_stat.h new file mode 100644 index 0000000..c884e62 --- /dev/null +++ b/libmu/get_stat.h @@ -0,0 +1,9 @@ +#ifndef _GET_STAT_H +#define _GET_STAT_H + +#include +int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path); +int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path); +int mu_get_stats(const char *prog_name, int flag, const char *path, struct stat *stat_path); + +#endif diff --git a/include/human.h b/libmu/human.c similarity index 62% rename from include/human.h rename to libmu/human.c index 458358c..6eb052d 100644 --- a/include/human.h +++ b/libmu/human.c @@ -1,13 +1,6 @@ -#ifndef _HUMAN_H -#define _HUMAN_H +#include "human.h" -#include -#include - -#define MU_HUMAN_BUF_SIZE 16 - -static char *mu_humansize(off_t n, off_t block) { - static char mu_hs_buf[MU_HUMAN_BUF_SIZE + 1]; +char *mu_humansize(off_t n, off_t block) { memset(mu_hs_buf, '\0', sizeof(mu_hs_buf)); char *postfixes = "BKMGTPE"; @@ -27,4 +20,3 @@ static char *mu_humansize(off_t n, off_t block) { return mu_hs_buf; } -#endif diff --git a/libmu/human.h b/libmu/human.h new file mode 100644 index 0000000..00f2a50 --- /dev/null +++ b/libmu/human.h @@ -0,0 +1,12 @@ +#ifndef _HUMAN_H +#define _HUMAN_H + +#include +#include +#include + +#define MU_HUMAN_BUF_SIZE 16 +static char mu_hs_buf[MU_HUMAN_BUF_SIZE + 1]; + +char *mu_humansize(off_t n, off_t block); +#endif diff --git a/include/make_path.h b/libmu/make_path.c similarity index 80% rename from include/make_path.h rename to libmu/make_path.c index 934130b..fa24470 100644 --- a/include/make_path.h +++ b/libmu/make_path.c @@ -1,11 +1,8 @@ -#ifndef _MAKE_PATH_H -#define _MAKE_PATH_H - #include #include #include -static char *mu_make_path(const char *prog_name, const char *src, const char *dst) { +char *mu_make_path(const char *prog_name, const char *src, const char *dst) { int flag = 0; if (src == NULL) { @@ -35,5 +32,3 @@ static char *mu_make_path(const char *prog_name, const char *src, const char *ds return full_path; } - -#endif diff --git a/libmu/make_path.h b/libmu/make_path.h new file mode 100644 index 0000000..4687185 --- /dev/null +++ b/libmu/make_path.h @@ -0,0 +1,5 @@ +#ifndef _MAKE_PATH_H +#define _MAKE_PATH_H + +char *mu_make_path(const char *prog_name, const char *src, const char *dst); +#endif diff --git a/include/mode_to_str.h b/libmu/mode_to_str.c similarity index 85% rename from include/mode_to_str.h rename to libmu/mode_to_str.c index b34728b..8be70f0 100644 --- a/include/mode_to_str.h +++ b/libmu/mode_to_str.c @@ -1,11 +1,8 @@ -#ifndef _MODE_TO_STR_H -#define _MODE_TO_STR_H - #include #include +#include "mode_to_str.h" -static char *mu_mode_2_str(mode_t file_mode) { - static char mode[11]; +char *mu_mode_2_str(mode_t file_mode) { snprintf(mode, sizeof(mode), "----------"); if (file_mode & S_IRUSR) @@ -54,4 +51,3 @@ static char *mu_mode_2_str(mode_t file_mode) { return mode; } -#endif diff --git a/libmu/mode_to_str.h b/libmu/mode_to_str.h new file mode 100644 index 0000000..34e6152 --- /dev/null +++ b/libmu/mode_to_str.h @@ -0,0 +1,6 @@ +#ifndef _MODE_TO_STR_H +#define _MODE_TO_STR_H +static char mode[11]; + +char *mu_mode_2_str(mode_t file_mode); +#endif diff --git a/libmu/mu_strlen.c b/libmu/mu_strlen.c new file mode 100644 index 0000000..13239e5 --- /dev/null +++ b/libmu/mu_strlen.c @@ -0,0 +1,11 @@ +#include "mu_strlen.h" + +size_t mu_strlen(const char *s) { + size_t i = 0; + + while (*s++) + i += (*s & 0xC0) != 0x80; + + return i; +} + diff --git a/libmu/mu_strlen.h b/libmu/mu_strlen.h new file mode 100644 index 0000000..a720249 --- /dev/null +++ b/libmu/mu_strlen.h @@ -0,0 +1,6 @@ +#ifndef _MU_STRLEN +#define _MU_STRLEN + +#include +size_t mu_strlen(const char *s); +#endif diff --git a/include/parse_mode.h b/libmu/parse_mode.c similarity index 92% rename from include/parse_mode.h rename to libmu/parse_mode.c index 19b4265..806caf9 100644 --- a/include/parse_mode.h +++ b/libmu/parse_mode.c @@ -1,6 +1,4 @@ -#ifndef _PARSE_MODE_H -#define _PARSE_MODE_H - +#include #include #include @@ -15,7 +13,7 @@ #define SU_PERM (S_ISUID | S_ISGID | S_ISVTX) #define FULL_PERM (WR_PERM | EX_PERM | RD_PERM) -static mode_t mu_parse_mode(const char *s, mode_t cur_mode) { +mode_t mu_parse_mode(const char *s, mode_t cur_mode) { char *p = NULL; mode_t mode = (mode_t)strtol(s, &p, 8); @@ -96,4 +94,3 @@ static mode_t mu_parse_mode(const char *s, mode_t cur_mode) { return (cur_mode & ~mask) | (mode & mask); } -#endif diff --git a/libmu/parse_mode.h b/libmu/parse_mode.h new file mode 100644 index 0000000..18e17ff --- /dev/null +++ b/libmu/parse_mode.h @@ -0,0 +1,5 @@ +#ifndef _PARSE_MODE_H +#define _PARSE_MODE_H + +mode_t mu_parse_mode(const char *s, mode_t cur_mode); +#endif diff --git a/include/parse_mount.h b/libmu/parse_mount.c similarity index 54% rename from include/parse_mount.h rename to libmu/parse_mount.c index 1dbcaed..b4ca1ec 100644 --- a/include/parse_mount.h +++ b/libmu/parse_mount.c @@ -1,26 +1,7 @@ -#ifndef _PARSE_MOUNT_H -#define _PARSE_MOUNT_H #include +#include "parse_mount.h" -typedef struct { - char *opt; - char *notopt; - char *desc; - unsigned int val; -} MU_MOUNT_OPTS; - -static MU_MOUNT_OPTS mu_options[] = { - {"defaults", NULL, NULL, 0}, - {"ro", "rw", "Read only / Read and write", MS_RDONLY}, - {"remount", NULL, "Remount a mounted filesystem", MS_REMOUNT}, - {"sync", "async", "Writes are [a]synchronous", MS_SYNCHRONOUS}, - {"nodev", "dev", "(Dis)allow use of special device files", MS_NODEV}, - {"bind", "rbind", "Bind a file or directory", MS_BIND}, - {"noexec", "exec", "(Dis)allow use of executable files", MS_NOEXEC}, - {"noatime", "atime", "Disable/enable updates to inode access times", MS_NOATIME} -}; - -static unsigned long mu_parse_opts(char *str, char *data, const size_t data_size) { +unsigned long mu_parse_opts(char *str, char *data, const size_t data_size) { memset(data, '\0', data_size); unsigned long opt = 0; size_t data_len = 0; @@ -68,5 +49,3 @@ static unsigned long mu_parse_opts(char *str, char *data, const size_t data_size return opt; } - -#endif diff --git a/libmu/parse_mount.h b/libmu/parse_mount.h new file mode 100644 index 0000000..e0b7e20 --- /dev/null +++ b/libmu/parse_mount.h @@ -0,0 +1,24 @@ +#ifndef _PARSE_MOUNT_H +#define _PARSE_MOUNT_H +#include + +typedef struct { + char *opt; + char *notopt; + char *desc; + unsigned int val; +} MU_MOUNT_OPTS; + +static MU_MOUNT_OPTS mu_options[] = { + {"defaults", NULL, NULL, 0}, + {"ro", "rw", "Read only / Read and write", MS_RDONLY}, + {"remount", NULL, "Remount a mounted filesystem", MS_REMOUNT}, + {"sync", "async", "Writes are [a]synchronous", MS_SYNCHRONOUS}, + {"nodev", "dev", "(Dis)allow use of special device files", MS_NODEV}, + {"bind", "rbind", "Bind a file or directory", MS_BIND}, + {"noexec", "exec", "(Dis)allow use of executable files", MS_NOEXEC}, + {"noatime", "atime", "Disable/enable updates to inode access times", MS_NOATIME} +}; + +unsigned long mu_parse_opts(char *str, char *data, const size_t data_size); +#endif diff --git a/include/proc_parser.h b/libmu/proc_parser.c similarity index 72% rename from include/proc_parser.h rename to libmu/proc_parser.c index f7a028a..b948a37 100644 --- a/include/proc_parser.h +++ b/libmu/proc_parser.c @@ -1,52 +1,13 @@ -#ifndef _PROC_PARSER -#define _PROC_PARSER - #include #include #include #include #include -#include #include #include -#include -#include +#include "proc_parser.h" -struct mu_proc { - /* from status */ - char prog[PATH_MAX + 1]; - uid_t uid; - gid_t gid; - - /* from stat */ - pid_t pid; - char cmdline[PATH_MAX + 1]; - char state; - int ppid; - int pgrp; - int sid; - int tty; - int tpgid; - unsigned flags; - unsigned long minflt; - unsigned long cminflt; - unsigned long majflt; - unsigned long cmajflt; - unsigned long utime; - unsigned long stime; - long cutime; - long cstime; - long priority; - long nice; - long num_threads; - long itrealvalue; - unsigned long long starttime; - unsigned long vsize; - long rss; - long rsslim; -}; - -static int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) { +int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) { proc_s->uid = -1; proc_s->gid = -1; @@ -98,7 +59,7 @@ static int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc return 0; } -static int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) { +int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) { char path[PATH_MAX + 1]; snprintf(path, sizeof(path), "/proc/%d/stat", pid); @@ -132,5 +93,3 @@ int mu_proc_parser(const char *prog_name, const pid_t pid, struct mu_proc *proc_ return ret; } - -#endif diff --git a/libmu/proc_parser.h b/libmu/proc_parser.h new file mode 100644 index 0000000..cc427dd --- /dev/null +++ b/libmu/proc_parser.h @@ -0,0 +1,45 @@ +#ifndef _PROC_PARSER +#define _PROC_PARSER + +#include +#include +#include + +struct mu_proc { + /* from status */ + char prog[PATH_MAX + 1]; + uid_t uid; + gid_t gid; + + /* from stat */ + pid_t pid; + char cmdline[PATH_MAX + 1]; + char state; + int ppid; + int pgrp; + int sid; + int tty; + int tpgid; + unsigned flags; + unsigned long minflt; + unsigned long cminflt; + unsigned long majflt; + unsigned long cmajflt; + unsigned long utime; + unsigned long stime; + long cutime; + long cstime; + long priority; + long nice; + long num_threads; + long itrealvalue; + unsigned long long starttime; + unsigned long vsize; + long rss; + long rsslim; +}; + +int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s); +int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s); +int mu_proc_parser(const char *prog_name, const pid_t pid, struct mu_proc *proc_s); +#endif diff --git a/include/pw_check.h b/libmu/pw_check.c similarity index 77% rename from include/pw_check.h rename to libmu/pw_check.c index d33dcd2..45c31f5 100644 --- a/include/pw_check.h +++ b/libmu/pw_check.c @@ -1,14 +1,11 @@ -#ifndef _PW_CHECK_H -#define _PW_CHECK_H - -#include #include #include #include #include +#include "pw_check.h" #include "config.h" -static void dec_salt(void) { +void dec_salt(void) { size_t i; for (i = 0; i < sizeof(MU_SALT_ENC) / sizeof(int); i++) MU_SALT_BUF[i] = (char)MU_SALT_ENC[i]; @@ -16,7 +13,7 @@ static void dec_salt(void) { MU_SALT_BUF[i + 1] = '\0'; } -static char *enc_password(const char *prog_name, const char *pass, const char *salt) { +char *enc_password(const char *prog_name, const char *pass, const char *salt) { if (salt == NULL) dec_salt(); @@ -31,7 +28,7 @@ static char *enc_password(const char *prog_name, const char *pass, const char *s return cpass; } -static int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) { +int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) { if (pw->pw_passwd[0] == '\0' && pass[0] == '\0') return 0; @@ -55,4 +52,3 @@ static int pw_check(const char *prog_name, const struct passwd *pw, const char * return 1; } -#endif diff --git a/libmu/pw_check.h b/libmu/pw_check.h new file mode 100644 index 0000000..5554617 --- /dev/null +++ b/libmu/pw_check.h @@ -0,0 +1,9 @@ +#ifndef _PW_CHECK_H +#define _PW_CHECK_H +#include + +void dec_salt(void); +char *enc_password(const char *prog_name, const char *pass, const char *salt); +int pw_check(const char *prog_name, const struct passwd *pw, const char *pass); + +#endif diff --git a/include/recurse.h b/libmu/recurse.c similarity index 78% rename from include/recurse.h rename to libmu/recurse.c index 4b01146..1edac27 100644 --- a/include/recurse.h +++ b/libmu/recurse.c @@ -1,13 +1,11 @@ -#ifndef _RECURSE_H -#define _RECURSE_H - #include #include +#include #include #include "make_path.h" #include "get_stat.h" -static int mu_recurse(const char *restrict prog_name, int link_flag, const char *restrict path, void *restrict arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)) { +int mu_recurse(const char *restrict prog_name, int link_flag, const char *restrict path, void *restrict arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)) { struct stat sb; if (mu_get_stats(prog_name, link_flag, path, &sb)) return 1; @@ -49,5 +47,3 @@ static int mu_recurse(const char *restrict prog_name, int link_flag, const char return ret; } - -#endif diff --git a/libmu/recurse.h b/libmu/recurse.h new file mode 100644 index 0000000..ce3dc19 --- /dev/null +++ b/libmu/recurse.h @@ -0,0 +1,5 @@ +#ifndef _RECURSE_H +#define _RECURSE_H + +int mu_recurse(const char *restrict prog_name, int link_flag, const char *restrict path, void *restrict arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)); +#endif diff --git a/include/unused.h b/libmu/unused.h similarity index 100% rename from include/unused.h rename to libmu/unused.h diff --git a/obj/.gitignore b/obj/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/chgrp.c b/src/chgrp.c index 982e594..757a475 100644 --- a/src/chgrp.c +++ b/src/chgrp.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "make_path.h" #include "get_stat.h" diff --git a/src/chmod.c b/src/chmod.c index 5b4bc72..c2f681c 100644 --- a/src/chmod.c +++ b/src/chmod.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "mode_to_str.h" #include "make_path.h" diff --git a/src/env.c b/src/env.c index 09aa151..331ecf9 100644 --- a/src/env.c +++ b/src/env.c @@ -4,7 +4,7 @@ #include #include -int main(const int argc, char **argv, const char **envp) { +int main(const int argc, char **argv) { int i; for (i = 1; i < argc; i++) { char *val = strchr(argv[i], '='); @@ -21,10 +21,8 @@ int main(const int argc, char **argv, const char **envp) { /* Print env */ if (i == argc) { - while (*envp) - puts(*envp++); - - return 0; + fputs("env: not enought opearands\n", stderr); + return 1; } execvp(argv[i], argv + i); diff --git a/src/grep.c b/src/grep.c index 5e48c62..4fbfa0b 100644 --- a/src/grep.c +++ b/src/grep.c @@ -11,6 +11,11 @@ typedef struct { static PATTERN *regexs; static size_t r_size; +static char i_flag; +static char F_flag; +static char E_flag; +static char H_flag; + static int addpattern(char *str) { if (regexs == NULL) { regexs = malloc(sizeof(PATTERN)); @@ -29,16 +34,16 @@ static int addpattern(char *str) { } regexs = regexs_tmp; + /* Add new pattern */ regexs[r_size].pattern = str; - + r_size++; return 0; } int main(int argc, char **argv) { - int opt; - while ((opt = getopt(argc, argv, "e:")) != -1) { + while ((opt = getopt(argc, argv, "e:iFH")) != -1) { switch (opt) { case 'e': if (addpattern(optarg)) @@ -46,8 +51,20 @@ int main(int argc, char **argv) { break; + case 'i': + i_flag = 1; + break; + + case 'F': + F_flag = 1; + break; + + case 'H': + H_flag = 1; + break; + default: - printf("grep [e] [FILE]\n\t-e PTRN Pattern to match\n"); + printf("grep [eiFH] [FILE]\n\t-e PTRN Pattern to match\n\t-i Ignore case\n\t-H Add 'filename:' prefix\n\t-F PATTERN is a literal (not regexp)\n"); return 0; } } diff --git a/src/kill.c b/src/kill.c index 0748208..bbb7d11 100644 --- a/src/kill.c +++ b/src/kill.c @@ -94,7 +94,7 @@ int main(int argc, char **argv) { argc -= optind; if ((n_flag || !a_flag) && argc == 0) { - fprintf(stderr, "kill: missing operands\n"); + fputs("kill: missing operands\n", stderr); return 1; } diff --git a/src/ln.c b/src/ln.c index b7f6c48..c48781d 100644 --- a/src/ln.c +++ b/src/ln.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include "make_path.h" diff --git a/src/printenv.c b/src/printenv.c index 860ccae..1542f7c 100644 --- a/src/printenv.c +++ b/src/printenv.c @@ -3,7 +3,9 @@ #include #include -static int printvars(int len, char **names) { +extern char **environ; + +static int printvars(int len, char **names, char n_flag) { int ret = 0; for (int i = 0; i < len; i++) { @@ -13,28 +15,38 @@ static int printvars(int len, char **names) { continue; } - printf("%s\n", val); + printf("%s%c", val, n_flag); } return ret; } -int main(int argc, char **argv, const char **envp) { +int main(int argc, char **argv) { + char n_flag = '\n'; + int opt; while ((opt = getopt(argc, argv, "0")) != -1) { - puts("printenv [var1 var2...]"); - return 0; + switch (opt) { + case 'n': + n_flag = '\0'; + break; + + default: + puts("printenv [var1 var2...]\n\t-0 NUL terminated output\n"); + return 0; + } } argv += optind; argc -= optind; - if (argc == 0) - while (*envp) - printf("%s\n", *envp++); + if (argc == 0) { + while (*environ) + printf("%s%c", *environ++, n_flag); + } else - return printvars(argc, argv); + return printvars(argc, argv, n_flag); return 0; } diff --git a/src/rm.c b/src/rm.c index d5c81da..f362a0c 100644 --- a/src/rm.c +++ b/src/rm.c @@ -77,7 +77,7 @@ int main(int argc, char **argv) { break; default: - puts("rm [rif] [file1 file2...]\n\t-f Force\n\t-r Recursive\n\t-i Print prompt before remove"); + puts("rm [rif] [file1 file2...]\n\t-f Never prompt\n\t-r Recursive\n\t-i Print prompt before remove"); return 0; } }