Exemple #1
0
    static int  acctinit(History_t *hp)
    {
	register char *cp, *acctfile;
	Namval_t *np = nv_search("ACCTFILE",((Shell_t*)hp->histshell)->var_tree,0);

	if(!np || !(acctfile=nv_getval(np)))
		return(0);
	if(!(cp = getlogin()))
	{
		struct passwd *userinfo = getpwuid(getuid());
		if(userinfo)
			cp = userinfo->pw_name;
		else
			cp = "unknown";
	}
	logname = strdup(cp);
	if((acctfd=sh_open(acctfile,
		O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR))>=0 &&
	    (unsigned)acctfd < 10)
	{
		int n;
		if((n = fcntl(acctfd, F_DUPFD, 10)) >= 0)
		{
			close(acctfd);
			acctfd = n;
		}
	}
	if(acctfd < 0)
	{
		acctfd = 0;
		return(0);
	}
	if(sh_isdevfd(acctfile))
	{
		char newfile[16];
		sfsprintf(newfile,sizeof(newfile),"%.8s%d\0",e_devfdNN,acctfd);
		nv_putval(np,newfile,NV_RDONLY);
	}
	else
		fcntl(acctfd,F_SETFD,FD_CLOEXEC);
	return(1);
    }
Exemple #2
0
char *getprompt()
{
  static char *prompt = NULL;
  static int len = 0;
  static int default_len = 30;
  if (dir_changed) {
    int i, j;
    char *tmp;
    int allocated = 0;
    int path_len = 100;
    dir_changed = 0;
    prompt = realloc(prompt, (len = default_len) * sizeof(char));
    for (i = 0, j = 0; i < strlen(PS1); ++i) {
      if (PS1[i] == '\\') {
        switch (PS1[++i]) {
        case 'u':
          tmp = getlogin(); allocated = 0; break;
        case 'w':
          tmp = sh_getcwd(); allocated = 1; break;
        default:
          fprintf(stderr, "shell: Unknown PS1: %s\n", PS1); exit(127);
        }
        prompt[j] = '\0';
        j += strlen(tmp);
        if (j >= len) {
          len *= 2;
          prompt = realloc(prompt, len * sizeof(char));
        }
        strcat(prompt, tmp);
        if (allocated) free(tmp);
      } else {
        if (j == len-1) {
          len *= 2;
          prompt = realloc(prompt, len * sizeof(char));
        }
        prompt[j++] = PS1[i];
      }
    }
    prompt[j] = '\0';
  }
  return prompt;
}
Exemple #3
0
int main(int argc, char* argv[]){
  char * send, * buffer;
  int buflen, fd;
  struct sockaddr_in addr;
  
  if(argc != 2){
    printf("Usage ./chrip \"Message\" (Message must be in quotes)\n");
    return 1;
  }
  /* Get the message from the command line */
  send = argv[1];
  /* Check constraints */
  if(strlen(send)>=BUF_LEN){
    printf("Message too long, message must be less than 1000 characters\n");
    return -1;
  }
  
  /* Allocate enough space for the entire send message and form the message */
  buffer = (char*) malloc(strlen(send)+sizeof(char)*24);
  sprintf(buffer,"FROM %s\n%s\n",getlogin(),send);
  buflen = strlen(buffer);

  /* Create the socket to send to */
  if((fd = socket(AF_INET,SOCK_DGRAM,0))<0){
    perror("Could not connect to socket");
    free(buffer);
    return -1;
  }
  
  /* Establish the port information */
  inet_pton(AF_INET,"224.0.0.22",&addr.sin_addr.s_addr);
  addr.sin_port = htons(5010);
  addr.sin_family = AF_INET;
  /* Send to the multicast group */
  if(sendto(fd,buffer,buflen,0,(struct sockaddr *)&addr,sizeof(addr))<0){
    perror("Unable to send message");
  }
  /* Clean up and exit */
  close(fd);
  free(buffer);
  return 1;
}
Exemple #4
0
/*
 * Determine username of caller
 */
