| 1 |
/*--------------------------------------------------------------------- |
| 2 |
* $Date: 2009-08-18 15:23:20 $ $Revision: 1.3 $ |
| 3 |
*--------------------------------------------------------------------- |
| 4 |
* |
| 5 |
* |
| 6 |
* Copyright (c) 1990, Visual Edge Software Ltd. |
| 7 |
* |
| 8 |
* ALL RIGHTS RESERVED. Permission to use, copy, modify, and |
| 9 |
* distribute this software and its documentation for any purpose |
| 10 |
* and without fee is hereby granted, provided that the above |
| 11 |
* copyright notice appear in all copies and that both that |
| 12 |
* copyright notice and this permission notice appear in supporting |
| 13 |
* documentation, and that the name of Visual Edge Software not be |
| 14 |
* used in advertising or publicity pertaining to distribution of |
| 15 |
* the software without specific, written prior permission. The year |
| 16 |
* included in the notice is the year of the creation of the work. |
| 17 |
|
| 18 |
.VERSION |
| 19 |
090813 last modif |
| 20 |
|
| 21 |
*-------------------------------------------------------------------*/ |
| 22 |
# define _XOPEN_SOURCE |
| 23 |
# define _XOPEN_SOURCE_EXTENDED |
| 24 |
|
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <ctype.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
#include <pwd.h> |
| 34 |
#include <X11/X.h> |
| 35 |
#include <X11/Xlib.h> |
| 36 |
#include <X11/Xos.h> |
| 37 |
#include <X11/Intrinsic.h> |
| 38 |
|
| 39 |
#if defined(sco) || defined(motor88) || defined(ncrix86) || defined(linux) || defined(vms) |
| 40 |
# include <unistd.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(vms) |
| 44 |
# include <limits.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "pathlist.h" |
| 48 |
#include "valloc.h" |
| 49 |
#include "global.h" |
| 50 |
#include "vtypes.h" |
| 51 |
#ifdef MOTIF_WIDGETS |
| 52 |
#include <sys/param.h> /* for MAXPATHLEN... */ |
| 53 |
#ifndef MAXPATHLEN |
| 54 |
#define MAXPATHLEN 1024 |
| 55 |
#endif /* MAXPATHLEN */ |
| 56 |
#endif /* MOTIF_WIDGETS */ |
| 57 |
|
| 58 |
/* header files needed for messages */ |
| 59 |
#include "uimx_cat.h" |
| 60 |
#include "misc1_ds.h" |
| 61 |
|
| 62 |
#define CGETS_MISC(ms,ds_ms) UxCatGets(MC_MISC,ms,ds_ms) |
| 63 |
#define GETMSG(ms) UXCATGETS(MC_MISC1,MS_MISC1_,ms) |
| 64 |
|
| 65 |
|
| 66 |
/**** This define is the maximum number of characters in a filename |
| 67 |
**** BEFORE the name is appended with '.rf' or '.c' |
| 68 |
****/ |
| 69 |
#define TRUNC_LENGTH 11 |
| 70 |
#define MAX_FILE_LEN 14 |
| 71 |
#define MAX_SUFFIX 10 |
| 72 |
|
| 73 |
extern char *getenv(); |
| 74 |
extern char *UxToLowerString(); |
| 75 |
char *UxGetDirName(); |
| 76 |
char *UxTruncateFilename(); |
| 77 |
|
| 78 |
extern int UxStrEqual(); |
| 79 |
extern void UxAppend_dstring(); |
| 80 |
|
| 81 |
|
| 82 |
/***************************************************************************** |
| 83 |
NAME: int UxCheckForInvalidCharacters (filename) |
| 84 |
|
| 85 |
INPUT: char *filename - filename to check |
| 86 |
|
| 87 |
RETURN: int - 0 = ok, 1 = there are invalid characters |
| 88 |
|
| 89 |
DESCRIPTION: Scans the filename entered by the user for invalid characters; |
| 90 |
also checks for '//', which is illegal. An empty filename is |
| 91 |
also an error. |
| 92 |
|
| 93 |
LAST REV: 10 April 1990 |
| 94 |
-- allow $, it's not invalid |
| 95 |
-----------------------------------------------------------------------------*/ |
| 96 |
|
| 97 |
int UxCheckForInvalidCharacters (filename) |
| 98 |
char *filename; |
| 99 |
{ |
| 100 |
|
| 101 |
char *Ux_strpbrk (); |
| 102 |
|
| 103 |
|
| 104 |
static char bad_set[] = "!@%^&*()=|\\`<>?\'\"[]{}; "; |
| 105 |
|
| 106 |
/* check for zero length */ |
| 107 |
if (strlen (filename) == 0) |
| 108 |
return 1; |
| 109 |
|
| 110 |
/* look for any occurence of bad characters */ |
| 111 |
if (Ux_strpbrk (filename, bad_set) != NULL) |
| 112 |
return 1; |
| 113 |
|
| 114 |
|
| 115 |
/* filename is ok (or Ux_strpbrk failed!) */ |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
/****************************************************************************** |
| 120 |
NAME: UxShouldTruncate () |
| 121 |
INPUT: |
| 122 |
OUTPUT: --- |
| 123 |
RETURN: int : TRUE / FALSE |
| 124 |
DESCRIPTION: Checks if the resource "truncateFilenames" is set to TRUE |
| 125 |
in which case we turn TRUE. |
| 126 |
EXT REFERENCES: --- |
| 127 |
EXT EFFECTS: --- |
| 128 |
LAST REV: October 9 1991 |
| 129 |
-----------------------------------------------------------------------------*/ |
| 130 |
int UxShouldTruncate() |
| 131 |
{ |
| 132 |
static int initialized = FALSE; |
| 133 |
static int truncate = FALSE; |
| 134 |
|
| 135 |
if ( initialized == FALSE ) |
| 136 |
{ |
| 137 |
char *UxGetUimxDefault(), *UxToLowerString(), *trunc_res; |
| 138 |
/***********************************************/ |
| 139 |
/* Find out if filenames need to be truncated */ |
| 140 |
/* by examining the truncateFilenames */ |
| 141 |
/* resource. */ |
| 142 |
/***********************************************/ |
| 143 |
|
| 144 |
initialized = TRUE; |
| 145 |
|
| 146 |
trunc_res = UxGetUimxDefault ("truncateFilenames", "false"); |
| 147 |
if (UxStrEqual ( UxToLowerString(trunc_res), "false")) |
| 148 |
truncate = FALSE; |
| 149 |
else |
| 150 |
truncate = TRUE; |
| 151 |
} |
| 152 |
return(truncate); |
| 153 |
} |
| 154 |
/****************************************************************************** |
| 155 |
NAME: UxTruncateFilename (fname, suffix_len) |
| 156 |
INPUT: char *fname : name to truncate |
| 157 |
int suffix_len : length of suffix to be added |
| 158 |
OUTPUT: --- |
| 159 |
RETURN: char * : truncated name |
| 160 |
DESCRIPTION: returns a filename |
| 161 |
with its last component truncated to not more than 14 |
| 162 |
characters including the possible suffix. |
| 163 |
NB! This function may modify the passed filename. |
| 164 |
EXT REFERENCES: --- |
| 165 |
EXT EFFECTS: --- |
| 166 |
LAST REV: 5 November 1990 IBM Beta1 |
| 167 |
-- made this function available to OLE and UIM/X. |
| 168 |
-- Also made the strcmp case insensitive by |
| 169 |
calling UxToLowerString() |
| 170 |
-----------------------------------------------------------------------------*/ |
| 171 |
|
| 172 |
char* UxTruncateName(fname, slen) |
| 173 |
char* fname; |
| 174 |
int slen; |
| 175 |
{ |
| 176 |
char *name; |
| 177 |
char str[MAX_FILE_LEN+1]; |
| 178 |
int len, tail_len, ncaps, ndigits; |
| 179 |
int max_len, max_tail; |
| 180 |
int i, j; |
| 181 |
|
| 182 |
/*----------------------------------------------------- |
| 183 |
* Get the file name itself, without path. |
| 184 |
*-----------------------------------------------------*/ |
| 185 |
|
| 186 |
if ((name = strrchr(fname, '/')) != NULL) { |
| 187 |
name++; |
| 188 |
} else { |
| 189 |
name = fname; |
| 190 |
} |
| 191 |
|
| 192 |
/*----------------------------------------------------- |
| 193 |
* Is name part too long? |
| 194 |
*-----------------------------------------------------*/ |
| 195 |
|
| 196 |
len= strlen(name); |
| 197 |
if ((len + slen) <= MAX_FILE_LEN) { |
| 198 |
return fname; |
| 199 |
} |
| 200 |
|
| 201 |
/*------------------------------------------------------- |
| 202 |
* We shorten the name by sliding digits and capitals |
| 203 |
* from the whole tail of the original name |
| 204 |
* into the last max_tail characters of the shorter name. |
| 205 |
* Count digits and caps so we can compute the tail length. |
| 206 |
* Keep counting until we've found all the digits we |
| 207 |
* can use; we'll drop caps in favour of digits. |
| 208 |
* 'max_len-max_tail' is the first char allowed to change. |
| 209 |
*-------------------------------------------------------*/ |
| 210 |
|
| 211 |
max_tail = (MAX_SUFFIX - slen); |
| 212 |
max_len = (MAX_FILE_LEN - slen); |
| 213 |
|
| 214 |
ncaps = ndigits = 0; |
| 215 |
|
| 216 |
for (i = len; i >= max_len-max_tail; i--) |
| 217 |
{ |
| 218 |
if (ndigits >= max_tail) |
| 219 |
break; |
| 220 |
else if (ndigits + ncaps + i < max_len) |
| 221 |
break; |
| 222 |
else if (isdigit(name[i])) |
| 223 |
ndigits++; |
| 224 |
else if (isupper(name[i])) |
| 225 |
ncaps++; |
| 226 |
} |
| 227 |
|
| 228 |
/*----------------------------------------------------- |
| 229 |
* How long a tail of digits and caps? |
| 230 |
* If there are too many, keep more digits. |
| 231 |
*-----------------------------------------------------*/ |
| 232 |
|
| 233 |
tail_len = ndigits + ncaps; |
| 234 |
|
| 235 |
if (tail_len > max_tail) { |
| 236 |
tail_len = max_tail; |
| 237 |
ncaps = tail_len - ndigits; |
| 238 |
} |
| 239 |
|
| 240 |
/*----------------------------------------------------- |
| 241 |
* Shorten the name by moving up digits and caps. |
| 242 |
* Note we may skip caps, but copy all digits we meet. |
| 243 |
*-----------------------------------------------------*/ |
| 244 |
|
| 245 |
strncpy (str, name, sizeof(str)); |
| 246 |
|
| 247 |
for(i = j = (max_len-tail_len); i < len && j < max_len; i++) |
| 248 |
{ |
| 249 |
if (isdigit(name[i])) { |
| 250 |
str[j++] = name[i]; |
| 251 |
} else if (isupper(name[i]) && ncaps-- > 0) { |
| 252 |
str[j++]= name[i]; |
| 253 |
} |
| 254 |
} |
| 255 |
str[j] = '\0'; |
| 256 |
|
| 257 |
/*----------------------------------------------------- |
| 258 |
* 'name' is a pointer to where the filename part of |
| 259 |
* the original path starts. str is a shortened name, |
| 260 |
* so it will surely fit into the end of the original. |
| 261 |
*-----------------------------------------------------*/ |
| 262 |
|
| 263 |
strcpy(name, str); |
| 264 |
return fname; |
| 265 |
} |
| 266 |
/****************************************************************************** |
| 267 |
NAME: UxTruncateFilename (fname, suffix_len) |
| 268 |
INPUT: char *fname : name to truncate |
| 269 |
int suffix_len : length of suffix to be added |
| 270 |
OUTPUT: --- |
| 271 |
RETURN: char * : truncated name |
| 272 |
DESCRIPTION: If the 'truncateFilenames' resource is true, returns a filename |
| 273 |
with its last component truncated to not more than 14 |
| 274 |
characters including the possible suffix. |
| 275 |
NB! This function may modify the passed filename. |
| 276 |
If the 'truncateFilenames' resource is false, just |
| 277 |
return the given filename unchanged. |
| 278 |
EXT REFERENCES: --- |
| 279 |
EXT EFFECTS: --- |
| 280 |
LAST REV: 5 November 1990 IBM Beta1 |
| 281 |
-- made this function available to OLE and UIM/X. |
| 282 |
-- Also made the strcmp case insensitive by |
| 283 |
calling UxToLowerString() |
| 284 |
-----------------------------------------------------------------------------*/ |
| 285 |
|
| 286 |
char* UxTruncateFilename(fname, slen) |
| 287 |
char* fname; |
| 288 |
int slen; |
| 289 |
{ |
| 290 |
if(UxShouldTruncate() == FALSE) { |
| 291 |
return(fname); |
| 292 |
} |
| 293 |
|
| 294 |
return UxTruncateName (fname, slen); |
| 295 |
} |
| 296 |
/***************************************************************************** |
| 297 |
NAME: dstring gethome(username) |
| 298 |
|
| 299 |
INPUT: dstring username - user name (with ~) |
| 300 |
|
| 301 |
RETURN: home directory |
| 302 |
|
| 303 |
DESCRIPTION: return the user 'uname's home directory |
| 304 |
|
| 305 |
Accepts a dstring parameter, which it frees. |
| 306 |
Creates and returns a dstring which should be freed by |
| 307 |
the calling function. |
| 308 |
|
| 309 |
LAST REV: 10 April 1990 |
| 310 |
-- parameter is a dstring |
| 311 |
-----------------------------------------------------------------------------*/ |
| 312 |
|
| 313 |
static dstring gethome(username) |
| 314 |
dstring username; |
| 315 |
{ |
| 316 |
int uid; |
| 317 |
char *uname; |
| 318 |
dstring home; |
| 319 |
char *pntr, hold, *end; |
| 320 |
struct passwd *pw; |
| 321 |
|
| 322 |
uname = dnstr(username); |
| 323 |
|
| 324 |
end= uname+1; |
| 325 |
|
| 326 |
while(isalnum(*end)) |
| 327 |
end++; |
| 328 |
|
| 329 |
hold= *end; |
| 330 |
*end= '\0'; |
| 331 |
|
| 332 |
if(strcmp(uname, "~") == 0) |
| 333 |
{ pntr= getenv("HOME"); |
| 334 |
|
| 335 |
if(pntr != NULL) |
| 336 |
{ home= dcreate(pntr); |
| 337 |
*end= hold; |
| 338 |
dappend(home, end); |
| 339 |
|
| 340 |
dfree(username); |
| 341 |
return(home); |
| 342 |
} |
| 343 |
|
| 344 |
if((pntr= getenv("USER")) != NULL) |
| 345 |
pw= getpwnam(pntr); |
| 346 |
else |
| 347 |
{ uid= getuid(); |
| 348 |
pw= getpwuid(uid); |
| 349 |
} |
| 350 |
|
| 351 |
if(pw) |
| 352 |
{ home= dcreate(pw->pw_dir); |
| 353 |
*end= hold; |
| 354 |
dappend(home, end); |
| 355 |
|
| 356 |
dfree(username); |
| 357 |
return(home); |
| 358 |
} |
| 359 |
|
| 360 |
dfree(username); |
| 361 |
return(dcreate(NULL)); |
| 362 |
} |
| 363 |
|
| 364 |
uname++; |
| 365 |
|
| 366 |
pw= getpwnam(uname); |
| 367 |
|
| 368 |
if(pw) |
| 369 |
home= dcreate(pw->pw_dir); |
| 370 |
else |
| 371 |
home= dcreate(NULL); |
| 372 |
|
| 373 |
*end= hold; |
| 374 |
dappend(home, end); |
| 375 |
|
| 376 |
dfree(username); |
| 377 |
return(home); |
| 378 |
} |
| 379 |
|
| 380 |
/***************************************************************************** |
| 381 |
NAME: dstring mkname(path, name) |
| 382 |
|
| 383 |
INPUT: char *path - path to file |
| 384 |
*name - name of file |
| 385 |
|
| 386 |
RETURN: full filename |
| 387 |
|
| 388 |
DESCRIPTION: return the full filename of 'file' in the directory 'path' |
| 389 |
LAST REV: Feb 19/1989 |
| 390 |
-----------------------------------------------------------------------------*/ |
| 391 |
|
| 392 |
static dstring mkname(path, name) |
| 393 |
char *path, *name; |
| 394 |
{ |
| 395 |
dstring rtrn; |
| 396 |
int len; |
| 397 |
|
| 398 |
rtrn= dcreate(path); |
| 399 |
|
| 400 |
len = strlen(path); |
| 401 |
if ( (len > 0) && (path[len-1] != '/') ) |
| 402 |
dappend(rtrn, "/"); |
| 403 |
|
| 404 |
dappend(rtrn, name); |
| 405 |
|
| 406 |
return(rtrn); |
| 407 |
} |
| 408 |
|
| 409 |
/***************************************************************************** |
| 410 |
NAME: dstring expand_macros(ds) |
| 411 |
|
| 412 |
INPUT: dstring ds - macros to expand |
| 413 |
|
| 414 |
RETURN: expanded macros |
| 415 |
|
| 416 |
DESCRIPTION: expand all the macros in 'ds', free it, and return expansion |
| 417 |
|
| 418 |
LAST REV: Feb 21/1989 |
| 419 |
-----------------------------------------------------------------------------*/ |
| 420 |
|
| 421 |
static dstring expand_macros(ds) |
| 422 |
dstring ds; |
| 423 |
{ |
| 424 |
char *pntr, *start, *end = NULL, endc; |
| 425 |
dstring rtrn; |
| 426 |
|
| 427 |
rtrn= dcreate(NULL); |
| 428 |
|
| 429 |
start= dnstr(ds); |
| 430 |
pntr= strchr(start, '$'); |
| 431 |
|
| 432 |
while(pntr) |
| 433 |
{ *(pntr++)= '\0'; |
| 434 |
dappend(rtrn, start); |
| 435 |
|
| 436 |
end= pntr; |
| 437 |
|
| 438 |
while(isalnum(*end)) |
| 439 |
end++; |
| 440 |
|
| 441 |
endc= *end; |
| 442 |
*end= '\0'; |
| 443 |
|
| 444 |
if(*pntr != '\0') |
| 445 |
dappend(rtrn, getenv(pntr)); |
| 446 |
|
| 447 |
*end= endc; |
| 448 |
|
| 449 |
start= end; |
| 450 |
pntr= strchr(start, '$'); |
| 451 |
} |
| 452 |
|
| 453 |
dappend(rtrn, end); |
| 454 |
dfree(ds); |
| 455 |
|
| 456 |
return(rtrn); |
| 457 |
} |
| 458 |
|
| 459 |
/***************************************************************************** |
| 460 |
NAME: dstring tokenize(ds) |
| 461 |
|
| 462 |
INPUT: dstring *ds - dstring to tokenize |
| 463 |
|
| 464 |
RETURN: first token in 'ds' |
| 465 |
|
| 466 |
DESCRIPTION: return the first token in 'ds' and reset 'ds' to point to |
| 467 |
the rest of the string |
| 468 |
|
| 469 |
LAST REV: July 25 1990 |
| 470 |
- handle separators at the end of a path. |
| 471 |
- handle a path consisting of only separators. |
| 472 |
-----------------------------------------------------------------------------*/ |
| 473 |
|
| 474 |
static dstring tokenize(ds) |
| 475 |
dstring *ds; |
| 476 |
{ |
| 477 |
char *pntr, *end; |
| 478 |
dstring rtrn, dsrtrn; |
| 479 |
|
| 480 |
pntr= dgetstr(*ds); |
| 481 |
|
| 482 |
if(pntr == NULL) |
| 483 |
return(dcreate(NULL)); |
| 484 |
|
| 485 |
while(*pntr && (strchr(": ,\n\t", *pntr) != NULL)) |
| 486 |
pntr++; |
| 487 |
|
| 488 |
end= strpbrk(pntr, ": ,\n\t"); |
| 489 |
|
| 490 |
if(end != NULL) |
| 491 |
*end++ = '\0'; |
| 492 |
|
| 493 |
rtrn= dcreate(pntr); |
| 494 |
|
| 495 |
if(end != NULL) { |
| 496 |
while (*end && (strchr(": ,\n\t", *end) != NULL)) |
| 497 |
end++; |
| 498 |
if (*end) |
| 499 |
dsrtrn= dcreate(end); |
| 500 |
else |
| 501 |
dsrtrn= dcreate(NULL); |
| 502 |
} else { |
| 503 |
dsrtrn= dcreate(NULL); |
| 504 |
} |
| 505 |
|
| 506 |
dfree(*ds); |
| 507 |
*ds= dsrtrn; |
| 508 |
|
| 509 |
return(rtrn); |
| 510 |
} |
| 511 |
|
| 512 |
/****************************************************************************** |
| 513 |
NAME: dstring expand_name(tok) |
| 514 |
INPUT: dstring tok - taken to expand |
| 515 |
RETURN: expanded token (original freed if needed) |
| 516 |
DESCRIPTION: expand the env vars and ~[uname]'s in tok |
| 517 |
LAST REV: January 03 1991 bugfix 1116. |
| 518 |
-----------------------------------------------------------------------------*/ |
| 519 |
|
| 520 |
/* |
| 521 |
* An always-true predicate to pass to XtResolvePath. |
| 522 |
*/ |
| 523 |
/*ARGSUSED*/ |
| 524 |
static Boolean AlwaysTrue(file) |
| 525 |
String file; |
| 526 |
{ return True; } |
| 527 |
|
| 528 |
static dstring expand_name(tok) |
| 529 |
dstring tok; |
| 530 |
{ |
| 531 |
dstring exp, rtrn; |
| 532 |
|
| 533 |
if(strchr(dnstr(tok), '$') != NULL) |
| 534 |
{ |
| 535 |
exp = expand_macros(tok); /* tok is freed in expand_macros */ |
| 536 |
return(expand_name(exp)); /* exp is freed in expand_name */ |
| 537 |
} |
| 538 |
else if(*(dnstr(tok)) == '~') |
| 539 |
{ |
| 540 |
exp = gethome(tok); /* tok is freed in gethome */ |
| 541 |
return(expand_name(exp)); /* exp is freed in expand_name */ |
| 542 |
} |
| 543 |
else |
| 544 |
|
| 545 |
#ifdef MOTIF_WIDGETS |
| 546 |
|
| 547 |
{ |
| 548 |
/* Expand language path character %L (and other toolkit defaults). |
| 549 |
* Several arguments to XtResolvePathname are NULL so the toolkit |
| 550 |
* defaults apply. We pass AlwyasTrue because UIM/X builds its |
| 551 |
* paths piece-by-piece; the partial paths we expand here aren't |
| 552 |
* meant to name exisiting files. |
| 553 |
*/ |
| 554 |
char *r; |
| 555 |
|
| 556 |
r = XtResolvePathname ( |
| 557 |
UxDisplay, |
| 558 |
(String) NULL, /* type o%T e ntion */ |
| 559 |
(String) getenv("UXAPP"), /* name (app s) for %N */ |
| 560 |
(String) NULL, /* suffix (none) */ |
| 561 |
(String) dnstr(tok), /* path */ |
| 562 |
(Substitution) NULL, /* substitutions */ |
| 563 |
0, /* num substitions */ |
| 564 |
(XtFilePredicate) AlwaysTrue /* file access predicate */ |
| 565 |
); |
| 566 |
if (r) |
| 567 |
{ |
| 568 |
dfree(tok); |
| 569 |
rtrn = dcreate(r); |
| 570 |
XtFree(r); |
| 571 |
return rtrn; |
| 572 |
} |
| 573 |
else |
| 574 |
rtrn = tok; |
| 575 |
|
| 576 |
return rtrn; |
| 577 |
} |
| 578 |
#else |
| 579 |
{ |
| 580 |
return(tok); |
| 581 |
} |
| 582 |
#endif /* MOTIF_WIDGETS */ |
| 583 |
} |
| 584 |
/***************************************************************************** |
| 585 |
NAME: int UxFileExists(fname) |
| 586 |
|
| 587 |
INPUT: char *fname - filename |
| 588 |
|
| 589 |
RETURN: 1: file or directory exists 0: file not found |
| 590 |
|
| 591 |
DESCRIPTION: return whether the file or directory 'fname' exists |
| 592 |
|
| 593 |
LAST REV: March 18, 1991 |
| 594 |
Replaced stat call by more efficient access call. |
| 595 |
-----------------------------------------------------------------------------*/ |
| 596 |
|
| 597 |
int UxFileExists(fname) |
| 598 |
char *fname; |
| 599 |
{ |
| 600 |
if (access(fname, 0) < 0) |
| 601 |
return(0); |
| 602 |
|
| 603 |
return(1); |
| 604 |
} |
| 605 |
|
| 606 |
/***************************************************************************** |
| 607 |
NAME: pathlist UxInitPath(str) |
| 608 |
|
| 609 |
INPUT: char *str - initial path list |
| 610 |
|
| 611 |
RETURN: pathlist object |
| 612 |
|
| 613 |
DESCRIPTION: initialize a path list to files. |
| 614 |
LAST REV: Feb 19/1989 |
| 615 |
-----------------------------------------------------------------------------*/ |
| 616 |
|
| 617 |
pathlist UxInitPath(str) |
| 618 |
char *str; |
| 619 |
{ |
| 620 |
pathlist path; |
| 621 |
|
| 622 |
path= (pathlist) UxMalloc(sizeof(dstring)); |
| 623 |
|
| 624 |
*path= dcreate(NULL); |
| 625 |
|
| 626 |
UxAddPath(path, str); |
| 627 |
|
| 628 |
return(path); |
| 629 |
} |
| 630 |
|
| 631 |
/***************************************************************************** |
| 632 |
NAME: void UxAddPath(path, str) |
| 633 |
|
| 634 |
INPUT: pathlist path - pathlist |
| 635 |
char *str - list of dirs to add |
| 636 |
|
| 637 |
DESCRIPTION: add the directories in 'str' to the pathlist 'path' |
| 638 |
|
| 639 |
LAST REV: Feb 19/1989 |
| 640 |
-----------------------------------------------------------------------------*/ |
| 641 |
|
| 642 |
void UxAddPath(path, str) |
| 643 |
pathlist path; |
| 644 |
char *str; |
| 645 |
{ |
| 646 |
dstring pcopy, tok; |
| 647 |
|
| 648 |
if((str == NULL) || (str[0] == '\0')) |
| 649 |
return; |
| 650 |
|
| 651 |
if(path == NULL) |
| 652 |
return; |
| 653 |
|
| 654 |
pcopy= dcreate(str); |
| 655 |
|
| 656 |
tok= tokenize(&pcopy); |
| 657 |
|
| 658 |
while(dgetstr(tok) != NULL) |
| 659 |
{ tok= expand_name(tok); |
| 660 |
|
| 661 |
if(strlen(dnstr(tok)) != 0) |
| 662 |
{ if(dlen(*path) != 0) |
| 663 |
dappend(*path, ":"); |
| 664 |
|
| 665 |
dconcat(*path, tok); |
| 666 |
} |
| 667 |
|
| 668 |
dfree(tok); |
| 669 |
tok= tokenize(&pcopy); |
| 670 |
} |
| 671 |
|
| 672 |
/* don't need to free tok or pcopy, since they are now null */ |
| 673 |
} |
| 674 |
|
| 675 |
/***************************************************************************** |
| 676 |
NAME: void UxResetPath(path, str) |
| 677 |
|
| 678 |
INPUT: pathlist path - pathlist |
| 679 |
char *str - new list of paths |
| 680 |
|
| 681 |
DESCRIPTION: reset the pathlist 'path' to be 'str' |
| 682 |
|
| 683 |
LAST REV: Feb 19/1989 |
| 684 |
-----------------------------------------------------------------------------*/ |
| 685 |
|
| 686 |
void UxResetPath(path, str) |
| 687 |
pathlist path; |
| 688 |
char *str; |
| 689 |
{ |
| 690 |
dfree(*path); |
| 691 |
|
| 692 |
UxAddPath(path, str); |
| 693 |
} |
| 694 |
|
| 695 |
/***************************************************************************** |
| 696 |
NAME: void UxFreePath(path) |
| 697 |
|
| 698 |
INPUT: pathlist path - pathlist |
| 699 |
|
| 700 |
DESCRIPTION: free the pathlist 'path' |
| 701 |
|
| 702 |
LAST REV: Feb 19/1989 |
| 703 |
-----------------------------------------------------------------------------*/ |
| 704 |
|
| 705 |
void UxFreePath(path) |
| 706 |
pathlist path; |
| 707 |
{ |
| 708 |
dfree(*path); |
| 709 |
UxFree(path); |
| 710 |
} |
| 711 |
|
| 712 |
/***************************************************************************** |
| 713 |
NAME: char *UxGetPath(path) |
| 714 |
|
| 715 |
INPUT: pathlist path - pathlist |
| 716 |
|
| 717 |
RETURN: pathlist in string form |
| 718 |
|
| 719 |
DESCRIPTION: return the pathlist as a string (not to be freed) |
| 720 |
|
| 721 |
LAST REV: Feb 19/1989 |
| 722 |
-----------------------------------------------------------------------------*/ |
| 723 |
|
| 724 |
char *UxGetPath(path) |
| 725 |
pathlist path; |
| 726 |
{ |
| 727 |
if(path == NULL) |
| 728 |
return(""); |
| 729 |
|
| 730 |
return(dnstr(*path)); |
| 731 |
} |
| 732 |
|
| 733 |
/***************************************************************************** |
| 734 |
NAME: char *UxExpandFilename(path_list, fname) |
| 735 |
|
| 736 |
INPUT: pathlist path_list - pathlist |
| 737 |
char *fname - filename |
| 738 |
|
| 739 |
RETURN: expanded filename |
| 740 |
|
| 741 |
DESCRIPTION: Find the expanded name of 'fname' using the dirs in 'path' |
| 742 |
|
| 743 |
LAST REV: April 10, 1990 |
| 744 |
-- free filename at the proper place |
| 745 |
-----------------------------------------------------------------------------*/ |
| 746 |
|
| 747 |
char *UxExpandFilename(path_list, fname) |
| 748 |
pathlist path_list; |
| 749 |
char *fname; |
| 750 |
{ |
| 751 |
static dstring filename= UxEmptyDstring; |
| 752 |
dstring path, file; |
| 753 |
char *pntr; |
| 754 |
|
| 755 |
file= expand_name(dcreate(fname)); |
| 756 |
fname= dnstr(file); |
| 757 |
|
| 758 |
if(*fname == '\0') |
| 759 |
return(NULL); |
| 760 |
|
| 761 |
if(fname[0] == '/') |
| 762 |
{ |
| 763 |
dfree(filename); |
| 764 |
filename= dcreate(fname); |
| 765 |
|
| 766 |
dfree(file); |
| 767 |
|
| 768 |
if(UxFileExists(dnstr(filename))) |
| 769 |
return(dnstr(filename)); |
| 770 |
else |
| 771 |
return(NULL); |
| 772 |
} |
| 773 |
|
| 774 |
if(path_list == NULL) |
| 775 |
{ dfree(file); |
| 776 |
return(NULL); |
| 777 |
} |
| 778 |
|
| 779 |
path= dcopy(*path_list); |
| 780 |
|
| 781 |
pntr= strtok(dnstr(path), ": ,\n\t"); |
| 782 |
|
| 783 |
while(pntr) |
| 784 |
{ |
| 785 |
dfree(filename); |
| 786 |
filename= mkname(pntr, fname); |
| 787 |
|
| 788 |
if(UxFileExists(dnstr(filename))) |
| 789 |
{ dfree(path); |
| 790 |
dfree(file); |
| 791 |
return(dnstr(filename)); |
| 792 |
} |
| 793 |
|
| 794 |
pntr= strtok(NULL, ": ,\n\t"); |
| 795 |
} |
| 796 |
|
| 797 |
dfree(file); |
| 798 |
dfree(path); |
| 799 |
|
| 800 |
return(NULL); |
| 801 |
} |
| 802 |
|
| 803 |
/***************************************************************************** |
| 804 |
NAME: char *UxExpandAllFilenames(path_list, fname) |
| 805 |
|
| 806 |
INPUT: pathlist path_list - pathlist or NULL |
| 807 |
char *fname - filename |
| 808 |
|
| 809 |
RETURN: expanded filename |
| 810 |
|
| 811 |
DESCRIPTION: Find the expanded name of 'fname' using the dirs in 'path_list' |
| 812 |
Remember position in path_list between calls so that subsequent |
| 813 |
calls can continue in the list where the previous call left off. |
| 814 |
If path_list is NULL then continue from previous path otherwise |
| 815 |
start from given path_list. Don't return same expanded name |
| 816 |
more than once. If fname is NULL then use previous fname. |
| 817 |
|
| 818 |
LAST REV: March 13, 1991 See bugfix 2111 |
| 819 |
-----------------------------------------------------------------------------*/ |
| 820 |
|
| 821 |
char *UxExpandAllFilenames(path_list, fname) |
| 822 |
pathlist path_list; |
| 823 |
char *fname; |
| 824 |
{ |
| 825 |
static dstring filename= UxEmptyDstring; |
| 826 |
static dstring path= UxEmptyDstring, file= UxEmptyDstring; |
| 827 |
static dstring alreadyReturned= UxEmptyDstring; |
| 828 |
static char *pntr; |
| 829 |
|
| 830 |
|
| 831 |
if (fname != NULL) { |
| 832 |
dfree(file); |
| 833 |
file= expand_name(dcreate(fname)); |
| 834 |
} |
| 835 |
|
| 836 |
fname= dnstr(file); |
| 837 |
|
| 838 |
if(*fname == '\0') |
| 839 |
return(NULL); |
| 840 |
|
| 841 |
if(fname[0] == '/') |
| 842 |
{ |
| 843 |
dfree(filename); |
| 844 |
filename= dcreate(fname); |
| 845 |
|
| 846 |
if (strstr(dnstr(alreadyReturned), fname) == NULL && |
| 847 |
UxFileExists(dnstr(filename))) { |
| 848 |
dappend(alreadyReturned, ":"); |
| 849 |
dconcat(alreadyReturned, filename); |
| 850 |
return(dnstr(filename)); |
| 851 |
} else |
| 852 |
return(NULL); |
| 853 |
} |
| 854 |
|
| 855 |
if (path_list != NULL) { |
| 856 |
dfree(path); |
| 857 |
path= dcopy(*path_list); |
| 858 |
dappend(path, ":"); /* Make sure we end with ':' */ |
| 859 |
dfree(alreadyReturned); |
| 860 |
alreadyReturned = dcreate(""); |
| 861 |
pntr= strtok(dnstr(path), ": ,\n\t"); |
| 862 |
} |
| 863 |
|
| 864 |
/* |
| 865 |
* Start from where we left off or from beginning if new path_list |
| 866 |
* was specified. |
| 867 |
*/ |
| 868 |
while(pntr) |
| 869 |
{ |
| 870 |
dfree(filename); |
| 871 |
filename= mkname(pntr, fname); |
| 872 |
|
| 873 |
pntr[strlen(pntr)] = ':'; |
| 874 |
pntr= strtok(NULL, ": ,\n\t"); |
| 875 |
|
| 876 |
if (strstr(dnstr(alreadyReturned), dgetstr(filename)) == NULL |
| 877 |
&& UxFileExists(dgetstr(filename))) { |
| 878 |
/* |
| 879 |
* Restore path string before returning. |
| 880 |
*/ |
| 881 |
dappend(alreadyReturned, ":"); |
| 882 |
|
| 883 |
/* The UIM/X 2.0 releases mostly have |
| 884 |
* 'dgetstr(filename)' in the line below, |
| 885 |
* which is garbage. |
| 886 |
*/ |
| 887 |
dconcat(alreadyReturned, filename); |
| 888 |
return(dgetstr(filename)); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
return(NULL); |
| 893 |
} |
| 894 |
|
| 895 |
/****************************************************************************** |
| 896 |
NAME: char *UxGetFilename(fname) |
| 897 |
|
| 898 |
INPUT: char *fname - filename |
| 899 |
|
| 900 |
RETURN: expanded filename |
| 901 |
|
| 902 |
DESCRIPTION: expand all ~'s and $VAR's in the passed in filename |
| 903 |
|
| 904 |
LAST REV: 15 August 1990 OLE MERGE |
| 905 |
-- removed the call to TruncateFilename now |
| 906 |
called from cfile.c and save.c |
| 907 |
-----------------------------------------------------------------------------*/ |
| 908 |
|
| 909 |
char *UxGetFilename(fname) |
| 910 |
char *fname; |
| 911 |
{ |
| 912 |
static dstring filename= UxEmptyDstring; |
| 913 |
|
| 914 |
/* check for invalid characters in supplied filename */ |
| 915 |
if (UxCheckForInvalidCharacters (fname)) |
| 916 |
return NULL; |
| 917 |
|
| 918 |
dfree(filename); |
| 919 |
|
| 920 |
filename= expand_name(dcreate(fname)); |
| 921 |
|
| 922 |
if (dnstr (filename)) { |
| 923 |
char *xname, *CondenseDots(); |
| 924 |
|
| 925 |
if ((xname = CondenseDots (dnstr(filename)))) { |
| 926 |
dfree (filename); |
| 927 |
filename = dcreate (xname); |
| 928 |
UxFree (xname); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
return(dnstr(filename)); |
| 933 |
} |
| 934 |
|
| 935 |
/***************************************************************************** |
| 936 |
NAME: char *UxExpandEnv(str) |
| 937 |
|
| 938 |
INPUT: char *str - string to expand |
| 939 |
|
| 940 |
RETURN: expanded string |
| 941 |
|
| 942 |
DESCRIPTION: Expand all the environment variables in 'str' until there are |
| 943 |
no more '$'s in the string: |
| 944 |
|
| 945 |
LAST REV: Feb 22/1989 |
| 946 |
-----------------------------------------------------------------------------*/ |
| 947 |
|
| 948 |
char *UxExpandEnv(str) |
| 949 |
char *str; |
| 950 |
{ |
| 951 |
static dstring rtrn= UxEmptyDstring; |
| 952 |
|
| 953 |
dfree(rtrn); |
| 954 |
|
| 955 |
rtrn= dcreate(str); |
| 956 |
|
| 957 |
while(strchr(dnstr(rtrn), '$') != NULL) |
| 958 |
rtrn= expand_macros(rtrn); |
| 959 |
|
| 960 |
return(dnstr(rtrn)); |
| 961 |
} |
| 962 |
/****************************************************************************** |
| 963 |
NAME: char * UxGetDirName(path) |
| 964 |
|
| 965 |
INPUT: char *path; -- path to extract the directory from |
| 966 |
RETURN: directory name if the path is not NULL, must be freed. |
| 967 |
otherwise, returns NULL, nothing should be freed.. |
| 968 |
|
| 969 |
DESCRIPTION: Strips the filename from the path. |
| 970 |
If the path is not valid or is just a filename, |
| 971 |
the function sets the path to the current directory |
| 972 |
The return value must be freed. |
| 973 |
|
| 974 |
LAST REV: Feb 28 1991 |
| 975 |
-----------------------------------------------------------------------------*/ |
| 976 |
char *UxGetDirName(path) |
| 977 |
char *path; |
| 978 |
{ |
| 979 |
int len; |
| 980 |
char *pntr, *dirname; |
| 981 |
|
| 982 |
if (path == (char *) NULL) { |
| 983 |
return (char *) NULL; |
| 984 |
} |
| 985 |
|
| 986 |
len = strlen(path); |
| 987 |
dirname = (char *) UxMalloc((len + 1) * sizeof(char)); |
| 988 |
|
| 989 |
strcpy(dirname, path); |
| 990 |
|
| 991 |
pntr = strrchr(dirname, '/'); |
| 992 |
if (pntr != NULL) { |
| 993 |
if (pntr == dirname) |
| 994 |
strcpy(dirname, "/"); |
| 995 |
*pntr = '\0'; |
| 996 |
} |
| 997 |
else { |
| 998 |
strcpy(dirname, "."); |
| 999 |
} |
| 1000 |
|
| 1001 |
return dirname; |
| 1002 |
|
| 1003 |
} |
| 1004 |
|
| 1005 |
/****************************************************************************** |
| 1006 |
NAME: int UxWriteAccess(path, error) |
| 1007 |
|
| 1008 |
INPUT: char *path; -- the path to access |
| 1009 |
int *error; -- a return error flag |
| 1010 |
RETURN: Checks if a path can be accessed. |
| 1011 |
|
| 1012 |
The calling function should check the return value from |
| 1013 |
UxWriteAccess(). |
| 1014 |
If it is -1, then it should check for the following error flags: |
| 1015 |
error = EACCES -> file or directory is not writable. |
| 1016 |
error = ENOENT -> file or directory do not exist |
| 1017 |
error = EISDIR -> path is a directory |
| 1018 |
|
| 1019 |
If it is 0, then it should check for the following error flags: |
| 1020 |
|
| 1021 |
error = EEXIST -> file exists already |
| 1022 |
error = ENOENT -> file does not exist |
| 1023 |
|
| 1024 |
DESCRIPTION: Checks for the write permission on path. |
| 1025 |
7 cases could arise: |
| 1026 |
|
| 1027 |
(a) path is NULL --> return -1. |
| 1028 |
(b) path is a directory --> return -1. |
| 1029 |
(c) directory specified in path is not writable |
| 1030 |
and file does not exist --> return -1. |
| 1031 |
(d) directory specified in path is not writable |
| 1032 |
and file does not exist --> return -1. |
| 1033 |
(e) dir is valid, and file does not exist. --> return 0. |
| 1034 |
(f) path is valid, and file is not writable --> return -1. |
| 1035 |
(g) path is valid, and file is writable --> return 0. |
| 1036 |
|
| 1037 |
LAST REV: Feb 28 1991 |
| 1038 |
-----------------------------------------------------------------------------*/ |
| 1039 |
int UxWriteAccess (path,error) |
| 1040 |
char *path; |
| 1041 |
int *error; |
| 1042 |
{ |
| 1043 |
int result; |
| 1044 |
char *dirname; |
| 1045 |
struct stat buf; |
| 1046 |
|
| 1047 |
|
| 1048 |
if ((path == NULL) || (*path == '\0')) { |
| 1049 |
*error = ENOENT; |
| 1050 |
return -1; |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* check if the path points to a filename, or a directory */ |
| 1054 |
result = stat(path, &buf); |
| 1055 |
|
| 1056 |
|
| 1057 |
/* path is not valid, find out why */ |
| 1058 |
if (result == -1) { |
| 1059 |
dirname = UxGetDirName (path); |
| 1060 |
result = access(dirname, W_OK); |
| 1061 |
free (dirname); |
| 1062 |
|
| 1063 |
/* directory is not writable, and file does not exist */ |
| 1064 |
if (result == -1) { |
| 1065 |
*error = EACCES; |
| 1066 |
return result; |
| 1067 |
} |
| 1068 |
/* directory is writable, file does not exist, this is OK */ |
| 1069 |
else { |
| 1070 |
*error = ENOENT; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
/* path is valid */ |
| 1074 |
else { |
| 1075 |
|
| 1076 |
/* path points to a directory */ |
| 1077 |
if (buf.st_mode & S_IFDIR) { |
| 1078 |
*error = EISDIR; |
| 1079 |
return -1; |
| 1080 |
} |
| 1081 |
|
| 1082 |
/* file is not writable */ |
| 1083 |
if (!(buf.st_mode & S_IWUSR)) { |
| 1084 |
*error = EACCES; |
| 1085 |
return -1; |
| 1086 |
} |
| 1087 |
/* file exists already and is writable */ |
| 1088 |
else { |
| 1089 |
*error = EEXIST; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
return 0; |
| 1095 |
|
| 1096 |
} |
| 1097 |
/****************************************************************************** |
| 1098 |
NAME: char *UxGetCurrentDir () |
| 1099 |
INPUT: |
| 1100 |
RETURN: char * -current directory |
| 1101 |
OUTPUT |
| 1102 |
DESCRIPTION: gets the current directory. Returns allocated memory, therefore |
| 1103 |
calling function should free memory. |
| 1104 |
CREATED: Oct 29/91 fix3099 |
| 1105 |
LAST REV: |
| 1106 |
-----------------------------------------------------------------------------*/ |
| 1107 |
char *UxGetCurrentDir () |
| 1108 |
{ |
| 1109 |
char *getcwd (), *cwd; |
| 1110 |
|
| 1111 |
if ((cwd = (char *) UxMalloc (MAXPATHLEN +1))) { |
| 1112 |
return getcwd (cwd, MAXPATHLEN + 1); |
| 1113 |
} |
| 1114 |
return (char *)NULL; |
| 1115 |
} |
| 1116 |
/****************************************************************************** |
| 1117 |
NAME: char CondenseDots (fname) |
| 1118 |
|
| 1119 |
INPUT: char *fname; -- path to extract the directory from |
| 1120 |
RETURN: expanded path; |
| 1121 |
OUTPUT |
| 1122 |
|
| 1123 |
DESCRIPTION: Expands dots in specified file path. Allocates |
| 1124 |
memory for buffer, therefore, calling function |
| 1125 |
should free return value. The argument is expanded |
| 1126 |
to an absolute name in the process. |
| 1127 |
|
| 1128 |
CREATED: July 25/91 |
| 1129 |
LAST REV: |
| 1130 |
-----------------------------------------------------------------------------*/ |
| 1131 |
|
| 1132 |
char *CondenseDots (fname) |
| 1133 |
char *fname; |
| 1134 |
{ |
| 1135 |
char *fstr, *xname = (char *)NULL; |
| 1136 |
|
| 1137 |
if (fname == NULL) { |
| 1138 |
return NULL; |
| 1139 |
} else if (*fname == 0) { |
| 1140 |
return UxCopyString(fname); |
| 1141 |
} |
| 1142 |
|
| 1143 |
/*------------------------------------------------------------- |
| 1144 |
* If fname does not begin with a slash, prepend current path. |
| 1145 |
*------------------------------------------------------------*/ |
| 1146 |
if (fname [0] != '/') { |
| 1147 |
char *cwd; |
| 1148 |
|
| 1149 |
cwd = UxGetCurrentDir (); |
| 1150 |
if (cwd) { |
| 1151 |
xname = (char *) UxMalloc (strlen(cwd) + strlen(fname) + 3); |
| 1152 |
sprintf (xname, "%s/%s/", cwd, fname); |
| 1153 |
UxFree (cwd); |
| 1154 |
} |
| 1155 |
else { |
| 1156 |
xname = (char *) UxMalloc (strlen(fname) + 2); |
| 1157 |
sprintf (xname, "%s/", fname); |
| 1158 |
} |
| 1159 |
} else { |
| 1160 |
/*---------------------------------------------------- |
| 1161 |
* Append a slash to the path, needed for below. |
| 1162 |
*----------------------------------------------------*/ |
| 1163 |
xname = UxMalloc (strlen (fname) + 2); |
| 1164 |
sprintf (xname, "%s/", fname); |
| 1165 |
} |
| 1166 |
|
| 1167 |
/*----------------------------------------------- |
| 1168 |
* Loop until all dots have been condensed out |
| 1169 |
*----------------------------------------------*/ |
| 1170 |
while (1) |
| 1171 |
{ |
| 1172 |
char *dots; |
| 1173 |
fstr = UxCopyString (xname); |
| 1174 |
|
| 1175 |
if ((dots = strstr(fstr, "/./"))) |
| 1176 |
{ |
| 1177 |
/*----------------------------- |
| 1178 |
* "/./" -- remove "./" |
| 1179 |
*-----------------------------*/ |
| 1180 |
dots [1] = '\0'; |
| 1181 |
sprintf (xname, "%s%s", fstr, dots +3); |
| 1182 |
} |
| 1183 |
else if ((dots = strstr(fstr, "/../"))) |
| 1184 |
{ |
| 1185 |
/*------------------------------------- |
| 1186 |
* remove /../ and preceding directory |
| 1187 |
*-------------------------------------*/ |
| 1188 |
if (dots == fstr) { |
| 1189 |
/* Path starts in "/../", not much to do. |
| 1190 |
*/ |
| 1191 |
break; |
| 1192 |
} else { |
| 1193 |
char *tail = dots + 4; |
| 1194 |
|
| 1195 |
while (*(--dots) != '/') { |
| 1196 |
if (dots == fstr) { |
| 1197 |
break; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
if (*dots == '/') { |
| 1201 |
dots [1] = '\0'; |
| 1202 |
sprintf (xname, "%s%s", fstr, tail); |
| 1203 |
} else { |
| 1204 |
break; /* No leading /name/../ */ |
| 1205 |
} |
| 1206 |
} |
| 1207 |
} else { |
| 1208 |
/*----------------------------------------------- |
| 1209 |
* No more dots. Remove the '/' we appended. |
| 1210 |
*-----------------------------------------------*/ |
| 1211 |
int len; |
| 1212 |
|
| 1213 |
if ((len = strlen (xname)) > 1) |
| 1214 |
xname [len - 1] = '\0'; |
| 1215 |
UxFree (fstr); |
| 1216 |
return xname; |
| 1217 |
} |
| 1218 |
UxFree (fstr); |
| 1219 |
} |
| 1220 |
|
| 1221 |
/*------------------------------------------------------------ |
| 1222 |
* If we failed the normal loop, just use the original. |
| 1223 |
*------------------------------------------------------------*/ |
| 1224 |
UxFree (fstr); |
| 1225 |
UxFree (xname); |
| 1226 |
return UxCopyString(fname); |
| 1227 |
} |
| 1228 |
|
| 1229 |
/****************************************************************************** |
| 1230 |
NAME: int CreateDirectory(path, mode) |
| 1231 |
|
| 1232 |
INPUT: char *path; -- directory to create. |
| 1233 |
unsigned short mode; -- protection mode. |
| 1234 |
RETURN: error status. |
| 1235 |
DESCRIPTION: Creates a directory with the specified mode, |
| 1236 |
if the directory does not exist, and the parent directory |
| 1237 |
is has write permission. |
| 1238 |
|
| 1239 |
LAST REV: July/31/91 |
| 1240 |
Oct/23/91 fix3074 |
| 1241 |
-----------------------------------------------------------------------------*/ |
| 1242 |
static int CreateDirectory (dir, mode) |
| 1243 |
char *dir; |
| 1244 |
unsigned short mode; |
| 1245 |
{ |
| 1246 |
int status; |
| 1247 |
|
| 1248 |
status = access (dir, 2); |
| 1249 |
|
| 1250 |
if (status == -1) { |
| 1251 |
if (errno == ENOENT) { |
| 1252 |
status = mkdir (dir, mode); |
| 1253 |
} |
| 1254 |
else if (errno == EISDIR) { |
| 1255 |
status = 0; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
return (status == 0 ? NO_ERROR : ERROR); |
| 1259 |
} |
| 1260 |
/****************************************************************************** |
| 1261 |
NAME: int UxGetDirMode(path, mode) |
| 1262 |
|
| 1263 |
INPUT: char *path; -- directory to create. |
| 1264 |
unsigned short *mode; -- protection mode. |
| 1265 |
RETURN: error status. |
| 1266 |
OUTPUT: protection mode |
| 1267 |
DESCRIPTION: gets the protection mode of the specified directory. |
| 1268 |
|
| 1269 |
LAST REV: July/31/91 |
| 1270 |
Oct 7/91 fix3031 |
| 1271 |
-----------------------------------------------------------------------------*/ |
| 1272 |
int UxGetDirMode (dir, mode) |
| 1273 |
char *dir; |
| 1274 |
unsigned short *mode; |
| 1275 |
{ |
| 1276 |
struct stat out; |
| 1277 |
int status; |
| 1278 |
|
| 1279 |
status = stat (dir, &out); |
| 1280 |
|
| 1281 |
if (status == 0) { |
| 1282 |
*mode = out.st_mode; |
| 1283 |
return NO_ERROR; |
| 1284 |
} |
| 1285 |
|
| 1286 |
return ERROR; |
| 1287 |
} |
| 1288 |
|
| 1289 |
/****************************************************************************** |
| 1290 |
NAME: int UxMakeRelativeDirs (absp, relp) |
| 1291 |
INPUT: char *absp; -- absolute path where to create relative dirs. |
| 1292 |
char *relp; -- relative directories to be created. |
| 1293 |
RETURN: error status. |
| 1294 |
DESCRIPTION: Create all directories in relp if they do not exist, and |
| 1295 |
if their parent directory has write access. |
| 1296 |
Each directory is built with the same access flags |
| 1297 |
as its parent directory. |
| 1298 |
|
| 1299 |
LAST REV: July/31/91 fix2845 |
| 1300 |
Oct 7/91 fix3031 |
| 1301 |
Oct/23/91 fix3074 |
| 1302 |
-----------------------------------------------------------------------------*/ |
| 1303 |
|
| 1304 |
int UxMakeRelativeDirs (absp, relp) |
| 1305 |
char *absp, *relp; |
| 1306 |
{ |
| 1307 |
char *token; |
| 1308 |
char *dir; |
| 1309 |
int status = NO_ERROR; |
| 1310 |
|
| 1311 |
if (!absp || !*absp) |
| 1312 |
return ERROR; |
| 1313 |
|
| 1314 |
if (!relp || !*relp) |
| 1315 |
return ERROR; |
| 1316 |
|
| 1317 |
dir = (char *)UxMalloc (strlen (absp) + 1); |
| 1318 |
strcpy (dir, absp); |
| 1319 |
|
| 1320 |
token = strtok (relp, "/"); |
| 1321 |
|
| 1322 |
while (token != NULL) { |
| 1323 |
char *tmp; |
| 1324 |
unsigned short mode; |
| 1325 |
|
| 1326 |
status = UxGetDirMode (dir, &mode); |
| 1327 |
tmp = UxCopyString (dir); |
| 1328 |
dir = UxRealloc (dir, strlen (tmp) + strlen (token) + 2); |
| 1329 |
sprintf (dir, "%s/%s", tmp, token); |
| 1330 |
|
| 1331 |
UxFree (tmp); |
| 1332 |
|
| 1333 |
if (status == NO_ERROR) |
| 1334 |
status = CreateDirectory (dir, mode); |
| 1335 |
if (status == ERROR) { |
| 1336 |
return ERROR; |
| 1337 |
} |
| 1338 |
|
| 1339 |
token = strtok (NULL, "/"); |
| 1340 |
|
| 1341 |
} |
| 1342 |
return status; |
| 1343 |
} |
| 1344 |
|
| 1345 |
/****************************************************************************** |
| 1346 |
NAME: UxValidateFilenameLength (fname) |
| 1347 |
INPUT: char *fname : name to truncate |
| 1348 |
int suffix_len : length of suffix to be added |
| 1349 |
OUTPUT: --- |
| 1350 |
RETURN: char * : truncated name |
| 1351 |
DESCRIPTION: Check if the passed filename satisfies the length requirements. |
| 1352 |
If the filename is too long, return an error message. Otherwise |
| 1353 |
return NULL. If filenameTruncation is not turned on, then the |
| 1354 |
filename is always valid. |
| 1355 |
EXT REFERENCES: --- |
| 1356 |
EXT EFFECTS: --- |
| 1357 |
LAST REV: |
| 1358 |
-----------------------------------------------------------------------------*/ |
| 1359 |
char *UxValidateFilenameLength (fname) |
| 1360 |
char *fname; |
| 1361 |
{ |
| 1362 |
int len; |
| 1363 |
char *name; |
| 1364 |
|
| 1365 |
if (!UxShouldTruncate()) |
| 1366 |
return NULL; |
| 1367 |
|
| 1368 |
/* If filename truncation is turned on, and the file exists, |
| 1369 |
* we will assume that the system can read the file and |
| 1370 |
* the validation is therefore successfull. |
| 1371 |
*/ |
| 1372 |
if (UxFileExists(fname)) |
| 1373 |
return NULL; |
| 1374 |
|
| 1375 |
name= strrchr(fname, '/'); |
| 1376 |
|
| 1377 |
if(name == NULL) { |
| 1378 |
name= fname; |
| 1379 |
} else { |
| 1380 |
name++; |
| 1381 |
} |
| 1382 |
|
| 1383 |
len = strlen(name); |
| 1384 |
if (len > MAX_FILE_LEN) { |
| 1385 |
return (GETMSG (LONG_FNAME)); |
| 1386 |
} else { |
| 1387 |
return NULL; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
/*------------------------------------------------------------------------ |
| 1394 |
* NAME: UxAbbreviateFileName |
| 1395 |
* |
| 1396 |
* INPUT: char* fname; |
| 1397 |
* RETURNS: a pointer to some point in 'fname', |
| 1398 |
* possibly fname itself. |
| 1399 |
* DESCRIPTION: |
| 1400 |
* If 'fname' is a full path to somewhere beneath |
| 1401 |
* the current directory, then return a pointer |
| 1402 |
* to the significant portion of the path : |
| 1403 |
* |
| 1404 |
* if pwd is /usr/bill/foo/proj: |
| 1405 |
* |
| 1406 |
* For: /usr/bill/foo/proj/sub/test.i |
| 1407 |
* | return ptr to here |
| 1408 |
* |
| 1409 |
* For: /usr/bill/foo/proj/a.i |
| 1410 |
* | return ptr to here |
| 1411 |
* |
| 1412 |
* For: /usr/bill/foo/other/ |
| 1413 |
* | return whole thing |
| 1414 |
* |
| 1415 |
* This is used to shorten expanded names when we emit messages. |
| 1416 |
* It does nothing fancy, only affecting paths that point inside |
| 1417 |
* the current directory. |
| 1418 |
* |
| 1419 |
* LAST REV: fix3553 Created. |
| 1420 |
*------------------------------------------------------------------------*/ |
| 1421 |
|
| 1422 |
char* UxAbbreviateFileName (fname) |
| 1423 |
char* fname; |
| 1424 |
{ |
| 1425 |
char* pwd = UxGetCurrentDir(); |
| 1426 |
int len = pwd ? strlen(pwd) : 0; |
| 1427 |
|
| 1428 |
/*------------------------------------------------------- |
| 1429 |
* Short-circuit of 'if' prevents filename[len] crashing. |
| 1430 |
*-------------------------------------------------------*/ |
| 1431 |
|
| 1432 |
if (pwd && |
| 1433 |
fname && |
| 1434 |
(strncmp(pwd, fname, len) == 0) && |
| 1435 |
fname[len] == '/') |
| 1436 |
{ |
| 1437 |
UxFree(pwd); |
| 1438 |
return fname + len + 1; |
| 1439 |
} |
| 1440 |
UxFree(pwd); |
| 1441 |
return fname; |
| 1442 |
} |