|
| 1 | +/* -*-c-*- */ |
| 2 | +/* |
| 3 | + * Fvwm command input interface. |
| 4 | + * |
| 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. |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 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. |
| 16 | + * |
| 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. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with this program; if not, see: <http://www.gnu.org/licenses/> |
| 24 | + */ |
| 25 | + |
| 26 | +#include "config.h" |
| 27 | +#include "FvwmCommand.h" |
| 28 | + |
| 29 | +#define MAXHOSTNAME 32 |
| 30 | + |
| 31 | +char * fifos_get_default_name(void) |
| 32 | +{ |
| 33 | + char file_suffix[] = { 'R', 'C', 'M', '\0' }; |
| 34 | + char *f_stem; |
| 35 | + char *dpy_name; |
| 36 | + char dpy_name_add[3]; |
| 37 | + char *c; |
| 38 | + int i; |
| 39 | + struct stat stat_buf; |
| 40 | + char hostname[MAXHOSTNAME]; |
| 41 | + int type_pos; |
| 42 | + uid_t owner; |
| 43 | + Bool is_path_valid; |
| 44 | + |
| 45 | + /* default name */ |
| 46 | + dpy_name = getenv("DISPLAY"); |
| 47 | + if (!dpy_name || *dpy_name == 0) |
| 48 | + { |
| 49 | + dpy_name = ":0.0"; |
| 50 | + } |
| 51 | + if (strncmp(dpy_name, "unix:", 5) == 0) |
| 52 | + { |
| 53 | + dpy_name += 4; |
| 54 | + } |
| 55 | + dpy_name_add[0] = 0; |
| 56 | + c = strrchr(dpy_name, '.'); |
| 57 | + i = 0; |
| 58 | + if (c != NULL) |
| 59 | + { |
| 60 | + if (*(c + 1) != 0) |
| 61 | + { |
| 62 | + for (c++, i = 0; isdigit(*c); c++, i++) |
| 63 | + { |
| 64 | + /* nothing */ |
| 65 | + } |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + /* cut off trailing period */ |
| 70 | + *c = 0; |
| 71 | + } |
| 72 | + } |
| 73 | + if (i == 0) |
| 74 | + { |
| 75 | + /* append screen number */ |
| 76 | + strcpy(dpy_name_add, ".0"); |
| 77 | + } |
| 78 | + f_stem = safemalloc(11 + strlen(F_NAME) + MAXHOSTNAME + |
| 79 | + strlen(dpy_name) + strlen(dpy_name_add)); |
| 80 | + |
| 81 | + if ( |
| 82 | + (stat("/var/tmp", &stat_buf) == 0) && |
| 83 | + (stat_buf.st_mode & S_IFDIR)) |
| 84 | + { |
| 85 | + strcpy (f_stem, "/var/tmp/"); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + strcpy (f_stem, "/tmp/"); |
| 90 | + } |
| 91 | + strcat(f_stem, F_NAME); |
| 92 | + |
| 93 | + /* Make it unique */ |
| 94 | + if (!dpy_name[0] || ':' == dpy_name[0]) |
| 95 | + { |
| 96 | + /* Put hostname before dpy if not there */ |
| 97 | + gethostname(hostname, MAXHOSTNAME); |
| 98 | + strcat(f_stem, hostname); |
| 99 | + } |
| 100 | + strcat(f_stem, dpy_name); |
| 101 | + strcat(f_stem, dpy_name_add); |
| 102 | + |
| 103 | + /* Verify that all files are either non-symlinks owned by the current |
| 104 | + * user or non-existing. If not: use FVWM_USERDIR as base instead. */ |
| 105 | + |
| 106 | + type_pos = strlen(f_stem); |
| 107 | + owner = geteuid(); |
| 108 | + is_path_valid = True; |
| 109 | + |
| 110 | + f_stem[type_pos+1] = 0; |
| 111 | + |
| 112 | + for (c = file_suffix; *c != 0 && is_path_valid; c++) |
| 113 | + { |
| 114 | + int rc; |
| 115 | + |
| 116 | + f_stem[type_pos] = *c; |
| 117 | + if (DO_USE_LSTAT) |
| 118 | + { |
| 119 | + rc = fvwm_lstat(f_stem, &stat_buf); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + rc = stat(f_stem, &stat_buf); |
| 124 | + } |
| 125 | + if (rc == 0) |
| 126 | + { |
| 127 | + /* stat successful */ |
| 128 | + if ( |
| 129 | + stat_buf.st_uid != owner || |
| 130 | + stat_buf.st_nlink > 1 || |
| 131 | + S_ISDIR(stat_buf.st_mode) || |
| 132 | + FVWM_S_ISLNK(stat_buf.st_mode) || |
| 133 | + (stat_buf.st_mode & FVWM_S_IFLNK) != 0) |
| 134 | + { |
| 135 | + is_path_valid = False; |
| 136 | + } |
| 137 | + break; |
| 138 | + } |
| 139 | + else if (errno != ENOENT) |
| 140 | + { |
| 141 | + is_path_valid = False; |
| 142 | + } |
| 143 | + } |
| 144 | + f_stem[type_pos] = 0; |
| 145 | + if (!is_path_valid) |
| 146 | + { |
| 147 | + char *userdir; |
| 148 | + char *tailname; |
| 149 | + char *tmp; |
| 150 | + tmp = f_stem; |
| 151 | + |
| 152 | + if (f_stem[1] == 't') /* /tmp/ */ |
| 153 | + { |
| 154 | + tailname = f_stem + 4; |
| 155 | + } |
| 156 | + else /* /var/tmp/ */ |
| 157 | + { |
| 158 | + tailname = f_stem + 8; |
| 159 | + } |
| 160 | + |
| 161 | + userdir = getenv("FVWM_USERDIR"); |
| 162 | + if (userdir == NULL) |
| 163 | + { |
| 164 | + free(tmp); |
| 165 | + return NULL; |
| 166 | + } |
| 167 | + f_stem = safemalloc(strlen(userdir) + strlen(tailname) + 1); |
| 168 | + strcpy(f_stem, userdir); |
| 169 | + strcat(f_stem, tailname); |
| 170 | + free(tmp); |
| 171 | + } |
| 172 | + |
| 173 | + return f_stem; |
| 174 | +} |
0 commit comments