This commit is contained in:
Your Name 2023-10-21 15:05:49 +03:00
parent 71ad4e384a
commit 62eafb82ca
14 changed files with 194 additions and 17 deletions

View File

@ -29,7 +29,7 @@ int main(const int argc, const char **argv) {
else if (!strcmp(argv[i], "-n"))
flag = 1;
else if (!strcmp(argv[i], "-h")) {
else if (!strcmp(argv[i], "--help")) {
printf("cat [-n (numerate)] [file1 file2 ...]\n");
return 0;
}

View File

@ -113,11 +113,13 @@ int cptree(const char *src, const char *dst) {
char *src_path = make_path(src, ep->d_name);
if (src_path == NULL)
return 1;
continue;
char *dst_path = make_path(dst, ep->d_name);
if (dst_path == NULL)
return 1;
if (dst_path == NULL) {
free(src_path);
continue;
}
cptree(src_path, dst_path);
@ -136,7 +138,7 @@ int main(const int argc, char **argv) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-h")) {
else if (!strcmp(argv[i], "--help")) {
printf("cp [Src] [Dst]\n");
return 0;
}

108
coreutils/du.c Normal file
View File

@ -0,0 +1,108 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
unsigned int h_flag;
unsigned int s_flag;
unsigned int b_flag;
unsigned int m_flag;
unsigned int L_flag;
void print(long size, const char *filename) {
/* Kb */
if (!b_flag)
size = size / 1024;
/* Mb */
else if (m_flag)
size = size / 1048576;
printf("%ld\t%s\n", size, filename);
}
long du(const char *path) {
long sum;
struct stat sb;
if (lstat(path, &sb) != 0) {
fprintf(stderr, "du: lstat() %s\n", strerror(errno));
return 0;
}
if (S_ISDIR(sb.st_mode)) {
DIR *dp = opendir(path);
if (!dp) {
fprintf(stderr, "du: %s\n", strerror(errno));
return 0;
}
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
size_t len = strlen(path) + strlen(ep->d_name) + 2;
char *new_path = malloc(len + 1);
if (new_path == NULL)
continue;
snprintf(new_path, len, "%s/%s", path, ep->d_name);
sum += du(new_path);
free(new_path);
}
closedir(dp);
print(sum, path);
}
else
sum += sb.st_size;
return sum;
}
int main(const int argc, const char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-h"))
h_flag = 1;
else if (!strcmp(argv[i], "-s"))
s_flag = 1;
else if (!strcmp(argv[i], "-b"))
b_flag = 1;
else if (!strcmp(argv[i], "-m")) {
b_flag = 1;
m_flag = 1;
}
else if (!strcmp(argv[i], "-L"))
L_flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("du [-h (Sizes in human readable format (e.g., 1K 243M 2G))] [-s (Display only a total for each argument)] [-b (Apparent size)] [-L (Follow all symlinks)] [-m (Size in megabyte)] [file file2...]\n");
return 0;
}
}
if (argc - i == 0)
du(".");
else
for (; i < argc; i++)
du(argv[i]);
return 0;
}

56
coreutils/du.c.save Normal file
View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
unsigned int h_flag;
unsigned int s_flag;
unsigned int b_flag;
unsigned int m_flag;
unsigned int L_flag;
int du(const char *path) {
struct stat sb;
if (lstat(path, &sb)) {
fprintf(stderr, "du: lstat() %s\n", strerror(errno));
return 0;
}
return 0;
}
int main(const int argc, const char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-h"))
h_flag = 1;
else if (!strcmp(argv[i], "-s"))
s_flag = 1;
else if (!strcmp(argv[i], "-b"))
b_flag = 1;
else if (!strcmp(argv[i], "-m"))
m_flag = 1;
else if (!strcmp(argv[i], "-L"))
L_flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("du [-h (Sizes in human readable format (e.g., 1K 243M 2G))] [-s (Display only a total for each argument)] [-b (Apparent size)] [-L (Follow all symlinks)] [-m (Size in megabyte)] [file file2...]\n");
return 0;
}
}
for (; i < argc; i++)
du(argv[i]);
return 0;
}

View File

@ -24,7 +24,7 @@ int main(const int argc, const char **argv) {
else if (!strcmp("-s", argv[i]))
s_flag = 1;
else if (!strcmp("-h", argv[i])) {
else if (!strcmp("--help", argv[i])) {
printf("ln [-s] [TARGET] [LINK/DIR]\n");
return 0;
}

View File

@ -100,8 +100,8 @@ int main(const int argc, const char **argv) {
else if (!strcmp(argv[i], "-l"))
l_flag = 1;
else if (!strcmp(argv[i], "-h")) {
printf("ls [-a (Show hidden files)] [-l (use a long listing format (month / day / hour:min))] [Path]\n");
else if (!strcmp(argv[i], "--help")) {
printf("ls [-a (show hidden files)] [-l (use a long listing format)] [Path]\n");
return 0;
}
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
#define MSG "Micro-coreutils: Version 0.1 LICENSE: wtfpl\n"
int main(void) {
puts(MSG);
return 0;
}

View File

@ -43,7 +43,7 @@ int main(const int argc, const char **argv) {
else if (!strcmp(argv[i], "-p"))
flag = 1;
else if (!strcmp(argv[i], "-h")) {
else if (!strcmp(argv[i], "--help")) {
printf("mkdir [-p (make parent dir)] [dir1 dir2...]\n");
return 0;
}

View File

@ -28,7 +28,7 @@ int main(const int argc, const char **argv, const char **envp) {
else if (!strcmp(argv[i], "-0"))
nullline = 1;
else if (!strcmp(argv[i], "-h")) {
else if (!strcmp(argv[i], "--help")) {
printf("printenv [-n (end each output line with NUL, not newline)]\n");
return 0;
}

5
coreutils/reset.c Normal file
View File

@ -0,0 +1,5 @@
#include <unistd.h>
int main(void) {
return write(1, "\033c\033(K\033[J\033[0m\033[?25h", 18) != 18;
}

View File

@ -42,10 +42,8 @@ int rmtree(const char *path) {
size_t len = strlen(path) + strlen(ep->d_name) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "rm: malloc() returned NULL\n");
return 1;
}
if (full_path == NULL)
continue;
snprintf(full_path, len, "%s/%s", path, ep->d_name);

View File

@ -68,7 +68,7 @@ int main(const int argc, const char **argv) {
n_loops = atoi(val + 1);
}
else if (!strcmp(argv[i], "-h")) {
else if (!strcmp(argv[i], "--help")) {
printf("shred [-n=N (Overwrite N times (default 3))] [-z (Final overwrite with zeros)] [-u (Remove file)] [-f (Chmod to ensure writability)] [FILE...]\n");
return 1;
}

View File

@ -35,7 +35,7 @@ int main(const int argc, const char **argv) {
else if (!strcmp("-m", argv[i]))
flags[O_MACHINE] = 1;
else if (!strcmp("-h", argv[i])) {
else if (!strcmp("--help", argv[i])) {
printf("uname [-a (All)] [-s (Sys)] [-n (Nodename)] [-r (Release)] [-v (Version)] [-m (Machine)]\n");
return 0;
}

View File

@ -75,7 +75,7 @@ int main(const int argc, const char **argv) {
else if (!strcmp(argv[i], "-w"))
w_flag = 1;
else if (!strcmp(argv[i], "-h")) {
else if (!strcmp(argv[i], "--help")) {
printf("wc [-l (lines)] [-c (bytes)] [-w (words)] [file1 file2...]\n");
return 0;
}