This commit is contained in:
Your Name 2024-09-29 19:06:31 +03:00
parent bfb3d3b2fb
commit 04ff3cf8e7
8 changed files with 135 additions and 132 deletions

View File

@ -1 +0,0 @@
nameserver 192.168.0.1

View File

@ -22,4 +22,3 @@ char *mu_humansize(off_t n, int block) {
return mu_hs_buf; return mu_hs_buf;
} }

41
libmu/parse_date.c Normal file
View File

@ -0,0 +1,41 @@
#define _XOPEN_SOURCE
#define _POSIX_C_SOURCE 1999309L
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include "parse_date.h"
time_t mu_parse_date(const char *prog_name, const char *str) {
time_t local = time(NULL);
struct tm *tm = localtime(&local);
char flag = 0;
for (size_t i = 0; i < sizeof(mu_date_fmts) / sizeof(char *); i++) {
char *res = strptime(str, mu_date_fmts[i], tm);
if (res && *res == '\0') {
flag = 1;
break;
}
}
if (flag == 0 && prog_name) {
fprintf(stderr, "%s: parsing: invalid format\n", prog_name);
return -1;
}
time_t rt = mktime(tm);
if (rt < 0) {
if (rt == -1 && prog_name)
fprintf(stderr, "%s: parsing: %s\n", prog_name, strerror(errno));
else if (prog_name)
fprintf(stderr, "%s: parsing: invalid date\n", prog_name);
return -1;
}
return rt;
}

18
libmu/parse_date.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _PARSE_DATE_H
#define _PARSE_DATE_H
static const char *mu_date_fmts[] = {
"%R",
"%T",
"%d-%m",
"%d-%m %H",
"%d-%m %R",
"%d-%m %T",
"%d-%m-%Y",
"%d-%m-%Y %H",
"%d-%m-%Y %R",
"%d-%m-%Y %T"
};
time_t mu_parse_date(const char *prog_name, const char *str);
#endif

View File

@ -1,69 +1,29 @@
#define _XOPEN_SOURCE
#define _POSIX_C_SOURCE 1999309L
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include "get_stat.h"
static const char *fmts[] = { #include "parse_date.h"
"%R",
"%T",
"%d-%m",
"%d-%m %H",
"%d-%m %R",
"%d-%m %T",
"%d-%m-%Y",
"%d-%m-%Y %H",
"%d-%m-%Y %R",
"%d-%m-%Y %T"
};
static time_t parse_date(char *str) {
time_t local = time(NULL);
struct tm *tm = localtime(&local);
char flag = 0;
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++) {
char *res = strptime(str, fmts[i], tm);
if (res && *res == '\0') {
flag = 1;
break;
}
}
if (flag == 0) {
fprintf(stderr, "date: parsing: invalid format\n");
exit(1);
}
time_t rt = mktime(tm);
if (rt < 0) {
if (rt == -1)
fprintf(stderr, "date: parsing: %s\n", strerror(errno));
else
fprintf(stderr, "date: parsing: invalid date\n");
exit(1);
}
return rt;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
time_t t = time(NULL); time_t t = time(NULL);
char *fmt = "%a %b %e %H:%M:%S %Z %Y"; char *fmt = "%a %b %e %H:%M:%S %Z %Y";
/* For -s flag */ /* For -s/-r flag */
struct timespec ts; struct timespec ts;
char *r_flag = NULL;
int opt; int opt;
while ((opt = getopt(argc, argv, "s:d:u")) != -1) { while ((opt = getopt(argc, argv, "s:d:ur:")) != -1) {
switch (opt) { switch (opt) {
case 's': case 's':
ts.tv_sec = parse_date(optarg); ts.tv_sec = mu_parse_date("date", optarg);
if (ts.tv_sec == -1)
return 1;
if (clock_settime(CLOCK_REALTIME, &ts) < 0) { if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
fprintf(stderr, "date: settime: %s\n", strerror(errno)); fprintf(stderr, "date: settime: %s\n", strerror(errno));
return 1; return 1;
@ -72,7 +32,10 @@ int main(int argc, char **argv) {
return 0; return 0;
case 'd': case 'd':
t = parse_date(optarg); t = mu_parse_date("date", optarg);
if (t == -1)
return 1;
break; break;
case 'u': case 'u':
@ -83,10 +46,14 @@ int main(int argc, char **argv) {
break; break;
case 'r':
r_flag = optarg;
break;
default: default:
puts("date [sdu] [+\"fmt\"]\n\t-s TIME Set new date\n\t-d TIME Print new date\n\t-u Work in UTC\n\nRecognized TIME formats:"); puts("date [sdur] [+\"fmt\"]\n\t-s TIME Set new date\n\t-d TIME Print new date\n\t-u Work in UTC\n\t-r FILE Display last modification time of FILE\n\nRecognized TIME formats:");
for (size_t i = 0; i < sizeof(fmts) / sizeof(char *); i++) for (size_t i = 0; i < sizeof(mu_date_fmts) / sizeof(char *); i++)
puts(fmts[i]); puts(mu_date_fmts[i]);
return 0; return 0;
} }
@ -99,6 +66,14 @@ int main(int argc, char **argv) {
if (argv[0][0] == '+') if (argv[0][0] == '+')
fmt = argv[0] + 1; fmt = argv[0] + 1;
if (r_flag) {
struct stat sb;
if (mu_get_lstat("date", r_flag, &sb))
return 1;
t = sb.st_mtime;
}
struct tm *tm = localtime(&t); struct tm *tm = localtime(&t);
char buf[256]; char buf[256];

View File

@ -90,7 +90,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
printf("mount [ato] [DEVICE] [NODE]\n\t-t Fs type, default %s\n\t-o Options\n\t-a Mount all fs in /etc/fstab\n\nOptions:", MOUNT_DEF_FS); printf("mount [ato] [DEVICE] [NODE]\n\t-t Fs type, default %s\n\t-o Options\n\t-a Mount all fs in /etc/fstab\n\nOptions:\n", MOUNT_DEF_FS);
print_opts(); print_opts();
return 0; return 0;
} }

View File

@ -13,11 +13,14 @@
static char c_flag; static char c_flag;
static int pscan(const pid_t pid) { static int pscan(const pid_t pid, const char *prog_name) {
struct mu_proc proc; struct mu_proc proc;
if (mu_proc_parser("ps", pid, &proc)) if (mu_proc_parser("ps", pid, &proc))
return 1; return 1;
if (prog_name != NULL && strncmp(proc.cmd, prog_name, strlen(prog_name)))
return 1;
/* Uid */ /* Uid */
char *name = "unknow"; char *name = "unknow";
struct passwd *pw = getpwuid(proc.uid); struct passwd *pw = getpwuid(proc.uid);
@ -42,6 +45,26 @@ static int pscan(const pid_t pid) {
return 0; return 0;
} }
static char name_2_pid(const char *prog_name) {
DIR *dp = opendir("/proc");
if (dp == NULL) {
fprintf(stderr, "ps: /proc: %s\n", strerror(errno));
return 1;
}
int ret = 0;
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
pid_t pid = strtoul(ep->d_name, 0L, 10);
if (pid)
if (pscan(pid, prog_name))
ret = 1;
}
closedir(dp);
return ret;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
int opt; int opt;
while ((opt = getopt(argc, argv, "c")) != -1) { while ((opt = getopt(argc, argv, "c")) != -1) {
@ -51,7 +74,7 @@ int main(int argc, char **argv) {
break; break;
default: default:
puts("ps [c] [PID]\n\t-c Print cmdline instead of program name"); puts("ps [c] [PID]\n\t-c Print cmdline instead of program name\n");
return 0; return 0;
} }
} }
@ -60,34 +83,23 @@ int main(int argc, char **argv) {
argc -= optind; argc -= optind;
puts(" PID USER PRI NICE VIRT RSS S RTIME CMD"); puts(" PID USER PRI NICE VIRT RSS S RTIME CMD");
if (argc == 0)
int ret = 0; return name_2_pid(NULL);
if (argc == 0) {
DIR *dp = opendir("/proc");
if (dp == NULL) {
fprintf(stderr, "ps: /proc: %s\n", strerror(errno));
return 1;
}
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
pid_t pid = strtoul(ep->d_name, 0L, 10);
if (pid)
if (pscan(pid))
ret = 1;
}
closedir(dp);
}
else { else {
int ret = 0;
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
pid_t pid = strtoul(argv[i], 0L, 10); pid_t pid = strtoul(argv[i], 0L, 10);
if (pid) if (pid) {
if (pscan(pid)) if (pscan(pid, NULL))
ret = 1;
}
else
if (name_2_pid(argv[i]))
ret = 1; ret = 1;
} }
}
return ret; return ret;
}
} }

