This commit is contained in:
Your Name 2024-07-03 17:22:50 +03:00
parent 4dda5db6f6
commit ec99d14ad6
34 changed files with 68 additions and 42 deletions

View File

@ -2,4 +2,4 @@
Own implementation of *nix utils Own implementation of *nix utils
License: wtfpl License: wtfpl
Author: 8nl https://trivial.technology/

View File

@ -4,12 +4,16 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
int main(const int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 3 || !strcmp(argv[argc - 1], "--help")) { int opt;
while ((opt = getopt(argc, argv, "")) != -1 || argc == 1) {
puts("chroot [dir] [command] [arg arg2...]"); puts("chroot [dir] [command] [arg arg2...]");
return 0; return 0;
} }
argv += optind;
argc -= optind;
if (chroot(argv[1]) < 0) { if (chroot(argv[1]) < 0) {
fprintf(stderr, "chroot: %s\n", strerror(errno)); fprintf(stderr, "chroot: %s\n", strerror(errno));
return 1; return 1;
@ -20,7 +24,7 @@ int main(const int argc, char **argv) {
return 1; return 1;
} }
if (execvp(argv[2], argv + 2) < 0) { if (execvp(argv[0], argv) < 0) {
fprintf(stderr, "chroot: %s\n", strerror(errno)); fprintf(stderr, "chroot: %s\n", strerror(errno));
return 1; return 1;
} }

View File

@ -101,14 +101,18 @@ int main(int argc, char **argv) {
int ofd = STDOUT_FILENO; int ofd = STDOUT_FILENO;
int ifd = STDIN_FILENO; int ifd = STDIN_FILENO;
char notrunc = 0;
char fsync_flag = 0;
/* Return value */ /* Return value */
int ret = 1; int ret = 1;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
char *arg = argv[i]; char *arg = argv[i];
char *val = strchr(arg, '='); char *val = strchr(arg, '=');
if (val == NULL) { if (val == NULL) {
printf("dd\n\tif=InputFile\n\tof=OutputFile\n\tbs=ibs and obs\n\tibs=Input buffer size\n\tobs=Output buffer size\n\tseek=Skip N obs-sized output blocks\n\tskip=Skip N ibs-sized output blocks\n\tcount=Copy only N input blocks\n\nN and BYTES may be followed by the following multiplicative\nsuffixes: w=2, b=512, k=1000, K=1024, m=1000*1000,\nM=1024*1024, g=1000*1000*1000, G=1024*1024*1024\n"); puts("dd\n\tif=InputFile\n\tof=OutputFile\n\tbs=ibs and obs\n\tibs=Input buffer size\n\tobs=Output buffer size\n\tseek=Skip N obs-sized output blocks\n\tskip=Skip N ibs-sized output blocks\n\tcount=Copy only N input blocks\n\tnconv=notrunc Don't truncate output file\n\tconv=fsync Physically write data out before finishing\n\nN and BYTES may be followed by the following multiplicative\nsuffixes: w=2, b=512, k=1000, K=1024, m=1000*1000,\nM=1024*1024, g=1000*1000*1000, G=1024*1024*1024");
return 1; return 1;
} }
@ -139,6 +143,15 @@ int main(int argc, char **argv) {
else if (!strcmp(arg, "obs")) else if (!strcmp(arg, "obs"))
obs = strtonum(val); obs = strtonum(val);
char *ptr = strstr(arg, "conv");
if (ptr) {
if (strstr(val, "notrunc"))
notrunc = 1;
if (strstr(val, "fsync"))
fsync_flag = 1;
}
} }
if (bs) { if (bs) {
@ -172,12 +185,17 @@ int main(int argc, char **argv) {
/* Output */ /* Output */
int oflag = O_WRONLY | O_CREAT; int oflag = O_WRONLY | O_CREAT;
if (seek) if (seek && !notrunc)
oflag |= O_TRUNC; oflag |= O_TRUNC;
ofd = openfile(1, ofp, oflag); ofd = openfile(1, ofp, oflag);
if (seek) { if (seek) {
if (ftruncate(ofd, seek * ibs) < 0 || lseek(ofd, seek * ibs, SEEK_SET) < 0) { if (!notrunc && ftruncate(ofd, seek * ibs) < 0) {
fprintf(stderr, "dd: %s\n", strerror(errno));
goto CLOSE;
}
if (lseek(ofd, seek * ibs, SEEK_SET) < 0) {
fprintf(stderr, "dd: %s\n", strerror(errno)); fprintf(stderr, "dd: %s\n", strerror(errno));
goto CLOSE; goto CLOSE;
} }
@ -233,7 +251,9 @@ int main(int argc, char **argv) {
} }
/* End */ /* End */
if (fsync_flag)
fsync(ofd); fsync(ofd);
summary(); summary();
ret = 0; ret = 0;

View File

@ -88,7 +88,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("du [hsc] [src1 src2...]\n\t-h Sizes in human readable format\n\t-s Display only a total for each argument\n\t-c produce a grand total\n"); puts("du [hsc] [src1 src2...]\n\t-h Sizes in human readable format\n\t-s Display only a total for each argument\n\t-c produce a grand total");
return 0; return 0;
} }
} }

View File

@ -35,6 +35,7 @@ int print(const char *file, FILE *fp, long lines, long bytes) {
long lmax = 0; long lmax = 0;
long bmax = 0; long bmax = 0;
/* Make buffer */
size_t buf_size = 0; size_t buf_size = 0;
char *buf = malloc(1); char *buf = malloc(1);
if (buf == NULL) { if (buf == NULL) {
@ -65,6 +66,7 @@ int print(const char *file, FILE *fp, long lines, long bytes) {
buf[buf_size] = '\0'; buf[buf_size] = '\0';
/* Print buffer */
for (size_t i = 0; i < buf_size; i++) { for (size_t i = 0; i < buf_size; i++) {
if (buf[i] == '\n') if (buf[i] == '\n')
lcount++; lcount++;
@ -119,7 +121,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("head [ncv] [file1 file2...]\n\t-n Print [-]N lines\n\t-c Print [-]N bytes\n\t-v Print headers\n"); puts("head [ncv] [file1 file2...]\n\t-n Print [-]N lines\n\t-c Print [-]N bytes\n\t-v Print headers");
return 0; return 0;
} }
} }

View File

@ -68,7 +68,7 @@ int def_ids(uid_t uid, struct passwd *pwd) {
} }
void usage(int sig) { void usage(int sig) {
printf("id [gGurn] [user]\n\t-u User ID\n\t-g Group ID\n\t-G Supplementary group IDs\n\t-n Print names instead of numbers\n\t-r Print real ID instead of effective ID\n"); puts("id [gGurn] [user]\n\t-u User ID\n\t-g Group ID\n\t-G Supplementary group IDs\n\t-n Print names instead of numbers\n\t-r Print real ID instead of effective ID");
exit(sig); exit(sig);
} }

View File

@ -43,7 +43,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("ln [sfv] [src] [dst]\n\t-f Force\n\t-s Symbolic\n\t-v Verbose\n"); puts("ln [sfv] [src] [dst]\n\t-f Force\n\t-s Symbolic\n\t-v Verbose");
return 0; return 0;
} }
} }

