This commit is contained in:
Your Name 2024-01-21 13:07:27 +03:00
parent 6cff13dd67
commit daf3409726
12 changed files with 126 additions and 16 deletions

View File

@ -4,7 +4,7 @@ if [ -z $PROJECT_DIR ]; then
fi
if [ -z $CFLAGS ]; then
CFLAGS="-pedantic -s -Os -flto -Werror -Wall -Wextra"
CFLAGS="-pedantic -s -Os -Werror -Wall -Wextra"
fi
if [ -z $CC ]; then

View File

@ -20,7 +20,7 @@
#define MOUNT_OPT_SIZE 512
/* nologin sleep */
#define NOLOGIN_SLEEP 60
#define NOLOGIN_SLEEP 0
/* colors for ls */
#define LS_DIR_COLOR "\033[1;34m"

1
configs/motd Normal file
View File

@ -0,0 +1 @@
Welcome!

View File

@ -1,2 +1,2 @@
root::0:0:root:/usr/root:/bin/sh
nobody:*:65534:65534:nobody:/nonexistent:/bin/false
nobody:*:65534:65534:nobody:/nonexistent:/bin/nologin

3
configs/rc.init Executable file → Normal file
View File

@ -5,5 +5,6 @@ mount -a
echo "Setuping hostname..."
hostname -c /etc/hostname
echo "Starting shell..."
reset
cat /etc/motd
env TERM="vt100" login

0
configs/rc.poweroff Executable file → Normal file
View File

View File

@ -17,7 +17,6 @@ char *mu_make_path(const char *restrict prog_name, const char *restrict src, con
dst = "";
}
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {

View File

@ -6,13 +6,8 @@
#include <unistd.h>
int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
if (pw->pw_passwd[0] == '\0' || pass[0] == '\0') {
if (pass[0] == '\0')
return 0;
fprintf(stderr, "%s: Incorrect password\n", prog_name);
return 1;
}
if (pw->pw_passwd[0] == '\0' && pass[0] == '\0')
return 0;
if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') {
fprintf(stderr, "%s: Denied\n", prog_name);
@ -22,6 +17,7 @@ int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
if (!strcmp(pass, pw->pw_passwd))
return 0;
fprintf(stderr, "%s: Incorrect password\n", prog_name);
return 1;
}

3
src/coreutils/date/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

98
src/coreutils/date/date.c Normal file
View File

@ -0,0 +1,98 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
const char *fmts[] = {
"%R",
"%T",
"%m.%d-%R",
"%m.%d-%T",
"%Y.%m.%d-%R",
"%Y.%m.%d-%T",
"%Y-%m-%d %R %z",
"%Y-%m-%d %T %z",
"%T %d-%m-%Y",
"%H %d-%m-%Y",
"%Y-%m-%d %H",
"%Y-%m-%d",
"%d-%m-%Y"
};
time_t parse_date(char *str) {
time_t local = time(NULL);
struct tm *tm = localtime(&local);
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++) {
char *res = strptime(str, fmts[i], tm);
if (res && *res == '\0')
break;
}
time_t rt = mktime(tm);
if (rt == -1) {
fprintf(stderr, "date: %s\n", strerror(errno));
exit(1);
}
return rt;
}
int main(int argc, char **argv) {
time_t t = time(NULL);
char *fmt = "%a %b %e %H:%M:%S %Z %Y";
/* For -s flag */
struct timespec ts;
int opt;
while ((opt = getopt(argc, argv, "s:d:u")) != -1) {
switch (opt) {
case 's':
ts.tv_sec = parse_date(optarg);
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
fprintf(stderr, "date: %s\n", strerror(errno));
return 1;
}
return 0;
case 'd':
t = parse_date(optarg);
break;
case 'u':
if (setenv("TZ", "UTC0", 1) < 0) {
fprintf(stderr, "date: %s\n", strerror(errno));
return 1;
}
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("\nFormats:\n");
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++)
printf("\t%s\n", fmts[i]);
return 0;
}
}
argv += optind;
argc -= optind;
if (argc != 0)
if (argv[0][0] == '+')
fmt = argv[0] + 1;
struct tm *tm = localtime(&t);
char buf[256];
strftime(buf, sizeof(buf), fmt, tm);
puts(buf);
return 0;
}

View File

@ -28,7 +28,9 @@ unsigned int h_flag;
unsigned int s_flag;
unsigned int i_flag;
unsigned int p_flag;
int (*sorter)(const void *p1, const void *p2) = NULL;
int sortd(const void *p1, const void *p2);
int (*sorter)(const void *p1, const void *p2) = sortd;
struct d_node {
/* basename */
@ -297,6 +299,13 @@ int sorts(const void *p1, const void *p2) {
return (*(struct d_node **)p2)->stats.st_size - (*(struct d_node **)p1)->stats.st_size;
}
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);
}
int ls(const char *dir_name, int label, struct winsize w) {
size_t files = 0;
int ret = 0;

View File

@ -36,6 +36,7 @@ void login(const struct passwd *pw) {
setenv("SHELL", shell, 1);
setenv("USER", pw->pw_name, 1);
setenv("LOGNAME", pw->pw_name, 1);
setenv("PATH", "/bin", 1);
if (chdir(pw->pw_dir) < 0) {
fprintf(stderr, "login: %s\n", strerror(errno));
@ -50,7 +51,8 @@ struct passwd *proccess_input(char *hostname) {
static char psswd[512];
/* Username */
printf("Login on %s:\n", hostname);
printf("Login on %s: ", hostname);
fflush(stdout);
if (!mu_get_string("login", user, sizeof(user)))
return NULL;
@ -61,16 +63,17 @@ struct passwd *proccess_input(char *hostname) {
}
/* Password */
printf("\nPassword:\n");
printf("Password: ");
fflush(stdout);
if (hide_input(STDIN_FILENO, 1))
return NULL;
if (!mu_get_string("login", psswd, sizeof(psswd)))
return NULL;
printf("\n");
if (pw_check("login", pw, psswd)) {
memset(psswd, '\0', sizeof(psswd));
fprintf(stderr, "login: Incorrect password\n");
return NULL;
}