Fudge PNG macro detection based on version of libpng.
[fvwm.git] / modules / FvwmCommand / fifos.c
blob8e98f2a599403c816135374191bf8d4578b4c5bb
1 /* -*-c-*- */
2 /*
3 * Fvwm command input interface.
5 * Copyright 1998, Toshi Isogai.
6 * Use this program at your own risk.
7 * Permission to use this program for any purpose is given,
8 * as long as the copyright is kept intact.
12 /* This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "config.h"
28 #include "FvwmCommand.h"
30 #define MAXHOSTNAME 32
32 char * fifos_get_default_name(void)
34 char file_suffix[] = { 'R', 'C', 'M', '\0' };
35 char *f_stem;
36 char *dpy_name;
37 char dpy_name_add[3];
38 char *c;
39 int i;
40 struct stat stat_buf;
41 char hostname[MAXHOSTNAME];
42 int type_pos;
43 uid_t owner;
44 Bool is_path_valid;
46 /* default name */
47 dpy_name = getenv("DISPLAY");
48 if (!dpy_name || *dpy_name == 0)
50 dpy_name = ":0.0";
52 if (strncmp(dpy_name, "unix:", 5) == 0)
54 dpy_name += 4;
56 dpy_name_add[0] = 0;
57 c = strrchr(dpy_name, '.');
58 i = 0;
59 if (c != NULL)
61 if (*(c + 1) != 0)
63 for (c++, i = 0; isdigit(*c); c++, i++)
65 /* nothing */
68 else
70 /* cut off trailing period */
71 *c = 0;
74 if (i == 0)
76 /* append screen number */
77 strcpy(dpy_name_add, ".0");
79 f_stem = safemalloc(11 + strlen(F_NAME) + MAXHOSTNAME +
80 strlen(dpy_name) + strlen(dpy_name_add));
82 if (
83 (stat("/var/tmp", &stat_buf) == 0) &&
84 (stat_buf.st_mode & S_IFDIR))
86 strcpy (f_stem, "/var/tmp/");
88 else
90 strcpy (f_stem, "/tmp/");
92 strcat(f_stem, F_NAME);
94 /* Make it unique */
95 if (!dpy_name[0] || ':' == dpy_name[0])
97 /* Put hostname before dpy if not there */
98 gethostname(hostname, MAXHOSTNAME);
99 strcat(f_stem, hostname);
101 strcat(f_stem, dpy_name);
102 strcat(f_stem, dpy_name_add);
104 /* Verify that all files are either non-symlinks owned by the current
105 * user or non-existing. If not: use FVWM_USERDIR as base instead. */
107 type_pos = strlen(f_stem);
108 owner = geteuid();
109 is_path_valid = True;
111 f_stem[type_pos+1] = 0;
113 for (c = file_suffix; *c != 0 && is_path_valid; c++)
115 int rc;
117 f_stem[type_pos] = *c;
118 if (DO_USE_LSTAT)
120 rc = fvwm_lstat(f_stem, &stat_buf);
122 else
124 rc = stat(f_stem, &stat_buf);
126 if (rc == 0)
128 /* stat successful */
129 if (
130 stat_buf.st_uid != owner ||
131 stat_buf.st_nlink > 1 ||
132 S_ISDIR(stat_buf.st_mode) ||
133 FVWM_S_ISLNK(stat_buf.st_mode) ||
134 (stat_buf.st_mode & FVWM_S_IFLNK) != 0)
136 is_path_valid = False;
138 break;
140 else if (errno != ENOENT)
142 is_path_valid = False;
145 f_stem[type_pos] = 0;
146 if (!is_path_valid)
148 char *userdir;
149 char *tailname;
150 char *tmp;
151 tmp = f_stem;
153 if (f_stem[1] == 't') /* /tmp/ */
155 tailname = f_stem + 4;
157 else /* /var/tmp/ */
159 tailname = f_stem + 8;
162 userdir = getenv("FVWM_USERDIR");
163 if (userdir == NULL)
165 free(tmp);
166 return NULL;
168 f_stem = safemalloc(strlen(userdir) + strlen(tailname) + 1);
169 strcpy(f_stem, userdir);
170 strcat(f_stem, tailname);
171 free(tmp);
174 return f_stem;