View File

@ -470,7 +470,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("ls [1alFcRdLhistS0] [dir1 file2...]\n\t-a Show hidden files\n\t-l Use a long listing format\n\t-F Append indicator to names\n\t-c Color mode\n\t-R Recursive\n\t-1 One column\n\t-d Print only dir names\n\t-L Follow symlinks\n\t-h Sizes in human readable format\n\t-i Listen inodes\n\t-t Sort by mtime\n\t-S Sort by size\n\t-s Print file size\n\t-0 End line with \\0\n"); puts("ls [1alFcRdLhistS0] [dir1 file2...]\n\t-a Show hidden files\n\t-l Use a long listing format\n\t-F Append indicator to names\n\t-c Color mode\n\t-R Recursive\n\t-1 One column\n\t-d Print only dir names\n\t-L Follow symlinks\n\t-h Sizes in human readable format\n\t-i Listen inodes\n\t-t Sort by mtime\n\t-S Sort by size\n\t-s Print file size\n\t-0 End line with \\0");
return 0; return 0;
} }
} }

View File

@ -61,7 +61,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("mkdir [pmv] [dst1 dst2...]\n\t-p Make parent dir\n\t-m MODE Mode\n\t-v Verbose\n"); puts("mkdir [pmv] [dst1 dst2...]\n\t-p Make parent dir\n\t-m MODE Mode\n\t-v Verbose");
return 0; return 0;
} }
} }

View File

