File: path.c

package info (click to toggle)
xdir 2.1-3
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 2,376 kB
  • ctags: 2,449
  • sloc: ansic: 30,140; makefile: 481; sh: 64
file content (994 lines) | stat: -rw-r--r-- 28,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/***************************************************************************/
/***************************************************************************/
/*                                                                         */
/*   (c) 1995-1998.  The Regents of the University of California.  All     */
/*   rights reserved.                                                      */
/*                                                                         */
/*   This work was produced at the University of California, Lawrence      */
/*   Livermore National Laboratory (UC LLNL) under contract no.            */
/*   W-7405-ENG-48 (Contract 48) between the U.S. Department of Energy     */
/*   (DOE) and The Regents of the University of California (University)    */
/*   for the operation of UC LLNL.  Copyright is reserved to the           */
/*   University for purposes of controlled dissemination,                  */
/*   commercialization through formal licensing, or other disposition      */
/*   under terms of Contract 48; DOE policies, regulations and orders;     */
/*   and U.S. statutes.  The rights of the Federal Government are          */
/*   reserved under Contract 48 subject to the restrictions agreed upon    */
/*   by the DOE and University.                                            */
/*                                                                         */
/*                                                                         */
/*                              DISCLAIMER                                 */
/*                                                                         */
/*   This software was prepared as an account of work sponsored by an      */
/*   agency of the United States Government.  Neither the United States    */
/*   Government nor the University of California nor any of their          */
/*   employees, makes any warranty, express or implied, or assumes any     */
/*   liability or responsibility for the accuracy, completeness, or        */
/*   usefulness of any information, apparatus, product, or process         */
/*   disclosed, or represents that its specific commercial products,       */
/*   process, or service by trade name, trademark, manufacturer, or        */
/*   otherwise, does not necessarily constitute or imply its               */
/*   endorsement, recommendation, or favoring by the United States         */
/*   Government or the University of California. The views and opinions    */
/*   of the authors expressed herein do not necessarily state or reflect   */
/*   those of the United States Government or the University of            */
/*   California, and shall not be used for advertising or product          */
/*   endorsement purposes.                                                 */
/*                                                                         */
/*   Permission to use, copy, modify and distribute this software and its  */
/*   documentation for any non-commercial purpose, without fee, is         */
/*   hereby granted, provided that the above copyright notice and this     */
/*   permission notice appear in all copies of the software and            */
/*   supporting documentation, and that all UC LLNL identification in      */
/*   the user interface remain unchanged.  The title to copyright LLNL     */
/*   XDIR shall at all times remain with The Regents of the University     */
/*   of California and users agree to preserve same. Users seeking the     */
/*   right to make derivative works with LLNL XDIR for commercial          */
/*   purposes may obtain a license from the Lawrence Livermore National    */
/*   Laboratory's Technology Transfer Office, P.O. Box 808, L-795,         */
/*   Livermore, CA 94550.                                                  */
/*                                                                         */
/***************************************************************************/
/***************************************************************************/

#include <ctype.h>
#include <X11/Intrinsic.h>
#include "xdir.h"
#include "str.h"


/*
 * unix_path_to_links - Parses the UNIX path name into its constituent links. 
 *                      unix_path_to_links returns a pointer to an array of
 *                		pointers to the link names.  The last entry in the
 *                 		array is a NULL pointer.  Call release_path_links()
 *                      to free memory returned by unix_path_to_links().
 */
char **
unix_path_to_links(path)
char *path;
{
	char *p;
	char *q;
	char *pathcopy;
	char **links;
	int nlinks;

	/* Check for null path name */
	if (path[0] == '\0')
		fatal_error("Trouble in unix_path_to_links()");

	/* Need to copy path because routine strtok() is destructive */
	pathcopy = XtNewString(path);

	/* Allocate space for array of link names */
	links = (char **)XtMalloc(sizeof(char *)*(MAXLINKS+1));
		
	/* Special case first link of full path name */
	if (pathcopy[0] == '/') {
		if (pathcopy[1] == '/') {              /* Handle Apollo */
			links[0] = XtNewString("//");
			p = &pathcopy[2];
		} else {
			links[0] = XtNewString("/");
			p = &pathcopy[1];
		}
		nlinks = 1;
	} else {
		p = pathcopy;
		nlinks = 0;
	}

	/* Loop through links of path name */
	while ((q=strtok(p, "/")) != NULL) {
		if (nlinks == MAXLINKS)
			fatal_error("Trouble in unix_path_to_links()");
		links[nlinks++] = XtNewString(q);
		p = NULL;
	}

	/* Success */
	links[nlinks] = NULL;
	XtFree(pathcopy);
	return links;
}


