~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to gui/GraphLib/libsrc/uimxR5/src/filepath.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

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