Modular SHell
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
msh/msh.c

82 lines
1.2 KiB

3 years ago
#define _POSIX_C_SOURCE 200809l
#include <sys/types.h>
3 years ago
#include <stdio.h>
#include <stdlib.h>
3 years ago
#include <unistd.h>
#include <pwd.h>
3 years ago
3 years ago
#include "config.h"
#include "fstr.h"
fstr_t PS1 = {0};
3 years ago
unsigned int histsize = DEF_HISTSIZE;
// Buffered user data
struct {
char *name;
char *home;
uid_t uid;
gid_t gid;
3 years ago
} uinfo = {0};
struct {
// $ or #
char tag;
} shinfo;
3 years ago
3 years ago
void sh_update_uinfo(void);
void sh_update_shinfo(void);
3 years ago
void sh_update_ps1(void);
inline void sh_print_ps1(void);
int main(int argc, char **argv)
{
3 years ago
(void)argc;
(void)argv;
3 years ago
for (;;) {
3 years ago
sh_update_uinfo();
sh_update_shinfo();
3 years ago
sh_update_ps1();
sh_print_ps1();
3 years ago
for(;;);
3 years ago
}
return EXIT_SUCCESS;
}
void sh_fill_uinfo(void)
{
3 years ago
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);
}
3 years ago
3 years ago
void sh_update_shinfo(void)
{
shinfo.tag = uinfo.uid ? '$' : '#';
3 years ago
}
inline void sh_print_ps1(void)
{
3 years ago
puts(PS1.s);
3 years ago
}
void sh_update_ps1(void)
{
3 years ago
fstr_append_char(&PS1, shinfo.tag);
fstr_append_char(&PS1, ' ');
}