This commit is contained in:
Your Name 2023-12-17 16:45:13 +03:00
parent 7bdd1bf610
commit 67c41c4c93
7 changed files with 95 additions and 66 deletions

1
TODO
View File

@ -1,7 +1,6 @@
With "micro-" prefix
*Todo:
*nl
tail
expr
uniq

View File

@ -19,6 +19,6 @@ const char *objects[] = {
/* Need by spark */
#define LDFLAGS "-lm"
#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-flto", "-Os", "-s", "-I../libmu", "-I../", LDFLAGS
#define CFLAGS "-Wall", "-Werror", "-Wextra", "-pedantic", "-flto", "-Os", "-s", "-I../libmu", "-I../", "--std", "c99", LDFLAGS
#define CC "cc"
#endif

View File

@ -20,17 +20,18 @@ int nl(const char *path) {
return 1;
}
char buf[BUF_SIZE + 1];
while (fgets(buf, sizeof(buf), fp)) {
size_t len = strlen(buf);
if (len > 1)
char *buf = NULL;
size_t size = 0;
while (getline(&buf, &size, fp) != EOF) {
if (strlen(buf) > 1)
fprintf(stdout, "%*u\t%s", w_flag, lines++, buf);
else
fprintf(stdout, "%*s\t%s", w_flag, " ", buf);
}
free(buf);
if (strcmp(path, "-"))
fclose(fp);

View File

@ -1,55 +0,0 @@
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv) {
unsigned int c_flag = 0;
unsigned int s_size = 0;
int opt;
while ((opt = getopt(argc, argv, "s:c")) != -1) {
switch (opt) {
case 's':
s_size = atoi(optarg);
break;
case 'c':
c_flag = 1;
break;
default:
printf("truncate [file]\n\t[-c Do not create files] [-s SIZE]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc == 0) {
fprintf(stderr, "truncate: missing operand\n");
return 1;
}
int flags = O_WRONLY | O_NONBLOCK;
if (!c_flag)
flags |= O_CREAT;
int fd = open(argv[0], flags, 0666);
if (fd < 0) {
fprintf(stderr, "truncate %s %s\n", argv[0], strerror(errno));
return 1;
}
if (ftruncate(fd, s_size) == -1) {
close(fd);
fprintf(stderr, "truncate %s %s\n", argv[0], strerror(errno));
return 1;
}
close(fd);
return 0;
}

35
coreutils/who.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <utmpx.h>
#include <time.h>
int time_to_str(char *buf, size_t len, struct utmpx *usr) {
time_t t = usr->ut_tv.tv_sec;
struct tm *tmp = localtime(&t);
if (tmp)
strftime(buf, len, "%F %H:%M", tmp);
else
return 1;
return 0;
}
int main(void) {
setutxent();
struct utmpx *usr;
while ((usr = getutxent())) {
if (usr->ut_type == USER_PROCESS) {
char time_buf[20];
if (time_to_str(time_buf, sizeof(time_buf), usr))
continue;
printf("%s\t%s\t%s\t(%s)\n", usr->ut_user, usr->ut_line, time_buf, usr->ut_host);
}
}
endutxent();
}

View File

@ -50,7 +50,7 @@ int stdin_read(void) {
break;
}
else if (isspace(c) && strlen(arg) > 0) {
else if ((isspace(c)) && strlen(arg) > 0) {
*p = '\0';
p = arg;
@ -61,6 +61,9 @@ int stdin_read(void) {
}
else {
if (c == '\\')
c = getchar();
*p = c;
if (p + 1 == arg + sizeof(arg))
break;
@ -96,9 +99,6 @@ int run(void) {
}
int main(int argc, char **argv) {
for (int i = 0; i < argc; i++)
if (argv[i][0] == '-')
argc--;
int opt;
while ((opt = getopt(argc, argv, "tn:r")) != -1) {

49
networking/nc.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "unused.h"
int main(int argc, char **argv) {
int family = AF_UNSPEC;
int port = 33137;
unsigned int l_flag;
UNUSED(port);
UNUSED(l_flag);
UNUSED(family);
int opt;
while ((opt = getopt(argc, argv, "46vp:nl")) != 1) {
switch (opt) {
case '4':
family = AF_INET;
break;
case '6':
family = AF_INET6;
break;
case 'l':
l_flag = 1;
break;
case 'p':
port = atoi(optarg);
break;
default:
printf("nc\n\t[-4 Use ipv4] [-6 Use ipv6]\n\t[-v Verbose] [-p Port Listen port]\n\t[-u Udp mode] [-l Listen]\n\t[-n Don't do DNS resolution]\n");
return 0;
}
}
argv += optind;
argc -= optind;
signal(SIGPIPE, SIG_IGN);
return 0;
}