#include "escheme.h" #include #include #define NAME "extension" #define FUNCTION "info" static Scheme_Object *get_info(int argc, Scheme_Object *argv[]) { struct passwd* who = NULL; // copied shamelessly from $plt/src/file.c if (argc == 0) { char *ptr; ptr = getenv("USER"); if (!ptr) ptr = getenv("LOGNAME"); if(ptr) { who = getpwnam(ptr); } else { who = getpwuid(getuid()); } } else { if (SCHEME_BYTE_STRINGP(argv[0])) { const char* user = SCHEME_BYTE_STR_VAL(argv[0]); who = getpwnam(user); if (!who) { scheme_signal_error("Bad user %s", user); } } else if (SCHEME_INTP(argv[0])) { who = getpwuid(SCHEME_INT_VAL(argv[0])); if (!who) { scheme_signal_error("Bad user ID %d",SCHEME_INT_VAL(argv[0])); } } else { scheme_wrong_type( "extension:get-info", "bytes or integer", 0, argc, argv); } } Scheme_Object* result[5]; // this should be closely synchronized with the user struct in main.ss result[0] = scheme_make_byte_string(who->pw_name); result[1] = scheme_make_integer(who->pw_uid); result[2] = scheme_make_integer(who->pw_gid); result[3] = scheme_make_byte_string(who->pw_dir); result[4] = scheme_make_byte_string(who->pw_shell); return scheme_values(5,result); } Scheme_Object *scheme_reload(Scheme_Env *env) { Scheme_Env* mod = scheme_primitive_module(scheme_module_name(),env); Scheme_Object* procedure = scheme_make_prim_w_arity(get_info,FUNCTION,0,1); scheme_add_global(FUNCTION,procedure,mod); scheme_finish_primitive_module(mod); return procedure; } Scheme_Object *scheme_initialize(Scheme_Env *env) { return scheme_reload(env); } Scheme_Object *scheme_module_name(void) { return scheme_intern_symbol(NAME); }