static char *
find_username(void)
{
  const char *u = getlogin();

  if (!u) {
    struct passwd *pw = getpwuid(getuid());
    if (pw)
      u = pw->pw_name;
  }

  if (!u)
    u = getenv("USER");
  if (!u)
    u = getenv("LOGNAME");
  if (!u)
    u = "root";

  return xstrdup(u);
}
Exemple #5
0
IoObject *IoUser_protoName(IoUser *self, IoObject *locals, IoMessage *m)
{
	/*doc User name
	Returns the current user's name.
	*/

	char *userName = (char *)getlogin();

	if (userName == NULL)
	{
		userName = getenv("LOGNAME");
	}

	if (userName == NULL)
	{
		return IONIL(self);
	}

	return IOSYMBOL(userName);
}
Exemple #6
0
int main(int argc, char const *argv[])
{

	printf("父亲实际的用户id%d,有效的用户id%d\n",getuid(),geteuid() );
	pid_t p;
	if((p=fork())==0){
		seteuid(getuid());
		//seteuid(0);
		//system("./system2");
		execl("./system2",NULL);
	//printf("儿子实际的用户id%d,有效的用户id%d\n",getuid(),geteuid() );

	}
	// system("date");
	// system("ls -al
	printf("%s\n",getlogin() );
	printf("%d\n",getpwuid() );

		return 0;
}
Exemple #7
0
// ****************************************************************
// * This function prepares the prompt and print it.			  *
// ****************************************************************
void print_prompt(void)
{
	char hostname[BUFFERSIZE];
	char *path = calloc(BUFSIZ, sizeof(char*));
	char *username = calloc(BUFFERSIZE, sizeof(char*));

	// get name of local machine
	gethostname(hostname, sizeof(hostname));
	rm_substr(hostname, LOCAL);

	// get current working directory name
	getcwd(path, BUFSIZ);
	path = strrchr(path, SLASH_SYMBOL) + 1;

	// get username
	username = getlogin();

	// print fetched information along with the prompt
	printf("%s:%s %s$ ", hostname, path, username);
}
Exemple #8
0
int main (int argc, char **argv)
{
	//объявляем символьный указатель
  char *cp;

//дергаем систеный вызов getlogin
  //читать тут http://www.opennet.ru/man.shtml?topic=getlogin&category=3&russian=5
  //короче она нам возвращает указатель на строку, содежащую имя пользователя котоырй активен сейчас в системе на данном терминале
  cp = getlogin ();
  //если не получили- так и сообщаем
  if (! cp)
  {
    printf("no login name");
    return 1;
	}

	//если все ок- печатаем ее
  puts (cp);
  return 0;
}
Exemple #9
0
/*
 * Get login name, or if that fails, get something suitable.
 * The result is always trimmed to fit in a score.
 */