@ -38,8 +38,8 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("mknod [m] [NAME] [TYPE] [MAJOR MINOR]\n\t-m MODE\n\n"); puts("mknod [m] [NAME] [TYPE] [MAJOR MINOR]\n\t-m MODE\n");
printf("Types:\n\tb - block device\n\tc or u - character device\n\tp - named pipe/fifo (MAJOR MINOR must be omitted)\n\ts - socket\n"); puts("Types:\n\tb - block device\n\tc or u - character device\n\tp - named pipe/fifo (MAJOR MINOR must be omitted)\n\ts - socket");
return 0; return 0;
} }
} }

View File

@ -60,7 +60,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("mktemp [dp] [file]\n\t-d Dir\n\t-p Base dir\n"); puts("mktemp [dp] [file]\n\t-d Dir\n\t-p Base dir");
return 0; return 0;
} }
} }

View File

@ -38,7 +38,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("mv [f] [src1 src2...] [dst]\n\t-f Force\n"); puts("mv [f] [src1 src2...] [dst]\n\t-f Force");
return 0; return 0;
} }
} }

View File

@ -17,7 +17,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("nice [n] [cmd] [arg1] [arg2]\n\t-n N Add N to the niceness\n"); puts("nice [n] [cmd] [arg1] [arg2]\n\t-n N Add N to the niceness");
return 0; return 0;
} }
} }

View File

@ -8,7 +8,7 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
printf("nohup: missing operand\nnohup [cmd]\n"); puts("nohup: missing operand\nnohup [cmd]");
return 0; return 0;
} }

View File

@ -22,7 +22,7 @@ int printvars(int len, char **names) {
int main(int argc, char **argv, const char **envp) { int main(int argc, char **argv, const char **envp) {
int opt; int opt;
while ((opt = getopt(argc, argv, "0")) != -1) { while ((opt = getopt(argc, argv, "0")) != -1) {
printf("printenv [var1 var2...]\n"); puts("printenv [var1 var2...]");
return 0; return 0;
} }

View File

@ -16,7 +16,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("readlink [n] [file1 file2...]\n\t-n Don't add newline\n"); puts("readlink [n] [file1 file2...]\n\t-n Don't add newline");
return 0; return 0;
} }
} }

View File

@ -41,7 +41,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("renice [ngup] [id1 id2...]\n\t-n N Add N to the niceness\n\t-g Process group ids\n\t-u Process user names\n\t-p Process ids\n"); puts("renice [ngup] [id1 id2...]\n\t-n N Add N to the niceness\n\t-g Process group ids\n\t-u Process user names\n\t-p Process ids");
return 0; return 0;
} }
} }

View File

@ -77,7 +77,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("rm [rif] [file1 file2...]\n\t-f Force\n\t-r Recursive\n\t-i Print prompt before remove\n"); puts("rm [rif] [file1 file2...]\n\t-f Force\n\t-r Recursive\n\t-i Print prompt before remove");
return 0; return 0;
} }
} }

View File

@ -9,7 +9,7 @@ int main(int argc, char **argv) {
UNUSED(argc); UNUSED(argc);
if (argv[1] == NULL) { if (argv[1] == NULL) {
printf("setsid [cmd] [arg1 arg2...]\n"); puts("setsid [cmd] [arg1 arg2...]");
return 1; return 1;
} }

View File

@ -68,7 +68,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("shred [fuzn] [file1 file2...]\n\t-n N Overwrite N times, default 3\n\t-z Final overwrite with zeros\n\t-u Remove file\n\t-f Chmod to ensure writability\n"); puts("shred [fuzn] [file1 file2...]\n\t-n N Overwrite N times, default 3\n\t-z Final overwrite with zeros\n\t-u Remove file\n\t-f Chmod to ensure writability");
return 0; return 0;
} }
} }

View File

@ -9,7 +9,7 @@ int main(int argc, char **argv) {
int opt; int opt;
while ((opt = getopt(argc, argv, "")) != -1 || argc == 1) { while ((opt = getopt(argc, argv, "")) != -1 || argc == 1) {
printf("sleep [num[SUFFIX]] or [inf Infinity]\nSUFFIXES:\n\tm - minute\n\th - hour\n\td - days\n"); puts("sleep [num[SUFFIX]] / [inf]\nSUFFIXES:\n\tm - minute\n\th - hour\n\td - days");
return 0; return 0;
} }

View File

@ -70,7 +70,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("split [alb] [file1]\n\t-l N Split by N lines\n\t-a N Use N letters as prefix\n\t-b N[k|m] Split by N (kilo|mega)bytes\n"); puts("split [alb] [file1]\n\t-l N Split by N lines\n\t-a N Use N letters as prefix\n\t-b N[k|m] Split by N (kilo|mega)bytes");
return 0; return 0;
} }
} }

