4 Module for vms <-> unix file name conversion
6 Written by Klaus Kämpf (kkaempf@didymus.rmi.de)
21 #include <lib$routines.h>
22 /* Initialize a string descriptor (struct dsc$descriptor_s) for an
23 arbitrary string. ADDR is a pointer to the first character
24 of the string, and LEN is the length of the string. */
26 #define INIT_DSC_S(dsc, addr, len) do { \
27 (dsc).dsc$b_dtype = DSC$K_DTYPE_T; \
28 (dsc).dsc$b_class = DSC$K_CLASS_S; \
29 (dsc).dsc$w_length = (len); \
30 (dsc).dsc$a_pointer = (addr); \
33 /* Initialize a string descriptor (struct dsc$descriptor_s) for a
34 NUL-terminated string. S is a pointer to the string; the length
35 is determined by calling strlen(). */
37 #define INIT_DSC_CSTRING(dsc, s) INIT_DSC_S(dsc, s, strlen(s))
41 copy 'from' to 'to' up to but not including 'upto'
42 return 0 if eos on from
43 return 1 if upto found
45 return 'to' at last char + 1
46 return 'from' at match + 1 or eos if no match
48 if as_dir == 1, change all '.' to '_'
49 else change all '.' but the last to '_'
53 copyto (char **to
, char **from
, char upto
, int as_dir
)
57 s
= strrchr (*from
, '.');
67 while (**from
== upto
);
81 **to
= toupper (**from
);
94 get translation of logical name
102 static char reslt
[1024];
103 $
DESCRIPTOR (reslt_dsc
, reslt
);
105 struct dsc$descriptor_s name_dsc
;
108 INIT_DSC_CSTRING (name_dsc
, name
);
110 stat
= lib$
sys_trnlog (&name_dsc
, &resltlen
, &reslt_dsc
);
116 if (stat
== SS$_NOTRAN
)
120 reslt
[resltlen
] = '\0';
122 s
= (char *)malloc (resltlen
+1);
129 enum namestate
{ N_START
, N_DEVICE
, N_OPEN
, N_DOT
, N_CLOSED
, N_DONE
};
132 convert unix style name to vms style
133 type = 0 -> name is a full name (directory and filename part)
134 type = 1 -> name is a directory
135 type = 2 -> name is a filename without directory
137 The following conversions are applied
139 input full name dir name file name
141 1 ./ <cwd> [] <current directory>.dir
142 2 ../ <home of cwd> <home of cwd> <home of cwd>.dir
144 3 // <dev of cwd>: <dev of cwd>:[000000] <dev of cwd>:000000.dir
146 5 //a/ a: a: a:000000.dir
148 9 / [000000] [000000] 000000.dir
149 10 /a [000000]a [a] [000000]a
150 11 /a/ [a] [a] [000000]a.dir
151 12 /a/b [a]b [a.b] [a]b
152 13 /a/b/ [a.b] [a.b] [a]b.dir
153 14 /a/b/c [a.b]c [a.b.c] [a.b]c
154 15 /a/b/c/ [a.b.c] [a.b.c] [a.b]c.dir
157 17 a/ [.a] [.a] a.dir
158 18 a/b [.a]b [.a.b] [.a]b
159 19 a/b/ [.a.b] [.a.b] [.a]b.dir
160 20 a/b/c [.a.b]c [.a.b.c] [.a.b]c
161 21 a/b/c/ [.a.b.c] [.a.b.c] [.a.b]c.dir
163 22 a.b.c a_b.c [.a_b_c] a_b_c.dir
165 23 [x][y]z [x.y]z [x.y]z [x.y]z
166 24 [x][.y]z [x.y]z [x.y]z [x.y]z
168 25 filenames with '$' are left unchanged if they contain no '/'
169 25 filenames with ':' are left unchanged
170 26 filenames with a single pair of '[' ']' are left unchanged
172 the input string is not written to
186 #define MAXPATHLEN 512
188 enum namestate nstate
;
189 static char vmsname
[MAXPATHLEN
+1];
204 s
= strpbrk (name
, "$:");
209 if (strchr (name
, '/') == 0)
222 s
= strchr (name
, '[');
226 s1
= strchr (s
+1, '[');
229 return name
; /* single [, keep unchanged */
234 return name
; /* not ][, keep unchanged */
241 /* s -> starting char
246 strncpy (vptr
, s
, s1
-s
); /* copy up to but not including ']' */
250 s
= s1
+ 1; /* s -> char behind ']' */
251 if (*s
!= '[') /* was '][' ? */
252 break; /* no, last ] found, exit */
256 s1
= strchr (s
, ']');
257 if (s1
== 0) /* no closing ] */
268 else /* no [ in name */
273 int rooted
= 1; /* flag if logical is rooted, else insert [000000] */
282 case 0: /* start of loop */
288 else if (*fptr
== '.')
297 case 1: /* '/' at start */
307 case 2: /* no '/' at start */
308 s
= strchr (fptr
, '/');
309 if (s
== 0) /* no '/' (16) */
316 copyto (&vptr
, &fptr
, 0, (type
==1));
321 else /* found '/' (17..21) */
324 && (*(s
+1) == 0)) /* 17(2) */
326 copyto (&vptr
, &fptr
, '/', 1);
334 copyto (&vptr
, &fptr
, '/', 1);
340 case 3: /* '//' at start */
341 while (*fptr
== '/') /* collapse all '/' */
343 if (*fptr
== 0) /* just // */
345 char cwdbuf
[MAXPATHLEN
+1];
347 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
350 return ""; /* FIXME, err getcwd */
352 s
= strchr (s1
, ':');
355 return ""; /* FIXME, err no device */
357 strncpy (vptr
, s1
, s
-s1
+1);
365 if (copyto (&vptr
, &fptr
, '/', 1) == 0) /* copy device part */
373 if (*fptr
== 0) /* just '//a/' */
375 strcpy (vptr
+1, "[000000]");
381 /* check logical for [000000] insertion */
384 { /* found translation */
386 for (;;) /* loop over all nested logicals */
388 s2
= s1
+ strlen (s1
) - 1;
389 if (*s2
== ':') /* translation ends in ':' */
399 continue; /* next iteration */
401 if (*s2
== ']') /* translation ends in ']' */
403 if (*(s2
-1) == '.') /* ends in '.]' */
405 if (strncmp (fptr
, "000000", 6) != 0)
410 strcpy (vmsname
, s1
);
411 s
= strchr (vmsname
, ']');
434 strcpy (vptr
, "[000000.");
442 /* s1-> '.' after 000000 or NULL */
444 s
= strchr (fptr
, '/');
447 if (*(vptr
-1) == '.')
449 else if (rooted
== 0)
451 copyto (&vptr
, &fptr
, 0, (type
== 1));
457 while (*(s
+1) == '/') /* skip multiple '/' */
462 && (*(vptr
-1) != '.'))
468 if ((nstate
== N_DOT
)
481 case 4: /* single '/' at start (9..15) */
488 case 5: /* just '/' at start (9) */
494 strcpy (vptr
, "000000");
502 case 6: /* chars following '/' at start 10..15 */
505 s
= strchr (fptr
, '/');
510 strcpy (vptr
, "000000]");
513 copyto (&vptr
, &fptr
, 0, (type
== 1));
523 && (*(s
+1) == 0)) /* 11(2) */
525 strcpy (vptr
, "000000]");
529 copyto (&vptr
, &fptr
, '/', (*(vptr
-1) != ']'));
534 case 7: /* add '.dir' and exit */
535 if ((nstate
== N_OPEN
)
536 || (nstate
== N_DOT
))
553 strcpy (vptr
, ".dir");
558 case 8: /* add ']' and exit */
563 case 9: /* 17..21, fptr -> 1st '/' + 1 */
574 s
= strchr (fptr
, '/');
579 if (nstate
== N_OPEN
)
588 if (nstate
== N_OPEN
)
598 while (*(s
+1) == '/')
601 && (*(s
+1) == 0)) /* 19(2), 21(2)*/
603 if (nstate
!= N_CLOSED
)
612 if (nstate
== N_OPEN
)
620 if ( (*fptr
== '.') /* check for '..' or '../' */
621 && (*(fptr
+1) == '.')
622 && ((*(fptr
+2) == '/')
623 || (*(fptr
+2) == 0)) )
632 while (*fptr
== '/');
636 vptr
--; /* vptr -> '.' or ']' */
641 if (*s1
== '.') /* one back */
647 if (*s1
== '[') /* top level reached */
651 strcpy (s1
, "[000000]");
668 copyto (&vptr
, &fptr
, '/', as_dir
);
682 if (type
== 2) /* 19,21 */
695 case 10: /* 1,2 first is '.' */
705 case 11: /* 2, '..' at start */
709 if (*fptr
!= '/') /* got ..xxx */
716 while (*fptr
== '/') fptr
++;
719 if (*(fptr
+1) != '.')
726 while (*fptr
== '/');
728 { /* got '..' or '../' */
729 char cwdbuf
[MAXPATHLEN
+1];
731 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
734 return ""; /* FIXME, err getcwd */
737 s
= strchr (vptr
, ']');
746 strcpy (s
, "000000]");
754 if (*fptr
== 0) /* had '..' or '../' */
759 else /* had '../xxx' */
769 vptr
+= strlen (vptr
);
773 case 12: /* 1, '.' at start */
784 char cwdbuf
[MAXPATHLEN
+1];
786 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
789 return ""; /*FIXME, err getcwd */
799 s
= strchr (vptr
, ']');
807 vptr
+= strlen (vptr
);
821 /* directory conversion done
822 fptr -> filename part of input string
823 vptr -> free space in vmsname
834 convert from vms-style to unix-style
836 dev:[dir1.dir2] //dev/dir1/dir2/
842 static char piece
[512];
845 if (strchr (name
, '/') != 0) /* already in unix style */
853 s
= strchr (name
, ':');
868 s
= strchr (name
, '[');
882 strcat (p
, "./"); /* [. */
902 if (*s
!= 0) /* more after ']' ?? */
906 strcpy (p
, s
); /* copy it anyway */
910 else /* no '[' anywhere */
916 /* force end with '/' */