fix: compiler warnings

Fix compiler warnings by
- replacing raw write() calls with printf()
- error checking chdir()
- printing '\x1b[H\x1b[J' instead of calling system(clear)
- using sys/reboot.h to power off and reboot instead of using system
This commit is contained in:
grialion 2024-09-04 21:15:30 +02:00
parent aba3b493bc
commit 64e101eafa
2 changed files with 18 additions and 10 deletions

View File

@ -1,3 +1,4 @@
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <security/_pam_types.h>
@ -37,6 +38,9 @@ int pam_conversation(int num_msg, const struct pam_message **msg,
pam_end(pamh, ret); \
return NULL; \
}
void clear_screen() { printf("\x1b[H\x1b[J"); }
pam_handle_t *get_pamh(char *user, char *passwd) {
pam_handle_t *pamh = NULL;
struct pam_conv pamc = {pam_conversation, (void *)passwd};
@ -59,7 +63,10 @@ void *shmalloc(size_t size) {
}
void moarEnv(char *user, struct session session, struct passwd *pw) {
chdir(pw->pw_dir);
if (chdir(pw->pw_dir) == -1) {
fprintf(stderr, "can't change directory to %s: %s\n", pw->pw_dir,
strerror(errno));
}
setenv("HOME", pw->pw_dir, true);
setenv("USER", pw->pw_name, true);
setenv("SHELL", pw->pw_shell, true);
@ -166,10 +173,10 @@ bool launch(char *user, char *passwd, struct session session,
// TODO: these will be different due to TryExec
// and, Exec/TryExec might contain spaces as args
if (session.type == SHELL) {
system("clear");
clear_screen();
execlp(session.exec, session.exec, NULL);
} else if (session.type == XORG || session.type == WAYLAND) {
system("clear");
clear_screen();
execlp(session.exec, session.exec, NULL);
}
perror("execl error");

View File

@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
@ -374,11 +375,11 @@ int load(struct users_list *users, struct sessions_list *sessions) {
return 0;
} else if (ansi_code == functions.reboot) {
restore_all();
system("reboot");
reboot(RB_AUTOBOOT);
exit(0);
} else if (ansi_code == functions.poweroff) {
restore_all();
system("poweroff");
reboot(RB_POWER_OFF);
exit(0);
} else if (ansi_code == A_UP || ansi_code == A_DOWN) {
ffield_move(ansi_code == A_DOWN);
@ -412,8 +413,8 @@ static void clean_line(struct uint_point origin, uint line) {
memset(line_cleaner, 32, boxw - 2);
}
printf("\x1b[%d;%dH", origin.y + line, origin.x + 1);
printf("%s", line_cleaner);
fflush(stdout);
write(STDOUT_FILENO, line_cleaner, boxw - 2);
}
// TODO: session_len > 32
@ -469,7 +470,7 @@ static void print_user(struct uint_point origin, struct user user,
}
}
static char *passwd_prompt[32];
static char passwd_prompt[32];
// TODO: passwd_len > 32
static void print_passwd(struct uint_point origin, uint length, bool err) {
clean_line(origin, 9);
@ -487,8 +488,8 @@ static void print_passwd(struct uint_point origin, uint length, bool err) {
memset(passwd_prompt, '*', length);
printf("\r\x1b[%dC\x1b[%sm", origin.x + 14, pass_color);
fflush(stdout);
write(STDOUT_FILENO, passwd_prompt, 32);
printf("%s", passwd_prompt);
printf("\x1b[%sm", theme.colors.fg);
}
@ -521,7 +522,7 @@ static void print_row(uint w, uint n, char *edge1, char *edge2, char **filler) {
}
for (uint i = 0; i < n; i++) {
write(STDOUT_FILENO, row, row_size);
printf("%s", row);
}
free(row);
}