This commit is contained in:
Your Name 2024-03-27 22:04:17 +03:00
parent 1552e3f553
commit 577f8312d7
16 changed files with 41 additions and 61 deletions

View File

@ -1,25 +0,0 @@
#ifndef _GET_STRING_H
#define _GET_STRING_H
#include <stdio.h>
#include <string.h>
#include <errno.h>
int mu_get_string(const char *prog_name, char *buf, const size_t len) {
off_t rbytes = read(STDIN_FILENO, buf, len);
if (rbytes <= 0) {
if (prog_name != NULL)
fprintf(stderr, "%s: %s\n", prog_name, strerror(errno));
return 0;
}
if (rbytes > 0)
buf[rbytes - 1] = '\0';
buf[rbytes] = '\0';
return rbytes;
}
#endif

View File

@ -22,7 +22,7 @@ char *mu_humansize(off_t n, off_t block) {
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]); snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
else else
snprintf(buf, sizeof(buf), "%ju", (uintmax_t)n); snprintf(buf, sizeof(buf), "%ju", n);
return buf; return buf;
} }

View File

@ -53,7 +53,7 @@ int copy(int mode, const char *src, const char *dst) {
return 1; return 1;
/* test ./testdir/ -> test ./testdir/test */ /* test ./testdir/ -> test ./testdir/test */
char *dup = NULL; char *str_dup = NULL;
char *new_path = (char *)dst; char *new_path = (char *)dst;
int flag = 0; int flag = 0;
int ret = 0; int ret = 0;
@ -62,14 +62,14 @@ int copy(int mode, const char *src, const char *dst) {
if (ofd < 0) { if (ofd < 0) {
ret = 1; ret = 1;
dup = strdup(src); str_dup = strdup(src);
if (dup == NULL) if (str_dup == NULL)
goto CLOSE; goto CLOSE;
flag = 1; flag = 1;
new_path = NULL; new_path = NULL;
new_path = mu_make_path(f_flag, dst, basename(dup)); new_path = mu_make_path(f_flag, dst, basename(str_dup));
if (new_path == NULL) if (new_path == NULL)
goto CLOSE; goto CLOSE;
@ -93,8 +93,8 @@ int copy(int mode, const char *src, const char *dst) {
CLOSE: CLOSE:
close(ifd); close(ifd);
if (flag) { if (flag) {
if (dup != NULL) if (str_dup != NULL)
free(dup); free(str_dup);
if (new_path != NULL) if (new_path != NULL)
free(new_path); free(new_path);

View File

@ -12,8 +12,8 @@ off_t outfull, outpart;
off_t tbytes; off_t tbytes;
void summary(void) { void summary(void) {
fprintf(stderr, "%zu+%zu records in\n", infull, inpart); fprintf(stderr, "%jd+%jd records in\n", infull, inpart);
fprintf(stderr, "%zu+%zu records out\n", outfull, outpart); fprintf(stderr, "%jd+%jd records out\n", outfull, outpart);
fprintf(stderr, "%s total bytes copied\n", mu_humansize(tbytes, 1024)); fprintf(stderr, "%s total bytes copied\n", mu_humansize(tbytes, 1024));
} }

View File

@ -69,7 +69,7 @@ int main(int argc, char **argv) {
off_t capacity = (used * 100 + 1) / (avail + used + 1); off_t capacity = (used * 100 + 1) / (avail + used + 1);
if (!h_flag) if (!h_flag)
printf("%-20s %12ju %12ju %12ju %12ju%% %s\n", me->mnt_fsname, total, used, avail, capacity, me->mnt_dir); printf("%-20s %12jd %12jd %12jd %12jd%% %s\n", me->mnt_fsname, total, used, avail, capacity, me->mnt_dir);
else { else {
char total_s[MU_HUMAN_BUF_SIZE + 1]; char total_s[MU_HUMAN_BUF_SIZE + 1];
@ -80,7 +80,7 @@ int main(int argc, char **argv) {
snprintf(used_s, sizeof(used_s), "%s", mu_humansize(used * block, block)); snprintf(used_s, sizeof(used_s), "%s", mu_humansize(used * block, block));
snprintf(avail_s, sizeof(avail_s), "%s", mu_humansize(avail * block, block)); snprintf(avail_s, sizeof(avail_s), "%s", mu_humansize(avail * block, block));
printf("%-20s %10s %9s %9s %3ju%% %s\n", me->mnt_fsname, total_s, used_s, avail_s, capacity, me->mnt_dir); printf("%-20s %10s %9s %9s %3jd%% %s\n", me->mnt_fsname, total_s, used_s, avail_s, capacity, me->mnt_dir);
} }
} }

View File

@ -33,9 +33,10 @@ void print(const char *file, FILE *fp, long lines, long bytes) {
if (c == '\n') if (c == '\n')
lcount++; lcount++;
putchar(c);
if (c == EOF || lcount == lines || (c_flag && bcount == bytes)) if (c == EOF || lcount == lines || (c_flag && bcount == bytes))
break; break;
putchar(c);
} }
} }

View File

@ -32,7 +32,7 @@ int print_groups(const struct passwd *pwd, const char *fmt, const int flag) {
for (int i = 0; i < ngroups; i++) { for (int i = 0; i < ngroups; i++) {
struct group *grp = getgrgid(groups[i]); struct group *grp = getgrgid(groups[i]);
if (grp && !n_flag) if (grp && !n_flag)
printf("%d", groups[i]); printf("%u", groups[i]);
if ((!r_flag && n_flag) || flag) if ((!r_flag && n_flag) || flag)
printf(fmt, grp->gr_name); printf(fmt, grp->gr_name);
@ -53,7 +53,7 @@ int ids(uid_t uid, struct passwd *pwd) {
printf("%s", pwd->pw_name); printf("%s", pwd->pw_name);
else else
printf("%d", uid); printf("%u", uid);
} }
else if (G_flag || g_flag) else if (G_flag || g_flag)

View File

@ -80,6 +80,8 @@ struct d_node **list(const char *path, size_t *nfiles, int *ret) {
DIR *dp = opendir(path); DIR *dp = opendir(path);
if (dp == NULL) { if (dp == NULL) {
free(dir);
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno)); fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
return NULL; return NULL;
} }
@ -145,11 +147,11 @@ void dfree(struct d_node **dir, size_t files) {
} }
char *get_date(time_t mtime) { char *get_date(time_t mtime) {
static char time[100]; static char date[100];
strftime(time, sizeof(time), "%b %d %H:%M", localtime(&mtime)); strftime(date, sizeof(date), "%b %d %H:%M", localtime(&mtime));
return time; return date;
} }
/* Print */ /* Print */
@ -196,7 +198,7 @@ int print(const struct d_node *node) {
int ret = 0; int ret = 0;
if (i_flag) if (i_flag)
ret += printf("%7ju ", (uintmax_t)node->stats.st_ino); ret += printf("%7jd ", node->stats.st_ino);
if (s_flag) { if (s_flag) {
off_t size = 512 * node->stats.st_blocks; off_t size = 512 * node->stats.st_blocks;
@ -204,7 +206,7 @@ int print(const struct d_node *node) {
ret += printf("%7s ", mu_humansize(size, 1024)); ret += printf("%7s ", mu_humansize(size, 1024));
else else
ret += printf("%7ju ", (uintmax_t)size / 1024); ret += printf("%7jd ", size / 1024);
} }
if (l_flag) { if (l_flag) {
@ -216,10 +218,10 @@ int print(const struct d_node *node) {
char *gr_name = (gr != 0) ? gr->gr_name : "nobody"; char *gr_name = (gr != 0) ? gr->gr_name : "nobody";
char *pw_name = (pw != 0) ? pw->pw_name : "nobody"; char *pw_name = (pw != 0) ? pw->pw_name : "nobody";
if (h_flag) if (h_flag)
ret += printf(" %4ju %4s %6s %6s %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, mu_humansize(node->stats.st_size, 1024), get_date(node->stats.st_mtime)); ret += printf(" %4u %4s %6s %6s %s ", node->stats.st_nlink, pw_name, gr_name, mu_humansize(node->stats.st_size, 1024), get_date(node->stats.st_mtime));
else else
ret += printf(" %4ju %4s %6s %10jd %s ", (uintmax_t)node->stats.st_nlink, pw_name, gr_name, (uintmax_t)node->stats.st_size, get_date(node->stats.st_mtime)); ret += printf(" %4u %4s %6s %10ld %s ", node->stats.st_nlink, pw_name, gr_name, node->stats.st_size, get_date(node->stats.st_mtime));
} }
if (c_flag && p_flag) if (c_flag && p_flag)

View File

@ -21,7 +21,7 @@ int do_mkdir(const char *path) {
} }
else if (v_flag) else if (v_flag)
printf("mkdir: %s created with mode %d\n", path, mode); printf("mkdir: %s created with mode %u\n", path, mode);
return 0; return 0;
} }

View File

@ -24,7 +24,7 @@ int nl(const char *path) {
size_t size = 0; size_t size = 0;
while (getline(&buf, &size, fp) != EOF) { while (getline(&buf, &size, fp) != EOF) {
if (strlen(buf) > 1) if (strlen(buf) > 1)
fprintf(stdout, "%*u\t%s", w_flag, lines++, buf); fprintf(stdout, "%*d\t%s", w_flag, lines++, buf);
else else
fprintf(stdout, "%*s\t%s", w_flag, " ", buf); fprintf(stdout, "%*s\t%s", w_flag, " ", buf);

View File

@ -17,9 +17,7 @@ char v_flag;
int verbose(const char *path) { int verbose(const char *path) {
if (v_flag) { if (v_flag) {
fprintf(stderr, "rm: remove %s? [y/n] ", path); fprintf(stderr, "rm: remove %s? [y/n] ", path);
fflush(stderr); fflush(stderr);
fflush(stdin);
int c = 0; int c = 0;
int key = 0; int key = 0;

View File

@ -13,8 +13,7 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
pid_t pid; if (fork() != 0)
if ((pid = fork()) != 0)
return 0; return 0;
setsid(); setsid();

View File

@ -52,15 +52,17 @@ int main(int argc, char **argv) {
case 'b': case 'b':
switch (optarg[strlen(optarg) - 1]) { switch (optarg[strlen(optarg) - 1]) {
case 'm': case 'm':
size = atoi(optarg) * 1048576; sscanf(optarg, "%ld", &size);
size *= 1048576;
break; break;
case 'k': case 'k':
size = atoi(optarg) * 1024; sscanf(optarg, "%ld", &size);
size *= 1024;
break; break;
default: default:
size = atoi(optarg); sscanf(optarg, "%ld", &size);
break; break;
} }

View File

@ -23,8 +23,7 @@ void poweroff(int sig) {
} }
void execute(char *argv[]) { void execute(char *argv[]) {
pid_t pid; if (fork() == 0) {
if ((pid = fork()) == 0) {
setsid(); setsid();
execvp(argv[0], argv); execvp(argv[0], argv);

View File

@ -7,7 +7,6 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <termios.h> #include <termios.h>
#include "get_string.h"
#include "pw_check.h" #include "pw_check.h"
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL) #define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
@ -53,9 +52,13 @@ struct passwd *proccess_input(char *hostname) {
/* Username */ /* Username */
printf("Login on %s: ", hostname); printf("Login on %s: ", hostname);
fflush(stdout); fflush(stdout);
if (!mu_get_string("login", user, sizeof(user)))
off_t ret = 0;
if ((ret = read(STDIN_FILENO, user, sizeof(user))) <= 0)
return NULL; return NULL;
user[ret - 1] = '\0';
struct passwd *pw = getpwnam(user); struct passwd *pw = getpwnam(user);
if (!pw) { if (!pw) {
fprintf(stderr, "login: Incorrent username\n"); fprintf(stderr, "login: Incorrent username\n");
@ -68,9 +71,10 @@ struct passwd *proccess_input(char *hostname) {
if (hide_input(STDIN_FILENO, 1)) if (hide_input(STDIN_FILENO, 1))
return NULL; return NULL;
if (!mu_get_string("login", psswd, sizeof(psswd))) if ((ret = read(STDIN_FILENO, psswd, sizeof(psswd))) <= 0)
return NULL; return NULL;
psswd[ret - 1] = '\0';
printf("\n"); printf("\n");
if (pw_check("login", pw, psswd)) { if (pw_check("login", pw, psswd)) {
memset(psswd, '\0', sizeof(psswd)); memset(psswd, '\0', sizeof(psswd));

View File

@ -20,7 +20,7 @@ int main(void) {
time(&current_secs); time(&current_secs);
struct tm *current_time = localtime(&current_secs); struct tm *current_time = localtime(&current_secs);
printf("%02u:%02u:%02u ", current_time->tm_hour, current_time->tm_min, current_time->tm_sec); printf("%02d:%02d:%02d ", current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
/* How long system has beep up */ /* How long system has beep up */
struct timespec uptime; struct timespec uptime;