Question

I'm making a simple game for Ubuntu and to update the highscore list, it needs a single file at runtime, called 'highscores.bin'.

I wish to put this file at

/home/(USER)/.game_name

I've researched a little and found that from inside a Makefile i can get the environment variable $USER. So in the Makefile, at the 'install' target, i've added:

mkdir -p $(DESTDIR)home/$$USER/.game_name

But when i run 'sudo make install', the Makefile installs it as:

/home/root/.game_name

How can i get the (non-root) user name in a Makefile?

P.S.: I'm writing the Makefile by hand. No ./configure

P.S.2: I dont want to do

mkdir -p ~/.game_name

because i want to be able to change DESTDIR if i want to install to a temporary directory.

Was it helpful?

Solution

Well, if you want that file during runtime I would recommend that you create it during runtime. The Makefile is considered only during compile time. This is obviously a problem if the program is compiled and then executed by different users, some of which may not even exist at compile time.

During runtime you can get the home directory as shown here (if you are using C/C++). Then you can check for existance of the highscore folder/file and create it if you need to. If you want a highscore file for all users, you should put it in the home directory of a special user, but rather somewhere else in the file system. This is for example done by the game xjump.

And about your Makefile, the $USER variable is translated to root because of the sudo command on

sudo make install

The user actually running make is the superuser: root.

OTHER TIPS

You want to install your program for every users? So it's better if you create the /home/user/.game_name directory when the user run the game, not when root installs it. Otherwise, only the user who call sudo make install will have it's highscore list. If you have only one user, do not install it system-wide, but in the user directory.

I suggest to initialize users files at game first run. So every users, even users that didn't exist when the game was installed, can have their highscore list. You can add the initializing code into your game (c++, java or anything else) or just call an external script that does it (not so elegant). Avoid to create user-related files at installation time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top