/*
 * dw_path_to_links - Parses the DOS/Windows path name into its constituent
 *                    links.  "delim" is the character that delimits
 *                    links in the path.  Use '\' for DOS and '/' for 
 *                    Windows.  dw_path_to_links returns a pointer to an
 *                    array of pointers to the link names.  The last entry
 *                    in the array is a NULL pointer.  Call
 *                    release_path_links() to free memory returned by
 *                    dw_path_to_links().
 */
char **
dw_path_to_links(path, delim)
char *path;
int delim;
{
	char *p;
	char *q;
	char *pathcopy;
	char **links;
	char temp[2];
	int nlinks;
	int len;

	/* Check for null path name */
	if (path[0] == '\0')
		fatal_error("Trouble in dw_path_to_links()");

	/* Need to copy path because routine strtok() is destructive */
	pathcopy = XtNewString(path);

	/* Allocate space for array of link names */
	links = (char **)XtMalloc(sizeof(char *)*(MAXLINKS+1));
		
	/* Special case first link of full path name */
	len = strlen(pathcopy);
	p = pathcopy;
	if ((len > 1) && (pathcopy[1] == ':')) {
		links[0] = XtMalloc(4);
		links[0][0] = *p++;
		links[0][1] = *p++;
		if ((len > 2) && (pathcopy[2] == delim)) {
			links[0][2] = *p++;
			links[0][3] = '\0';
		} else
			links[0][2] = '\0';
		nlinks = 1;
	} else
		nlinks = 0;

	/* Loop through links of path name */
	temp[0] = delim;
	temp[1] = '\0';
	while ((q=strtok(p, temp)) != NULL) {
		if (nlinks == MAXLINKS)
			fatal_error("Trouble in dw_path_to_links()");
		links[nlinks++] = XtNewString(q);
		p = NULL;
	}

	/* Success */
	links[nlinks] = NULL;
	XtFree(pathcopy);
	return links;
}


/*
 * dos_path_to_links - Parses the DOS path name into its constituent links. 
 *                     dos_path_to_links returns a pointer to an array of
 *              	   pointers to the link names.  The last entry in the
 *                 	   array is a NULL pointer.  Call release_path_links()
 *                     to free memory returned by dos_path_to_links().
 */
char **
dos_path_to_links(path)
char *path;
{
	return dw_path_to_links(path, '\\');
}


/*
 * windows_path_to_links - Parses the Windows path name into its
 *                         constituent links.  windows_path_to_links
 *                         returns a pointer to an array of pointers
 *                         to the link names.  The last entry in the
 *                         array is a NULL pointer. Call
 *                         release_path_links() to free memory returned
 *                         by windows_path_to_links().
 */
char **
windows_path_to_links(path)
char *path;
{
	return dw_path_to_links(path, '/');
}


/*
 * vms_path_to_links - Parses the path name into its constituent links. 
 *                     vms_path_to_links returns a pointer to an array of
 *                	   pointers to the link names.  The last entry in the
 *                 	   array is a NULL pointer.  Call release_path_links()
 *                     to free memory returned by vms_path_to_links().
 *                     Some examples:
 *
 *                        a::b:[c.d]e --> a::, b:, c.dir, d.dir, e
 *                        b:[c.d]e    --> b:, c.dir, d.dir, e
 *                        [.c.d]e     --> c.dir, d.dir, e
 *                        [.c]        --> c.dir
 *                        e           --> e
 *                        c.dir       --> c.dir
 */
char **
vms_path_to_links(path)
char *path;
{
	char *pathcopy;
	char **links;
	int nlinks = 0;
	char *q;
	char *p;
	int len;
	char *filename;
	int i;

	/* Check for null path name */
	if (path[0] == '\0')
		fatal_error("Trouble in vms_path_to_links()");

	/* Need to copy path because routine strtok() is destructive */
	pathcopy = XtNewString(path);

	/* Allocate space for array of link names */
	links = (char **)XtMalloc(sizeof(char *)*(MAXLINKS+1));

    /* Network node */
    p = pathcopy;
    if ((q = strstr(p, "::"))) {
        len = q-p+2;
        links[nlinks] = XtMalloc(len+1);
        for (i=0; i<len; i++)
            links[nlinks][i] = p[i];
        links[nlinks][len] = '\0';
        nlinks++;
        p = q+2;
    }

    /* Device */
    if ((q = strchr(p, ':'))) {
        len = q-p+1;
        links[nlinks] = XtMalloc(len+1);
        for (i=0; i<len; i++)
            links[nlinks][i] = p[i];
        links[nlinks][len] = '\0';
        nlinks++;
        p = q+1;
    }

    /* Directory and file */
    if (*p == '[') {
        p++;
		if (*p == '.') {
			if (nlinks != 0)
				fatal_error("Trouble in vms_path_to_links()");
			p++;
		} else if (nlinks == 0)
			fatal_error("Trouble in vms_path_to_links()");
        filename = strchr(pathcopy, ']');
        if (filename == NULL || filename == p)
            fatal_error("Trouble in vms_path_to_links()");
        *filename++ = '\0';
        while ((q=strtok(p, ".")) != NULL) {
            if (nlinks == MAXLINKS)
                fatal_error("Trouble in vms_path_to_links()");
            links[nlinks] = XtMalloc(strlen(q)+5);
            strcpy(links[nlinks], q);
            strcat(links[nlinks], ".dir");
            nlinks++;
            p = NULL;
        }
        if (strlen(filename)) {
            if (nlinks == MAXLINKS)
                fatal_error("Trouble in vms_path_to_links()");
            links[nlinks++] = XtNewString(filename);
        }
    } else {
        links[nlinks++] = XtNewString(p);
    }

	/* Success */
	links[nlinks] = NULL;
	XtFree(pathcopy);
	return links;
}


/*
 * path_to_links - Parses the path name into its constituent links. 
 *                 path_to_links returns a pointer to an array of
 *                 pointers to the link names.  The last entry in the
 *                 array is a NULL pointer.  Call release_path_links()
 *                 to free memory returned by path_to_links().  "system"
 *                 identifies the system (e.g.,  SYS_UNIX).
 */
char **
path_to_links(system, path)
int system;
char *path;
{
	switch (system) {
	case SYS_VMS:
		return vms_path_to_links(path);
	case SYS_DOS:
		return dos_path_to_links(path);
	case SYS_WINDOWS:
		return windows_path_to_links(path);
	default:
		return unix_path_to_links(path);
	}
}


/*
 * release_path_links - Release the memory returned by path_to_links().
 */
release_path_links(links)
char **links;
{
	char **ptr = links;
	
	/* Check for null pointer */
	if (links == NULL)
		return;

	/* Release memory */
	while (*ptr)
		XtFree(*ptr++);
	XtFree ((char *)links);
	links = NULL;
}


/*
 * reverse_link_order - Reverse the order of the directory links in the
 *                      array returned by path_to_links().
 */
reverse_link_order(links)
char **links;
{
	int nlinks = 0;
	int nlim;
	int i;
	char **ptr = links;
	char *temp;

	while (*ptr++)
		nlinks++;

	nlim = nlinks/2;

	for (i=0; i<nlim; i++) {
		temp = links[i];
		links[i] = links[nlinks-1-i];
		links[nlinks-1-i] = temp;
	}
}


/*
 * unix_links_to_path - Construct a Unix path from its constituent links.
 *                      "links" is a pointer to an array of pointers to the
 *                      link names.  "nlinks" is the number of entries in
 *                      "links".  Call XtFree() to free the memory returned
 *                      by unix_links_to_path().
 */
char *
unix_links_to_path(links, nlinks)
char **links;
int nlinks;
{
	int len = 0;
	int i;
	char *path;

    /* Determine length of path name */
    for (i=0; i<nlinks; i++)
        len += strlen(links[i])+1;

    /* Build path name for new working directory */
    path = XtMalloc(len+1);
    strcpy(path, "");
    for (i=0; i<nlinks; i++) {
        strcat(path, links[i]);
        if (path[strlen(path)-1] != '/' && i < nlinks-1)
            strcat(path, "/");
    };

	return path;
}


/*
 * nt_links_to_path - Construct NT path from its constituent links.
 *                    "links" is a pointer to an array of pointers to the
 *                    link names.  "nlinks" is the number of entries in
 *                    "links".  Call XtFree() to free the memory returned
 *                    by nt_links_to_path().
 */
char *
nt_links_to_path(links, nlinks)
char **links;
int nlinks;
{
	int len = 0;
	int i;
	char *path;

	/* Determine length of path name */
	for (i=0; i<nlinks; i++)
		len += strlen(links[i])+1;

	/* Build path name for new working directory */
	path = XtMalloc(len+1);
	strcpy(path, "");
	for (i=0; i<nlinks; i++) {
		strcat(path, links[i]);
		if (path[strlen(path)-1] != '/' && i < nlinks-1)
			strcat(path, "/");
	};

	/* Make sure that "x:" looks like "x:/" */
	if (nlinks == 1 && strlen(path) == 2 && path[1] == ':')
		strcat(path, "/");

	return path;
}


/*
 * dw_links_to_path - Construct a DOS/Windows path from its constituent
 *                    links. "delim" is the character that delimits links
 *                    in the path.  Use '\' for DOS and '/' for Windows.
 *                    "links" is a pointer to an array of pointers to the
 *                    link names.  "nlinks" is the number of entries in
 *                    "links".  Call XtFree() to free the memory returned
 *                    by dw_links_to_path().
 */
char *
dw_links_to_path(links, nlinks, delim)
char **links;
int nlinks;
int delim;
{
	int len = 0;
	int i;
	char *path;
	char temp[2];

    /* Determine length of path name */
    for (i=0; i<nlinks; i++)
        len += strlen(links[i])+1;

    /* Build path name for new working directory */
	temp[0] = delim;
	temp[1] = '\0';
    path = XtMalloc(len+1);
    strcpy(path, "");
    for (i=0; i<nlinks; i++) {
        strcat(path, links[i]);
        if (path[strlen(path)-1] != delim && i < nlinks-1)
            strcat(path, temp);
    };

	return path;
}


/*
 * dos_links_to_path - Construct a DOS path from its constituent links.
 *                     "links" is a pointer to an array of pointers to the
 *                     link names.  "nlinks" is the number of entries in
 *                     "links".  Call XtFree() to free the memory returned
 *                     by dos_links_to_path().
 */
char *
dos_links_to_path(links, nlinks)
char **links;
int nlinks;
{
	return dw_links_to_path(links, nlinks, '\\');
}


/*
 * windows_links_to_path - Construct a Windows path from its constituent
 *                         links.  "links" is a pointer to an array of
 *                         pointers to the link names.  "nlinks" is the
 *                         number of entries in "links".  Call XtFree()
 *                         to free the memory returned by
 *                         windows_links_to_path().
 */
char *
windows_links_to_path(links, nlinks)
char **links;
int nlinks;
{
	return dw_links_to_path(links, nlinks, '/');
}


/*
 * vms_links_to_path - Construct a VMS path from its constituent links.
 *                     "links" is a pointer to an array of pointers to the
 *                     link names.  "nlinks" is the number of entries in
 *                     "links".  Call XtFree() to free the memory returned
 *                     by vms_links_to_path().
 */
