int main() { char host[256] = "--------------------------"; printf("gethostid: %d\n", gethostid()); printf("errno: %d\n\n", errno); errno = 0; printf("gethostname/2 ret: %d\n", gethostname(host, 2)); printf("gethostname/2: %s\n", host); printf("errno: %d\n\n", errno); errno = 0; printf("gethostname/256 ret: %d\n", gethostname(host, 256)); printf("gethostname/256: %s\n", host); printf("errno: %d\n\n", errno); errno = 0; char login[256] = "--------------------------"; printf("login: %s\n", getlogin()); printf("errno: %d\n\n", errno); errno = 0; printf("login_r/2 ret: %d\n", getlogin_r(login, 2)); printf("login_r/2: %s\n", login); printf("errno: %d\n\n", errno); errno = 0; printf("login_r/256 ret: %d\n", getlogin_r(login, 256)); printf("login_r/256: %s\n", login); printf("errno: %d\n", errno); errno = 0; return 0; }
void pathf90_getlog(char *name, int len) { char *l = alloca(len + 1); #ifdef __sun char *res = getlogin_r(l, len + 1); int err = res == NULL; #else int err = getlogin_r(l, len + 1); #endif b_char((err == 0) ?l:" ", name, len); }
Variant f_posix_getlogin() { char buf[L_cuserid]; if (!getlogin_r(buf, sizeof(buf) - 1)) { return String(buf, CopyString); } return false; }
std::string Client::getDefaultNick() noexcept { char buf[64] = {0}; if (getlogin_r(buf, sizeof(buf)-1) != 0) { return "airdcpp-web"; } return buf; }
int __getlogin_r_chk (char *buf, size_t buflen, size_t nreal) { if (buflen > nreal) __chk_fail (); return getlogin_r (buf, buflen); }
/* getlogin */ char * getlogin(void) { static char login[LOGIN_NAME_MAX + 1]; if(getlogin_r(login, sizeof(login)) != 0) return NULL; return login; }
static const char *get_dump_name(char c) { static char name[] = "/tmp/rom.\0\0\0\0\0\0\0\0\0"; if ('\0' == name[strlen("/tmp/rom.")]) if (0 != getlogin_r(name + strlen("/tmp/rom."), 9)) perror("getting username for rom/ram dump"); name[strlen("/tmp/r")] = c; return name; }
char * cuserid(char *s) { if (s != NULL && getlogin_r(s, L_cuserid)) return s; return getlogin(); }
int main(int argc, char *argv[]) { int sockfd, fdmax, i, ret; struct sockaddr_in server_addr; fd_set master, read_fds; struct User user; strncpy(user.room, "Default", 8); if (argc < 2) { getlogin_r(user.name, sizeof(user.name)); if (strcmp(user.name,"") == 0 ) printf ("\nNO user informed: Using system's username: %s\n", user.name); } else if (argc == 3 || argc > 3) { strncpy(user.room, argv[2], sizeof(argv[2])+1); strncpy(user.name, argv[1], sizeof(argv[1])+1); } else strncpy(user.name, argv[1], sizeof(argv[1])+1); ret = connect_request(&sockfd, &server_addr); if (ret) exit (1); FD_ZERO(&master); FD_ZERO(&read_fds); FD_SET(0, &master); FD_SET(sockfd, &master); fdmax = sockfd + 1; ret = auth(user, sockfd); if (ret) { printf("\nERROR: authentication error\n"); return (401); } printf("\n\nWellcome to Room #(%s) \n\n", user.room); fflush(stdout); while (1) { read_fds = master; if (select(fdmax, &read_fds, NULL, NULL, NULL) == -1) { perror("select"); exit (4); } for (i = 0; i <= fdmax; i++) if (FD_ISSET(i, &read_fds)) send_recv(i, sockfd, user); } close (sockfd); return 0; }
/** * Atemting to get the user name from the system * @param host - the string that will hold the user name * @return 0 if all went well, -1 (ERROR) otherwise */ int getUserName(char* name) { if(getlogin_r(name, HOST_USER_LEN) != 0) { fprintf(stderr, "%s\n", "Error: while getting User name"); return ERROR; } return 0; }
static void get_username(std::ostream& os, void*) { char buf[32]; if (getlogin_r(buf, sizeof(buf)) == 0) { buf[sizeof(buf)-1] = '\0'; os << buf; } else { os << "unknown (" << berror() << ')' ; } }
const char *ucs_get_user_name() { static char username[256] = {0}; if (*username == 0) { getlogin_r(username, sizeof(username)); } return username; }
void print_prompt() { char buffer[BUFFER_SIZE]; printf("juShell "); getlogin_r(buffer, BUFFER_SIZE); printf("\033[32m%s\033[m:", buffer); getcwd(buffer, BUFFER_SIZE); printf("\033[33;1m%s\033[m $ ", buffer); }
extern "C" char *getlogin(void) { const size_t buflen = 1024; static __thread char buf[buflen]; int res = getlogin_r(buf, buflen); if (res != 0) { return NULL; } return buf; }
//static void XLinuxSystem::GetLoginUserName(VString& outName) { char name[LOGIN_NAME_MAX+1]; name[0]=0; int res=getlogin_r(name, sizeof(name)); xbox_assert(res==0); outName.FromBlock(name, strlen(name), VTC_UTF_8); }
Variant HHVM_FUNCTION(posix_getlogin) { #if !defined(__APPLE__) && !defined(__FreeBSD__) char buf[L_cuserid]; #else char buf[MAXLOGNAME]; #endif if (!getlogin_r(buf, sizeof(buf) - 1)) { return String(buf, CopyString); } return false; }
BrokerQueryManager::BrokerQueryManager(broker::endpoint* lhost, broker::message_queue* mq,std::string btp) { //point to broker topic object bTopic = btp; firstTime = true; //point to local host object ptlocalhost = lhost; //pointer to message queue object ptmq = mq; getlogin_r(username,SIZE); }
String REG_POLY_FUN_HDR(sml_getlogin, Region rs) { String s; int r; s = REG_POLY_CALL(allocStringC,rs, L_cuserid + 1); r = getlogin_r(&(s->data), L_cuserid); if (r != 0) { return NULL; } return s; }
int main(int argc, char *argv[]) { char *delimiter_position = NULL; char *command = NULL; int i = 0; if (argc < 2) { while(1) { if (getcwd(current_work_path, sizeof(current_work_path)) == NULL) { break; } if (getlogin_r(user, sizeof(user)) != 0) { break; } sprintf(PS1, "%s:%s$ ", user, current_work_path); if (command != NULL) { free(command); command = NULL; } command = readline(PS1); if ((command != NULL) && (*command != '\0')) { add_history(command); } delimiter_position = strtok(command, " "); while (delimiter_position != NULL) { argv[i++] = delimiter_position; delimiter_position = strtok(NULL, " "); } argv[i] = NULL; i = 0; if (strcmp(argv[0], "quit") == 0) { break; } if (strcmp(argv[0], "cd") == 0) { cd(argv); continue; } execute(argv); } } else { readopt(argc, argv); } return 0; }
char * getlogin(void) { _THREAD_PRIVATE_KEY(getlogin); char * name = (char *)_THREAD_PRIVATE(getlogin, logname, NULL); if ((errno = getlogin_r(name, sizeof logname)) != 0) return NULL; if (*name == '\0') { errno = ENOENT; /* well? */ return NULL; } return name; }
const char* system_username( void ) { static char username[64] = {0}; if( username[0] ) return username; strcpy( username, "<unknown>" ); #if FOUNDATION_PLATFORM_ANDROID strncpy( username, getlogin(), 64 ); #else getlogin_r( username, 64 ); #endif username[63] = 0; return username; }
/** * Generates the prompt that lets the user know they can enter * a new command. * * By default, the prompt symbol is provided by PROMPT_SYM. * * @returns nothing */ char* generatePrompt() { char user[100]; char hostname[31]; char* promptString = "%s@%s%c "; char* prompt = malloc(sizeof(char*)); getlogin_r(user, 99); gethostname(hostname, 30); sprintf(prompt, promptString, user, hostname, PROMPT_SYM); return prompt; }
/* See unistd.in.h for documentation. */ int getlogin_r (char *name, size_t size) { #undef getlogin_r #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ /* Native Windows platform. */ DWORD sz; /* When size > 0x7fff, the doc says that GetUserName will fail. Actually, on Windows XP SP3, it succeeds. But let's be safe, for the sake of older Windows versions. */ if (size > 0x7fff) size = 0x7fff; sz = size; if (!GetUserName (name, &sz)) { if (GetLastError () == ERROR_INSUFFICIENT_BUFFER) /* In this case, the doc says that sz contains the required size, but actually, on Windows XP SP3, it contains 2 * the required size. */ return ERANGE; else return ENOENT; } return 0; #elif HAVE_GETLOGIN_R /* Platform with a getlogin_r() function. */ int ret = getlogin_r (name, size); if (ret == 0 && memchr (name, '\0', size) == NULL) /* name contains a truncated result. */ return ERANGE; return ret; #else /* Platform with a getlogin() function. */ char *n; size_t nlen; errno = 0; n = getlogin (); if (!n) /* ENOENT is a reasonable errno value if getlogin returns NULL. */ return (errno != 0 ? errno : ENOENT); nlen = strlen (n); if (size <= nlen) return ERANGE; memcpy (name, n, nlen + 1); return 0; #endif }
/* ================ Sys_GetCurrentUser returns username for current profile ================ */ char *Sys_GetCurrentUser( void ) { static string s_userName; dword size = sizeof( s_userName ); #if defined(_WIN32) if( !GetUserName( s_userName, &size ) || !s_userName[0] ) #elif !defined(__ANDROID__) if( !getlogin_r( s_userName, size || !s_userName[0] ) ) #endif Q_strcpy( s_userName, "player" ); return s_userName; }
void test_getlogin(void) { int sc; char ch; puts( "setuid(5)" ); sc = setuid(5); rtems_test_assert( sc == 0 ); printf( "getlogin() -- (%s)\n", getlogin() ); puts( "setuid(0)" ); sc = setuid(0); rtems_test_assert( sc == 0 ); printf( "getlogin() -- (%s)\n", getlogin() ); puts( "getlogin_r(NULL, LOGIN_NAME_MAX) -- EFAULT" ); sc = getlogin_r( NULL, LOGIN_NAME_MAX ); rtems_test_assert( sc == EFAULT ); puts( "getlogin_r(buffer, 0) -- ERANGE" ); sc = getlogin_r( &ch, 0 ); rtems_test_assert( sc == ERANGE ); }
ATF_TC_BODY(getlogin_same, tc) { char buf[MAXLOGNAME]; char *str; str = getlogin(); if (str == NULL) return; ATF_REQUIRE(getlogin_r(buf, sizeof(buf)) == 0); if (strcmp(str, buf) != 0) atf_tc_fail("getlogin(2) and getlogin_r(2) differ"); }
void promptPrefix() { // Getting user name char userName[INPUT_SIZE]; getlogin_r(userName, INPUT_SIZE); // Getting host name char hostName[INPUT_SIZE]; gethostname(hostName, INPUT_SIZE); // Getting current directory char currentDirectory[INPUT_SIZE]; getcwd(currentDirectory, INPUT_SIZE); // Printing prefix printf("%s@%s:%s> ", userName, hostName, currentDirectory); }
void setUser ( void ) { std::string tmp; char rawUser[150]; int i = 0; getlogin_r(rawUser, 150); while (rawUser[i]) { tmp += rawUser[i]; i++; } this->_user = tmp; }
int main() { char a[] = "himanshu"; printf("%s\n", a); if(strcmp(a, "himanshu") == 0) printf("They both are equal\n"); // send user/system name char username[50]; getlogin_r(username, sizeof(username)); printf("Username : |%s|\n", username); char arr[10] = "ls"; char newArr[1000] = *exec(arr); printf("SHELL OUTPUT : %s\n", a); return 0; }
static inline void logline(const char *line) { char login[256]; char exepath[256], args[512]; char* exename; int ret; int i; FILE* fd; #if ROOT_ONLY if (getuid() != 0) return; #endif #if IGNORE_NULL if (getlogin() == 0) return; #endif if( getlogin_r(login, 255) != 0 ) { strcpy(login,"unknown"); } ret = readlink("/proc/self/exe", exepath, sizeof(exepath)); exepath[ret] = '\0'; exename = basename(exepath); openlog(exename, LOG_PID, LOG_USER); fd = fopen("/proc/self/cmdline", "r"); if(fd){ ssize_t size = fread(args, 1, sizeof(args), fd); if(size > 0){ for(i = 0; i < size - 2; i++){ if(args[i] == '\0' && args[i + 1] != '\0'){ args[i] = ' '; } } args[size-1] = '\0'; } fclose(fd); }else{ args[0] = '\0'; } syslog(LOG_INFO, "[%s, uid:%d sid:%d, cmdline:%s]: %s", login, getuid(), getsid(0), args, line); }