1
/*---------------------------------------------------------------------
2
* $Date: 2009-08-18 15:23:20 $ $Revision: 1.3 $
3
*---------------------------------------------------------------------
6
* Copyright (c) 1990, Visual Edge Software Ltd.
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.
21
*-------------------------------------------------------------------*/
28
#include <sys/types.h>
34
#include <X11/Intrinsic.h>
36
#if defined(sco) || defined(motor88) || defined(ncrix86) || defined(linux) || defined(vms)
49
#include <sys/param.h> /* for MAXPATHLEN... */
51
#define MAXPATHLEN 1024
52
#endif /* MAXPATHLEN */
53
#endif /* MOTIF_WIDGETS */
55
/* header files needed for messages */
59
#define CGETS_MISC(ms,ds_ms) UxCatGets(MC_MISC,ms,ds_ms)
60
#define GETMSG(ms) UXCATGETS(MC_MISC1,MS_MISC1_,ms)
63
/**** This define is the maximum number of characters in a filename
64
**** BEFORE the name is appended with '.rf' or '.c'
66
#define TRUNC_LENGTH 11
67
#define MAX_FILE_LEN 14
70
extern char *getenv();
71
extern char *UxToLowerString();
73
char *UxTruncateFilename();
75
extern int UxStrEqual();
76
extern void UxAppend_dstring();
79
/*****************************************************************************
80
NAME: int UxCheckForInvalidCharacters (filename)
82
INPUT: char *filename - filename to check
84
RETURN: int - 0 = ok, 1 = there are invalid characters
86
DESCRIPTION: Scans the filename entered by the user for invalid characters;
87
also checks for '//', which is illegal. An empty filename is
90
LAST REV: 10 April 1990
91
-- allow $, it's not invalid
92
-----------------------------------------------------------------------------*/
94
int UxCheckForInvalidCharacters (filename)
101
static char bad_set[] = "!@%^&*()=|\\`<>?\'\"[]{}; ";
103
/* check for zero length */
104
if (strlen (filename) == 0)
107
/* look for any occurence of bad characters */
108
if (Ux_strpbrk (filename, bad_set) != NULL)
112
/* filename is ok (or Ux_strpbrk failed!) */
116
/******************************************************************************
117
NAME: UxShouldTruncate ()
120
RETURN: int : TRUE / FALSE
121
DESCRIPTION: Checks if the resource "truncateFilenames" is set to TRUE
122
in which case we turn TRUE.
125
LAST REV: October 9 1991
126
-----------------------------------------------------------------------------*/
127
int UxShouldTruncate()
129
static int initialized = FALSE;
130
static int truncate = FALSE;
132
if ( initialized == FALSE )
134
char *UxGetUimxDefault(), *UxToLowerString(), *trunc_res;
135
/***********************************************/
136
/* Find out if filenames need to be truncated */
137
/* by examining the truncateFilenames */
139
/***********************************************/
143
trunc_res = UxGetUimxDefault ("truncateFilenames", "false");
144
if (UxStrEqual ( UxToLowerString(trunc_res), "false"))
151
/******************************************************************************
152
NAME: UxTruncateFilename (fname, suffix_len)
153
INPUT: char *fname : name to truncate
154
int suffix_len : length of suffix to be added
156
RETURN: char * : truncated name
157
DESCRIPTION: returns a filename
158
with its last component truncated to not more than 14
159
characters including the possible suffix.
160
NB! This function may modify the passed filename.
163
LAST REV: 5 November 1990 IBM Beta1
164
-- made this function available to OLE and UIM/X.
165
-- Also made the strcmp case insensitive by
166
calling UxToLowerString()
167
-----------------------------------------------------------------------------*/
169
char* UxTruncateName(fname, slen)
174
char str[MAX_FILE_LEN+1];
175
int len, tail_len, ncaps, ndigits;
176
int max_len, max_tail;
179
/*-----------------------------------------------------
180
* Get the file name itself, without path.
181
*-----------------------------------------------------*/
183
if ((name = strrchr(fname, '/')) != NULL) {
189
/*-----------------------------------------------------
190
* Is name part too long?
191
*-----------------------------------------------------*/
194
if ((len + slen) <= MAX_FILE_LEN) {
198
/*-------------------------------------------------------
199
* We shorten the name by sliding digits and capitals
200
* from the whole tail of the original name
201
* into the last max_tail characters of the shorter name.
202
* Count digits and caps so we can compute the tail length.
203
* Keep counting until we've found all the digits we
204
* can use; we'll drop caps in favour of digits.
205
* 'max_len-max_tail' is the first char allowed to change.
206
*-------------------------------------------------------*/
208
max_tail = (MAX_SUFFIX - slen);
209
max_len = (MAX_FILE_LEN - slen);
213
for (i = len; i >= max_len-max_tail; i--)
215
if (ndigits >= max_tail)
217
else if (ndigits + ncaps + i < max_len)
219
else if (isdigit(name[i]))
221
else if (isupper(name[i]))
225
/*-----------------------------------------------------
226
* How long a tail of digits and caps?
227
* If there are too many, keep more digits.
228
*-----------------------------------------------------*/
230
tail_len = ndigits + ncaps;
232
if (tail_len > max_tail) {
234
ncaps = tail_len - ndigits;
237
/*-----------------------------------------------------
238
* Shorten the name by moving up digits and caps.
239
* Note we may skip caps, but copy all digits we meet.
240
*-----------------------------------------------------*/
242
strncpy (str, name, sizeof(str));
244
for(i = j = (max_len-tail_len); i < len && j < max_len; i++)
246
if (isdigit(name[i])) {
248
} else if (isupper(name[i]) && ncaps-- > 0) {
254
/*-----------------------------------------------------
255
* 'name' is a pointer to where the filename part of
256
* the original path starts. str is a shortened name,
257
* so it will surely fit into the end of the original.
258
*-----------------------------------------------------*/
263
/******************************************************************************
264
NAME: UxTruncateFilename (fname, suffix_len)
265
INPUT: char *fname : name to truncate
266
int suffix_len : length of suffix to be added
268
RETURN: char * : truncated name
269
DESCRIPTION: If the 'truncateFilenames' resource is true, returns a filename
270
with its last component truncated to not more than 14
271
characters including the possible suffix.
272
NB! This function may modify the passed filename.
273
If the 'truncateFilenames' resource is false, just
274
return the given filename unchanged.
277
LAST REV: 5 November 1990 IBM Beta1
278
-- made this function available to OLE and UIM/X.
279
-- Also made the strcmp case insensitive by
280
calling UxToLowerString()
281
-----------------------------------------------------------------------------*/
283
char* UxTruncateFilename(fname, slen)
287
if(UxShouldTruncate() == FALSE) {
291
return UxTruncateName (fname, slen);
293
/*****************************************************************************
294
NAME: dstring gethome(username)
296
INPUT: dstring username - user name (with ~)
298
RETURN: home directory
300
DESCRIPTION: return the user 'uname's home directory
302
Accepts a dstring parameter, which it frees.
303
Creates and returns a dstring which should be freed by
304
the calling function.
306
LAST REV: 10 April 1990
307
-- parameter is a dstring
308
-----------------------------------------------------------------------------*/
310
static dstring gethome(username)
316
char *pntr, hold, *end;
319
uname = dnstr(username);
329
if(strcmp(uname, "~") == 0)
330
{ pntr= getenv("HOME");
333
{ home= dcreate(pntr);
341
if((pntr= getenv("USER")) != NULL)
349
{ home= dcreate(pw->pw_dir);
358
return(dcreate(NULL));
366
home= dcreate(pw->pw_dir);
377
/*****************************************************************************
378
NAME: dstring mkname(path, name)
380
INPUT: char *path - path to file
383
RETURN: full filename
385
DESCRIPTION: return the full filename of 'file' in the directory 'path'
386
LAST REV: Feb 19/1989
387
-----------------------------------------------------------------------------*/
389
static dstring mkname(path, name)
398
if ( (len > 0) && (path[len-1] != '/') )
406
/*****************************************************************************
407
NAME: dstring expand_macros(ds)
409
INPUT: dstring ds - macros to expand
411
RETURN: expanded macros
413
DESCRIPTION: expand all the macros in 'ds', free it, and return expansion
415
LAST REV: Feb 21/1989
416
-----------------------------------------------------------------------------*/
418
static dstring expand_macros(ds)
421
char *pntr, *start, *end = NULL, endc;
427
pntr= strchr(start, '$');
431
dappend(rtrn, start);
442
dappend(rtrn, getenv(pntr));
447
pntr= strchr(start, '$');
456
/*****************************************************************************
457
NAME: dstring tokenize(ds)
459
INPUT: dstring *ds - dstring to tokenize
461
RETURN: first token in 'ds'
463
DESCRIPTION: return the first token in 'ds' and reset 'ds' to point to
464
the rest of the string
466
LAST REV: July 25 1990
467
- handle separators at the end of a path.
468
- handle a path consisting of only separators.
469
-----------------------------------------------------------------------------*/
471
static dstring tokenize(ds)
475
dstring rtrn, dsrtrn;
480
return(dcreate(NULL));
482
while(*pntr && (strchr(": ,\n\t", *pntr) != NULL))
485
end= strpbrk(pntr, ": ,\n\t");
493
while (*end && (strchr(": ,\n\t", *end) != NULL))
496
dsrtrn= dcreate(end);
498
dsrtrn= dcreate(NULL);
500
dsrtrn= dcreate(NULL);
509
/******************************************************************************
510
NAME: dstring expand_name(tok)
511
INPUT: dstring tok - taken to expand
512
RETURN: expanded token (original freed if needed)
513
DESCRIPTION: expand the env vars and ~[uname]'s in tok
514
LAST REV: January 03 1991 bugfix 1116.
515
-----------------------------------------------------------------------------*/
518
* An always-true predicate to pass to XtResolvePath.
521
static Boolean AlwaysTrue(file)
525
static dstring expand_name(tok)
530
if(strchr(dnstr(tok), '$') != NULL)
532
exp = expand_macros(tok); /* tok is freed in expand_macros */
533
return(expand_name(exp)); /* exp is freed in expand_name */
535
else if(*(dnstr(tok)) == '~')
537
exp = gethome(tok); /* tok is freed in gethome */
538
return(expand_name(exp)); /* exp is freed in expand_name */
545
/* Expand language path character %L (and other toolkit defaults).
546
* Several arguments to XtResolvePathname are NULL so the toolkit
547
* defaults apply. We pass AlwyasTrue because UIM/X builds its
548
* paths piece-by-piece; the partial paths we expand here aren't
549
* meant to name exisiting files.
553
r = XtResolvePathname (
555
(String) NULL, /* type o%T e ntion */
556
(String) getenv("UXAPP"), /* name (app s) for %N */
557
(String) NULL, /* suffix (none) */
558
(String) dnstr(tok), /* path */
559
(Substitution) NULL, /* substitutions */
560
0, /* num substitions */
561
(XtFilePredicate) AlwaysTrue /* file access predicate */
579
#endif /* MOTIF_WIDGETS */
581
/*****************************************************************************
582
NAME: int UxFileExists(fname)
584
INPUT: char *fname - filename
586
RETURN: 1: file or directory exists 0: file not found
588
DESCRIPTION: return whether the file or directory 'fname' exists
590
LAST REV: March 18, 1991
591
Replaced stat call by more efficient access call.
592
-----------------------------------------------------------------------------*/
594
int UxFileExists(fname)
597
if (access(fname, 0) < 0)
603
/*****************************************************************************
604
NAME: pathlist UxInitPath(str)
606
INPUT: char *str - initial path list
608
RETURN: pathlist object
610
DESCRIPTION: initialize a path list to files.
611
LAST REV: Feb 19/1989
612
-----------------------------------------------------------------------------*/
614
pathlist UxInitPath(str)
619
path= (pathlist) UxMalloc(sizeof(dstring));
621
*path= dcreate(NULL);
623
UxAddPath(path, str);
628
/*****************************************************************************
629
NAME: void UxAddPath(path, str)
631
INPUT: pathlist path - pathlist
632
char *str - list of dirs to add
634
DESCRIPTION: add the directories in 'str' to the pathlist 'path'
636
LAST REV: Feb 19/1989
637
-----------------------------------------------------------------------------*/
639
void UxAddPath(path, str)
645
if((str == NULL) || (str[0] == '\0'))
653
tok= tokenize(&pcopy);
655
while(dgetstr(tok) != NULL)
656
{ tok= expand_name(tok);
658
if(strlen(dnstr(tok)) != 0)
659
{ if(dlen(*path) != 0)
666
tok= tokenize(&pcopy);
669
/* don't need to free tok or pcopy, since they are now null */
672
/*****************************************************************************
673
NAME: void UxResetPath(path, str)
675
INPUT: pathlist path - pathlist
676
char *str - new list of paths
678
DESCRIPTION: reset the pathlist 'path' to be 'str'
680
LAST REV: Feb 19/1989
681
-----------------------------------------------------------------------------*/
683
void UxResetPath(path, str)
689
UxAddPath(path, str);
692
/*****************************************************************************
693
NAME: void UxFreePath(path)
695
INPUT: pathlist path - pathlist
697
DESCRIPTION: free the pathlist 'path'
699
LAST REV: Feb 19/1989
700
-----------------------------------------------------------------------------*/
702
void UxFreePath(path)
709
/*****************************************************************************
710
NAME: char *UxGetPath(path)
712
INPUT: pathlist path - pathlist
714
RETURN: pathlist in string form
716
DESCRIPTION: return the pathlist as a string (not to be freed)
718
LAST REV: Feb 19/1989
719
-----------------------------------------------------------------------------*/
721
char *UxGetPath(path)
727
return(dnstr(*path));
730
/*****************************************************************************
731
NAME: char *UxExpandFilename(path_list, fname)
733
INPUT: pathlist path_list - pathlist
734
char *fname - filename
736
RETURN: expanded filename
738
DESCRIPTION: Find the expanded name of 'fname' using the dirs in 'path'
740
LAST REV: April 10, 1990
741
-- free filename at the proper place
742
-----------------------------------------------------------------------------*/
744
char *UxExpandFilename(path_list, fname)
748
static dstring filename= UxEmptyDstring;
752
file= expand_name(dcreate(fname));
761
filename= dcreate(fname);
765
if(UxFileExists(dnstr(filename)))
766
return(dnstr(filename));
771
if(path_list == NULL)
776
path= dcopy(*path_list);
778
pntr= strtok(dnstr(path), ": ,\n\t");
783
filename= mkname(pntr, fname);
785
if(UxFileExists(dnstr(filename)))
788
return(dnstr(filename));
791
pntr= strtok(NULL, ": ,\n\t");
800
/*****************************************************************************
801
NAME: char *UxExpandAllFilenames(path_list, fname)
803
INPUT: pathlist path_list - pathlist or NULL
804
char *fname - filename
806
RETURN: expanded filename
808
DESCRIPTION: Find the expanded name of 'fname' using the dirs in 'path_list'
809
Remember position in path_list between calls so that subsequent
810
calls can continue in the list where the previous call left off.
811
If path_list is NULL then continue from previous path otherwise
812
start from given path_list. Don't return same expanded name
813
more than once. If fname is NULL then use previous fname.
815
LAST REV: March 13, 1991 See bugfix 2111
816
-----------------------------------------------------------------------------*/
818
char *UxExpandAllFilenames(path_list, fname)
822
static dstring filename= UxEmptyDstring;
823
static dstring path= UxEmptyDstring, file= UxEmptyDstring;
824
static dstring alreadyReturned= UxEmptyDstring;
830
file= expand_name(dcreate(fname));
841
filename= dcreate(fname);
843
if (strstr(dnstr(alreadyReturned), fname) == NULL &&
844
UxFileExists(dnstr(filename))) {
845
dappend(alreadyReturned, ":");
846
dconcat(alreadyReturned, filename);
847
return(dnstr(filename));
852
if (path_list != NULL) {
854
path= dcopy(*path_list);
855
dappend(path, ":"); /* Make sure we end with ':' */
856
dfree(alreadyReturned);
857
alreadyReturned = dcreate("");
858
pntr= strtok(dnstr(path), ": ,\n\t");
862
* Start from where we left off or from beginning if new path_list
868
filename= mkname(pntr, fname);
870
pntr[strlen(pntr)] = ':';
871
pntr= strtok(NULL, ": ,\n\t");
873
if (strstr(dnstr(alreadyReturned), dgetstr(filename)) == NULL
874
&& UxFileExists(dgetstr(filename))) {
876
* Restore path string before returning.
878
dappend(alreadyReturned, ":");
880
/* The UIM/X 2.0 releases mostly have
881
* 'dgetstr(filename)' in the line below,
884
dconcat(alreadyReturned, filename);
885
return(dgetstr(filename));
892
/******************************************************************************
893
NAME: char *UxGetFilename(fname)
895
INPUT: char *fname - filename
897
RETURN: expanded filename
899
DESCRIPTION: expand all ~'s and $VAR's in the passed in filename
901
LAST REV: 15 August 1990 OLE MERGE
902
-- removed the call to TruncateFilename now
903
called from cfile.c and save.c
904
-----------------------------------------------------------------------------*/
906
char *UxGetFilename(fname)
909
static dstring filename= UxEmptyDstring;
911
/* check for invalid characters in supplied filename */
912
if (UxCheckForInvalidCharacters (fname))
917
filename= expand_name(dcreate(fname));
919
if (dnstr (filename)) {
920
char *xname, *CondenseDots();
922
if ((xname = CondenseDots (dnstr(filename)))) {
924
filename = dcreate (xname);
929
return(dnstr(filename));
932
/*****************************************************************************
933
NAME: char *UxExpandEnv(str)
935
INPUT: char *str - string to expand
937
RETURN: expanded string
939
DESCRIPTION: Expand all the environment variables in 'str' until there are
940
no more '$'s in the string:
942
LAST REV: Feb 22/1989
943
-----------------------------------------------------------------------------*/
945
char *UxExpandEnv(str)
948
static dstring rtrn= UxEmptyDstring;
954
while(strchr(dnstr(rtrn), '$') != NULL)
955
rtrn= expand_macros(rtrn);
959
/******************************************************************************
960
NAME: char * UxGetDirName(path)
962
INPUT: char *path; -- path to extract the directory from
963
RETURN: directory name if the path is not NULL, must be freed.
964
otherwise, returns NULL, nothing should be freed..
966
DESCRIPTION: Strips the filename from the path.
967
If the path is not valid or is just a filename,
968
the function sets the path to the current directory
969
The return value must be freed.
971
LAST REV: Feb 28 1991
972
-----------------------------------------------------------------------------*/
973
char *UxGetDirName(path)
977
char *pntr, *dirname;
979
if (path == (char *) NULL) {
980
return (char *) NULL;
984
dirname = (char *) UxMalloc((len + 1) * sizeof(char));
986
strcpy(dirname, path);
988
pntr = strrchr(dirname, '/');
991
strcpy(dirname, "/");
995
strcpy(dirname, ".");
1002
/******************************************************************************
1003
NAME: int UxWriteAccess(path, error)
1005
INPUT: char *path; -- the path to access
1006
int *error; -- a return error flag
1007
RETURN: Checks if a path can be accessed.
1009
The calling function should check the return value from
1011
If it is -1, then it should check for the following error flags:
1012
error = EACCES -> file or directory is not writable.
1013
error = ENOENT -> file or directory do not exist
1014
error = EISDIR -> path is a directory
1016
If it is 0, then it should check for the following error flags:
1018
error = EEXIST -> file exists already
1019
error = ENOENT -> file does not exist
1021
DESCRIPTION: Checks for the write permission on path.
1022
7 cases could arise:
1024
(a) path is NULL --> return -1.
1025
(b) path is a directory --> return -1.
1026
(c) directory specified in path is not writable
1027
and file does not exist --> return -1.
1028
(d) directory specified in path is not writable
1029
and file does not exist --> return -1.
1030
(e) dir is valid, and file does not exist. --> return 0.
1031
(f) path is valid, and file is not writable --> return -1.
1032
(g) path is valid, and file is writable --> return 0.
1034
LAST REV: Feb 28 1991
1035
-----------------------------------------------------------------------------*/
1036
int UxWriteAccess (path,error)
1045
if ((path == NULL) || (*path == '\0')) {
1050
/* check if the path points to a filename, or a directory */
1051
result = stat(path, &buf);
1054
/* path is not valid, find out why */
1056
dirname = UxGetDirName (path);
1057
result = access(dirname, W_OK);
1060
/* directory is not writable, and file does not exist */
1065
/* directory is writable, file does not exist, this is OK */
1073
/* path points to a directory */
1074
if (buf.st_mode & S_IFDIR) {
1079
/* file is not writable */
1080
if (!(buf.st_mode & S_IWRITE)) {
1084
/* file exists already and is writable */
1094
/******************************************************************************
1095
NAME: char *UxGetCurrentDir ()
1097
RETURN: char * -current directory
1099
DESCRIPTION: gets the current directory. Returns allocated memory, therefore
1100
calling function should free memory.
1101
CREATED: Oct 29/91 fix3099
1103
-----------------------------------------------------------------------------*/
1104
char *UxGetCurrentDir ()
1106
char *getcwd (), *cwd;
1108
if ((cwd = (char *) UxMalloc (MAXPATHLEN +1))) {
1109
return getcwd (cwd, MAXPATHLEN + 1);
1111
return (char *)NULL;
1113
/******************************************************************************
1114
NAME: char CondenseDots (fname)
1116
INPUT: char *fname; -- path to extract the directory from
1117
RETURN: expanded path;
1120
DESCRIPTION: Expands dots in specified file path. Allocates
1121
memory for buffer, therefore, calling function
1122
should free return value. The argument is expanded
1123
to an absolute name in the process.
1127
-----------------------------------------------------------------------------*/
1129
char *CondenseDots (fname)
1132
char *fstr, *xname = (char *)NULL;
1134
if (fname == NULL) {
1136
} else if (*fname == 0) {
1137
return UxCopyString(fname);
1140
/*-------------------------------------------------------------
1141
* If fname does not begin with a slash, prepend current path.
1142
*------------------------------------------------------------*/
1143
if (fname [0] != '/') {
1146
cwd = UxGetCurrentDir ();
1148
xname = (char *) UxMalloc (strlen(cwd) + strlen(fname) + 3);
1149
sprintf (xname, "%s/%s/", cwd, fname);
1153
xname = (char *) UxMalloc (strlen(fname) + 2);
1154
sprintf (xname, "%s/", fname);
1157
/*----------------------------------------------------
1158
* Append a slash to the path, needed for below.
1159
*----------------------------------------------------*/
1160
xname = UxMalloc (strlen (fname) + 2);
1161
sprintf (xname, "%s/", fname);
1164
/*-----------------------------------------------
1165
* Loop until all dots have been condensed out
1166
*----------------------------------------------*/
1170
fstr = UxCopyString (xname);
1172
if ((dots = strstr(fstr, "/./")))
1174
/*-----------------------------
1175
* "/./" -- remove "./"
1176
*-----------------------------*/
1178
sprintf (xname, "%s%s", fstr, dots +3);
1180
else if ((dots = strstr(fstr, "/../")))
1182
/*-------------------------------------
1183
* remove /../ and preceding directory
1184
*-------------------------------------*/
1186
/* Path starts in "/../", not much to do.
1190
char *tail = dots + 4;
1192
while (*(--dots) != '/') {
1199
sprintf (xname, "%s%s", fstr, tail);
1201
break; /* No leading /name/../ */
1205
/*-----------------------------------------------
1206
* No more dots. Remove the '/' we appended.
1207
*-----------------------------------------------*/
1210
if ((len = strlen (xname)) > 1)
1211
xname [len - 1] = '\0';
1218
/*------------------------------------------------------------
1219
* If we failed the normal loop, just use the original.
1220
*------------------------------------------------------------*/
1223
return UxCopyString(fname);
1226
/******************************************************************************
1227
NAME: int CreateDirectory(path, mode)
1229
INPUT: char *path; -- directory to create.
1230
ushort mode; -- protection mode.
1231
RETURN: error status.
1232
DESCRIPTION: Creates a directory with the specified mode,
1233
if the directory does not exist, and the parent directory
1234
is has write permission.
1236
LAST REV: July/31/91
1238
-----------------------------------------------------------------------------*/
1239
static int CreateDirectory (dir, mode)
1245
status = access (dir, 2);
1248
if (errno == ENOENT) {
1249
status = mkdir (dir, mode);
1251
else if (errno == EISDIR) {
1255
return (status == 0 ? NO_ERROR : ERROR);
1257
/******************************************************************************
1258
NAME: int UxGetDirMode(path, mode)
1260
INPUT: char *path; -- directory to create.
1261
ushort *mode; -- protection mode.
1262
RETURN: error status.
1263
OUTPUT: protection mode
1264
DESCRIPTION: gets the protection mode of the specified directory.
1266
LAST REV: July/31/91
1268
-----------------------------------------------------------------------------*/
1269
int UxGetDirMode (dir, mode)
1276
status = stat (dir, &out);
1279
*mode = out.st_mode;
1286
/******************************************************************************
1287
NAME: int UxMakeRelativeDirs (absp, relp)
1288
INPUT: char *absp; -- absolute path where to create relative dirs.
1289
char *relp; -- relative directories to be created.
1290
RETURN: error status.
1291
DESCRIPTION: Create all directories in relp if they do not exist, and
1292
if their parent directory has write access.
1293
Each directory is built with the same access flags
1294
as its parent directory.
1296
LAST REV: July/31/91 fix2845
1299
-----------------------------------------------------------------------------*/
1301
int UxMakeRelativeDirs (absp, relp)
1306
int status = NO_ERROR;
1308
if (!absp || !*absp)
1311
if (!relp || !*relp)
1314
dir = (char *)UxMalloc (strlen (absp) + 1);
1317
token = strtok (relp, "/");
1319
while (token != NULL) {
1323
status = UxGetDirMode (dir, &mode);
1324
tmp = UxCopyString (dir);
1325
dir = UxRealloc (dir, strlen (tmp) + strlen (token) + 2);
1326
sprintf (dir, "%s/%s", tmp, token);
1330
if (status == NO_ERROR)
1331
status = CreateDirectory (dir, mode);
1332
if (status == ERROR) {
1336
token = strtok (NULL, "/");
1342
/******************************************************************************
1343
NAME: UxValidateFilenameLength (fname)
1344
INPUT: char *fname : name to truncate
1345
int suffix_len : length of suffix to be added
1347
RETURN: char * : truncated name
1348
DESCRIPTION: Check if the passed filename satisfies the length requirements.
1349
If the filename is too long, return an error message. Otherwise
1350
return NULL. If filenameTruncation is not turned on, then the
1351
filename is always valid.
1355
-----------------------------------------------------------------------------*/
1356
char *UxValidateFilenameLength (fname)
1362
if (!UxShouldTruncate())
1365
/* If filename truncation is turned on, and the file exists,
1366
* we will assume that the system can read the file and
1367
* the validation is therefore successfull.
1369
if (UxFileExists(fname))
1372
name= strrchr(fname, '/');
1381
if (len > MAX_FILE_LEN) {
1382
return (GETMSG (LONG_FNAME));
1390
/*------------------------------------------------------------------------
1391
* NAME: UxAbbreviateFileName
1393
* INPUT: char* fname;
1394
* RETURNS: a pointer to some point in 'fname',
1395
* possibly fname itself.
1397
* If 'fname' is a full path to somewhere beneath
1398
* the current directory, then return a pointer
1399
* to the significant portion of the path :
1401
* if pwd is /usr/bill/foo/proj:
1403
* For: /usr/bill/foo/proj/sub/test.i
1404
* | return ptr to here
1406
* For: /usr/bill/foo/proj/a.i
1407
* | return ptr to here
1409
* For: /usr/bill/foo/other/
1410
* | return whole thing
1412
* This is used to shorten expanded names when we emit messages.
1413
* It does nothing fancy, only affecting paths that point inside
1414
* the current directory.
1416
* LAST REV: fix3553 Created.
1417
*------------------------------------------------------------------------*/
1419
char* UxAbbreviateFileName (fname)
1422
char* pwd = UxGetCurrentDir();
1423
int len = pwd ? strlen(pwd) : 0;
1425
/*-------------------------------------------------------
1426
* Short-circuit of 'if' prevents filename[len] crashing.
1427
*-------------------------------------------------------*/
1431
(strncmp(pwd, fname, len) == 0) &&
1435
return fname + len + 1;