View File

@ -22,7 +22,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("tee [ai] [file]\n\t-a Append\n\t-i Ignore interrupt signals\n"); puts("tee [ai] [file]\n\t-a Append\n\t-i Ignore interrupt signals");
return 0; return 0;
} }
} }

View File

@ -9,7 +9,7 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
printf("time: missing operand\ntime [cmd]\n"); puts("time: missing operand\ntime [cmd]");
return 0; return 0;
} }

View File

@ -126,7 +126,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("touch [cmad] [file1 file2...]\n\t-c Don't create files\n\t-a Change only atime\n\t-m Change only mtime\n\t-d Date/time to use\n"); puts("touch [cmad] [file1 file2...]\n\t-c Don't create files\n\t-a Change only atime\n\t-m Change only mtime\n\t-d Date/time to use");
return 0; return 0;
} }
} }

View File

@ -102,7 +102,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("uname [asnrvmop]\n\t-a All\n\t-s Sys\n\t-n Nodename\n\t-r Release\n\t-v Version\n\t-m Machine\n\t-o Os name\n"); puts("uname [asnrvmop]\n\t-a All\n\t-s Sys\n\t-n Nodename\n\t-r Release\n\t-v Version\n\t-m Machine\n\t-o Os name");
return 0; return 0;
} }
} }

View File

@ -84,7 +84,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("wc [lcwt] [file1 file2...]\n\t-l Lines\n\t-c Bytes\n\t-w Words\n\t-t Don't print total\n"); puts("wc [lcwt] [file1 file2...]\n\t-l Lines\n\t-c Bytes\n\t-w Words\n\t-t Don't print total");
return 0; return 0;
} }
} }

View File

@ -266,7 +266,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("xargs [tnsrP0I] [cmd [arg1] [arg2...]\n\t-t Print the command before start\n\t-n Pass no more than N args\n\t-r Don't run command if input is empty\n\t-s Pass command line of no more than N bytes\n\t-0 NUL terminated input\n\t-I STR Replace STR within PROG ARGS with input line\n"); puts("xargs [tnsrP0I] [cmd [arg1] [arg2...]\n\t-t Print the command before start\n\t-n Pass no more than N args\n\t-r Don't run command if input is empty\n\t-s Pass command line of no more than N bytes\n\t-0 NUL terminated input\n\t-I STR Replace STR within PROG ARGS with input line");
return 0; return 0;
} }
} }

View File

@ -3,7 +3,7 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
printf("cryptpw: missing operand\ncryptpw [password] [own salt]\n"); puts("cryptpw: missing operand\ncryptpw [password] [own salt]");
return 0; return 0;
} }

View File

@ -105,7 +105,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("su [cps] [user Default: root]\n\t-c CMD Command to pass to \'sh -c\'\n\t-s SHELL Shell to use instead of user's default\n\t-p Do not set new env\n"); puts("su [cps] [user Default: root]\n\t-c CMD Command to pass to \'sh -c\'\n\t-s SHELL Shell to use instead of user's default\n\t-p Do not set new env");
return 0; return 0;
} }
} }

View File

@ -22,7 +22,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("dmesg [sn]\n\t-n LEVEL Set console logging level\n\t-s SIZE Buffer Size\n"); puts("dmesg [sn]\n\t-n LEVEL Set console logging level\n\t-s SIZE Buffer Size");
return 0; return 0;
} }
} }

View File

@ -90,7 +90,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("mount [ato] [DEVICE] [NODE]\n\t-t Fs type, default %s\n\t-o Options\n\t-a Mount all fs in /etc/fstab\n\nOptions:\n", MOUNT_DEF_FS); printf("mount [ato] [DEVICE] [NODE]\n\t-t Fs type, default %s\n\t-o Options\n\t-a Mount all fs in /etc/fstab\n\nOptions:", MOUNT_DEF_FS);
print_opts(); print_opts();
return 0; return 0;
} }

View File

@ -50,7 +50,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("umount [afl] [dst1 dst2...]\n\t-a Umount all\n\t-f Force umount\n\t-l Lazy umount\n"); puts("umount [afl] [dst1 dst2...]\n\t-a Umount all\n\t-f Force umount\n\t-l Lazy umount");
return 0; return 0;
} }
} }