1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see: <http://www.gnu.org/licenses/>
*/
/*
* System.c: code for dealing with various OS system call variants
*/
#include "config.h"
#include "fvwmlib.h"
#include "envvar.h"
#include "System.h"
#include "Strings.h"
#if HAVE_UNAME
#include <sys/utsname.h>
#endif
#include "libs/fvwm_sys_stat.h"
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
/*
* just in case...
*/
#ifndef FD_SETSIZE
#define FD_SETSIZE 2048
#endif
fd_set_size_t fvwmlib_max_fd = (fd_set_size_t)-9999999;
fd_set_size_t GetFdWidth(void)
{
#if HAVE_SYSCONF
return min(sysconf(_SC_OPEN_MAX),FD_SETSIZE);
#else
return min(getdtablesize(),FD_SETSIZE);
#endif
}
void fvwmlib_init_max_fd(void)
{
fvwmlib_max_fd = GetFdWidth();
return;
}
/* return a string indicating the OS type (i.e. "Linux", "SINIX-D", ... ) */
int getostype(char *buf, int max)
{
#if HAVE_UNAME
struct utsname sysname;
if (uname( &sysname ) >= 0)
{
buf[0] = '\0';
strncat(buf, sysname.sysname, max);
return 0;
}
#endif
strcpy (buf, "");
return -1;
}
/*
* Set a colon-separated path, with environment variable expansions,
* and expand '+' to be the value of the previous path.
*/
void setPath(char **p_path, const char *newpath, int free_old_path)
{
char *oldpath = *p_path;
int oldlen = strlen(oldpath);
char *stripped_path = stripcpy(newpath);
int found_plus = strchr(newpath, '+') != NULL;
/* Leave room for the old path, if we find a '+' in newpath */
*p_path = envDupExpand(stripped_path, found_plus ? oldlen - 1 : 0);
free(stripped_path);
if (found_plus)
{
char *p = strchr(*p_path, '+');
memmove(p + oldlen, p + 1, strlen(p + 1) + 1);
memmove(p, oldpath, oldlen);
}
if (free_old_path)
{
free(oldpath);
}
}
/*
*
* Find the specified file somewhere along the given path.
*
* There is a possible race condition here: We check the file and later
* do something with it. By then, the file might not be accessible.
* Oh well.
*
*/
#include <stdio.h>
char *searchPath(
const char *pathlist, const char *filename, const char *suffix,
int type)
{
char *path;
int filename_len;
int maxpath_len;
if (filename == NULL || *filename == 0)
{
return NULL;
}
if (pathlist == NULL || *pathlist == 0)
{
/* use pwd if no path list is given */
pathlist = ".";
}
filename_len = strlen(filename);
maxpath_len = (pathlist) ? strlen(pathlist) : 0;
maxpath_len += (suffix) ? strlen(suffix) : 0;
/* +1 for extra / and +1 for null termination */
path = fxmalloc(maxpath_len + filename_len + 2);
*path = '\0';
if (*filename == '/')
{
/* No search if filename begins with a slash */
strcpy(path, filename);
/* test if the path is accessable -- the module code assumes
* this is done */
if (access(filename, type) == 0)
{
return path;
}
/* the file is not accessable (don't test suffixes with full
* path), return NULL */
free(path);
return NULL;
}
/* Search each element of the pathlist for the file */
while (pathlist && *pathlist)
{
char *path_end = strchr(pathlist, ':');
char *curr_end;
if (path_end != NULL)
{
strncpy(path, pathlist, path_end - pathlist);
path[path_end - pathlist] = '\0';
}
else
{
strcpy(path, pathlist);
}
/* handle specially the path extention using semicolon */
curr_end = strchr(path, ';');
if (curr_end != NULL)
{
char *dot = strrchr(filename, '.');
int filebase_len;
/* count a leading nil in newext_len too */
int newext_len = path + strlen(path) - curr_end;
if (dot != NULL)
{
filebase_len = dot - filename;
}
else
{
filebase_len = filename_len;
}
*(curr_end++) = '/';
memmove(curr_end + filebase_len, curr_end, newext_len);
strncpy(curr_end, filename, filebase_len);
}
else
{
strcat(path, "/");
strcat(path, filename);
}
if (access(path, type) == 0)
{
return path;
}
if (suffix && *suffix != '\0') {
strcat(path, suffix);
if (access(path, type) == 0)
{
return path;
}
}
/* Point to next element of the path */
if (path_end == NULL)
{
break;
}
else
{
pathlist = path_end + 1;
}
}
/* Hmm, couldn't find the file. Return NULL */
free(path);
return NULL;
}
/*
* void setFileStamp(FileStamp *stamp, const char *name);
* Bool isFileStampChanged(const FileStamp *stamp, const char *name);
*
* An interface for verifying cached files.
* The first function associates a file stamp with file (named by name).
* The second function returns True or False in case the file was changed
* from the time the stamp was associated.
*
* FileStamp can be a structure; try to save memory by evaluating a checksum.
*/
FileStamp getFileStamp(const char *name)
{
static struct stat buf;
if (!name || stat(name, &buf))
{
return 0;
}
return ((FileStamp)buf.st_mtime << 13) + (FileStamp)buf.st_size;
}
void setFileStamp(FileStamp *stamp, const char *name)
{
*stamp = getFileStamp(name);
}
Bool isFileStampChanged(const FileStamp *stamp, const char *name)
{
return *stamp != getFileStamp(name);
}
#ifdef HAVE_SAFETY_MKSTEMP
int fvwm_mkstemp (char *TEMPLATE)
{
return mkstemp(TEMPLATE);
}
#else
/* This function is an adaptation of the mkstemp() function included
* in the Gnu C Library, version 2.2.2. The Gnu C library, in turn,
* adapted the function from OpenBSD.
* Michael Han <mikehan@mikehan.com> */
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "ftime.h"
#define __set_errno(val) errno = (val)
/* These are the characters used in temporary filenames. */
static const char letters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/* Generate a unique temporary file name from TEMPLATE.
The last six characters of TEMPLATE must be "XXXXXX";
they are replaced with a string that makes the filename unique.
Then open the file and return a fd. */
int fvwm_mkstemp (char *template)
{
int len;
char *XXXXXX;
static unsigned long value;
struct timeval tv;
int count, fd = -1;
int save_errno = errno;
len = strlen (template);
if (len < 6 || strcmp (&template[len - 6], "XXXXXX"))
{
__set_errno (EINVAL);
return -1;
}
/* This is where the Xs start. */
XXXXXX = &template[len - 6];
/* Get some more or less random data. */
gettimeofday (&tv, NULL);
value = ((unsigned long) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
for (count = 0; count < TMP_MAX; value = 7777, count)
{
unsigned long v = value;
/* Fill in the random bits. */
XXXXXX[0] = letters[v % 62];
v /= 62;
XXXXXX[1] = letters[v % 62];
v /= 62;
XXXXXX[2] = letters[v % 62];
v /= 62;
XXXXXX[3] = letters[v % 62];
v /= 62;
XXXXXX[4] = letters[v % 62];
v /= 62;
XXXXXX[5] = letters[v % 62];
fd = open(
template, O_RDWR | O_CREAT | O_EXCL,
FVWM_S_IRUSR | FVWM_S_IWUSR);
if (fd >= 0)
{
__set_errno (save_errno);
return fd;
}
else if (errno != EEXIST)
{
return -1;
}
}
/* We got out of the loop because we ran out of combinations to try. */
__set_errno (EEXIST);
return -1;
}
#endif /* HAVE_SAFETY_MKSTEMP */
|