Files
.github
AnyEvent-I3
contrib
debian
docs
etc
i3-config-wizard
i3-dump-log
i3-input
i3-msg
i3-nagbar
i3bar
include
libi3
README
dpi.c
draw_util.c
fake_configure_notify.c
font.c
format_placeholders.c
get_colorpixel.c
get_config_path.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
mkdirp.c
resolve_tilde.c
root_atom_contents.c
safewrappers.c
string.c
strndup.c
ucs2_conversion.c
m4
man
parser-specs
share
src
testcases
travis
.clang-format
.editorconfig
.gitignore
.travis.yml
DEPENDS
I3_VERSION
LICENSE
Makefile.am
PACKAGE-MAINTAINER
README.md
RELEASE-NOTES-4.15
configure.ac
generate-command-parser.pl
i3-dmenu-desktop
i3-migrate-config-to-v4
i3-save-tree
i3-sensible-editor
i3-sensible-pager
i3-sensible-terminal
logo.svg
pseudo-doc.doxygen
release.sh
i3/libi3/get_process_filename.c
Michael Stapelberg f354f53435 Ensure all *.[ch] files include config.h
Including config.h is necessary to get e.g. the _GNU_SOURCE define and
any other definitions that autoconf declares. Hence, config.h needs to
be included as the first header in each file.

This is done either via:
1. Including "common.h" (i3bar)
2. Including "libi3.h"
3. Including "all.h" (i3)
4. Including <config.h> directly

Also remove now-unused I3__FILE__, add copyright/license statement
where missing and switch include/all.h to #pragma once.
2016-10-23 21:09:24 +02:00

63 lines
1.9 KiB
C

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
#include "libi3.h"
#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 <err.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) {
warn("Could not mkdir(%s)", dir);
errx(EXIT_FAILURE, "Check permissions of $XDG_RUNTIME_DIR = '%s'",
getenv("XDG_RUNTIME_DIR"));
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;
}