This commit is contained in:
Your Name 2023-11-08 19:47:09 +03:00
parent 88ac445f22
commit 11b3a7907a
7 changed files with 108 additions and 108 deletions

3
TODO
View File

@ -2,7 +2,8 @@ PlainOs
With "micro-" prefix With "micro-" prefix
*Todo: *Todo:
ls **ls
**tee
tail tail
uniq uniq
head head

View File

@ -40,6 +40,9 @@ void Compile(const char *src, const char *output_dir) {
pid_t pid; pid_t pid;
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
if (pid == 1)
exit(1);
execlp(CC, CC, CFLAGS, src, "-o", path, NULL); execlp(CC, CC, CFLAGS, src, "-o", path, NULL);
kill(getpid(), 9); kill(getpid(), 9);
@ -47,21 +50,21 @@ void Compile(const char *src, const char *output_dir) {
exit(1); exit(1);
} }
free(path);
int status = 0; int status = 0;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (status) if (status)
exit(1); exit(1);
free(path);
} }
void ListAndCompile(const char *dir, const char *output_dir) { void ListAndCompile(const char *dir, const char *output_dir) {
printf("[CHDIR] %s\n", dir);
if (chdir(dir) < 0) { if (chdir(dir) < 0) {
fprintf(stderr, "builder: %s: %s\n", dir, strerror(errno)); fprintf(stderr, "builder: %s: %s\n", dir, strerror(errno));
exit(1); exit(1);
} }
printf("[CHDIR] %s\n", dir);
DIR *dp = opendir("."); DIR *dp = opendir(".");
struct dirent *ep; struct dirent *ep;

View File

@ -14,6 +14,6 @@ const char *libs[] = {
"readline" "readline"
}; };
#define CFLAGS "-Wall", "-Wextra", "-pedantic", "-Os", "-s", "-I", "../libmu" #define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-Os", "-s", "-I", "../libmu"
#define CC "cc" #define CC "cc"
#endif #endif

View File

@ -8,6 +8,7 @@
#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 "make_path.h"
#include "get_stat.h" #include "get_stat.h"
@ -16,92 +17,33 @@ unsigned int a_flag;
unsigned int l_flag; unsigned int l_flag;
unsigned int p_flag; unsigned int p_flag;
void PrintPerm(struct stat sb) { struct d_node {
if (S_ISDIR(sb.st_mode)) char *name;
printf("d"); struct d_node *next;
struct stat stats;
};
else if (S_ISLNK(sb.st_mode)) struct d_node *stat_file(char *filename) {
printf("l"); struct d_node *file = malloc(sizeof(struct d_node));
if (file == NULL)
return NULL;
else if (S_ISCHR(sb.st_mode)) if (mu_get_lstat("ls", filename, &file->stats))
printf("c"); return NULL;
else if (S_ISFIFO(sb.st_mode)) return file;
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' : '-');
} }
char *fileflag(const char *path) { struct d_node **list(const char *path, size_t *nfiles) {
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 */
DIR *dp = opendir(path); DIR *dp = opendir(path);
if (dp == NULL) { if (dp == NULL) {
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno)); 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; struct dirent *ep;
while ((ep = readdir(dp)) != NULL) { while ((ep = readdir(dp)) != NULL) {
if (ep->d_name[0] == '.' && !a_flag) 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); char *full_path = mu_make_path("ls", path, ep->d_name);
if (full_path == NULL) if (full_path == NULL)
return 1; continue;
if (l_flag) { cur = stat_file(full_path);
if (mu_get_lstat("ls", full_path, &sb)) if (cur == NULL) {
return 1; free(full_path);
continue;
PrintInfo(sb, full_path);
} }
else
printf("%s%s\n", ep->d_name, fileflag(full_path));
free(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); closedir(dp);
printf("\n"); return dir;
return 0;
} }
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 main(int argc, char **argv) {
int opt; int opt;
@ -153,8 +125,18 @@ int main(int argc, char **argv) {
argv += optind; argv += optind;
argc -= optind; argc -= optind;
if (argc == 0) struct d_node **dir = NULL;
list(".", 0); 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; return 0;
} }

View File

@ -28,16 +28,32 @@ int move(const char *src, const char *dst) {
return ret; return ret;
} }
int main(const int argc, const char **argv) { int main(int argc, char **argv) {
if (argc <= 2 || argv[argc - 1][0] == '-') { unsigned int f_flag = 0;
printf("mv [src1 src2...] [dst]\n");
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; 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 (rename(argv[i], argv[argc - 1]) < 0) {
if (move(argv[i], argv[argc - 1])) { if (move(argv[i], argv[argc - 1])) {
if (!f_flag)
fprintf(stderr, "mv: %s %s\n", argv[i], strerror(errno)); fprintf(stderr, "mv: %s %s\n", argv[i], strerror(errno));
return 1; return 1;
} }
} }

View File

@ -1,6 +0,0 @@
#include <stdio.h>
int main(const int argc, const char **argv) {
return 0;
}

View File

@ -38,7 +38,7 @@ int main(void) {
printf(" %d mins", mins); printf(" %d mins", mins);
} }
#ifndef __ANDROID__
/* Print 1, 5 and 15 minute load averages */ /* Print 1, 5 and 15 minute load averages */
double avg[3] = {0, 0, 0}; double avg[3] = {0, 0, 0};
@ -47,6 +47,10 @@ int main(void) {
return 1; 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; return 0;
} }