This commit is contained in:
Your Name 2023-10-31 12:53:32 +03:00
parent 5eda08233d
commit 6d1cf79ca6
10 changed files with 92 additions and 119 deletions

View File

@ -1,4 +1,4 @@
objects = coreutils console-tools networking procps
objects = coreutils console-tools networking procps shell
CFLAGS?=-s -Os -flto -pedantic -Wall -Wextra
CC?=cc
@ -7,7 +7,7 @@ all: $(objects)
$(objects):
echo MAKE $@
cd $@ && $(MAKE) -B CC="$(CC)" CFLAGS="$(CFLAGS)" && cd ..
cd $@ && $(MAKE) -B CC="$(CC)" CFLAGS="$(CFLAGS) -I../libmu" && cd ..
clean:
rm bin/*

View File

@ -6,35 +6,28 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "make_path.h"
#include "get_stat.h"
unsigned int r_flag;
unsigned int stat_flag;
int (*chown_func)(const char *pathname, uid_t owner, gid_t group);
int (*stat_func)(const char *restrict pathname, struct stat *restrict statbuf);
long gid;
long uid;
int get_stat(const char *path, struct stat *stat_path) {
if (stat_func(path, stat_path)) {
fprintf(stderr, "chown: unable to stat %s: %s\n", path, strerror(errno));
if (stat_flag) {
if (mu_get_stat("chown", path, stat_path))
return 1;
}
else if (mu_get_lstat("chown", path, stat_path))
return 1;
return 0;
}
char *make_path(const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "chown: malloc() returned NULL\n");
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
int change(const char *file) {
struct stat old_file;
if (get_stat(file, &old_file))
@ -79,7 +72,7 @@ int cntree(const char *dst) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
char *full_path = make_path(dst, ep->d_name);
char *full_path = mu_make_path("chown", dst, ep->d_name);
if (full_path == NULL)
continue;
@ -130,7 +123,6 @@ void get_owner(const char *arg) {
int main(const int argc, char **argv) {
chown_func = lchown;
stat_func = stat;
int i;
for (i = 1; i < argc; i++) {
@ -142,7 +134,7 @@ int main(const int argc, char **argv) {
else if (!strcmp(argv[i], "-H")) {
chown_func = chown;
stat_func = stat;
stat_flag = 1;
}
else if (!strcmp(argv[i], "--help")) {

View File

@ -7,19 +7,8 @@
#include <unistd.h>
#include <dirent.h>
#include <sys/mman.h>
#include <sys/stat.h>
char *make_path(const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "cp: malloc() returned NULL\n");
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
#include "make_path.h"
#include "get_stat.h"
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
off_t len = lseek(ifd, 0, SEEK_END);
@ -77,7 +66,7 @@ int copy(int mode, const char *src, const char *dst) {
flag = 1;
new_path = NULL;
new_path = make_path(dst, basename(dup));
new_path = mu_make_path("cp", dst, basename(dup));
if (new_path == NULL)
goto CLOSE;
@ -110,18 +99,9 @@ CLOSE:
return ret;
}
int get_stat(const char *path, struct stat *stat_path) {
if (stat(path, stat_path)) {
fprintf(stderr, "cp: unable to stat %s: %s\n", path, strerror(errno));
return 1;
}
return 0;
}
int cptree(const char *src, const char *dst) {
struct stat stat_path;
if (get_stat(src, &stat_path))
if (mu_get_lstat("cp", src, &stat_path))
return 1;
if (!S_ISDIR(stat_path.st_mode)) {
@ -147,11 +127,11 @@ int cptree(const char *src, const char *dst) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..") || !strcmp(dst, ep->d_name))
continue;
char *src_path = make_path(src, ep->d_name);
char *src_path = mu_make_path("cp", src, ep->d_name);
if (src_path == NULL)
continue;
char *dst_path = make_path(dst, ep->d_name);
char *dst_path = mu_make_path("cp", dst, ep->d_name);
if (dst_path == NULL) {
free(src_path);
continue;
@ -186,7 +166,7 @@ int main(const int argc, char **argv) {
else {
for (; i < argc - 1; i++) {
char *new_path = make_path(argv[argc - 1], basename(argv[i]));
char *new_path = mu_make_path("cp", argv[argc - 1], basename(argv[i]));
if (new_path == NULL)
return 1;

View File

@ -4,7 +4,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include "get_stat.h"
#include "make_path.h"
unsigned int h_flag;
unsigned int s_flag;
@ -44,10 +45,8 @@ double du(const char *path, int recurs_flag) {
double sum = 0;
struct stat sb;
if (lstat(path, &sb) != 0) {
fprintf(stderr, "du: lstat() %s\n", strerror(errno));
return 0;
}
if (mu_get_lstat("du", path, &sb))
return 1;
if (S_ISDIR(sb.st_mode)) {
DIR *dp = opendir(path);
@ -61,12 +60,9 @@ double du(const char *path, int recurs_flag) {
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);
char *new_path = mu_make_path("du", path, ep->d_name);
if (new_path == NULL)
continue;
snprintf(new_path, len, "%s/%s", path, ep->d_name);
return 1;
sum += du(new_path, 1);

View File

@ -8,33 +8,13 @@
#include <stdint.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "make_path.h"
#include "get_stat.h"
unsigned int a_flag;
unsigned int l_flag;
int get_stat(const char *path, struct stat *stat_path) {
if (stat(path, stat_path)) {
fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno));
return 1;
}
return 0;
}
char *make_path(const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "cp: malloc() returned NULL\n");
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
void PrintPerm(struct stat sb) {
printf("%c", (S_ISDIR(sb.st_mode)) ? 'd' : '-');
printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-');
@ -44,7 +24,7 @@ void PrintPerm(struct stat sb) {
char *fileflag(const char *path) {
struct stat sb;
if (get_stat(path, &sb))
if (mu_get_lstat("ls", path, &sb))
return " ";
if (S_ISDIR(sb.st_mode))
@ -79,10 +59,8 @@ void PrintInfo(struct stat sb, const char *filename) {
int list(const char *path, int label) {
struct stat sb;
if (lstat(path, &sb)) {
fprintf(stderr, "ls: unable to lstat %s: %s\n", path, strerror(errno));
if (mu_get_lstat("ls", path, &sb))
return 1;
}
/* If its file */
if (!S_ISDIR(sb.st_mode)) {
@ -111,12 +89,12 @@ int list(const char *path, int label) {
if (ep->d_name[0] == '.' && !a_flag)
continue;
char *full_path = make_path(path, ep->d_name);
char *full_path = mu_make_path("ls", path, ep->d_name);
if (full_path == NULL)
return 1;
if (l_flag) {
if (get_stat(full_path, &sb))
if (mu_get_lstat("ls", full_path, &sb))
return 1;
PrintInfo(sb, full_path);

View File

@ -5,20 +5,18 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "make_path.h"
int move(const char *src, const char *dst) {
char *copy = strdup(src);
if (!copy)
return 1;
char *bname = basename(copy);
size_t len = strlen(dst) + strlen(bname) + 2;
char *new_path = malloc(len + 1);
if (new_path == NULL)
char *new_path = mu_make_path("mv", dst, basename(copy));
if (new_path == NULL) {
free(copy);
return 1;
snprintf(new_path, len, "%s/%s", dst, bname);
}
int ret = 0;
if (rename(src, new_path) < 0)

View File

@ -3,35 +3,14 @@
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include "make_path.h"
#include "get_stat.h"
unsigned int f_flag;
int get_stat(const char *path, struct stat *stat_path) {
if (lstat(path, stat_path)) {
if (!f_flag)
fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno));
return 1;
}
return 0;
}
char *make_path(const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "rm: malloc() returned NULL\n");
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
int rmtree(const char *path) {
struct stat stat_path;
if (get_stat(path, &stat_path))
if (mu_get_lstat("rm", path, &stat_path))
return 1;
if (!S_ISDIR(stat_path.st_mode) || S_ISLNK(stat_path.st_mode)) {
@ -56,7 +35,7 @@ int rmtree(const char *path) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
continue;
char *full_path = make_path(path, ep->d_name);
char *full_path = mu_make_path("rm", path, ep->d_name);
if (full_path == NULL)
continue;

27
libmu/get_stat.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef _GET_STAT_H
#define _GET_STAT_H
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path) {
if (stat(path, stat_path)) {
fprintf(stderr, "%s: unable to stat %s: %s\n", prog_name, path, strerror(errno));
return 1;
}
return 0;
}
int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path) {
if (lstat(path, stat_path)) {
fprintf(stderr, "%s: unable to stat %s: %s\n", prog_name, path, strerror(errno));
return 1;
}
return 0;
}
#endif

20
libmu/make_path.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef _MAKE_PATH_H
#define _MAKE_PATH_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *mu_make_path(const char *prog_name, const char *src, const char *dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "%s: malloc() fail\n", prog_name);
return NULL;
}
snprintf(full_path, len, "%s/%s", src, dst);
return full_path;
}
#endif

View File

@ -1,3 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) {
return 0;
}