static char *
thisuser(void)
{
	const char *p;
	struct passwd *pw;
	static char u[sizeof(scores[0].hs_name)];

	if (u[0])
		return (u);
	p = getlogin();
	if (p == NULL || *p == '\0') {
		pw = getpwuid(getuid());
		if (pw != NULL)
			p = pw->pw_name;
		else
			p = "  ???";
	}
	strlcpy(u, p, sizeof(u));
	return (u);
}
Exemple #10
0
int main()
{
    uid_t uid = getuid();
    gid_t gid = getgid();

    printf("User is %s\n", getlogin());

    printf("User IDs: uid=%d, gid=%d\n", uid, gid);

    struct passwd *pw = getpwuid(uid);
    printf("UID passwd entry:\nname=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
            pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);

    pw = getpwnam("root");
    printf("root passwd entry:\n");
    printf("name=%s, uid=%d, gid=%d, home=%s, shell=%s\n",
            pw->pw_name, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);

    exit(0);
}
Exemple #11
0
ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL
get_default_username (void)
{
    const char *user;

    user = getenv ("USER");
    if (user == NULL)
	user = getenv ("LOGNAME");
    if (user == NULL)
	user = getenv ("USERNAME");

#if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN)
    if (user == NULL) {
	user = (const char *)getlogin ();
	if (user != NULL)
	    return user;
    }
#endif
#ifdef HAVE_PWD_H
    {
	uid_t uid = getuid ();
	struct passwd *pwd;

	if (user != NULL) {
	    pwd = k_getpwnam (user);
	    if (pwd != NULL && pwd->pw_uid == uid)
		return user;
	}
	pwd = k_getpwuid (uid);
	if (pwd != NULL)
	    return pwd->pw_name;
    }
#endif
#ifdef _WIN32
    /* TODO: We can call GetUserNameEx() and figure out a
       username. However, callers do not free the return value of this
       function. */
#endif

    return user;
}
Exemple #12
0
Fichier : irc.c Projet : Limsik/e17
int
protocol_irc_connect(const char *server, int port, const char *nick, const char *user, const char *pass)
{
   Ecore_Con_Server *serv = NULL;
   IRC_Server_Params *params;

   if (!server) return 0;
   if (!(serv = eina_hash_find(_irc_servers, server)))
     {
        if (port <= 0) port = 6667;
        if (!user)
          user = getlogin();
        if (!nick)
          nick = user;
        if (!pass)
          pass = user;

        // Create params
        params = EMOTE_NEW(IRC_Server_Params, 1);
        params->name = ((server != NULL) ? eina_stringshare_add(server) : NULL);
        params->nick = ((nick != NULL) ? eina_stringshare_add(nick) : NULL);
        params->user = ((user != NULL) ? eina_stringshare_add(user) : NULL);
        params->pass = ((pass != NULL) ? eina_stringshare_add(pass) : NULL);
        params->port = port;

        serv = ecore_con_server_connect(ECORE_CON_REMOTE_SYSTEM,
                                        server, port, params);
        ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD,
                                _irc_cb_server_add, NULL);
        ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL,
                                _irc_cb_server_del, NULL);
        ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA,
                                _irc_cb_server_data, NULL);
        eina_hash_add(_irc_servers, server, serv);

        protocol_irc_pass(server, pass);
        protocol_irc_user(server, user);
        protocol_irc_nick(server, nick);
     }
   return 1;
}
Exemple #13
0
/**
 * lw6sys_get_username
 *
 * @sys_context: global system context
 *
 * Gets the name of the current user. Difference with the standard
 * function @getlogin is that this function will returned a dynamically
 * allocated pointer, and provide a default value if it's undefined.
 * Also, if will look at the content of the 'LOGNAME' environment
 * variable if needed, and will even provide a default value.
 *
 * Return value: a newly allocated pointer, must be freed.
 */
