fixed. Support building in one binary

This commit is contained in:
Your Name 2024-07-09 22:33:45 +03:00
parent 7d0207ace2
commit 33b89e64da
38 changed files with 285 additions and 163 deletions

50
build-box.sh Executable file
View File

@ -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 <stdio.h>
#include <string.h>
$(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

View File

@ -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

3
clean.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
rm -f obj/* bin/* mutils.c mutils

View File

@ -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

View File

@ -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

4
libmu/build-libmu.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
cd obj
cc ../libmu/*.c -I ../configs -I ../libmu -c

View File

@ -1,9 +1,7 @@
#ifndef _DURATION_H
#define _DURATION_H
#include <stdlib.h>
#include <string.h>
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

7
libmu/duration.h Normal file
View File

@ -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

View File

@ -1,12 +1,9 @@
#ifndef _GET_STAT_H
#define _GET_STAT_H
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#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

9
libmu/get_stat.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _GET_STAT_H
#define _GET_STAT_H
#include <sys/stat.h>
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

View File

@ -1,13 +1,6 @@
#ifndef _HUMAN_H
#define _HUMAN_H
#include "human.h"
#include <stdlib.h>
#include <stdint.h>
#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

12
libmu/human.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _HUMAN_H
#define _HUMAN_H
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#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

View File

@ -1,11 +1,8 @@
#ifndef _MAKE_PATH_H
#define _MAKE_PATH_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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

5
libmu/make_path.h Normal file
View File

@ -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

View File

@ -1,11 +1,8 @@
#ifndef _MODE_TO_STR_H
#define _MODE_TO_STR_H
#include <stdio.h>
#include <sys/stat.h>
#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

6
libmu/mode_to_str.h Normal file
View File

@ -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

11
libmu/mu_strlen.c Normal file
View File

@ -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;
}

6
libmu/mu_strlen.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _MU_STRLEN
#define _MU_STRLEN
#include <stdio.h>
size_t mu_strlen(const char *s);
#endif

View File

@ -1,6 +1,4 @@
#ifndef _PARSE_MODE_H
#define _PARSE_MODE_H
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
@ -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

5
libmu/parse_mode.h Normal file
View File

@ -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

View File

@ -1,26 +1,7 @@
#ifndef _PARSE_MOUNT_H
#define _PARSE_MOUNT_H
#include <string.h>
#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

24
libmu/parse_mount.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef _PARSE_MOUNT_H
#define _PARSE_MOUNT_H
#include <sys/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}
};
unsigned long mu_parse_opts(char *str, char *data, const size_t data_size);
#endif

View File

@ -1,52 +1,13 @@
#ifndef _PROC_PARSER
#define _PROC_PARSER
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#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

45
libmu/proc_parser.h Normal file
View File

@ -0,0 +1,45 @@
#ifndef _PROC_PARSER
#define _PROC_PARSER
#include <pwd.h>
#include <grp.h>
#include <limits.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;
};
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

View File

@ -1,14 +1,11 @@
#ifndef _PW_CHECK_H
#define _PW_CHECK_H
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#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

9
libmu/pw_check.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _PW_CHECK_H
#define _PW_CHECK_H
#include <pwd.h>
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

View File

@ -1,13 +1,11 @@
#ifndef _RECURSE_H
#define _RECURSE_H
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#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

5
libmu/recurse.h Normal file
View File

@ -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

0
obj/.gitignore vendored Normal file
View File

View File

@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include "make_path.h"
#include "get_stat.h"

View File

@ -3,6 +3,7 @@
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include "mode_to_str.h"
#include "make_path.h"

View File

@ -4,7 +4,7 @@
#include <string.h>
#include <unistd.h>
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);

View File

@ -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,6 +34,7 @@ static int addpattern(char *str) {
}
regexs = regexs_tmp;
/* Add new pattern */
regexs[r_size].pattern = str;
r_size++;
@ -36,9 +42,8 @@ static int addpattern(char *str) {
}
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;
}
}

View File

@ -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;
}

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "make_path.h"

View File

@ -3,7 +3,9 @@
#include <string.h>
#include <unistd.h>
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;
}

View File

@ -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;
}
}