diff --git a/config.h b/config.h index 25980b7..3e62a81 100644 --- a/config.h +++ b/config.h @@ -47,10 +47,8 @@ char *INIT_START[] = {"/etc/rc.init", NULL}; /* #define OS_NAME "unknow" */ /* Options: To disable, comment line */ -/* Add escape-char support in echo */ -#define ECHO_FANCY -/* getloadavg() unavailable in android os */ +/* getloadavg() unavailable in android */ #ifndef __ANDROID__ #define UPTIME_LOADAVG #endif diff --git a/src/coreutils/echo/echo.c b/src/coreutils/echo/echo.c index ad8e362..186e44e 100644 --- a/src/coreutils/echo/echo.c +++ b/src/coreutils/echo/echo.c @@ -2,78 +2,85 @@ #include #include #include -#include "config.h" + +char n_flag = 0; +char e_flag = 0; void format(char *str) { -#ifdef ECHO_FANCY - for (size_t i = 0; i < strlen(str); i++) { - unsigned int c = str[i]; - if (c == '\\') { - switch (str[i + 1]) { - case 'a': - c = '\a'; - break; + if (e_flag) { + for (size_t i = 0; i < strlen(str); i++) { + unsigned int c = str[i]; + if (c == '\\') { + switch (str[i + 1]) { + case 'a': + c = '\a'; + break; - case 'n': - c = '\n'; - break; + case 'n': + c = '\n'; + break; - case 't': - c = '\t'; - break; + case 't': + c = '\t'; + break; - case 'c': - exit(0); + case 'c': + exit(0); - case 'v': - c = '\v'; - break; + case 'v': + c = '\v'; + break; - case 'r': - c = '\r'; - break; + case 'r': + c = '\r'; + break; - case 'f': - c = '\f'; - break; + case 'f': + c = '\f'; + break; - case 'e': - c = '\033'; - break; + case 'e': + c = '\033'; + break; - case 'b': - c = '\b'; - break; + case 'b': + c = '\b'; + break; - default: - c = '\\'; + default: + c = '\\'; + } + + i++; } - i++; + putchar(c); } - - putchar(c); } -#else - fputs(str, stdout); - -#endif + else + fputs(str, stdout); } int main(int argc, char **argv) { - unsigned int n_flag = 0; - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] != '-') - break; + argv++; + argc--; - else if (!strcmp(argv[i], "-n")) - n_flag = 1; + for (int i = 0; i < argc; i++) { + if (argv[i][0] == '-') { + if (strstr(argv[i] + 1, "n")) + n_flag = 1; + + else if (strstr(argv[i] + 1, "e")) + e_flag = 1; + + argc--; + argv++; + } } - for (; i < argc; i++) { + for (int i = 0; i < argc; i++) { format(argv[i]); if (i < argc - 1) putchar(' '); diff --git a/src/coreutils/ls/ls.c b/src/coreutils/ls/ls.c index eb16a72..8945cb2 100644 --- a/src/coreutils/ls/ls.c +++ b/src/coreutils/ls/ls.c @@ -129,22 +129,24 @@ struct d_node **list_one(const char *path, int *ret) { } char *new_path = strdup(path); - if (new_path == NULL) { - free(dir); - return NULL; - } + if (new_path == NULL) + goto ERROR; dir[0] = stat_file(new_path, 1); - if (dir[0] == NULL) { - free(dir); - free(new_path); - *ret = 1; - return NULL; - } + if (dir[0] == NULL) + goto ERROR_PATH; dir[1] = NULL; return dir; +ERROR_PATH: + *ret = 1; + free(new_path); + +ERROR: + free(dir); + return NULL; + } void dfree(struct d_node **dir, size_t files) { @@ -158,7 +160,9 @@ void dfree(struct d_node **dir, size_t files) { char *get_date(time_t mtime) { static char time[100]; + strftime(time, sizeof(time), "%b %d %H:%M", localtime(&mtime)); + return time; } @@ -300,11 +304,11 @@ int sortt(const void *p1, const void *p2) { } int sorts(const void *p1, const void *p2) { - return (*(struct d_node **)p2)->stats.st_size - (*(struct d_node **)p1)->stats.st_size; + return (*(struct d_node **)p2)->stats.st_size - (*(struct d_node **)p1)->stats.st_size <= 10; } int sortd(const void *p1, const void *p2) { - return (*(struct d_node **)p1)->stats.st_nlink - (*(struct d_node **)p2)->stats.st_nlink; + return strcmp((*(struct d_node **)p1)->full_name, (*(struct d_node **)p2)->full_name); } int ls(const char *dir_name, int label, struct winsize w) { @@ -328,8 +332,7 @@ int ls(const char *dir_name, int label, struct winsize w) { if (dir == NULL) return 1; - if (sorter != NULL) - qsort(dir, files, sizeof(struct d_node *), sorter); + qsort(dir, files, sizeof(struct d_node *), sorter); if ((label || R_flag) && !d_flag && !its_file) printf("%s:\n", dir_name); diff --git a/src/coreutils/rm/rm.c b/src/coreutils/rm/rm.c index 521a0f9..7ebd56a 100644 --- a/src/coreutils/rm/rm.c +++ b/src/coreutils/rm/rm.c @@ -84,14 +84,14 @@ int main(int argc, char **argv) { } } - if (argv[optind] == NULL) { + argc -= optind; + argv += optind; + + if (argc == 0) { fprintf(stderr, "rm: missing operand\n"); return 1; } - argv += optind; - argc -= optind; - int ret = 0; for (int i = 0; i < argc; i++) { if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){ diff --git a/src/procps/kill/kill.c b/src/procps/kill/kill.c index 08cccf6..47361b6 100644 --- a/src/procps/kill/kill.c +++ b/src/procps/kill/kill.c @@ -12,15 +12,15 @@ typedef struct { } SIG; SIG signals[] = { - {"HUP", SIGHUP}, - {"INT", SIGINT}, + {"HUP", SIGHUP}, + {"INT", SIGINT}, {"QUIT", SIGQUIT}, - {"ILL", SIGILL}, + {"ILL", SIGILL}, {"TRAP", SIGTRAP}, {"ABRT", SIGABRT}, - {"IOT", SIGIOT}, - {"BUS", SIGBUS}, - {"FPE", SIGFPE}, + {"IOT", SIGIOT}, + {"BUS", SIGBUS}, + {"FPE", SIGFPE}, {"KILL", SIGKILL}, {"USR1", SIGUSR1}, {"SEGV", SIGSEGV}, @@ -35,10 +35,9 @@ SIG signals[] = { int parse_sig(char *arg) { int sig = atoi(arg); - if (sig >= 0 && sig >= NSIG) + if (sig >= 0 && sig <= NSIG) return sig; - if (!strncasecmp(arg, "SIG", 3)) arg += 3; diff --git a/src/procps/ps/build.sh b/src/procps/ps/build.sh new file mode 100755 index 0000000..6d8974b --- /dev/null +++ b/src/procps/ps/build.sh @@ -0,0 +1,3 @@ +#!/bin/sh +project_dir=$(pwd) +echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC diff --git a/src/procps/ps/ps.c b/src/procps/ps/ps.c new file mode 100644 index 0000000..ef57b22 --- /dev/null +++ b/src/procps/ps/ps.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +int open_file(const char *path) { + int fd = open(path, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "ps: %s: %s\n", path, strerror(errno)); + return -1; + } + + return fd; +} + +int pscan(const char *pid) { + char path[PATH_MAX + 1]; + char cmdline[PATH_MAX + 1]; + + /* Arguments */ + snprintf(path, sizeof(path), "/proc/%s/cmdline", pid); + int fd = open_file(path); + if (fd == -1) + return 1; + + read(fd, cmdline, sizeof(cmdline)); + close(fd); + + printf("%5s %5s\n", pid, cmdline); + return 0; +} + +int main(int argc, char **argv) { + + int opt; + while ((opt = getopt(argc, argv, "o:")) != -1) { + switch (opt) { + case 'o': + break; + + default: + printf("ps [o] [PID]\n\t-o Options (nice, tty, cmd, pid, nice)\n"); + return 0; + } + } + + DIR *dp = opendir("/proc"); + if (dp == NULL) { + fprintf(stderr, "ps: /proc: %s\n", strerror(errno)); + return 1; + } + + printf("PID CMD\n"); + + int ret = 0; + struct dirent *ep; + while ((ep = readdir(dp)) != NULL) + if (atoi(ep->d_name)) + if (pscan(ep->d_name)) + ret = 1; + + closedir(dp); + return ret; +}