| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: fileopen.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: definition of a fopen fct. that accepts UNIX-style pathnames */ |
| 10 |
/* */ |
| 11 |
/* Author: Henrik Rentz-Reichert */ |
| 12 |
/* Institut fuer Computeranwendungen III */ |
| 13 |
/* Universitaet Stuttgart */ |
| 14 |
/* Pfaffenwaldring 27 */ |
| 15 |
/* 70569 Stuttgart */ |
| 16 |
/* email: [email protected] */ |
| 17 |
/* */ |
| 18 |
/* History: 02.02.95 new for ug version 3.0 */ |
| 19 |
/* */ |
| 20 |
/* Remarks: */ |
| 21 |
/* */ |
| 22 |
/****************************************************************************/ |
| 23 |
|
| 24 |
/****************************************************************************/ |
| 25 |
/* */ |
| 26 |
/* include files */ |
| 27 |
/* system include files */ |
| 28 |
/* application include files */ |
| 29 |
/* */ |
| 30 |
/****************************************************************************/ |
| 31 |
|
| 32 |
|
| 33 |
/* standard C library */ |
| 34 |
#include <config.h> |
| 35 |
#include <cstdlib> |
| 36 |
#include <cstdio> |
| 37 |
#include <cstring> |
| 38 |
#include <fcntl.h> |
| 39 |
#include <cassert> |
| 40 |
#include <ctime> |
| 41 |
|
| 42 |
#include "ugtypes.h" |
| 43 |
|
| 44 |
/* includes for filesize(), filetype() */ |
| 45 |
#ifdef __MACINTOSH__ |
| 46 |
#include <unistd.h> |
| 47 |
#include <stat.h> |
| 48 |
#include <Files.h> |
| 49 |
#include <TextUtils.h> |
| 50 |
#include <Errors.h> |
| 51 |
/* NB: On Macs the structs of <types.h> are defined locally in <stat.h> */ |
| 52 |
#else |
| 53 |
#include <sys/types.h> |
| 54 |
#include <sys/stat.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
/* low module */ |
| 59 |
/* avoid assertion in REP_ERR_RETURN for ModelP and GUI */ |
| 60 |
#if defined(ModelP) && defined(__GUI__) |
| 61 |
#define PARALLEL |
| 62 |
#undef ModelP |
| 63 |
#endif |
| 64 |
|
| 65 |
#include "debug.h" |
| 66 |
|
| 67 |
#ifdef PARALLEL |
| 68 |
#define ModelP |
| 69 |
#undef PARALLEL |
| 70 |
#endif |
| 71 |
|
| 72 |
#include "ugenv.h" |
| 73 |
#include <dune/uggrid/ugdevices.h> |
| 74 |
|
| 75 |
#include "fileopen.h" |
| 76 |
|
| 77 |
#if defined __HP__ || __SGI__ || __PARAGON__ || __DEC__ || __SUN__ || __PC__ || __LINUXPPC__ |
| 78 |
#include <dirent.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
USING_UG_NAMESPACE |
| 82 |
|
| 83 |
/****************************************************************************/ |
| 84 |
/* */ |
| 85 |
/* defines in the following order */ |
| 86 |
/* */ |
| 87 |
/* compile time constants defining static data size (i.e. arrays) */ |
| 88 |
/* other constants */ |
| 89 |
/* macros */ |
| 90 |
/* */ |
| 91 |
/****************************************************************************/ |
| 92 |
|
| 93 |
#define MAXPATHLENGTH 256 |
| 94 |
#define MAXPATHS 16 |
| 95 |
|
| 96 |
#define MAX_PATH_LEN 1024 |
| 97 |
#define BASE_PATH_SIZE 512 |
| 98 |
|
| 99 |
#ifndef __MACINTOSH__ |
| 100 |
#define ConvertUNIX_2_MachinePath(fname) fname |
| 101 |
#define ConvertMachine_2_UNIXPath(fname) fname |
| 102 |
#endif |
| 103 |
|
| 104 |
/****************************************************************************/ |
| 105 |
/* */ |
| 106 |
/* data structures used in this source file (exported data structures are */ |
| 107 |
/* in the corresponding include file!) */ |
| 108 |
/* */ |
| 109 |
/****************************************************************************/ |
| 110 |
|
| 111 |
typedef char PATH[MAXPATHLENGTH]; |
| 112 |
|
| 113 |
typedef struct |
| 114 |
{ |
| 115 |
|
| 116 |
/* env item */ |
| 117 |
ENVVAR v; |
| 118 |
|
| 119 |
INT nPaths; /* number of paths stored */ |
| 120 |
PATH path[1]; /* begin of path list */ |
| 121 |
|
| 122 |
} PATHS; |
| 123 |
|
| 124 |
/****************************************************************************/ |
| 125 |
/* */ |
| 126 |
/* definition of variables global to this source file only (static!) */ |
| 127 |
/* */ |
| 128 |
/****************************************************************************/ |
| 129 |
|
| 130 |
static INT thePathsDirID; |
| 131 |
static INT thePathsVarID; |
| 132 |
|
| 133 |
|
| 134 |
static char BasePath[BASE_PATH_SIZE] = "./"; |
| 135 |
|
| 136 |
/****************************************************************************/ |
| 137 |
/* */ |
| 138 |
/* Function: GetPaths */ |
| 139 |
/* */ |
| 140 |
/* Purpose: find the Paths environment item with name */ |
| 141 |
/* */ |
| 142 |
/* Input: name of the Paths to find */ |
| 143 |
/* */ |
| 144 |
/* Output: PATHS *: pointer to the multigrid struct */ |
| 145 |
/* NULL: if an error occurred */ |
| 146 |
/* */ |
| 147 |
/****************************************************************************/ |
| 148 |
|
| 149 |
static PATHS *GetPaths (const char *name) |
| 150 |
{ |
| 151 |
return ((PATHS *) SearchEnv(name,"/Paths",thePathsVarID,thePathsDirID)); |
| 152 |
} |
| 153 |
|
| 154 |
/****************************************************************************/ |
| 155 |
/*D |
| 156 |
ConvertUNIX_2_MachinePath - convert UNIX-style file paths to machine format |
| 157 |
|
| 158 |
SYNOPSIS: |
| 159 |
const char *ConvertUNIX_2_MachinePath (const char *fname) |
| 160 |
|
| 161 |
|
| 162 |
PARAMETERS: |
| 163 |
. fname - filename with path convention in UNIX-style |
| 164 |
|
| 165 |
DESCRIPTION: |
| 166 |
Convert UNIX-style paths to machine format. If '__MACINTOSH__' |
| 167 |
is defined (Apple Macintosh computer) the UNIX-sytle path is converted to |
| 168 |
Macintosh-style. |
| 169 |
|
| 170 |
For other platforms 'ConvertUNIX_2_MachinePath' returns 'fname' (macro). |
| 171 |
|
| 172 |
RETURN VALUE: |
| 173 |
char * |
| 174 |
.n converted file name (NULL if error) |
| 175 |
D*/ |
| 176 |
/****************************************************************************/ |
| 177 |
|
| 178 |
#ifdef __MACINTOSH__ |
| 179 |
|
| 180 |
/* Macintosh computers */ |
| 181 |
static const char *ConvertUNIX_2_MachinePath (const char *fname) |
| 182 |
{ |
| 183 |
static char fullpath[MAXPATHLENGTH]; |
| 184 |
int pos; |
| 185 |
|
| 186 |
if (*fname=='/') |
| 187 |
/* root not defined on Macintosh computers */ |
| 188 |
return (NULL); |
| 189 |
if (*fname=='~') |
| 190 |
/* home directory not defined on Macintosh computers */ |
| 191 |
return (NULL); |
| 192 |
|
| 193 |
/* something to convert? */ |
| 194 |
if (strchr(fname,'/')==NULL) |
| 195 |
return (fname); |
| 196 |
|
| 197 |
pos = 0; |
| 198 |
while ((*fname!='\0') && (pos<MAXPATHLENGTH-2)) |
| 199 |
switch (fname[0]) |
| 200 |
{ |
| 201 |
case '.' : |
| 202 |
if ((fname[1]=='.')&&(fname[2]=='/')) |
| 203 |
{ |
| 204 |
/* "../" */ |
| 205 |
|
| 206 |
/* if the path starts with "../" we interpret "./../", i.e. "::" */ |
| 207 |
if (pos==0) |
| 208 |
fullpath[pos++] = ':'; |
| 209 |
|
| 210 |
fullpath[pos++] = ':'; |
| 211 |
fname += 3; |
| 212 |
} |
| 213 |
else if (fname[1]=='/') |
| 214 |
{ |
| 215 |
if (pos) |
| 216 |
{ |
| 217 |
/* eat ./ */ |
| 218 |
fname += 2; |
| 219 |
continue; |
| 220 |
} |
| 221 |
|
| 222 |
/* "./" --> ":" */ |
| 223 |
fullpath[pos++] = ':'; |
| 224 |
fname += 2; |
| 225 |
} |
| 226 |
else |
| 227 |
fullpath[pos++] = *(fname++); |
| 228 |
break; |
| 229 |
|
| 230 |
case '/' : |
| 231 |
/* "/" --> ":" */ |
| 232 |
fullpath[pos++] = ':'; |
| 233 |
while (*fname=='/') fname++; |
| 234 |
break; |
| 235 |
|
| 236 |
default : |
| 237 |
fullpath[pos++] = *(fname++); |
| 238 |
} |
| 239 |
|
| 240 |
if (pos>=MAXPATHLENGTH) |
| 241 |
/* filename too long */ |
| 242 |
return (NULL); |
| 243 |
|
| 244 |
/* 0-terminate string */ |
| 245 |
fullpath[pos] = '\0'; |
| 246 |
|
| 247 |
return (fullpath); |
| 248 |
} |
| 249 |
|
| 250 |
static const char* ConvertMachine_2_UNIXPath (const char* fname) |
| 251 |
{ |
| 252 |
static char fullpath[MAXPATHLENGTH]; |
| 253 |
int pos; |
| 254 |
|
| 255 |
/* something to convert? */ |
| 256 |
if (strchr(fname,':')==NULL) |
| 257 |
return (fname); |
| 258 |
|
| 259 |
pos = 0; |
| 260 |
while ((*fname!='\0') && (pos<MAXPATHLENGTH-2)) |
| 261 |
switch (fname[0]) |
| 262 |
{ |
| 263 |
case ':' : |
| 264 |
if (fname[1]==':') |
| 265 |
{ |
| 266 |
/* "::" */ |
| 267 |
|
| 268 |
fullpath[pos++] = '.'; |
| 269 |
fullpath[pos++] = '.'; |
| 270 |
fullpath[pos++] = '/'; |
| 271 |
fname++; |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
/* ":" */ |
| 276 |
if (fullpath[pos]!='/') |
| 277 |
fullpath[pos++] = '/'; |
| 278 |
fname++; |
| 279 |
} |
| 280 |
break; |
| 281 |
|
| 282 |
default : |
| 283 |
fullpath[pos++] = *(fname++); |
| 284 |
} |
| 285 |
|
| 286 |
if (pos>=MAXPATHLENGTH) |
| 287 |
/* filename too long */ |
| 288 |
return (NULL); |
| 289 |
|
| 290 |
/* 0-terminate string */ |
| 291 |
fullpath[pos] = '\0'; |
| 292 |
|
| 293 |
return (fullpath); |
| 294 |
} |
| 295 |
|
| 296 |
#endif |
| 297 |
|
| 298 |
const char * NS_PREFIX BasedConvertedFilename (const char *fname) |
| 299 |
/* NOTE: once a filename has passed through BasedConvertedFilename() it is forbidden to |
| 300 |
call BasedConvertedFilename() a second time for this filename due to |
| 301 |
static result string. |
| 302 |
*/ |
| 303 |
{ |
| 304 |
PRINTDEBUG(low,2,("BasedConvertedFilename: fname= '%s'\n",fname)); |
| 305 |
if (fname[0]!='/' && fname[0]!='~') /* use BasePath only if no absolute path specified */ |
| 306 |
{ |
| 307 |
static char based_filename[MAXPATHLENGTH]; |
| 308 |
|
| 309 |
assert(fname!=based_filename); /* avoid that the result of a previous call to BasedConvertedFilename() is fed back; |
| 310 |
in this case the new result would interfere with the given input name and corrupt the result |
| 311 |
(for example only ./ would be returned)*/ |
| 312 |
strcpy(based_filename,BasePath); |
| 313 |
strcat(based_filename,fname); |
| 314 |
SimplifyPath(based_filename); |
| 315 |
PRINTDEBUG(low,1,("BasedConvertedFilename: based_filename= '%s'\n",based_filename)); |
| 316 |
return ConvertUNIX_2_MachinePath(based_filename); |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
PRINTDEBUG(low,1,("BasedConvertedFilename: filename not based= '%s'\n",fname)); |
| 321 |
return ConvertUNIX_2_MachinePath(fname); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
static int rename_if_necessary( const char *fname, int do_rename) |
| 326 |
{ |
| 327 |
FILE *f; |
| 328 |
|
| 329 |
if (do_rename && (f=fopen(fname,"r"))!=NULL) |
| 330 |
{ |
| 331 |
time_t Time; |
| 332 |
struct stat fstat; |
| 333 |
char new_fname[128]; |
| 334 |
|
| 335 |
fclose(f); |
| 336 |
|
| 337 |
strcpy(new_fname,fname); |
| 338 |
strcat(new_fname,"."); |
| 339 |
|
| 340 |
if (stat(fname, &fstat)<0) |
| 341 |
return(1); |
| 342 |
Time = fstat.st_mtime; |
| 343 |
strftime(new_fname+strlen(fname)+1,64,"%y%m%d%H%M%S",localtime(&Time)); |
| 344 |
|
| 345 |
if (rename(fname,new_fname)!=0) |
| 346 |
return(1); |
| 347 |
} |
| 348 |
return (0); /* ok */ |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
/****************************************************************************/ |
| 353 |
/*D |
| 354 |
mkdir_r - create a directory incl. renaming option |
| 355 |
|
| 356 |
SYNOPSIS: |
| 357 |
int mkdir_r (const char *fname, mode_t mode, int do_rename) |
| 358 |
|
| 359 |
PARAMETERS: |
| 360 |
. fname - directory name with path convention in UNIX-style; may not be passed through BasedConvertedFilename() |
| 361 |
. mode - creation mode in UNIX-style (see mkdir(2)) |
| 362 |
. do_rename - if true an already existing subdirectory will be renamed |
| 363 |
|
| 364 |
DESCRIPTION: |
| 365 |
This function creates an directory and renames an already existing one |
| 366 |
instead of overwriting it if specified. |
| 367 |
fname may not be passed through BasedConvertedFilename(). |
| 368 |
|
| 369 |
RETURN VALUE: |
| 370 |
int |
| 371 |
.n 0 successful completion |
| 372 |
.n != 0 error occurred |
| 373 |
|
| 374 |
SEE ALSO: |
| 375 |
mkdir(2), fopen_r |
| 376 |
D*/ |
| 377 |
/****************************************************************************/ |
| 378 |
|
| 379 |
int NS_PREFIX mkdir_r (const char *fname, std::filesystem::perms mode, int do_rename) |
| 380 |
{ |
| 381 |
const char *converted_name = BasedConvertedFilename(fname); |
| 382 |
|
| 383 |
std::error_code ec; |
| 384 |
if (do_rename) |
| 385 |
{ |
| 386 |
if (rename_if_necessary( converted_name, do_rename)!=0) |
| 387 |
return (1); |
| 388 |
if (std::filesystem::create_directory(converted_name, ec)) |
| 389 |
std::filesystem::permissions(converted_name, mode, ec); |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
switch (filetype(fname)) /* filetype needs an NOT BasedConvertedFilename'ed filename */ |
| 394 |
{ |
| 395 |
case FT_UNKNOWN : /* file doesn't exist, thus create it */ |
| 396 |
if (std::filesystem::create_directory(converted_name, ec)) |
| 397 |
std::filesystem::permissions(converted_name, mode, ec); |
| 398 |
|
| 399 |
case FT_DIR : |
| 400 |
return ec.value(); /* OK, directory exists already */ |
| 401 |
|
| 402 |
case FT_FILE : |
| 403 |
UserWriteF("mkdir_r(): file %s exists already as ordinary file; can't create directory with same name.\n",converted_name); |
| 404 |
return 1; |
| 405 |
|
| 406 |
case FT_LINK : |
| 407 |
UserWriteF("mkdir_r(): file %s exists already as a link; can't create directory with same name.\n",converted_name); |
| 408 |
return 1; |
| 409 |
|
| 410 |
default : |
| 411 |
UserWriteF("mkdir_r(): unknown file type %d for file %s\n",filetype(fname),converted_name); |
| 412 |
return 1; |
| 413 |
} |
| 414 |
} |
| 415 |
return ec.value(); |
| 416 |
} |
| 417 |
|
| 418 |
/****************************************************************************/ |
| 419 |
/*D |
| 420 |
fopen_r - create a file incl. renaming option |
| 421 |
|
| 422 |
SYNOPSIS: |
| 423 |
int fopen_r (const char *fname, const char *mode, int do_rename) |
| 424 |
|
| 425 |
PARAMETERS: |
| 426 |
. fname - file name with path convention in UNIX-style |
| 427 |
. mode - file opening mode in UNIX-style (see fopen(2)) |
| 428 |
. do_rename - if true an already existing file will be renamed |
| 429 |
|
| 430 |
DESCRIPTION: |
| 431 |
This function opens a file and renames an already existing one |
| 432 |
instead of overwriting it if specified. |
| 433 |
|
| 434 |
RETURN VALUE: |
| 435 |
int |
| 436 |
.n 0 successful completion |
| 437 |
.n != 0 error occurred |
| 438 |
|
| 439 |
SEE ALSO: |
| 440 |
fopen(2), mkdir_r |
| 441 |
D*/ |
| 442 |
/****************************************************************************/ |
| 443 |
|
| 444 |
FILE * NS_PREFIX fopen_r (const char *fname, const char *mode, int do_rename) |
| 445 |
{ |
| 446 |
if (rename_if_necessary( fname, do_rename)!=0) |
| 447 |
return (NULL); |
| 448 |
|
| 449 |
return fopen(fname,mode); |
| 450 |
} |
| 451 |
|
| 452 |
/****************************************************************************/ |
| 453 |
/*D |
| 454 |
filetype - get type of a file with given name |
| 455 |
|
| 456 |
SYNOPSIS: |
| 457 |
int filetype (const char *fname) |
| 458 |
|
| 459 |
PARAMETERS: |
| 460 |
. fname - filename with path convention in UNIX-style; it may not be passed through BasedConvertedFilename() |
| 461 |
|
| 462 |
DESCRIPTION: |
| 463 |
This function returns the type of the given file |
| 464 |
or FT_UNKNOWN if an error occurs. |
| 465 |
fname may not be passed through BasedConvertedFilename(). |
| 466 |
|
| 467 |
RETURN VALUE: |
| 468 |
int |
| 469 |
.n file type (one of FT_UNKNOWN, FT_FILE, FT_DIR, FT_LINK) |
| 470 |
|
| 471 |
SEE ALSO: |
| 472 |
fopen, fileopen |
| 473 |
D*/ |
| 474 |
/****************************************************************************/ |
| 475 |
|
| 476 |
int NS_PREFIX filetype (const char *fname) |
| 477 |
{ |
| 478 |
struct stat fstat; |
| 479 |
int r; |
| 480 |
|
| 481 |
/* get Unix file descriptor */ |
| 482 |
PRINTDEBUG(low,1,("filetype\n")); |
| 483 |
if ((r=stat(BasedConvertedFilename(fname), &fstat))<0) |
| 484 |
return(FT_UNKNOWN); |
| 485 |
|
| 486 |
#ifdef __CC__ |
| 487 |
switch (fstat.st_mode & _S_IFMT) |
| 488 |
{ |
| 489 |
case _S_IFREG : return FT_FILE; |
| 490 |
case _S_IFDIR : return FT_DIR; |
| 491 |
#ifdef S_IFLNK |
| 492 |
case _S_IFLNK : return FT_LINK; |
| 493 |
#endif |
| 494 |
} |
| 495 |
#else |
| 496 |
switch (fstat.st_mode & S_IFMT) |
| 497 |
{ |
| 498 |
case S_IFREG : return FT_FILE; |
| 499 |
case S_IFDIR : return FT_DIR; |
| 500 |
#ifdef S_IFLNK |
| 501 |
case S_IFLNK : return FT_LINK; |
| 502 |
#endif |
| 503 |
} |
| 504 |
#endif |
| 505 |
return(FT_UNKNOWN); |
| 506 |
} |
| 507 |
|
| 508 |
/****************************************************************************/ |
| 509 |
/*D |
| 510 |
ReadSearchingPaths - read searching paths from a defaults file |
| 511 |
|
| 512 |
SYNOPSIS: |
| 513 |
INT ReadSearchingPaths (const char *filename, const char *paths) |
| 514 |
|
| 515 |
PARAMETERS: |
| 516 |
. filename - search paths in a defaults file with this name (most likely to be |
| 517 |
`the` --> 'defaults' file, use 'DEFAULTSFILENAME') |
| 518 |
. paths - name of the paths item looked for in a defaults file |
| 519 |
|
| 520 |
DESCRIPTION: |
| 521 |
From a defaultsfile using --> 'GetDefaultValue' the specified paths item |
| 522 |
is read containing one or more paths sperated by blanks (tab or space). |
| 523 |
The paths are stored in an environment item with that same name in the |
| 524 |
environment directory '/Paths'. The function --> 'FileOpenUsingSearchPaths' |
| 525 |
is looking up the paths to be tried there. |
| 526 |
|
| 527 |
RETURN VALUE: |
| 528 |
INT |
| 529 |
.n 1: failed to 'GetDefaultValue' |
| 530 |
.n 2: more than 'MAXPATHS' specified in the defaults file |
| 531 |
.n 3: failed to 'MakePathsItem' |
| 532 |
.n 0: ok |
| 533 |
|
| 534 |
SEE ALSO: |
| 535 |
FileOpenUsingSearchPaths |
| 536 |
D*/ |
| 537 |
/****************************************************************************/ |
| 538 |
|
| 539 |
INT NS_PREFIX ReadSearchingPaths (const char *filename, const char *paths) |
| 540 |
{ |
| 541 |
return (1); |
| 542 |
} |
| 543 |
|
| 544 |
/****************************************************************************/ |
| 545 |
/*D |
| 546 |
DirCreateUsingSearchPaths - create a directory searching in the directories specified |
| 547 |
in the environment item '/Paths/<paths>' |
| 548 |
|
| 549 |
SYNOPSIS: |
| 550 |
int DirCreateUsingSearchPaths (const char *fname, const char *paths); |
| 551 |
|
| 552 |
PARAMETERS: |
| 553 |
. fname - subdirectory name to be created |
| 554 |
. paths - try paths specified in the environment item '/Paths/<paths>' which was |
| 555 |
set by --> 'ReadSearchingPaths' |
| 556 |
|
| 557 |
DESCRIPTION: |
| 558 |
The functions tries to create a directory with 'filename' using one by one the |
| 559 |
paths specified in the environment item '/Paths/<paths>' which was |
| 560 |
set by --> 'ReadSearchingPaths'. It is used in several places in ug (all paths |
| 561 |
are read from the standard --> 'defaults' file)":" |
| 562 |
|
| 563 |
.n 'scriptpaths' is used by the interpreter for script execution |
| 564 |
.n 'gridpaths' is used by ugio to read grids from (they are stored in the |
| 565 |
.n first path |
| 566 |
|
| 567 |
RETURN VALUE: |
| 568 |
int |
| 569 |
.n 0 successful completion |
| 570 |
.n != 0 error occurred |
| 571 |
|
| 572 |
SEE ALSO: |
| 573 |
DirCreateUsingSearchPaths_r, mkdir(2) |
| 574 |
D*/ |
| 575 |
/****************************************************************************/ |
| 576 |
|
| 577 |
int NS_PREFIX DirCreateUsingSearchPaths (const char *fname, const char *paths) |
| 578 |
{ |
| 579 |
return DirCreateUsingSearchPaths_r ( fname, paths, false); /* no renaming */ |
| 580 |
} |
| 581 |
|
| 582 |
/****************************************************************************/ |
| 583 |
/*D |
| 584 |
DirCreateUsingSearchPaths_r - create a subdirectory searching in the directories specified |
| 585 |
in the environment item '/Paths/<paths>' incl. renaming option |
| 586 |
|
| 587 |
SYNOPSIS: |
| 588 |
int DirCreateUsingSearchPaths (const char *fname, const char *paths, int rename); |
| 589 |
|
| 590 |
PARAMETERS: |
| 591 |
. fname - subdirectory name to be created |
| 592 |
. paths - try paths specified in the environment item '/Paths/<paths>' which was |
| 593 |
set by --> 'ReadSearchingPaths' |
| 594 |
. rename - if true an already existing subdirectory will be renamed |
| 595 |
|
| 596 |
DESCRIPTION: |
| 597 |
The functions tries to create a subdirectory with 'filename' using one by one the |
| 598 |
paths specified in the environment item '/Paths/<paths>' which was |
| 599 |
set by --> 'ReadSearchingPaths'. It is used in several places in ug (all paths |
| 600 |
are read from the standard --> 'defaults' file)":" |
| 601 |
|
| 602 |
.n 'srciptpaths' is used by the interpreter for script execution |
| 603 |
.n 'gridpaths' is used by ugio to read grids from (they are stored in the |
| 604 |
.n first path |
| 605 |
|
| 606 |
RETURN VALUE: |
| 607 |
int |
| 608 |
.n 0 successful completion |
| 609 |
.n != 0 error occurred |
| 610 |
|
| 611 |
SEE ALSO: |
| 612 |
DirCreateUsingSearchPaths, mkdir(2) |
| 613 |
D*/ |
| 614 |
/****************************************************************************/ |
| 615 |
|
| 616 |
int NS_PREFIX DirCreateUsingSearchPaths_r (const char *fname, const char *paths, int rename) |
| 617 |
{ |
| 618 |
INT fnamelen = strlen(fname); |
| 619 |
using perms = std::filesystem::perms; |
| 620 |
perms mode = perms::owner_write | perms::owner_read | perms::owner_exec | perms::group_read | perms::group_exec; |
| 621 |
|
| 622 |
PRINTDEBUG(low,1,("DirCreateUsingSearchPaths\n")); |
| 623 |
if (paths == nullptr) |
| 624 |
{ |
| 625 |
if (mkdir_r(fname, mode, rename) != 0) |
| 626 |
return (1); |
| 627 |
return (0); |
| 628 |
} |
| 629 |
|
| 630 |
const PATHS *thePaths = GetPaths(paths); |
| 631 |
if (thePaths == nullptr) |
| 632 |
return (1); |
| 633 |
|
| 634 |
for (INT i = 0; i < thePaths->nPaths; i++) |
| 635 |
{ |
| 636 |
/* test whether parent directory exists */ |
| 637 |
FILE *parentDir = fopen(thePaths->path[i],"r"); |
| 638 |
if (parentDir == nullptr) |
| 639 |
continue; /* this parent directory doesn't exist; try the next one */ |
| 640 |
if (fclose(parentDir) != 0) |
| 641 |
return (1); |
| 642 |
|
| 643 |
if (strlen(thePaths->path[i])+fnamelen>MAXPATHLENGTH) |
| 644 |
return (1); |
| 645 |
|
| 646 |
char fullname[MAXPATHLENGTH]; |
| 647 |
strcpy(fullname,thePaths->path[i]); |
| 648 |
strcat(fullname,fname); |
| 649 |
|
| 650 |
if (mkdir_r(fullname,mode,rename) != 0) |
| 651 |
return (1); |
| 652 |
return (0); /* subdirectory created successfully */ |
| 653 |
} |
| 654 |
return (1); |
| 655 |
} |
| 656 |
|
| 657 |
/****************************************************************************/ |
| 658 |
/*D |
| 659 |
FileOpenUsingSearchPaths - open file searching in the directories specified |
| 660 |
in the environment item '/Paths/<paths>' |
| 661 |
|
| 662 |
SYNOPSIS: |
| 663 |
FILE *FileOpenUsingSearchPaths (const char *fname, const char *mode, const char *paths) |
| 664 |
|
| 665 |
PARAMETERS: |
| 666 |
. fname - file name to be opened |
| 667 |
. mode - see ANSI-C 'fopen' |
| 668 |
. paths - try paths specified in the environment item '/Paths/<paths>' which was |
| 669 |
set by --> 'ReadSearchingPaths' |
| 670 |
|
| 671 |
DESCRIPTION: |
| 672 |
The functions tries to open the file with 'filename' using one by one the |
| 673 |
paths specified in the environment item '/Paths/<paths>' which was |
| 674 |
set by --> 'ReadSearchingPaths'. It is used in several places in ug (all paths |
| 675 |
are read from the standard --> 'defaults' file)":" |
| 676 |
|
| 677 |
.n 'srciptpaths' is used by the interpreter for script execution |
| 678 |
.n 'gridpaths' is used by ugio to read grids from (they are stored in the |
| 679 |
.n first path |
| 680 |
|
| 681 |
RETURN VALUE: |
| 682 |
FILE * |
| 683 |
.n pointer to file opened, 'NULL' if error |
| 684 |
|
| 685 |
SEE ALSO: |
| 686 |
FileOpenUsingSearchPaths_r, FileOpenUsingSearchPath, FileOpenUsingSearchPath_r, GetPaths, ReadSearchingPaths, fileopen |
| 687 |
D*/ |
| 688 |
/****************************************************************************/ |
| 689 |
|
| 690 |
FILE * NS_PREFIX FileOpenUsingSearchPaths (const char *fname, const char *mode, const char *paths) |
| 691 |
{ |
| 692 |
return FileOpenUsingSearchPaths_r( fname, mode, paths, false ); /* no renaming */ |
| 693 |
} |
| 694 |
|
| 695 |
/****************************************************************************/ |
| 696 |
/*D |
| 697 |
FileOpenUsingSearchPaths_r - open file searching in the directories specified |
| 698 |
in the environment item '/Paths/<paths>' incl. renaming option |
| 699 |
|
| 700 |
SYNOPSIS: |
| 701 |
FILE *FileOpenUsingSearchPaths_r (const char *fname, const char *mode, const char *paths, int rename) |
| 702 |
|
| 703 |
PARAMETERS: |
| 704 |
. fname - file name to be opened |
| 705 |
. mode - see ANSI-C 'fopen' |
| 706 |
. paths - try paths specified in the environment item '/Paths/<paths>' which was |
| 707 |
set by --> 'ReadSearchingPaths' |
| 708 |
. rename - if true an already existing file will be renamed (wise only for writing) |
| 709 |
|
| 710 |
DESCRIPTION: |
| 711 |
The functions tries to open the file with 'filename' using one by one the |
| 712 |
paths specified in the environment item '/Paths/<paths>' which was |
| 713 |
set by --> 'ReadSearchingPaths'. It is used in several places in ug (all paths |
| 714 |
are read from the standard --> 'defaults' file)":" |
| 715 |
|
| 716 |
.n 'srciptpaths' is used by the interpreter for script execution |
| 717 |
.n 'gridpaths' is used by ugio to read grids from (they are stored in the |
| 718 |
.n first path |
| 719 |
|
| 720 |
RETURN VALUE: |
| 721 |
FILE * |
| 722 |
.n pointer to file opened, 'NULL' if error |
| 723 |
|
| 724 |
SEE ALSO: |
| 725 |
FileOpenUsingSearchPaths, FileOpenUsingSearchPath, FileOpenUsingSearchPath_r, GetPaths, ReadSearchingPaths, fileopen |
| 726 |
D*/ |
| 727 |
/****************************************************************************/ |
| 728 |
|
| 729 |
FILE * NS_PREFIX FileOpenUsingSearchPaths_r (const char *fname, const char *mode, const char *paths, int rename) |
| 730 |
{ |
| 731 |
INT fnamelen = strlen(fname); |
| 732 |
|
| 733 |
const PATHS *thePaths = GetPaths(paths); |
| 734 |
if (thePaths == nullptr) |
| 735 |
return nullptr; |
| 736 |
|
| 737 |
for (INT i = 0; i < thePaths->nPaths; i++) |
| 738 |
{ |
| 739 |
if (strlen(thePaths->path[i])+fnamelen>MAXPATHLENGTH) |
| 740 |
return nullptr; |
| 741 |
|
| 742 |
char fullname[MAXPATHLENGTH]; |
| 743 |
strcpy(fullname,thePaths->path[i]); |
| 744 |
strcat(fullname,fname); |
| 745 |
|
| 746 |
FILE *theFile = fileopen_r(fullname,mode,rename); |
| 747 |
if (theFile != nullptr) |
| 748 |
return theFile; |
| 749 |
} |
| 750 |
|
| 751 |
return nullptr; |
| 752 |
} |
| 753 |
|
| 754 |
/****************************************************************************/ |
| 755 |
/*D |
| 756 |
FileOpenUsingSearchPath - try to open a file in the specified path |
| 757 |
|
| 758 |
SYNOPSIS: |
| 759 |
FILE *FileOpenUsingSearchPath (const char *fname, const char *mode, const char *path) |
| 760 |
|
| 761 |
PARAMETERS: |
| 762 |
. fname - open file with this name |
| 763 |
. mode - see ANSI-C 'fopen' |
| 764 |
. path - path to which fname is to be appended |
| 765 |
|
| 766 |
DESCRIPTION: |
| 767 |
Try to open a file in the specified path. |
| 768 |
|
| 769 |
RETURN VALUE: |
| 770 |
FILE * |
| 771 |
.n pointer to file opened, 'NULL' if error |
| 772 |
|
| 773 |
SEE ALSO: |
| 774 |
FileOpenUsingSearchPath_r, FileOpenUsingSearchPaths, FileOpenUsingSearchPaths_r, fileopen |
| 775 |
D*/ |
| 776 |
/****************************************************************************/ |
| 777 |
|
| 778 |
FILE * NS_PREFIX FileOpenUsingSearchPath (const char *fname, const char *mode, const char *path) |
| 779 |
{ |
| 780 |
return FileOpenUsingSearchPath_r( fname, mode, path, false ); /* no renaming */ |
| 781 |
} |
| 782 |
|
| 783 |
/****************************************************************************/ |
| 784 |
/*D |
| 785 |
FileOpenUsingSearchPath_r - try to open a file in the specified path and renaming option |
| 786 |
|
| 787 |
SYNOPSIS: |
| 788 |
FILE *FileOpenUsingSearchPath_r (const char *fname, const char *mode, const char *path, int rename) |
| 789 |
|
| 790 |
PARAMETERS: |
| 791 |
. fname - open file with this name |
| 792 |
. mode - see ANSI-C 'fopen' |
| 793 |
. path - path to which fname is to be appended |
| 794 |
. rename - if true an already existing file will be renamed (wise only for writing) |
| 795 |
|
| 796 |
DESCRIPTION: |
| 797 |
Try to open a file in the specified path. |
| 798 |
|
| 799 |
RETURN VALUE: |
| 800 |
FILE * |
| 801 |
.n pointer to file opened, 'NULL' if error |
| 802 |
|
| 803 |
SEE ALSO: |
| 804 |
FileOpenUsingSearchPath, FileOpenUsingSearchPaths, FileOpenUsingSearchPaths_r, fileopen |
| 805 |
D*/ |
| 806 |
/****************************************************************************/ |
| 807 |
|
| 808 |
FILE * NS_PREFIX FileOpenUsingSearchPath_r (const char *fname, const char *mode, const char *path, int rename) |
| 809 |
{ |
| 810 |
FILE *theFile; |
| 811 |
char fullname[MAXPATHLENGTH]; |
| 812 |
|
| 813 |
if (strlen(path)+strlen(fname)>MAXPATHLENGTH) |
| 814 |
return (NULL); |
| 815 |
|
| 816 |
strcpy(fullname,path); |
| 817 |
strcat(fullname,fname); |
| 818 |
|
| 819 |
if ((theFile=fileopen_r(fullname,mode,rename))!=NULL) |
| 820 |
return (theFile); |
| 821 |
|
| 822 |
return (NULL); |
| 823 |
} |
| 824 |
|
| 825 |
/****************************************************************************/ |
| 826 |
/*D |
| 827 |
FileTypeUsingSearchPaths - give type of file searching in the |
| 828 |
directories specified in the environment item '/Paths/<paths>' |
| 829 |
|
| 830 |
SYNOPSIS: |
| 831 |
int FileTypeUsingSearchPaths (const char *fname, const char *paths) |
| 832 |
|
| 833 |
PARAMETERS: |
| 834 |
. fname - file name to be opened |
| 835 |
. paths - try paths specified in the environment item '/Paths/<paths>' which was |
| 836 |
set by --> 'ReadSearchingPaths' |
| 837 |
|
| 838 |
DESCRIPTION: |
| 839 |
The functions tries to determine the file type of the file named |
| 840 |
'filename' using one by one the paths specified in the environment |
| 841 |
item '/Paths/<paths>' which was set by --> 'ReadSearchingPaths'. |
| 842 |
It is used in several places in ug (all paths are read from the |
| 843 |
standard --> 'defaults' file)":" |
| 844 |
|
| 845 |
.n 'srciptpaths' is used by the interpreter for script execution |
| 846 |
.n 'gridpaths' is used by ugio to read grids from (they are stored in the |
| 847 |
.n first path) |
| 848 |
|
| 849 |
RETURN VALUE: |
| 850 |
int |
| 851 |
.n file type (one of FT_UNKNOWN, FT_FILE, FT_DIR, FT_LINK) |
| 852 |
|
| 853 |
SEE ALSO: |
| 854 |
ReadSearchingPaths, filetype |
| 855 |
D*/ |
| 856 |
/****************************************************************************/ |
| 857 |
|
| 858 |
int NS_PREFIX FileTypeUsingSearchPaths (const char *fname, const char *paths) |
| 859 |
{ |
| 860 |
INT fnamelen = strlen(fname); |
| 861 |
|
| 862 |
const PATHS *thePaths = GetPaths(paths); |
| 863 |
if (thePaths == nullptr) |
| 864 |
return (FT_UNKNOWN); |
| 865 |
|
| 866 |
for (INT i = 0; i < thePaths->nPaths; i++) |
| 867 |
{ |
| 868 |
if (strlen(thePaths->path[i])+fnamelen>MAXPATHLENGTH) |
| 869 |
return (FT_UNKNOWN); |
| 870 |
|
| 871 |
char fullname[MAXPATHLENGTH]; |
| 872 |
strcpy(fullname,thePaths->path[i]); |
| 873 |
strcat(fullname,fname); |
| 874 |
|
| 875 |
int ftype = filetype(fullname); |
| 876 |
if (ftype != FT_UNKNOWN) |
| 877 |
return (ftype); |
| 878 |
} |
| 879 |
|
| 880 |
return (FT_UNKNOWN); |
| 881 |
} |
| 882 |
|
| 883 |
bool NS_PREFIX AppendTrailingSlash (char *path) |
| 884 |
{ |
| 885 |
if (path[0]!='\0' && path[strlen(path)-1]!='/') |
| 886 |
{ |
| 887 |
strcat(path,"/"); |
| 888 |
return true; |
| 889 |
} |
| 890 |
return false; |
| 891 |
} |
| 892 |
|
| 893 |
char * NS_PREFIX SimplifyPath (char *path) |
| 894 |
{ |
| 895 |
const char *pf; |
| 896 |
char *pt; |
| 897 |
|
| 898 |
PRINTDEBUG(low,2,("SimplifyPath: original path= '%s'\n",path)); |
| 899 |
|
| 900 |
/* cancel ./ (not first one) */ |
| 901 |
pf = pt = strchr(path,'/'); |
| 902 |
if (pf!=NULL) |
| 903 |
{ |
| 904 |
while (*pf) |
| 905 |
{ |
| 906 |
if (pf[0]=='.' && pf[1]=='/') |
| 907 |
if (*(pf-1)=='/') |
| 908 |
{ |
| 909 |
/* eat ./ */ |
| 910 |
pf += 2; |
| 911 |
continue; |
| 912 |
} |
| 913 |
if (pt!=pf) |
| 914 |
*pt = *pf; |
| 915 |
pf++; |
| 916 |
pt++; |
| 917 |
} |
| 918 |
*pt = '\0'; |
| 919 |
} |
| 920 |
|
| 921 |
PRINTDEBUG(low,2,("SimplifyPath: path= '%s'\n",path)); |
| 922 |
|
| 923 |
/* cancel ../ where possible */ |
| 924 |
pf = pt = path; |
| 925 |
for (; *pf; pf++,pt++) |
| 926 |
{ |
| 927 |
if (pf[0]=='.' && pf[1]=='.' && pf[2]=='/') |
| 928 |
if (pf==path || *(pf-1)=='/') |
| 929 |
{ |
| 930 |
char *pd = pt-1; |
| 931 |
|
| 932 |
while (pd>path) |
| 933 |
if (*(--pd)=='/') |
| 934 |
break; |
| 935 |
if (*pd=='/' && !(pd[0]=='/' && pd[1]=='.' && pd[2]=='.' && pd[3]=='/')) |
| 936 |
{ |
| 937 |
/* eat ../ and reset pt */ |
| 938 |
pf += 2; |
| 939 |
pt = pd; |
| 940 |
continue; |
| 941 |
} |
| 942 |
} |
| 943 |
*pt = *pf; |
| 944 |
} |
| 945 |
*pt = '\0'; |
| 946 |
|
| 947 |
return path; |
| 948 |
} |
| 949 |
|
| 950 |
/****************************************************************************/ |
| 951 |
/*D |
| 952 |
InitFileOpen - init 'fileopen.c' |
| 953 |
|
| 954 |
SYNOPSIS: |
| 955 |
INT InitFileOpen () |
| 956 |
|
| 957 |
PARAMETERS: |
| 958 |
-- |
| 959 |
|
| 960 |
DESCRIPTION: |
| 961 |
An environment directory '/Paths' is created where the paths read by |
| 962 |
'ReadSearchingPaths' are stored. |
| 963 |
|
| 964 |
RETURN VALUE: |
| 965 |
INT |
| 966 |
.n __LINE__: could not create '/Paths' |
| 967 |
.n 0: ok |
| 968 |
D*/ |
| 969 |
/****************************************************************************/ |
| 970 |
|
| 971 |
INT NS_PREFIX InitFileOpen () |
| 972 |
{ |
| 973 |
/* install the /Paths directory */ |
| 974 |
if (ChangeEnvDir("/")==NULL) |
| 975 |
return(__LINE__); |
| 976 |
|
| 977 |
thePathsDirID = GetNewEnvDirID(); |
| 978 |
if (MakeEnvItem("Paths",thePathsDirID,sizeof(ENVDIR))==NULL) |
| 979 |
return(__LINE__); |
| 980 |
|
| 981 |
thePathsVarID = GetNewEnvVarID(); |
| 982 |
|
| 983 |
return (0); |
| 984 |
} |