diff --git a/TODO b/TODO index 34ed47e..003c407 100644 --- a/TODO +++ b/TODO @@ -5,8 +5,6 @@ tail expr uniq od -split -date tr cut shuf @@ -17,9 +15,10 @@ stat sort test tar +sha* +md5 Other: - *mount ps sysctl ping @@ -41,7 +40,6 @@ Loginutils: addgroup deluser passwd - login delgroup getty diff --git a/config.h b/config.h index 5bebf18..a628814 100644 --- a/config.h +++ b/config.h @@ -1,7 +1,7 @@ #ifndef _CONFIG_H #define _CONFIG_H -/* (cat tee wc xargs rev) */ +/* (cat tee wc xargs rev split) */ #define BUF_SIZE 4096 /* Random source (shred) */ diff --git a/src/coreutils/date/date.c b/src/coreutils/date/date.c index 0512592..26905e7 100644 --- a/src/coreutils/date/date.c +++ b/src/coreutils/date/date.c @@ -72,7 +72,7 @@ int main(int argc, char **argv) { break; default: - printf("date\n\t[-s DATE Set new date]\n\t[-d DATE Print new date]\n\t[-u Work in UTC]\n"); + printf("date [+\"fmt\"]\n\t[-s DATE Set new date]\n\t[-d DATE Print new date]\n\t[-u Work in UTC]\n"); printf("\nFormats:\n"); for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++) printf("\t%s\n", fmts[i]); diff --git a/src/coreutils/ls/ls.c b/src/coreutils/ls/ls.c index a1e872d..89c475e 100644 --- a/src/coreutils/ls/ls.c +++ b/src/coreutils/ls/ls.c @@ -303,7 +303,7 @@ int sortd(const void *p1, const void *p2) { struct d_node *l1 = *(struct d_node **)p1; struct d_node *l2 = *(struct d_node **)p2; - return strlen(l1->name) - strlen(l2->name); + return l2->name[1] - l1->name[0]; } int ls(const char *dir_name, int label, struct winsize w) { diff --git a/src/coreutils/split/build.sh b/src/coreutils/split/build.sh new file mode 100755 index 0000000..6d8974b --- /dev/null +++ b/src/coreutils/split/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/coreutils/split/split.c b/src/coreutils/split/split.c new file mode 100644 index 0000000..8c37b03 --- /dev/null +++ b/src/coreutils/split/split.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include "config.h" + +FILE *next_file(FILE *old, int x, int slen, char *prefix) { + if (old != NULL) + fclose(old); + + /* Gen file name */ + char name[BUF_SIZE + 1]; + int len = snprintf(name, sizeof(name), "%s", prefix); + + for (int i = slen; i >= 0; i--) { + if (len + i >= (int)(BUF_SIZE * sizeof(char))) + break; + + name[len + i] = 'a' + (x % 26); + x /= 26; + } + + /* Open file */ + FILE *fp = fopen(name, "w"); + if (fp == NULL) { + fprintf(stderr, "split: %s\n", strerror(errno)); + return NULL; + } + + return fp; +} + +int main(int argc, char **argv) { + off_t size = 0; + int a_flag = 1; + int b_flag = 0; + char *prefix = "x"; + + int opt; + while ((opt = getopt(argc, argv, "l:a:b:")) != -1) { + switch (opt) { + case 'a': + a_flag = atoi(optarg); + break; + + case 'l': + size = atoi(optarg); + break; + + case 'b': + switch (optarg[strlen(optarg) - 1]) { + case 'm': + size = atoi(optarg) * 1048576; + break; + + case 'k': + size = atoi(optarg) * 1024; + break; + + default: + size = atoi(optarg); + break; + } + + b_flag = 1; + break; + + default: + printf("split [file1]\n\t[-l N Split by N lines]\n\t[-a N Use N letters as prefix]\n\t[-b N[k|m] Split by N (kilo|mega)bytes]\n"); + return 0; + } + } + + argv += optind; + argc -= optind; + + FILE *fp = stdin; + if (argv[0] != NULL && strcmp(argv[0], "-")) { + fp = fopen(argv[0], "r"); + if (fp == NULL) { + fprintf(stderr, "split: %s: %s\n", argv[0], strerror(errno)); + return 1; + } + } + + if (argc == 2 && argv[1] != NULL) + prefix = argv[1]; + + + int ret = 0; + int files = 0; + FILE *out = NULL; + + off_t n = 0; + int ch; + while ((ch = getc(fp)) != EOF) { + if (out == NULL || n >= size) { + out = next_file(out, files++, a_flag, prefix); + if (out == NULL) { + ret = 1; + break; + } + + n = 0; + } + + if (ch == '\n' || b_flag) + n++; + + putc(ch, out); + } + + fclose(fp); + return ret; +}