Sponsored Content
Full Discussion: Getting Home Directory
Top Forums Programming Getting Home Directory Post 302348376 by Loic Domaigne on Friday 28th of August 2009 05:05:10 AM
Old 08-28-2009
Hello,

you can get this information using getuid() combined with getpwuid(). See an example below (error handling etc. have been omitted...)

Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>

int
main()
{
   struct passwd *pwd;

   if ( (pwd=getpwuid(getuid())) != NULL )
   {
      printf("Home Dir of user running the program: %s\n", pwd->pw_dir);
   }

   return 0;
}

HTH,
Loïc
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

c++ home directory??

when i compile *.cpp files the compiler didn't find the non standart includes.If i have to put the full path of the includet files where shall i begin from root dirctory or i heve to put includet files in cpp home directory??? can i compile java files in unix(linux mandrake 7) if yes haw... (3 Replies)
Discussion started by: user666
3 Replies

2. UNIX for Dummies Questions & Answers

cd into home directory ~

I'm trying to cd into a home directory with cd ~username_here and I'm getting the following error: ~username_here: does not exist The directory exists and I can directly go to it via cd /export/home/username_here without any problems. Any suggestions? (4 Replies)
Discussion started by: here2learn
4 Replies

3. UNIX for Dummies Questions & Answers

home directory

Hi what is the difference between the directory named /home and the user's home directory? can anyone plz reply? really confuse about it!!!!!!!! thank you (1 Reply)
Discussion started by: nokia3100
1 Replies

4. Shell Programming and Scripting

home directory

hello i want shell script. as root , i want to untar specific.tar.gz to all home user directory and after untar , there is 1.txt 2.txt ~~ 26.txt in/public_html/test1/ i want randomly selected 6 text files in 1.txt 2.txt ~26.txt to be renamed newword1.word , newword2.word , ~~... (8 Replies)
Discussion started by: topic32428285
8 Replies

5. SuSE

Could not chdir to home directory

Hi, on logging into oracle account i got these error message Could not chdir to home directory /home/oracle: No such file or directory /usr/X11R6/bin/xauth: error in locking authority file /home/oracle/.Xauthority found the command used in creating user was usermod -d /home/oracle -m... (5 Replies)
Discussion started by: saha
5 Replies

6. Solaris

Restricting SFTP user to a defined directory and home directory

Hi, I've created solaris user which has both FTP and SFTP Access. Using the "ftpaccess" configuration file options "guest-root" and "restricted-uid", i can restrict the user to a specific directory. But I'm unable to restrict the user when the user is logged in using SFTP. The aim is to... (1 Reply)
Discussion started by: sftpuser
1 Replies

7. Solaris

home directory for roleadd

hi all.. by seeing the subject, you can judge that i am new bee to solaris. my question in mind is 'Why do we need to specify a home directory for roleadd ?'. specifying a home directory for user is meaningful because user oriented preferences can be placed inside it. RBAC uses attributes in... (1 Reply)
Discussion started by: starworse@yahoo
1 Replies

8. Solaris

Not able to create directory in /home.

I am working on Solaris 10 machine with autofs(auto_home) disabled. But when I am creating any directory inside /home like /home/Telco, it's going in sleep, and nothing is happening until I manually end the process. Can any one help me out here? ---------- Post updated 2014-01-14 at 05:05... (2 Replies)
Discussion started by: nixhead
2 Replies

9. Solaris

SunOS confusing root directory and user home directory

Hello, I've just started using a Solaris machine with SunOS 5.10. After the machine is turned on, I open a Console window and at the prompt, if I execute a pwd command, it tells me I'm at my home directory (someone configured "myuser" as default user after init). ... (2 Replies)
Discussion started by: egyassun
2 Replies
GETPWUID(3P)						     POSIX Programmer's Manual						      GETPWUID(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
getpwuid, getpwuid_r - search user database for a user ID SYNOPSIS
#include <pwd.h> struct passwd *getpwuid(uid_t uid); int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result); DESCRIPTION
The getpwuid() function shall search the user database for an entry with a matching uid. The getpwuid() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe. Applications wishing to check for error situations should set errno to 0 before calling getpwuid(). If getpwuid() returns a null pointer and errno is set to non-zero, an error occurred. The getpwuid_r() function shall update the passwd structure pointed to by pwd and store a pointer to that structure at the location pointed to by result. The structure shall contain an entry from the user database with a matching uid. Storage referenced by the structure is allo- cated from the memory provided with the buffer parameter, which is bufsize bytes in size. The maximum size needed for this buffer can be determined with the {_SC_GETPW_R_SIZE_MAX} sysconf() parameter. A NULL pointer shall be returned at the location pointed to by result on error or if the requested entry is not found. RETURN VALUE
The getpwuid() function shall return a pointer to a struct passwd with the structure as defined in <pwd.h> with a matching entry if found. A null pointer shall be returned if the requested entry is not found, or an error occurs. On error, errno shall be set to indicate the error. The return value may point to a static area which is overwritten by a subsequent call to getpwent(), getpwnam(), or getpwuid(). If successful, the getpwuid_r() function shall return zero; otherwise, an error number shall be returned to indicate the error. ERRORS
The getpwuid() and getpwuid_r() functions may fail if: EIO An I/O error has occurred. EINTR A signal was caught during getpwuid(). EMFILE {OPEN_MAX} file descriptors are currently open in the calling process. ENFILE The maximum allowable number of files is currently open in the system. The getpwuid_r() function may fail if: ERANGE Insufficient storage was supplied via buffer and bufsize to contain the data to be referenced by the resulting passwd structure. The following sections are informative. EXAMPLES
Getting an Entry for the Root User The following example gets the user database entry for the user with user ID 0 (root). #include <sys/types.h> #include <pwd.h> ... uid_t id = 0; struct passwd *pwd; pwd = getpwuid(id); Finding the Name for the Effective User ID The following example defines pws as a pointer to a structure of type passwd, which is used to store the structure pointer returned by the call to the getpwuid() function. The geteuid() function shall return the effective user ID of the calling process; this is used as the search criteria for the getpwuid() function. The call to getpwuid() shall return a pointer to the structure containing that user ID value. #include <unistd.h> #include <sys/types.h> #include <pwd.h> ... struct passwd *pws; pws = getpwuid(geteuid()); Finding an Entry in the User Database The following example uses getpwuid() to search the user database for a user ID that was previously stored in a stat structure, then prints out the user name if it is found. If the user is not found, the program prints the numeric value of the user ID for the entry. #include <sys/types.h> #include <pwd.h> #include <stdio.h> ... struct stat statbuf; struct passwd *pwd; ... if ((pwd = getpwuid(statbuf.st_uid)) != NULL) printf(" %-8.8s", pwd->pw_name); else printf(" %-8d", statbuf.st_uid); APPLICATION USAGE
Three names associated with the current process can be determined: getpwuid( geteuid()) returns the name associated with the effective user ID of the process; getlogin() returns the name associated with the current login activity; and getpwuid( getuid()) returns the name associ- ated with the real user ID of the process. The getpwuid_r() function is thread-safe and returns values in a user-supplied buffer instead of possibly using a static data area that may be overwritten by each call. RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
getpwnam(), geteuid(), getuid(), getlogin(), the Base Definitions volume of IEEE Std 1003.1-2001, <limits.h>, <pwd.h>, <sys/types.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 GETPWUID(3P)
All times are GMT -4. The time now is 08:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy