9

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.

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
kure
  • 93
  • 1
  • 1
  • 4
  • 1
    It's worth pointing out that there's no guarantee that the home-directory for a given user will be at `/home/username` (e.g. `root`'s homedir is usually `/root`) - use the `HOME` environment-variable instead. – nickgrim Nov 23 '11 at 00:00
  • Why the hell is this so hard to find on google? The lack of discussion about this specific scenario is worrying. – Braden Best May 25 '14 at 21:10

2 Answers2

14

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.

Community
  • 1
  • 1
Exp
  • 196
  • 1
  • 5
  • Thanks for the answer! I haven't thought of another user executing it and not having the 'hightscores.bin' file. But if i install the binary at /usr/local/bin, all the users would have access to it? – kure Nov 22 '11 at 23:01
  • `/usr/local/bin` is generally not writable by an ordinary user, and it should only contain executables. On my gentoo system games with shared scores put them in `/var/games` (you need to do it at install time, and then `chmod 660; chgrp games` it, so users can update it) (and you should make that path easily changeable, since other distributions may have different directory layout) – DirtY iCE Nov 22 '11 at 23:29
5

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.

Alessandro Pezzato
  • 7,820
  • 5
  • 39
  • 59
  • Thanks! It makes a lot of sense to create user files at first run. I'm sorry that i didn't understand your answer at first. – kure Nov 22 '11 at 23:40