char *
lw6sys_get_username (lw6sys_context_t * sys_context)
{
  char *ret = NULL;
#ifdef LW6_MS_WINDOWS
  DWORD size;
#else
  char *login = NULL;
#endif

#ifdef LW6_MS_WINDOWS
  ret = LW6SYS_CALLOC (sys_context, UNLEN + 2);
  if (ret)
    {
      size = UNLEN + 1;
      if (GetUserName (ret, &size) && size > 0)
	{
	  // OK
	}
      else
	{
	  LW6SYS_FREE (sys_context, ret);
	  ret = lw6sys_str_copy (sys_context, DEFAULT_USERNAME);
	}
    }
#else
  login = getlogin ();
  if (!login)
    {
      login = getenv ("LOGNAME");
    }
  if (!login)
    {
      login = DEFAULT_USERNAME;
    }

  ret = lw6sys_str_copy (sys_context, login);
#endif

  return ret;
}
int CServerApp::MonitorThread( void* arg )
{
    CServerApp* pServApp = (CServerApp*)arg;
    ASSERT( pServApp!=NULL );
    char buf[5*MAXLINE];

    while( true ) {
        MilliSleep( 20*1000 );

        bool bMoreLogon = false;

        FILE* pf = popen( "users", "r" );
        if( pf!=NULL && !feof(pf) ) {
            fgets( buf, ELEMENTS(buf), pf );
            istrstream istr(buf, strlen(buf)+1);
            //retrieve the service login name
            char username[MAXLINE];
            strcpy( username, getlogin() );
            //check if other user is logged in
            while( true ) {
                char login_name[MAXLINE];
                istr>>login_name;
                if( istr.fail() || istr.eof() )break;
                if( strcmp(username, login_name)!=0 ) {
                    bMoreLogon = true;
                    break;
                }
            }
        }
        if( pf!=NULL )pclose( pf );

        int nSysState = pServApp->GetServState();
        if( bMoreLogon ) {
            SetBit( nSysState, SYSTEM_SUSPEND_SERVICE );
            SetBit( nSysState, SYSTEM_USER_LOGON );
        } else {
            ClrBit( nSysState, SYSTEM_SUSPEND_SERVICE );
            ClrBit( nSysState, SYSTEM_USER_LOGON );
        }
        pServApp->SetServState( nSysState );
    }
Exemple #15
0
int auth_login(struct config *config)
{
	struct stream *s;
	char hostbuf[MAXHOSTNAMELEN];
	char *login, *host;
	int error;

	s = config->server;
	error = gethostname(hostbuf, sizeof(hostbuf));
	hostbuf[sizeof(hostbuf) - 1] = '\0';
	if (error)
		host = NULL;
	else
		host = hostbuf;
	login = getlogin();
	proto_printf(s, "USER %s %s\n", login != NULL ? login : "******",
	    host != NULL ? host : "?");
	stream_flush(s);
	error = auth_domd5auth(config);
	return (error);
}
Exemple #16
0
i4_str *i4_get_user_name()
{
	char buf[256];

#ifdef _WINDOWS
	DWORD s=sizeof(buf);
	GetUserName(buf, &s);
#else
	char * gl=getlogin();
	if (gl)
	{
		strcpy(buf,gl);
	}
	else
	{
		strcpy(buf,"unknown");
	}
#endif

	return i4_from_ascii(buf);
}
Exemple #17
0
int main()
{
	uid_t uid = 0;
	gid_t gid = 0;
	struct passwd *pw = NULL;

	uid = getuid();
	gid = getgid();

	printf("usr name: %s\n", getlogin());
	printf("usr IDs: uid(%d), gid(%d)\n", uid, gid);

	pw = getpwuid(uid);
	if (NULL != pw)
	{
		printf("usr name: %s, pass word: %s, uid: %d, gid: %d, home: %s, shell: %s\n", 
			pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_dir, pw->pw_shell);
	}

	return 0;
}
Exemple #18
0
static const char *
get_username(void)
{
	const struct passwd *pw;
	const char *login;
	uid_t uid;

	/*
	 * Attempt to determine the username corresponding to this login
	 * session.  First, validate the results of getlogin() against
	 * the password database.  If getlogin() returns invalid data,
	 * return an arbitrary username corresponding to this uid.
	 */
	uid = getuid();
	if ((login = getlogin()) != NULL && (pw = getpwnam(login)) != NULL &&
	    pw->pw_uid == uid)
		return (login);
	if ((pw = getpwuid(uid)) != NULL)
		return (pw->pw_name);
	return (NULL);
}
Exemple #19
0
struct passwd *get_my_pwent(void)
{
	struct passwd *pw;
	const char *cp = getlogin();
	uid_t ruid = getuid();

	/*
	 * Try getlogin() first - if it fails or returns a non-existent
	 * username, or a username which doesn't match the real UID, fall
	 * back to getpwuid(getuid()).  This should work reasonably with
	 * usernames longer than the utmp limit (8 characters), as well as
	 * shared UIDs - but not both at the same time...
	 *
	 * XXX - when running from su, will return the current user (not
	 * the original user, like getlogin() does).  Does this matter?
	 */
	if (cp && *cp && (pw = getpwnam(cp)) && pw->pw_uid == ruid)
		return pw;

	return getpwuid(ruid);
}
    char *silc_get_username()
    {
#if 0
        char *logname = NULL;

        logname = getlogin();
        if (!logname) {
            struct passwd *pw;

            pw = getpwuid(getuid());
            if (!pw)
                return strdup("User");

            logname = pw->pw_name;
        }

        return strdup(logname);
#else
        return strdup("Symbian");
#endif /* 0 */
    }
main(){
	char buffer[256];
	char cwd[1024];
	char* token;
	char* username;
	username = getlogin();
	getcwd(cwd, sizeof(cwd));
	printf("Hello %s, your current directory is: %s. \n", username, cwd);
	
	for(;;){
		if(fgets(buffer, 256, stdin) == NULL || (strcmp(buffer, "exit\n") == 0)){
			return(0);
		}
		
		//strips the newline character at end of input
		size_t ln = strlen(buffer) - 1;
		if(buffer[ln] == '\n') buffer[ln] = '\0';
		
		if(strlen(buffer) > 0){
			
			token = strtok(buffer, " ,.");
			
			if(strcmp(token, "repeat") == 0){
				token = strtok(NULL,  " ,.");
				while(token != NULL){
					printf("%s ", token);
					token = strtok(NULL, " ,.");
				}
				printf("\n");
			}
			else if(strcmp(token, "welcome") == 0){
				token = strtok(NULL,  " ,.");
				printf("Hello, %s\n", token);
			}
			else if(strcmp(token, "junk") == 0){
				printf("junk is here\n");
			}
		}
	}
}
Exemple #22
0
void whoami(char *addr, char *defaultname)
{
  char *p = NULL;

#if defined(HAVE_GETDOMAINNAME) || (defined(HAVE_GETHOSTNAME) && ! defined(HAVE_UNAME))
  char line[LINELEN];

#endif /* defined(HAVE_GETDOMAINNAME) || [...] */
#ifdef HAVE_UNAME
  struct utsname uts;

#endif /* HAVE_UNAME */
#ifdef POSIX
  p = getlogin();
#endif /* POSIX */
  if (p == NULL)
    strcpy(addr, defaultname);
  else
    strncpy(addr, p, LINELEN);

  strcatn(addr, "@", LINELEN);
#ifdef HAVE_UNAME
  if (uname(&uts) != -1)
    strcatn(addr, uts.nodename, LINELEN);
#elif defined(HAVE_GETHOSTNAME) /* end of HAVE_UNAME */
  if (gethostname(line, LINELEN) == 0)
    strcatn(addr, line, LINELEN);
#endif /* defined(HAVE_GETHOSTNAME) */
  if (addr[strlen(addr) - 1] == '@')
    strcatn(addr, SHORTNAME, LINELEN);

  if (strchr(strchr(addr, '@'), '.') == NULL) {
#ifdef HAVE_GETDOMAINNAME
    if (getdomainname(line, LINELEN) == 0 && !streq(line, "(none)")) {
      strcatn(addr, ".", LINELEN);
      strcatn(addr, line, LINELEN);
    }
#endif /* HAVE_GETDOMAINNAME */
  }
}
Exemple #23
0
SEXP do_sysinfo(SEXP call, SEXP op, SEXP args, SEXP rho)
{
    SEXP ans, ansnames;
    struct utsname name;
    char *login;

    checkArity(op, args);
    PROTECT(ans = allocVector(STRSXP, 7));
    if(uname(&name) == -1) {
	UNPROTECT(1);
	return R_NilValue;
    }
    SET_STRING_ELT(ans, 0, mkChar(name.sysname));
    SET_STRING_ELT(ans, 1, mkChar(name.release));
    SET_STRING_ELT(ans, 2, mkChar(name.version));
    SET_STRING_ELT(ans, 3, mkChar(name.nodename));
    SET_STRING_ELT(ans, 4, mkChar(name.machine));
    login = getlogin();
    SET_STRING_ELT(ans, 5, login ? mkChar(login) : mkChar("unknown"));
#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID) && defined(HAVE_GETUID)
    {
	struct passwd *stpwd;
	stpwd = getpwuid(getuid());
	SET_STRING_ELT(ans, 6, stpwd ? mkChar(stpwd->pw_name) : mkChar("unknown"));
    }
#else
    SET_STRING_ELT(ans, 6, mkChar("unknown"));
#endif
    PROTECT(ansnames = allocVector(STRSXP, 7));
    SET_STRING_ELT(ansnames, 0, mkChar("sysname"));
    SET_STRING_ELT(ansnames, 1, mkChar("release"));
    SET_STRING_ELT(ansnames, 2, mkChar("version"));
    SET_STRING_ELT(ansnames, 3, mkChar("nodename"));
    SET_STRING_ELT(ansnames, 4, mkChar("machine"));
    SET_STRING_ELT(ansnames, 5, mkChar("login"));
    SET_STRING_ELT(ansnames, 6, mkChar("user"));
    setAttrib(ans, R_NamesSymbol, ansnames);
    UNPROTECT(2);
    return ans;
}
Exemple #24
0
BOOL GetUserNameExA(EXTENDED_NAME_FORMAT NameFormat, LPSTR lpNameBuffer, PULONG nSize)
{
    int length;
    char* login;

    switch (NameFormat)
    {
    case NameSamCompatible:

        login = getlogin();
        length = strlen(login);

        if (*nSize >= length)
        {
            CopyMemory(lpNameBuffer, login, length + 1);
            return 1;
        }
        else
        {
            *nSize = length + 1;
        }

        break;

    case NameFullyQualifiedDN:
    case NameDisplay:
    case NameUniqueId:
    case NameCanonical:
    case NameUserPrincipal:
    case NameCanonicalEx:
    case NameServicePrincipal:
    case NameDnsDomain:
        break;

    default:
        break;
    }

    return 0;
}
Exemple #25
0
char *get_username(void)
{
    struct passwd *p;
    uid_t uid = getuid();
    char *user, *ret = NULL;

    /*
     * First, find who we think we are using getlogin. If this
     * agrees with our uid, we'll go along with it. This should
     * allow sharing of uids between several login names whilst
     * coping correctly with people who have su'ed.
     */
    user = getlogin();
    setpwent();
    if (user)
	p = getpwnam(user);
    else
	p = NULL;
    if (p && p->pw_uid == uid) {
	/*
	 * The result of getlogin() really does correspond to
	 * our uid. Fine.
	 */
	ret = user;
    } else {
	/*
	 * If that didn't work, for whatever reason, we'll do
	 * the simpler version: look up our uid in the password
	 * file and map it straight to a name.
	 */
	p = getpwuid(uid);
	if (!p)
	    return NULL;
	ret = p->pw_name;
    }
    endpwent();

    return dupstr(ret);
}
Exemple #26
0
std::wstring sys_get_user_name()
{
	// Prefer LOGNAME, fall back on getlogin

	const char* logname = getenv("LOGNAME");
	if (logname && strcmp(logname, "") != 0)
		return std::wstring(logname, logname + strlen(logname));
	// TODO: maybe we should do locale conversion?

#if OS_ANDROID
#warning TODO: sys_get_user_name: do something more appropriate and more thread-safe
	char* buf = getlogin();
	if (buf)
		return std::wstring(buf, buf + strlen(buf));
#else
	char buf[256];
	if (getlogin_r(buf, ARRAY_SIZE(buf)) == 0)
		return std::wstring(buf, buf + strlen(buf));
#endif

	return L"";
}
Exemple #27
0
void
intro()
{
char *tmpname;

    srand(time(0L));			/* Kick the random number generator */

    signal(SIGINT,uninitgame);
    if(signal(SIGQUIT,SIG_IGN) != SIG_IGN) signal(SIGQUIT,uninitgame);
#if 1
	/* for some bizzare reason, getlogin or cuserid cause havoc with the terminal */
	if ((tmpname = getlogin()) != NULL) {
		strcpy(name, tmpname);
	} else
#endif
	strcpy(name,dftname);
    name[0] = toupper(name[0]);

    initscr();
    savetty();
    nonl();
    cbreak();
    noecho();
    clear();
    mvaddstr(4,29,"Welcome to Battleship!");
    move(8,0);
PR("                                                  \\\n");
PR("                           \\                     \\ \\\n");
PR("                          \\ \\                   \\ \\ \\_____________\n");
PR("                         \\ \\ \\_____________      \\ \\/            |\n");
PR("                          \\ \\/             \\      \\/             |\n");
PR("                           \\/               \\_____/              |__\n");
PR("           ________________/                                       |\n");
PR("           \\  S.S. Penguin                                         |\n");
PR("            \\                                                     /\n");
PR("             \\___________________________________________________/\n");
    mvaddstr(20,27,"Hit any key to continue..."); refresh();
    getch();
}
Exemple #28
0
/**
 * Prints the current process id, its parent process id, the hostname, username, time of day,
 * current working directory, and that it is the parent process. All sys calls are checked
 * for errors and output streams are flushed after print.
 */
void print_base_process_info(){
    // temporary variables for printing values
    char concat_temp[20];
    char hostname[256];

    //print process id, parent process id, hostname, username, time of day, cwd, that it is the parent process.
    fflush(stdout);
    if(gethostname(hostname, sizeof(hostname)) != 0){
       perror("gethostname");
       fflush(stderr);
    }

    // try to get the login name
    char *login_name = getlogin();

    // print error if cannot get login name
    if (login_name == NULL){
        perror("cuserid");
        fflush(stderr);
    }

    // try to get the cwd
    char *cwd = getcwd(NULL, 0);

    // print error if cannot get cwd
    if (cwd == NULL){
    	perror("getcwd");
    	fflush(stderr);
    }

    // get the current time in a pretty-printed format
    time_t curr_time;
    char* time_str = ctime(&curr_time);

    // print all information
    printf("pid: %d\nppid: %d\nhostname: %s\nusername: %s\ntime: %scwd: %s\nThe parent process for setting WHALE is %d\n",
           getpid(), getppid(), hostname, login_name, time_str, cwd, getpid());
    fflush(stdout);
}
Exemple #29
0
void
pretty(struct passwd *pw)
{
	struct group *gr;
	uid_t eid, rid;
	char *login;

	if (pw) {
		(void)printf("uid\t%s\n", pw->pw_name);
		(void)printf("groups\t");
		group(pw, 1);
	} else {
		if ((login = getlogin()) == NULL)
			err(1, "getlogin");

		pw = getpwuid(rid = getuid());
		if (pw == NULL || strcmp(login, pw->pw_name))
			(void)printf("login\t%s\n", login);
		if (pw)
			(void)printf("uid\t%s\n", pw->pw_name);
		else
			(void)printf("uid\t%u\n", rid);
		
		if ((eid = geteuid()) != rid) {
			if ((pw = getpwuid(eid)))
				(void)printf("euid\t%s\n", pw->pw_name);
			else
				(void)printf("euid\t%u\n", eid);
		}
		if ((rid = getgid()) != (eid = getegid())) {
			if ((gr = getgrgid(rid)))
				(void)printf("rgid\t%s\n", gr->gr_name);
			else
				(void)printf("rgid\t%u\n", rid);
		}
		(void)printf("groups\t");
		group(NULL, 1);
	}
}
Exemple #30
0
static char*
username()
{
    char *tmp;

    tmp = getlogin();
    if (!tmp) {
        struct passwd *user;

        user = getpwuid(getuid());
        if (!user) {
            wsyserror(_("could not get password entry for UID %i"), getuid());
            return NULL;
        }
        if (!user->pw_name) {
            return NULL;
        } else {
            return user->pw_name;
        }
    }
    return tmp;
}