This commit is contained in:
Your Name 2023-11-05 18:31:17 +03:00
parent 34778ac3d1
commit dd6b6e771b
8 changed files with 136 additions and 14 deletions

View File

@ -1,7 +1,7 @@
# micro-utils # micro-utils
[Compile] [Compile]
cc builder.c -Ilibmu -obuilder cc builder/builder.c -Ilibmu -obuild
./builder ./build
[or] [or]
sh build.sh sh build.sh

1
TODO
View File

@ -2,7 +2,6 @@ PlainOs
With "micro-" prefix With "micro-" prefix
*Todo: *Todo:
cmp
tail tail
uniq uniq
head head

4
build.sh Executable file → Normal file
View File

@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
cc builder.c -Wall -Wextra -Os -s -pedantic -Ilibmu -obuilder cc builder/builder.c -Wall -Wextra -Os -s -pedantic -Ilibmu -obuild
./builder && rm builder ./build && rm build

View File

@ -13,7 +13,7 @@ void remove_suffix(char *base, const char *suffix) {
*ptr = '\0'; *ptr = '\0';
} }
char *MakePath(char *src) { char *MakePath(const char *src, const char *output_dir) {
char *dup = strdup(src); char *dup = strdup(src);
if (dup == NULL) { if (dup == NULL) {
fprintf(stderr, "builder: strdup failed"); fprintf(stderr, "builder: strdup failed");
@ -21,7 +21,7 @@ char *MakePath(char *src) {
} }
remove_suffix(dup, ".c"); remove_suffix(dup, ".c");
char *new_path = mu_make_path("builder", "../bin", dup); char *new_path = mu_make_path("builder", output_dir, dup);
if (new_path == NULL) { if (new_path == NULL) {
free(dup); free(dup);
exit(1); exit(1);
@ -31,8 +31,8 @@ char *MakePath(char *src) {
return new_path; return new_path;
} }
void Compile(char *src) { void Compile(const char *src, const char *output_dir) {
char *path = MakePath(src); char *path = MakePath(src, output_dir);
size_t len = strlen(CC) + strlen(CFLAGS) + strlen(src) + strlen(path) + 7; size_t len = strlen(CC) + strlen(CFLAGS) + strlen(src) + strlen(path) + 7;
char *arg = malloc(len + 1); char *arg = malloc(len + 1);
@ -49,7 +49,7 @@ void Compile(char *src) {
free(path); free(path);
} }
void ListAndCompile(const char *dir) { void ListAndCompile(const char *dir, const char *output_dir) {
chdir(dir); chdir(dir);
DIR *dp = opendir("."); DIR *dp = opendir(".");
if (dp == NULL) { if (dp == NULL) {
@ -63,7 +63,7 @@ void ListAndCompile(const char *dir) {
continue; continue;
printf("[INFO] Building %s\n", ep->d_name); printf("[INFO] Building %s\n", ep->d_name);
Compile(ep->d_name); Compile(ep->d_name, output_dir);
} }
@ -72,8 +72,11 @@ void ListAndCompile(const char *dir) {
} }
int main(void) { int main(void) {
/* for (size_t i = 0; i < sizeof(libs) / sizeof(char *); i++)
ListAndCompile(objects[i], "../obj"); */
for (size_t i = 0; i < sizeof(objects) / sizeof(char *); i++) for (size_t i = 0; i < sizeof(objects) / sizeof(char *); i++)
ListAndCompile(objects[i]); ListAndCompile(objects[i], "../bin");
return 0; return 0;
} }

View File

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

114
coreutils/cmp.c Normal file
View File

@ -0,0 +1,114 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
unsigned int s_flag;
int compare(FILE *fp1, FILE *fp2) {
if (fp1 == fp2)
return 0;
int ch1;
int ch2;
size_t byte = 0;
do {
ch1 = getc(fp1);
ch2 = getc(fp2);
if (ch1 != ch2) {
if (!s_flag)
printf("files differ at byte %lu\n", byte);
return 1;
}
byte++;
} while(ch1 != EOF);
return 0;
}
long parse_int(const char *str) {
char *ptr;
long val = strtol(str, &ptr, 0);
if (*ptr || val < 0) {
fprintf(stderr, "cmp: invalid offset: %s\n", str);
exit(1);
}
return val;
}
FILE *file_open(const char *path) {
if (!strcmp(path, "-"))
return stdin;
FILE *fp = fopen(path, "r");
if (fp == NULL) {
fprintf(stderr, "cmp: %s\n", strerror(errno));
exit(1);
}
return fp;
}
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], "-s"))
s_flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("cmp [-s silent] FILE1 FILE2 [SKIP1] [SKIP2]\n");
return 0;
}
}
long skip1 = 0;
long skip2 = 0;
FILE *fp1 = NULL;
FILE *fp2 = stdin;
switch (argc - i) {
case 4:
skip2 = parse_int(argv[i + 3]);
case 3:
skip1 = parse_int(argv[i + 2]);
case 2:
fp2 = file_open(argv[i + 1]);
case 1:
fp1 = file_open(argv[i]);
break;
default:
return 1;
}
if (skip1 && fseek(fp1, skip1, SEEK_SET) < 0) {
if (!s_flag)
fprintf(stderr, "cmp: %s\n", strerror(errno));
return 1;
}
if (skip2 && fseek(fp2, skip2, SEEK_SET) < 0) {
if (!s_flag)
fprintf(stderr, "cmp: %s\n", strerror(errno));
return 1;
}
int ret = compare(fp1, fp2);
fclose(fp1);
fclose(fp2);
return ret;
}

View File

@ -16,8 +16,10 @@ void print(double size, const char *filename) {
char c = 0; char c = 0;
if (h_flag) { if (h_flag) {
if (size < 1048576) if (size < 1048576) {
size = size / 1024;
c = 'K'; c = 'K';
}
else if (size < 1073741824) { else if (size < 1073741824) {
size = size / 1048576; size = size / 1048576;
@ -38,7 +40,7 @@ void print(double size, const char *filename) {
else if (m_flag) else if (m_flag)
size = size / 1048576; size = size / 1048576;
printf("%.1f%c\t%s\n", size, c, filename); printf("%.1f%c %s\n", size, c, filename);
} }
double du(const char *path, int recurs_flag) { double du(const char *path, int recurs_flag) {

0
obj/.gitignore vendored Normal file
View File