This commit is contained in:
Your Name 2023-10-24 13:59:42 +03:00
parent d3d24e46b4
commit 3bbadc8f11
7 changed files with 116 additions and 15 deletions

13
TODO
View File

@ -1,17 +1,22 @@
Makefile(
*Todo:
dirname
basename
mktemp
ln
nice
nohup
chmod
chown
split
truncate
date
tee
tr
cut
shuf
id
df
dd
stty
stat
sort
printf
test

40
coreutils/basename.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>
#include <libgen.h>
void remove_suffix(char *base, const char *suffix) {
char *ptr = base + strlen(base) - strlen(suffix);
if (!strcmp(ptr, suffix))
*ptr = '\0';
}
int main(const int argc, char **argv) {
char *suffix = NULL;
char *base = NULL;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "--help")) {
printf("basename [name] [suffix]\n");
return 0;
}
}
if (argc - i < 1) {
fprintf(stderr, "basename: missing operand\n");
return 1;
}
else if (argc - i == 2)
suffix = argv[i + 1];
base = basename(argv[i]);
if (suffix)
remove_suffix(base, suffix);
puts(base);
return 0;
}

View File

@ -4,7 +4,7 @@
#include <string.h>
#include <unistd.h>
unsigned int flag;
unsigned int n_flag;
unsigned int lines;
void cat(const int fd) {
char buf[4024];
@ -12,7 +12,7 @@ void cat(const int fd) {
while ((len = read(fd, buf, sizeof(buf))) > 0) {
for (ssize_t i = 0; i < len; i++) {
if (((i > 0) ? buf[i - 1] == '\n' : 1) && flag)
if (((i > 0) ? buf[i - 1] == '\n' : 1) && n_flag)
printf(" %d ", ++lines);
printf("%c", buf[i]);
@ -27,7 +27,7 @@ int main(const int argc, const char **argv) {
break;
else if (!strcmp(argv[i], "-n"))
flag = 1;
n_flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("cat [-n (numerate)] [file1 file2 ...]\n");

32
coreutils/chmod.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
//TODO
int main(const int argc, const char **argv) {
unsigned int r_flag = 0;
unsigned int s_flag = 0;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-r"))
r_flag = 1;
else if (!strcmp(argv[i], "-s"))
s_flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("chmod [-r (recursive)] [-s (silent)] [file1 file2...]\n");
return 0;
}
}
return 0;
}

16
coreutils/dirname.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <libgen.h>
int main(const int argc, const char **argv) {
if (argc <= 1) {
printf("dirname [dirname]\n");
return 1;
}
for (int i = 1; i < argc; i++)
puts(dirname(argv[i]));
return 0;
}

16
coreutils/logname.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
char *p = getlogin();
if (p != NULL) {
puts(p);
return 0;
}
fprintf(stderr, "logname: %s\n", strerror(errno));
return 1;
}

View File

@ -1,8 +0,0 @@
#include <stdio.h>
#define MSG "Micro-coreutils: Version 0.1 LICENSE: wtfpl\n"
int main(void) {
puts(MSG);
return 0;
}