fix
This commit is contained in:
parent
5eda08233d
commit
6d1cf79ca6
4
Makefile
4
Makefile
@ -1,4 +1,4 @@
|
|||||||
objects = coreutils console-tools networking procps
|
objects = coreutils console-tools networking procps shell
|
||||||
|
|
||||||
CFLAGS?=-s -Os -flto -pedantic -Wall -Wextra
|
CFLAGS?=-s -Os -flto -pedantic -Wall -Wextra
|
||||||
CC?=cc
|
CC?=cc
|
||||||
@ -7,7 +7,7 @@ all: $(objects)
|
|||||||
|
|
||||||
$(objects):
|
$(objects):
|
||||||
echo MAKE $@
|
echo MAKE $@
|
||||||
cd $@ && $(MAKE) -B CC="$(CC)" CFLAGS="$(CFLAGS)" && cd ..
|
cd $@ && $(MAKE) -B CC="$(CC)" CFLAGS="$(CFLAGS) -I../libmu" && cd ..
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm bin/*
|
rm bin/*
|
||||||
|
@ -6,35 +6,28 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include "make_path.h"
|
||||||
|
#include "get_stat.h"
|
||||||
|
|
||||||
unsigned int r_flag;
|
unsigned int r_flag;
|
||||||
|
unsigned int stat_flag;
|
||||||
|
|
||||||
int (*chown_func)(const char *pathname, uid_t owner, gid_t group);
|
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 gid;
|
||||||
long uid;
|
long uid;
|
||||||
|
|
||||||
int get_stat(const char *path, struct stat *stat_path) {
|
int get_stat(const char *path, struct stat *stat_path) {
|
||||||
if (stat_func(path, stat_path)) {
|
if (stat_flag) {
|
||||||
fprintf(stderr, "chown: unable to stat %s: %s\n", path, strerror(errno));
|
if (mu_get_stat("chown", path, stat_path))
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (mu_get_lstat("chown", path, stat_path))
|
||||||
|
return 1;
|
||||||
|
|
||||||
return 0;
|
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) {
|
int change(const char *file) {
|
||||||
struct stat old_file;
|
struct stat old_file;
|
||||||
if (get_stat(file, &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, ".."))
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
|
||||||
continue;
|
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)
|
if (full_path == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -130,7 +123,6 @@ void get_owner(const char *arg) {
|
|||||||
|
|
||||||
int main(const int argc, char **argv) {
|
int main(const int argc, char **argv) {
|
||||||
chown_func = lchown;
|
chown_func = lchown;
|
||||||
stat_func = stat;
|
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i++) {
|
||||||
@ -142,7 +134,7 @@ int main(const int argc, char **argv) {
|
|||||||
|
|
||||||
else if (!strcmp(argv[i], "-H")) {
|
else if (!strcmp(argv[i], "-H")) {
|
||||||
chown_func = chown;
|
chown_func = chown;
|
||||||
stat_func = stat;
|
stat_flag = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (!strcmp(argv[i], "--help")) {
|
else if (!strcmp(argv[i], "--help")) {
|
||||||
|
@ -7,19 +7,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/stat.h>
|
#include "make_path.h"
|
||||||
|
#include "get_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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
|
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
|
||||||
off_t len = lseek(ifd, 0, SEEK_END);
|
off_t len = lseek(ifd, 0, SEEK_END);
|
||||||
@ -77,7 +66,7 @@ int copy(int mode, const char *src, const char *dst) {
|
|||||||
flag = 1;
|
flag = 1;
|
||||||
new_path = NULL;
|
new_path = NULL;
|
||||||
|
|
||||||
new_path = make_path(dst, basename(dup));
|
new_path = mu_make_path("cp", dst, basename(dup));
|
||||||
if (new_path == NULL)
|
if (new_path == NULL)
|
||||||
goto CLOSE;
|
goto CLOSE;
|
||||||
|
|
||||||
@ -110,18 +99,9 @@ CLOSE:
|
|||||||
return ret;
|
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) {
|
int cptree(const char *src, const char *dst) {
|
||||||
struct stat stat_path;
|
struct stat stat_path;
|
||||||
if (get_stat(src, &stat_path))
|
if (mu_get_lstat("cp", src, &stat_path))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (!S_ISDIR(stat_path.st_mode)) {
|
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))
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..") || !strcmp(dst, ep->d_name))
|
||||||
continue;
|
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)
|
if (src_path == NULL)
|
||||||
continue;
|
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) {
|
if (dst_path == NULL) {
|
||||||
free(src_path);
|
free(src_path);
|
||||||
continue;
|
continue;
|
||||||
@ -186,7 +166,7 @@ int main(const int argc, char **argv) {
|
|||||||
|
|
||||||
else {
|
else {
|
||||||
for (; i < argc - 1; i++) {
|
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)
|
if (new_path == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/stat.h>
|
#include "get_stat.h"
|
||||||
|
#include "make_path.h"
|
||||||
|
|
||||||
unsigned int h_flag;
|
unsigned int h_flag;
|
||||||
unsigned int s_flag;
|
unsigned int s_flag;
|
||||||
@ -44,10 +45,8 @@ double du(const char *path, int recurs_flag) {
|
|||||||
double sum = 0;
|
double sum = 0;
|
||||||
|
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
if (lstat(path, &sb) != 0) {
|
if (mu_get_lstat("du", path, &sb))
|
||||||
fprintf(stderr, "du: lstat() %s\n", strerror(errno));
|
return 1;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (S_ISDIR(sb.st_mode)) {
|
if (S_ISDIR(sb.st_mode)) {
|
||||||
DIR *dp = opendir(path);
|
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, ".."))
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
size_t len = strlen(path) + strlen(ep->d_name) + 2;
|
char *new_path = mu_make_path("du", path, ep->d_name);
|
||||||
char *new_path = malloc(len + 1);
|
|
||||||
if (new_path == NULL)
|
if (new_path == NULL)
|
||||||
continue;
|
return 1;
|
||||||
|
|
||||||
snprintf(new_path, len, "%s/%s", path, ep->d_name);
|
|
||||||
|
|
||||||
sum += du(new_path, 1);
|
sum += du(new_path, 1);
|
||||||
|
|
||||||
|
@ -8,33 +8,13 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include "make_path.h"
|
||||||
|
#include "get_stat.h"
|
||||||
|
|
||||||
unsigned int a_flag;
|
unsigned int a_flag;
|
||||||
unsigned int l_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) {
|
void PrintPerm(struct stat sb) {
|
||||||
printf("%c", (S_ISDIR(sb.st_mode)) ? 'd' : '-');
|
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' : '-');
|
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) {
|
char *fileflag(const char *path) {
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
if (get_stat(path, &sb))
|
if (mu_get_lstat("ls", path, &sb))
|
||||||
return " ";
|
return " ";
|
||||||
|
|
||||||
if (S_ISDIR(sb.st_mode))
|
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) {
|
int list(const char *path, int label) {
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
if (lstat(path, &sb)) {
|
if (mu_get_lstat("ls", path, &sb))
|
||||||
fprintf(stderr, "ls: unable to lstat %s: %s\n", path, strerror(errno));
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
|
|
||||||
/* If its file */
|
/* If its file */
|
||||||
if (!S_ISDIR(sb.st_mode)) {
|
if (!S_ISDIR(sb.st_mode)) {
|
||||||
@ -111,12 +89,12 @@ int list(const char *path, int label) {
|
|||||||
if (ep->d_name[0] == '.' && !a_flag)
|
if (ep->d_name[0] == '.' && !a_flag)
|
||||||
continue;
|
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)
|
if (full_path == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (l_flag) {
|
if (l_flag) {
|
||||||
if (get_stat(full_path, &sb))
|
if (mu_get_lstat("ls", full_path, &sb))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
PrintInfo(sb, full_path);
|
PrintInfo(sb, full_path);
|
||||||
|
@ -5,20 +5,18 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "make_path.h"
|
||||||
|
|
||||||
int move(const char *src, const char *dst) {
|
int move(const char *src, const char *dst) {
|
||||||
char *copy = strdup(src);
|
char *copy = strdup(src);
|
||||||
if (!copy)
|
if (!copy)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
char *bname = basename(copy);
|
char *new_path = mu_make_path("mv", dst, basename(copy));
|
||||||
|
if (new_path == NULL) {
|
||||||
size_t len = strlen(dst) + strlen(bname) + 2;
|
free(copy);
|
||||||
char *new_path = malloc(len + 1);
|
|
||||||
if (new_path == NULL)
|
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
snprintf(new_path, len, "%s/%s", dst, bname);
|
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
if (rename(src, new_path) < 0)
|
if (rename(src, new_path) < 0)
|
||||||
|
@ -3,35 +3,14 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "make_path.h"
|
||||||
|
#include "get_stat.h"
|
||||||
unsigned int f_flag;
|
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) {
|
int rmtree(const char *path) {
|
||||||
struct stat stat_path;
|
struct stat stat_path;
|
||||||
if (get_stat(path, &stat_path))
|
if (mu_get_lstat("rm", path, &stat_path))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (!S_ISDIR(stat_path.st_mode) || S_ISLNK(stat_path.st_mode)) {
|
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, ".."))
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
|
||||||
continue;
|
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)
|
if (full_path == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
27
libmu/get_stat.h
Normal file
27
libmu/get_stat.h
Normal 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
20
libmu/make_path.h
Normal 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
|
@ -1,3 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user