# 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.

## 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 on other sites
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 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 on other sites
On Windows you can use SHGetFolderPath() with CSIDL_APPDATA to get the user's application data folder. Then you can create your engine's folder inside that.

##### Share on other sites
%appdata% should be sufficient in windows

##### 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 on other sites
Quote:
 Original post by Unrealhow 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 KazadeI'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 haegarrA 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:

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632982
• Total Posts
3009690
• ### Who's Online (See full list)

There are no registered users currently online

×