split add

This commit is contained in:
Your Name 2024-01-31 15:08:20 +03:00
parent 10c0181465
commit 2aca0e1900
6 changed files with 125 additions and 7 deletions

6
TODO
View File

@ -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

View File

@ -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) */

View File

@ -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]);

View File

@ -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) {

3
src/coreutils/split/build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
project_dir=$(pwd)
echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC

117
src/coreutils/split/split.c Normal file
View File

@ -0,0 +1,117 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#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;
}