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