This repository has been archived on 2021-09-16. You can view files and clone it, but cannot push or open issues or pull requests.
msh/msh.c

82 lines
1.2 KiB
C
Raw Normal View History

2021-09-14 19:00:19 +02:00
#define _POSIX_C_SOURCE 200809l
#include <sys/types.h>
2021-09-13 23:46:17 +02:00
#include <stdio.h>
#include <stdlib.h>
2021-09-14 19:00:19 +02:00
#include <unistd.h>
#include <pwd.h>
2021-09-13 23:46:17 +02:00
2021-09-14 19:00:19 +02:00
#include "config.h"
#include "fstr.h"
fstr_t PS1 = {0};
2021-09-13 23:46:17 +02:00
unsigned int histsize = DEF_HISTSIZE;
// Buffered user data
struct {
char *name;
char *home;
uid_t uid;
gid_t gid;
2021-09-14 19:00:19 +02:00
} uinfo = {0};
struct {
// $ or #
char tag;
} shinfo;
2021-09-13 23:46:17 +02:00
2021-09-14 19:00:19 +02:00
void sh_update_uinfo(void);
void sh_update_shinfo(void);
2021-09-13 23:46:17 +02:00
void sh_update_ps1(void);
inline void sh_print_ps1(void);
int main(int argc, char **argv)
{
2021-09-14 19:00:19 +02:00
(void)argc;
(void)argv;
2021-09-13 23:46:17 +02:00
for (;;) {
2021-09-14 19:00:19 +02:00
sh_update_uinfo();
sh_update_shinfo();
2021-09-13 23:46:17 +02:00
sh_update_ps1();
sh_print_ps1();
2021-09-14 19:00:19 +02:00
for(;;);
2021-09-13 23:46:17 +02:00
}
return EXIT_SUCCESS;
}
void sh_fill_uinfo(void)
{
2021-09-14 19:00:19 +02:00
struct passwd *pw;
uid_t nuid;
nuid = getuid();
if (nuid == uinfo.uid && uinfo.name)
return;
uinfo.uid = nuid;
pw = getpwuid(uinfo.uid);
// User not found
if (!pw)
exit(EXIT_FAILURE);
uinfo.gid = pw->pw_gid;
uinfo.name = estrdup(pw->pw_name);
uinfo.home = estrdup(pw->pw_dir);
}
2021-09-13 23:46:17 +02:00
2021-09-14 19:00:19 +02:00
void sh_update_shinfo(void)
{
shinfo.tag = uinfo.uid ? '$' : '#';
2021-09-13 23:46:17 +02:00
}
inline void sh_print_ps1(void)
{
2021-09-14 19:00:19 +02:00
puts(PS1.s);
2021-09-13 23:46:17 +02:00
}
void sh_update_ps1(void)
{
2021-09-14 19:00:19 +02:00
fstr_append_char(&PS1, shinfo.tag);
fstr_append_char(&PS1, ' ');
}