fix
This commit is contained in:
parent
88ac445f22
commit
11b3a7907a
@ -40,6 +40,9 @@ void Compile(const char *src, const char *output_dir) {
|
||||
|
||||
pid_t pid;
|
||||
if ((pid = fork()) == 0) {
|
||||
if (pid == 1)
|
||||
exit(1);
|
||||
|
||||
execlp(CC, CC, CFLAGS, src, "-o", path, NULL);
|
||||
kill(getpid(), 9);
|
||||
|
||||
@ -47,21 +50,21 @@ void Compile(const char *src, const char *output_dir) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
free(path);
|
||||
|
||||
int status = 0;
|
||||
waitpid(pid, &status, 0);
|
||||
if (status)
|
||||
exit(1);
|
||||
|
||||
free(path);
|
||||
}
|
||||
|
||||
void ListAndCompile(const char *dir, const char *output_dir) {
|
||||
printf("[CHDIR] %s\n", dir);
|
||||
if (chdir(dir) < 0) {
|
||||
fprintf(stderr, "builder: %s: %s\n", dir, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("[CHDIR] %s\n", dir);
|
||||
DIR *dp = opendir(".");
|
||||
|
||||
struct dirent *ep;
|
||||
|
@ -14,6 +14,6 @@ const char *libs[] = {
|
||||
"readline"
|
||||
};
|
||||
|
||||
#define CFLAGS "-Wall", "-Wextra", "-pedantic", "-Os", "-s", "-I", "../libmu"
|
||||
#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-Os", "-s", "-I", "../libmu"
|
||||
#define CC "cc"
|
||||
#endif
|
||||
|
160
coreutils/ls.c
160
coreutils/ls.c
@ -8,6 +8,7 @@
|
||||
#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"
|
||||
@ -16,92 +17,33 @@ unsigned int a_flag;
|
||||
unsigned int l_flag;
|
||||
unsigned int p_flag;
|
||||
|
||||
void PrintPerm(struct stat sb) {
|
||||
if (S_ISDIR(sb.st_mode))
|
||||
printf("d");
|
||||
struct d_node {
|
||||
char *name;
|
||||
struct d_node *next;
|
||||
struct stat stats;
|
||||
};
|
||||
|
||||
else if (S_ISLNK(sb.st_mode))
|
||||
printf("l");
|
||||
struct d_node *stat_file(char *filename) {
|
||||
struct d_node *file = malloc(sizeof(struct d_node));
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
else if (S_ISCHR(sb.st_mode))
|
||||
printf("c");
|
||||
if (mu_get_lstat("ls", filename, &file->stats))
|
||||
return NULL;
|
||||
|
||||
else if (S_ISFIFO(sb.st_mode))
|
||||
printf("p");
|
||||
|
||||
else if (S_ISSOCK(sb.st_mode))
|
||||
printf("s");
|
||||
|
||||
else
|
||||
printf("-");
|
||||
|
||||
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_IRGRP) ? 'r' : '-', (sb.st_mode & S_IWGRP) ? 'w' : '-', (sb.st_mode & S_IXGRP) ? 'x' : '-');
|
||||
printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-');
|
||||
return file;
|
||||
}
|
||||
|
||||
char *fileflag(const char *path) {
|
||||
struct stat sb;
|
||||
if (mu_get_lstat("ls", path, &sb))
|
||||
return " ";
|
||||
|
||||
if (S_ISDIR(sb.st_mode))
|
||||
return "/";
|
||||
|
||||
else if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) || (sb.st_mode & S_IXOTH))
|
||||
return "*";
|
||||
|
||||
else
|
||||
return " ";
|
||||
}
|
||||
|
||||
void PrintInfo(struct stat sb, const char *filename) {
|
||||
/* Permissions */
|
||||
PrintPerm(sb);
|
||||
|
||||
/* Date */
|
||||
struct tm *tm = localtime(&sb.st_mtime);
|
||||
|
||||
char date[14];
|
||||
if (strftime(date, sizeof(date), "%b %d %H:%M", tm) == 0) {
|
||||
fprintf(stderr, "ls: strftime()\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Group and user name */
|
||||
struct passwd *pw = getpwuid(sb.st_uid);
|
||||
struct group *gr = getgrgid(sb.st_gid);
|
||||
|
||||
printf(" %s %s %jd %s %s%s\n", (pw != 0) ? pw->pw_name : "nobody", (gr != 0) ? gr->gr_name : "nobody", (uintmax_t)sb.st_size, date, filename, fileflag(filename));
|
||||
}
|
||||
|
||||
int list(const char *path, int label) {
|
||||
struct stat sb;
|
||||
if (mu_get_lstat("ls", path, &sb))
|
||||
return 1;
|
||||
|
||||
/* If its file */
|
||||
if (!S_ISDIR(sb.st_mode)) {
|
||||
if (l_flag)
|
||||
PrintInfo(sb, path);
|
||||
|
||||
else
|
||||
puts(path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Make label */
|
||||
if (label)
|
||||
printf("\n%s: \n", path);
|
||||
|
||||
/* Open and print dir */
|
||||
struct d_node **list(const char *path, size_t *nfiles) {
|
||||
DIR *dp = opendir(path);
|
||||
if (dp == NULL) {
|
||||
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
|
||||
return 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct d_node **dir, *cur, *dr = NULL;
|
||||
size_t files = 0;
|
||||
|
||||
struct dirent *ep;
|
||||
while ((ep = readdir(dp)) != NULL) {
|
||||
if (ep->d_name[0] == '.' && !a_flag)
|
||||
@ -109,28 +51,58 @@ int list(const char *path, int label) {
|
||||
|
||||
char *full_path = mu_make_path("ls", path, ep->d_name);
|
||||
if (full_path == NULL)
|
||||
return 1;
|
||||
continue;
|
||||
|
||||
if (l_flag) {
|
||||
if (mu_get_lstat("ls", full_path, &sb))
|
||||
return 1;
|
||||
|
||||
PrintInfo(sb, full_path);
|
||||
cur = stat_file(full_path);
|
||||
if (cur == NULL) {
|
||||
free(full_path);
|
||||
continue;
|
||||
}
|
||||
|
||||
else
|
||||
printf("%s%s\n", ep->d_name, fileflag(full_path));
|
||||
|
||||
free(full_path);
|
||||
|
||||
cur->name = ep->d_name;
|
||||
cur->next = dr;
|
||||
dr = cur;
|
||||
|
||||
files++;
|
||||
}
|
||||
|
||||
if (dr == NULL)
|
||||
return NULL;
|
||||
|
||||
*nfiles = files;
|
||||
|
||||
dir = malloc((files + 1) * sizeof(struct d_node *));
|
||||
if (dir == NULL) {
|
||||
fprintf(stderr, "ls: malloc failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (size_t i = 0; ; i++) {
|
||||
dir[i] = dr;
|
||||
dr = dr->next;
|
||||
if (dr == NULL)
|
||||
break;
|
||||
}
|
||||
|
||||
closedir(dp);
|
||||
printf("\n");
|
||||
return 0;
|
||||
return dir;
|
||||
}
|
||||
|
||||
void dfree(struct d_node **dir) {
|
||||
if (dir == NULL)
|
||||
return;
|
||||
|
||||
struct d_node *cur = dir[0], *next;
|
||||
while (cur != NULL) {
|
||||
next = cur->next;
|
||||
free(cur);
|
||||
cur = next;
|
||||
}
|
||||
|
||||
free(dir);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int opt;
|
||||
@ -153,8 +125,18 @@ int main(int argc, char **argv) {
|
||||
argv += optind;
|
||||
argc -= optind;
|
||||
|
||||
if (argc == 0)
|
||||
list(".", 0);
|
||||
struct d_node **dir = NULL;
|
||||
size_t files = 0;
|
||||
|
||||
if (argc < 1)
|
||||
dir = list(".", &files);
|
||||
|
||||
if (argc == 1)
|
||||
dir = list(argv[0], &files);
|
||||
|
||||
for (size_t i = 0; i < files; i++)
|
||||
puts(dir[i]->name);
|
||||
|
||||
dfree(dir);
|
||||
return 0;
|
||||
}
|
||||
|
@ -28,16 +28,32 @@ int move(const char *src, const char *dst) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(const int argc, const char **argv) {
|
||||
if (argc <= 2 || argv[argc - 1][0] == '-') {
|
||||
printf("mv [src1 src2...] [dst]\n");
|
||||
return 0;
|
||||
int main(int argc, char **argv) {
|
||||
unsigned int f_flag = 0;
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "f")) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
f_flag = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("mv [src1 src2...] [dst]\n\t[-f Force]\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc - 1; i++) {
|
||||
argv += optind;
|
||||
argc -= optind;
|
||||
|
||||
/* Move code */
|
||||
for (int i = 0; i < argc - 1; i++) {
|
||||
if (rename(argv[i], argv[argc - 1]) < 0) {
|
||||
if (move(argv[i], argv[argc - 1])) {
|
||||
fprintf(stderr, "mv: %s %s\n", argv[i], strerror(errno));
|
||||
if (!f_flag)
|
||||
fprintf(stderr, "mv: %s %s\n", argv[i], strerror(errno));
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(const int argc, const char **argv) {
|
||||
|
||||
return 0;
|
||||
}
|
@ -38,7 +38,7 @@ int main(void) {
|
||||
printf(" %d mins", mins);
|
||||
}
|
||||
|
||||
|
||||
#ifndef __ANDROID__
|
||||
/* Print 1, 5 and 15 minute load averages */
|
||||
double avg[3] = {0, 0, 0};
|
||||
|
||||
@ -47,6 +47,10 @@ int main(void) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf(" load average: %.2f %.2f %.2f\n", avg[0], avg[1], avg[2]);
|
||||
printf(" load average: %.2f %.2f %.2f", avg[0], avg[1], avg[2]);
|
||||
|
||||
#endif
|
||||
|
||||
putchar('\n');
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user