Compare commits
7 Commits
3afc38f7e7
...
c595117839
Author | SHA1 | Date | |
---|---|---|---|
|
c595117839 | ||
|
11b3a7907a | ||
|
88ac445f22 | ||
|
e2934e4d5b | ||
|
5d1a153fca | ||
|
e1b6a32504 | ||
|
c6b750f966 |
15
TODO
15
TODO
@ -2,6 +2,8 @@ PlainOs
|
||||
With "micro-" prefix
|
||||
|
||||
*Todo:
|
||||
**ls
|
||||
**tee
|
||||
tail
|
||||
uniq
|
||||
head
|
||||
@ -31,13 +33,16 @@ Other:
|
||||
umount
|
||||
sysctl
|
||||
ping
|
||||
ping6
|
||||
nc
|
||||
ntpd
|
||||
ifconfig
|
||||
dhcp-client
|
||||
getopt
|
||||
fdisk
|
||||
ntpd
|
||||
less
|
||||
free
|
||||
swapon
|
||||
swapoff
|
||||
|
||||
Loginutils:
|
||||
su
|
||||
@ -49,8 +54,10 @@ Loginutils:
|
||||
delgroup
|
||||
getty
|
||||
|
||||
Miscutils:
|
||||
time
|
||||
Modutils (linux only):
|
||||
modprobe
|
||||
rmmod
|
||||
lsmod
|
||||
|
||||
Findutils:
|
||||
grep
|
||||
|
3
build.sh
3
build.sh
@ -1,3 +1,4 @@
|
||||
#!/bin/sh
|
||||
cc builder/builder.c -Wall -Wextra -Os -s -pedantic -Ilibmu -obuild
|
||||
./build && rm build
|
||||
./build
|
||||
rm build
|
||||
|
@ -4,6 +4,9 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <signal.h>
|
||||
#include "make_path.h"
|
||||
#include "config.h"
|
||||
|
||||
@ -35,20 +38,33 @@ void Compile(const char *src, const char *output_dir) {
|
||||
char *path = MakePath(src, output_dir);
|
||||
printf("[CC] Building %s -> %s\n", src, path);
|
||||
|
||||
if (fork()) {
|
||||
pid_t pid;
|
||||
if ((pid = fork()) == 0) {
|
||||
if (pid == 1)
|
||||
exit(1);
|
||||
|
||||
execlp(CC, CC, CFLAGS, src, "-o", path, NULL);
|
||||
kill(getpid(), 9);
|
||||
|
||||
/* If compiler return 1 */
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
free(path);
|
||||
|
||||
int status = 0;
|
||||
waitpid(pid, &status, 0);
|
||||
if (status)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -19,12 +19,16 @@ int main(const int argc, const char **argv) {
|
||||
|
||||
else {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (argv[i][0] == '-')
|
||||
if (argv[i][0] == '-') {
|
||||
if (argv[i][1])
|
||||
break;
|
||||
continue;
|
||||
|
||||
else
|
||||
cat(STDIN_FILENO);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
int fd = open(argv[i], O_RDONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "cat: %s %s\n", argv[i], strerror(errno));
|
||||
|
@ -8,8 +8,9 @@ int main(const int argc, const char **argv) {
|
||||
putchar(' ');
|
||||
}
|
||||
}
|
||||
// https://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html
|
||||
// This version does not support -n option and escape-sequences
|
||||
|
||||
/* https://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html */
|
||||
/* This version does not support -n option and escape-sequences */
|
||||
putchar('\n');
|
||||
return 0;
|
||||
}
|
||||
|
239
coreutils/ls.c
239
coreutils/ls.c
@ -8,99 +8,45 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include "make_path.h"
|
||||
#include "get_stat.h"
|
||||
|
||||
unsigned int a_flag;
|
||||
unsigned int l_flag;
|
||||
unsigned int F_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");
|
||||
/* Work with dir */
|
||||
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)
|
||||
@ -108,57 +54,136 @@ 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 dir;
|
||||
}
|
||||
|
||||
void dfree(struct d_node **dir) {
|
||||
struct d_node *cur = dir[0], *next;
|
||||
while (cur != NULL) {
|
||||
next = cur->next;
|
||||
free(cur);
|
||||
cur = next;
|
||||
}
|
||||
|
||||
free(dir);
|
||||
}
|
||||
|
||||
/* Print */
|
||||
void print(const struct d_node *node) {
|
||||
char suf = 0;
|
||||
if (F_flag) {
|
||||
if (S_ISDIR(node->stats.st_mode))
|
||||
suf = '/';
|
||||
|
||||
else if ((node->stats.st_mode & S_IXUSR) || (node->stats.st_mode & S_IXGRP) || (node->stats.st_mode & S_IXOTH))
|
||||
suf = '*';
|
||||
}
|
||||
|
||||
printf("%s%c", node->name, suf);
|
||||
}
|
||||
|
||||
int ls(const char *dir_name, int label, struct winsize w) {
|
||||
/* Unused, tmp */
|
||||
(void)w;
|
||||
|
||||
size_t files = 0;
|
||||
struct d_node **dir = list(dir_name, &files);
|
||||
if (dir == NULL)
|
||||
return 1;
|
||||
|
||||
if (label)
|
||||
printf("\n%s:\n", dir_name);
|
||||
|
||||
if (!p_flag)
|
||||
for (size_t i = 0; i < files; i++) {
|
||||
print(dir[i]);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
/* Todo: sort and print */
|
||||
else {}
|
||||
|
||||
dfree(dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "alF")) != -1) {
|
||||
switch (opt) {
|
||||
case 'a':
|
||||
a_flag = 1;
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
l_flag = 1;
|
||||
break;
|
||||
|
||||
int main(const int argc, const char **argv) {
|
||||
int i;
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (argv[i][0] != '-')
|
||||
break;
|
||||
case 'F':
|
||||
F_flag = 1;
|
||||
break;
|
||||
|
||||
else if (!strcmp(argv[i], "-a"))
|
||||
a_flag = 1;
|
||||
|
||||
else if (!strcmp(argv[i], "-l"))
|
||||
l_flag = 1;
|
||||
|
||||
else if (!strcmp(argv[i], "--help")) {
|
||||
printf("ls [-a show hidden files] [-l use a long listing format] [Path]\n");
|
||||
return 0;
|
||||
default:
|
||||
printf("ls [path]\n\t[-a Show hidden files] [-l Use a long listing format]\n\t[-F Append indicator to names]\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == argc)
|
||||
return list(".", 0);
|
||||
argv += optind;
|
||||
argc -= optind;
|
||||
|
||||
if (i == argc - 1)
|
||||
return list(argv[i], 0);
|
||||
struct winsize w;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
|
||||
/* Check if programm piped, 1 - flase, 0 - true */
|
||||
p_flag = isatty(STDOUT_FILENO);
|
||||
|
||||
if (argc < 1)
|
||||
ls(".", 0, w);
|
||||
|
||||
if (argc == 1)
|
||||
ls(argv[0], 0, w);
|
||||
|
||||
else
|
||||
for (; i < argc; i++)
|
||||
if (list(argv[i], 1))
|
||||
return 1;
|
||||
for (int i = 0; i < argc; i++)
|
||||
ls(argv[i], 1, w);
|
||||
|
||||
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;
|
||||
}
|
@ -5,20 +5,13 @@
|
||||
#include <unistd.h>
|
||||
|
||||
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], "--help")) {
|
||||
printf("hostname [hostname Set new hostname]\n");
|
||||
return 0;
|
||||
}
|
||||
if (argv[argc - 1][0] == '-') {
|
||||
printf("hostname [hostname Set new hostname]\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Set hostname */
|
||||
if (argc - i == 1) {
|
||||
if (argc == 2) {
|
||||
if (sethostname(argv[argc - 1], strlen(argv[argc - 1])) < 0) {
|
||||
fprintf(stderr, "hostname: %s\n", strerror(errno));
|
||||
return 1;
|
||||
@ -34,7 +27,6 @@ int main(const int argc, const char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
puts(hostname);
|
||||
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