char *
vms_links_to_path(links, nlinks)
char **links;
int nlinks;
{
	int len; 
	int i;
	int state = 0;
	char *path;
	char *q;
	char *temp;

	/* Conservatively allocate enough memory to hold path */
	len = 2;
	for (i=0; i<nlinks; i++)
		len += strlen(links[i])+1;
	path = XtMalloc(len+1);

    /* Construct VMS path */
    path[0] = '\0';
	for (i=0; i<nlinks; i++) {
        len = strlen(links[i]);
        if (len == 0)
            fatal_error("Problem in vms_links_to_path()");
        switch (state) {
        case 0:
            if ((len > 2) && !strcmp(&links[i][len-2], "::")) {
                strcat(path, links[i]);
                state = 1;
            } else if ((len > 1) && (links[i][len-1] == ':')) {
                strcat(path, links[i]);
                state = 2;
            } else if (strstr(links[i], ".dir")) {
                strcat(path, "[.");
				temp = XtNewString(links[i]);
				q = strstr(temp, ".dir");
                *q = '\0';
                strcat(path, temp);
				XtFree(temp);
                state = 3;
            } else {
                strcat(path, links[i]);
                state = 4;
        }
            break;
        case 1:
            if ((len > 1) && (links[i][len-1] == ':')) {
                strcat(path, links[i]);
                state = 2;
            } else if (strstr(links[i], ".dir")) {
                strcat(path, "[.");
				temp = XtNewString(links[i]);
				q = strstr(temp, ".dir");
				*q = '\0';
                strcat(path, temp);
				XtFree(temp);
                state = 3;
            } else {
                strcat(path, links[i]);
                state = 4;
            }
            break;
        case 2:
            if (strstr(links[i], ".dir")) {
                strcat(path, "[");
				temp = XtNewString(links[i]);
				q = strstr(temp, ".dir");
				*q = '\0';
                strcat(path, temp);
				XtFree(temp);
                state = 3;
            } else {
                strcat(path, links[i]);
                state = 4;
            }
            break;
        case 3:
            if (strstr(links[i], ".dir")) {
                strcat(path, ".");
				temp = XtNewString(links[i]);
				q = strstr(temp, ".dir");
				*q = '\0';
                strcat(path, temp);
				XtFree(temp);
            } else {
                strcat(path, "]");
                strcat(path, links[i]);
                state = 4;
            }
            break;
        case 4:
            fatal_error("Problem in vms_links_to_path()");
        }
    }

    /* Is a closed bracket needed? */
    if (state == 3)
        strcat(path, "]");

    return path;
}


/*
 * links_to_path - Construct a  path from its constituent links.  "system"
 *                 identifies the system (e.g., SYS_UNIX).  "links" is a
 *                 pointer to an array of pointers to the link names.
 *                 "nlinks" is the number of entries in "links".  Call 
 *                 XtFree() to free the memory returned by links_to_path().
 */
char *
links_to_path(system, links, nlinks)
int system;
char **links;
int nlinks;
{
	switch (system) {
	case SYS_VMS:
		return vms_links_to_path(links, nlinks);
	case SYS_DOS:
		return dos_links_to_path(links, nlinks);
	case SYS_WINDOWS:
		return windows_links_to_path(links, nlinks);
	case SYS_NT:
		return nt_links_to_path(links, nlinks);
	default:
		return unix_links_to_path(links, nlinks);
	}
}


/*
 * merge_paths - Merge paths "full_path" and "rel_path".  "full_path"
 *               should be a full directory path and "rel_path" should be
 *               relative to "full_path".  The intent is to return a full
 *               path that is equivalent to referencing "rel_path" from
 *               directory "full_path".  "system" identifies the system
 *               (e.g., "SYS_UNIX").  Call XtFree() to free memory returned
 *               by merge_paths().
 */
