|> >Aghh! Don't you ever read the news?? I posted this on uwe.general about a week
|> >ago and so did ac-gavin, but incase you missed it, here it is again..
|> >#include <pwd.h>
|> [STUFF DELETED]
|> >endpwent();
|>
|> Problem is it didn't work!
|>
I have tested this code and it DOES work.
Here it is as a full runnable piece of code:
#include <stdio.h>
#include <pwd.h>
int main()
{
struct passwd *my_passwd;
my_passwd=getpwuid(getuid());
printf("%s\n",my_passwd->pw_name);
endpwent();
}
Both myself and Andy have tested this, infact we both replied with the same
code.. (Andy finished with endpwent(), but I forgot.. :) )
If you wanted to copy the returned username to another string, use this code
fragment...
#include <stdio.h>
#include <pwd.h>
int main()
{
char *my_name;
struct passwd *my_passwd;
my_passwd=getpwuid(getuid());
my_name=(char *)malloc(strlen(my_passwd->pw_name));
strcpy(my_name,my_passwd->pw_name);
/* String my_name now contains the username */
}
Geraint...
--
*******************************************************************************
* Without going outside, you may know the whole world. *
* Without looking through the window, you may see the ways of heaven. *
* The farther you go, the less you know. *
* *
* Thus the sage knows without traveling; *
* He sees without looking; *
* He works without doing. *
* (Lao Tsu) - 'Tao te ching' *
*******************************************************************************