View File

@ -7,6 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "parse_date.h"
static char c_flag; static char c_flag;
static char m_flag; static char m_flag;
@ -30,7 +31,6 @@ static int setdate(const char *path, const time_t date, char flag) {
} }
static int touch(const char *path, const time_t date) { static int touch(const char *path, const time_t date) {
if (!c_flag) { if (!c_flag) {
int fd = open(path, O_CREAT | O_RDONLY, 0666); int fd = open(path, O_CREAT | O_RDONLY, 0666);
if (fd < 0) { if (fd < 0) {
@ -56,53 +56,6 @@ static int touch(const char *path, const time_t date) {
return 0; return 0;
} }
static time_t parse_date(char *str) {
char *fmt = NULL;
switch (strlen(str)) {
case 8:
fmt = "%m%d%H%M";
break;
case 10:
fmt = "%y%m%d%H%M";
break;
case 11:
fmt = "%m%d%H%M.%S";
break;
case 12:
fmt = "%Y%m%d%H%M";
break;
case 13:
fmt = "%y%m%d%H%M.%S";
break;
case 15:
fmt = "%Y%m%d%H%M.%S";
break;
case 19:
fmt = "%Y-%m-%dT%H:%M:%S";
break;
default:
fprintf(stderr, "touch: invalid date format\n");
exit(1);
}
time_t now = time(NULL);
struct tm tm = *localtime(&now);
if (!strptime(str, fmt, &tm)) {
fprintf(stderr, "touch: %s\n", strerror(errno));
exit(1);
}
return mktime(&tm);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
time_t date = -1; time_t date = -1;
@ -123,11 +76,17 @@ int main(int argc, char **argv) {
case 't': case 't':
case 'd': case 'd':
date = parse_date(optarg); date = mu_parse_date("touch", optarg);
if (date == -1)
return 1;
break; break;
default: default:
puts("touch [cmad] [file1 file2...]\n\t-c Don't create files\n\t-a Change only atime\n\t-m Change only mtime\n\t-d Date/time to use"); puts("touch [cmad] [file1 file2...]\n\t-c Don't create files\n\t-a Change only atime\n\t-m Change only mtime\n\t-d Date/time to use\n\nRecognized TIME formats:");
for (size_t i = 0; i < sizeof(mu_date_fmts) / sizeof(char *); i++)
puts(mu_date_fmts[i]);
return 0; return 0;
} }
} }