char *
merge_paths(system, full_path, rel_path)
int system;
char *full_path;
char *rel_path;
{
	char **full_path_links;
	char **rel_path_links;
	char **links;
	char **ptr;
	char *path;
	int nlinks = 0;
	int indx;

	full_path_links = path_to_links(system, full_path);
	rel_path_links = path_to_links(system, rel_path);

	/* Allocate space for array of link names */
	ptr = full_path_links;
	while (*ptr++)
		nlinks++;
	ptr = rel_path_links;
	while (*ptr++)
		nlinks++;
	links = (char **)XtMalloc(sizeof(char *)*(nlinks+1));

	/* Merge links */
	indx = 0;
	ptr = full_path_links;
	while (*ptr) {
		links[indx++] = XtNewString(*ptr);
		ptr++;
	}
	ptr = rel_path_links;
	while (*ptr) {
		links[indx++] = XtNewString(*ptr);
		ptr++;
	}
	links[indx] = NULL;

	/* Create a merged path */
	path = links_to_path(system, links, nlinks);

	/* Free memory before returning */
	release_path_links(full_path_links);
	release_path_links(rel_path_links);
	release_path_links(links);

	return path;
}


/*
 * parse_path - Parses path name "path" into its constituent directory
 *              "dir" and entry "entry".  The caller is responsible
 *              for calling XtFree() to release memory.  "system" identifies
 *              the system (e.g., SYS_UNIX).  If "dir" or "entry" is NULL,
 *              no corresponding value is returned.
 */
parse_path(system, path, dir, entry)
int system;
char *path;
char **dir;
char **entry;
{
	char **links;
	char **dir_links;
	char **ptr;
	int nlinks = 0;
	int i;

	/* Break path into links and count them */
	links = path_to_links(system, path);
	ptr = links;
	while (*ptr++)
		nlinks++;
	
	/* Sanity check */
	if (nlinks < 2)
		fatal_error("Problem in parse_path()");

	/* Create directory path from links */
	dir_links = (char **)XtMalloc(sizeof(char *)*nlinks);
	for (i=0; i<nlinks-1; i++)
		dir_links[i] = XtNewString(links[i]);
	dir_links[nlinks-1] = NULL;
	if (dir)
		*dir = links_to_path(system, dir_links, nlinks-1);

	/* Pick off entry */
	if (entry)
		*entry = XtNewString(links[nlinks-1]);

	/* Free memory before returning */
	release_path_links(links);
	release_path_links(dir_links);
}


/*
 * unix_rel_dir_path_to_dos - Convert the Unix relative directory path to
 *                            the format used by the DOS system.  Call
 *                            XtFree() to release returned memory.
 */
char *
unix_rel_dir_path_to_dos(path)
char *path;
{
	char *dos_path;
	char *p;

	/* Same as Unix relative path, with the /'s changed to \'s */
	dos_path = XtNewString(path);
	p = dos_path;
	while (*p) {
		if (*p == '/')
			*p = '\\';
		p++;
	}

	return dos_path;
}


/*
 * unix_rel_dir_path_to_windows - Convert the Unix relative directory path
 *                                to the format used by the Windows system.
 *                                Call XtFree() to release returned memory.
 */
char *
unix_rel_dir_path_to_windows(path)
char *path;
{
	return XtNewString(path);
}


/*
 * unix_rel_dir_path_to_vms - Convert the Unix relative directory path to
 *                            the format used by the VMS system.  Call
 *                            XtFree() to release returned memory.
 */
char *
unix_rel_dir_path_to_vms(path)
char *path;
{
	char **unix_links;
	char **vms_links;
	char *vms_path;
	char **ptr;
	int nlinks = 0;
	int i;

	/* Parse Unix relative path into links */
	unix_links = unix_path_to_links(path);

	/* Allocate space for array of VMS links names */
	ptr = unix_links;
	while (*ptr++)
		nlinks++;
	vms_links = (char **)XtMalloc(sizeof(char *)*(nlinks+1));

	/* Create VMS links */
	for (i=0; i<nlinks; i++) {
		vms_links[i] = XtMalloc(strlen(unix_links[i])+5);
		strcpy(vms_links[i], unix_links[i]);
		strcat(vms_links[i], ".dir");
	}
	vms_links[nlinks] = NULL;

	/* Create VMS path from links */
	vms_path = vms_links_to_path(vms_links, nlinks);

	/* Release memory before returning */
	release_path_links(unix_links);
	release_path_links(vms_links);

	return vms_path;
}


