Files
contrib
debian
docs
i3-config-wizard
i3-dump-log
i3-input
i3-msg
i3-nagbar
i3bar
include
libi3
Makefile
README
fake_configure_notify.c
font.c
get_colorpixel.c
get_exe_path.c
get_mod_mask.c
get_process_filename.c
get_visualtype.c
ipc_connect.c
ipc_recv_message.c
ipc_send_message.c
is_debug_build.c
libi3.mk
root_atom_contents.c
safewrappers.c
string.c
strndup.c
ucs2_conversion.c
man
parser-specs
src
testcases
tests
yajl-fallback
.gitignore
DEPENDS
LICENSE
Makefile
PACKAGE-MAINTAINER
RELEASE-NOTES-4.2
RELEASE-NOTES-4.3
RELEASE-NOTES-4.4
RELEASE-NOTES-4.5
RELEASE-NOTES-4.5.1
RELEASE-NOTES-4.6
common.mk
generate-command-parser.pl
i3-dmenu-desktop
i3-migrate-config-to-v4
i3-sensible-editor
i3-sensible-pager
i3-sensible-terminal
i3.applications.desktop
i3.config
i3.config.keycodes
i3.xsession.desktop
logo.svg
pseudo-doc.doxygen
i3/libi3/get_process_filename.c
2012-12-24 16:57:30 +01:00

59 lines
1.7 KiB
C

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <err.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#include "libi3.h"
/*
* Returns the name of a temporary file with the specified prefix.
*
*/
char *get_process_filename(const char *prefix) {
/* dir stores the directory path for this and all subsequent calls so that
* we only create a temporary directory once per i3 instance. */
static char *dir = NULL;
if (dir == NULL) {
/* Check if XDG_RUNTIME_DIR is set. If so, we use XDG_RUNTIME_DIR/i3 */
if ((dir = getenv("XDG_RUNTIME_DIR"))) {
char *tmp;
sasprintf(&tmp, "%s/i3", dir);
dir = tmp;
struct stat buf;
if (stat(dir, &buf) != 0) {
if (mkdir(dir, 0700) == -1) {
perror("mkdir()");
return NULL;
}
}
} else {
/* If not, we create a (secure) temp directory using the template
* /tmp/i3-<user>.XXXXXX */
struct passwd *pw = getpwuid(getuid());
const char *username = pw ? pw->pw_name : "unknown";
sasprintf(&dir, "/tmp/i3-%s.XXXXXX", username);
/* mkdtemp modifies dir */
if (mkdtemp(dir) == NULL) {
perror("mkdtemp()");
return NULL;
}
}
}
char *filename;
sasprintf(&filename, "%s/%s.%d", dir, prefix, getpid());
return filename;
}