Jump to content
  • Advertisement
Sign in to follow this  
Unreal

Getting Username (Windows And Linux)

This topic is 4396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there! I want to save my Engine config files at: \documents and settings\<user>\application data\my engine\ for windows and /home/<user>/my engine/ for Linux where <user> is the current logged in user that runs the application. how i will get the username for both windows and linux? any idea? thanks for your time!

Share this post


Link to post
Share on other sites
Advertisement
A simpe version under linux is to investigate the environment variables, as are available from the main() invocation. Herein the variable USER denotes the user's name. Even better is to use HOME that already denotes the user's home directory (and hence would be more suitable instead of assuming it being located in /home/).

Another way is to use getlogin() or getlogin_r() from the libc, included by unistd.h. There're some quirks with this functions, so you should carefully read the man pages of them.

Sorry, but I don't know to do this things under windows. Perhaps there is also something to find in the environment variables?

Share this post


Link to post
Share on other sites
To get the user's home directory, use the environment variable "HOME" under Linux / Unix (and the env var "userprofile" on Windows).

Environment variables are case sensitive.

Finding the username is something different, independent. Both Linux and Windows can be configured to place users' home directories somewhere other than the default, so you shouldn't rely on that.

Mark

Share this post


Link to post
Share on other sites
I'm pretty sure that saving to:

~/my engine/

would work in linux. ~/ is a shortcut to the home directory.

Share this post


Link to post
Share on other sites
Quote:
Original post by Unreal
how i will get the username for both windows and linux?


I don't know about Windows. For Posix systems (e.g. GNU/Linux), you can use getpwuid(3) with getuid(2).


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#include <errno.h>

int main() {
struct passwd *pwd;
pwd = getpwuid(getuid());
if (NULL != pwd) {
printf("Username: %s\n", pwd->pw_name);
printf("Home directory: %s\n", pwd->pw_dir);
}
else {
fprintf(stderr, "Error: %s\n", strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}




Quote:
Original post by Kazade
I'm pretty sure that saving to:

~/my engine/

would work in linux. ~/ is a shortcut to the home directory.


It's the shell that expands ~ into the home directory. The C library doesn't.


Quote:
Original post by haegarr
A simpe version under linux is to investigate the environment variables, as are available from the main() invocation. Herein the variable USER denotes the user's name. Even better is to use HOME that already denotes the user's home directory (and hence would be more suitable instead of assuming it being located in /home/).


That's dangerous. Consider:


$ cat foo.c
#include <stdio.h>
#include <stdlib.h>

int main() {
char *home = getenv("HOME");
if (home) {
printf("%s\n", home);
}
return 0;
}
$ ./foo
/home/somebody
$ HOME=/root ./foo
/root


This isn't dangerous per se (lambda users can't write to /root), but it illustrates that you can't trust environment variables to get meaningful data.

Share this post


Link to post
Share on other sites
Original post by let_bound
Quote:
Original post by Unreal

Quote:
Original post by Kazade
I'm pretty sure that saving to:

~/my engine/

would work in linux. ~/ is a shortcut to the home directory.


It's the shell that expands ~ into the home directory. The C library doesn't.


Oops. My mistake.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by let_bound
This isn't dangerous per se (lambda users can't write to /root), but it illustrates that you can't trust environment variables to get meaningful data.


On the other hand, sometimes it can be a good idea to let the user specify another directory by redefining HOME in a shell. I remember doing that a few fimes, but don't remember the exact purpose of it...

Share this post


Link to post
Share on other sites
Quote:
Original post by Unreal
Quote:
Original post by haegarr
A simpe version under linux is to investigate the environment variables, as are available from the main() invocation. Herein the variable USER denotes the user's name. Even better is to use HOME that already denotes the user's home directory (and hence would be more suitable instead of assuming it being located in /home/).


That's dangerous. Consider:

code removed

This isn't dangerous per se (lambda users can't write to /root), but it illustrates that you can't trust environment variables to get meaningful data.

Its true that environment variables can simply be faked, e.g. I can tell the computer to display me chinese messages by setting $LANG to the appropriate value. Well, then I have to live with the result ;) But it isn't dangerous to let the user write to any directory the user has write permissions for. Of course, it is best practise to check whether opening/writing files is possible instead of breaking the program. The only "security" issue would be that the user may overwrite the same files of another user, but that can (and should) simply be avoided by file permissions. Last but not least I second the hint of the AP above.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!