/*
 * unix_rel_dir_path_to_native - Convert the Unix relative directory path
 *                               to the native "system" format.  "system"
 *                               identifies the system (e.g., SYS_UNIX).
 *                               Call XtFree() to release returned memory.
 */
char *
unix_rel_dir_path_to_native(system, path)
int system;
char *path;
{
	switch (system) {
	case SYS_VMS:
		return unix_rel_dir_path_to_vms(path);
	case SYS_DOS:
		return unix_rel_dir_path_to_dos(path);
	case SYS_WINDOWS:
		return unix_rel_dir_path_to_windows(path);
	default:
		return XtNewString(path);
	}
}


/*
 * unix_rel_file_path_to_vms - Convert the Unix relative file path to
 *                             the format used by the VMS system.  Call
 *                             XtFree() to release returned memory.
 */
char *
unix_rel_file_path_to_vms(path)
char *path;
{
	char **unix_links;
	char **vms_links;
	char *vms_path;
	char **ptr;
	int nlinks = 0;
	int i;

	/* Parse Unix relative path into links */
	unix_links = unix_path_to_links(path);

	/* Allocate space for array of VMS links names */
	ptr = unix_links;
	while (*ptr++)
		nlinks++;
	vms_links = (char **)XtMalloc(sizeof(char *)*(nlinks+1));

	/* Create VMS links */
	for (i=0; i<nlinks-1; i++) {
		vms_links[i] = XtMalloc(strlen(unix_links[i])+5);
		strcpy(vms_links[i], unix_links[i]);
		strcat(vms_links[i], ".dir");
	}
	vms_links[nlinks-1] = XtNewString(unix_links[nlinks-1]);
	vms_links[nlinks] = NULL;

	/* Create VMS path from links */
	vms_path = vms_links_to_path(vms_links, nlinks);

	/* Release memory before returning */
	release_path_links(unix_links);
	release_path_links(vms_links);

	return vms_path;
}


/*
 * unix_rel_file_path_to_native - Convert the Unix relative file path
 *                                to the native "system" format.  "system"
 *                                identifies the system (e.g., SYS_UNIX).
 *                                Call XtFree() to release returned memory.
 */
char *
unix_rel_file_path_to_native(system, path)
int system;
char *path;
{
	switch (system) {
	case SYS_VMS:
		return unix_rel_file_path_to_vms(path);
	case SYS_DOS:
		return unix_rel_dir_path_to_dos(path);
	case SYS_WINDOWS:
		return unix_rel_dir_path_to_windows(path);
	default:
		return XtNewString(path);
	}
}


/*
 * equivalent_file_path - The purpose of this kludgy function is to convert
 *                        a directory path into a file path.  This is a no-op
 *                        if "system" is not SYS_VMS.  If "system" is SYS_VMS,
 *                        the following sort of conversion take place:
 *
 *                            a::b:[c.d.e]   -->  a::b:[c.d]e.dir
 *                            a::b:[c.d]e    -->  a::b:[c.d]e     (no-op)
 *                            a::b:[c]       -->  a::b:c.dir
 *
 *                        Call XtFree() to release returned memory.
 */
char *
equivalent_file_path(system, path)
int system;
char *path;
{
	char **links;
	char **ptr;
	char *dir_path;
	char *new_path;
	int nlinks = 0;
	int len = 0;

	if (system == SYS_VMS) {
		links = vms_path_to_links(path);
		ptr = links;
		while (*ptr++)
			nlinks++;
		len = strlen(links[nlinks-1]);
		if ((len > 4) && !strcmp(&links[nlinks-1][len-4], ".dir")) {
			dir_path = vms_links_to_path(links, nlinks-1);
			new_path = XtMalloc(strlen(dir_path)+len+1);
			strcpy(new_path, dir_path);
			strcat(new_path, links[nlinks-1]);
			XtFree(dir_path);
		} else {
			new_path = XtNewString(path);
		}
		release_path_links(links);
	} else
		new_path = XtNewString(path);

	return new_path;
}