4 Module for vms <-> unix file name conversion
6 Written by Klaus Kämpf (kkaempf@progis.de)
7 of proGIS Software, Aachen, Germany
23 #include <lib$routines.h>
24 /* Initialize a string descriptor (struct dsc$descriptor_s) for an
25 arbitrary string. ADDR is a pointer to the first character
26 of the string, and LEN is the length of the string. */
28 #define INIT_DSC_S(dsc, addr, len) do { \
29 (dsc).dsc$b_dtype = DSC$K_DTYPE_T; \
30 (dsc).dsc$b_class = DSC$K_CLASS_S; \
31 (dsc).dsc$w_length = (len); \
32 (dsc).dsc$a_pointer = (addr); \
35 /* Initialize a string descriptor (struct dsc$descriptor_s) for a
36 NUL-terminated string. S is a pointer to the string; the length
37 is determined by calling strlen(). */
39 #define INIT_DSC_CSTRING(dsc, s) INIT_DSC_S(dsc, s, strlen(s))
43 copy 'from' to 'to' up to but not including 'upto'
44 return 0 if eos on from
45 return 1 if upto found
47 return 'to' at last char + 1
48 return 'from' at match + 1 or eos if no match
50 if as_dir == 1, change all '.' to '_'
51 else change all '.' but the last to '_'
55 copyto (char **to
, char **from
, char upto
, int as_dir
)
59 s
= strrchr (*from
, '.');
69 while (**from
== upto
);
82 if (isupper ((unsigned char)**from
))
83 **to
= tolower ((unsigned char)**from
);
96 get translation of logical name
104 static char reslt
[1024];
105 $
DESCRIPTOR (reslt_dsc
, reslt
);
107 struct dsc$descriptor_s name_dsc
;
110 INIT_DSC_CSTRING (name_dsc
, name
);
112 stat
= lib$
sys_trnlog (&name_dsc
, &resltlen
, &reslt_dsc
);
118 if (stat
== SS$_NOTRAN
)
122 reslt
[resltlen
] = '\0';
124 s
= (char *)malloc (resltlen
+1);
138 if (strchr (s
, '\\') == 0)
152 enum namestate
{ N_START
, N_DEVICE
, N_OPEN
, N_DOT
, N_CLOSED
, N_DONE
};
155 convert unix style name to vms style
156 type = 0 -> name is a full name (directory and filename part)
157 type = 1 -> name is a directory
158 type = 2 -> name is a filename without directory
160 The following conversions are applied
162 input full name dir name file name
164 1 ./ <cwd> [] <current directory>.dir
165 2 ../ <home of cwd> <home of cwd> <home of cwd>.dir
167 3 // <dev of cwd>: <dev of cwd>:[000000] <dev of cwd>:000000.dir
169 5 //a/ a: a: a:000000.dir
171 9 / [000000] [000000] 000000.dir
172 10 /a [000000]a [a] [000000]a
173 11 /a/ [a] [a] [000000]a.dir
174 12 /a/b [a]b [a.b] [a]b
175 13 /a/b/ [a.b] [a.b] [a]b.dir
176 14 /a/b/c [a.b]c [a.b.c] [a.b]c
177 15 /a/b/c/ [a.b.c] [a.b.c] [a.b]c.dir
180 17 a/ [.a] [.a] a.dir
181 18 a/b [.a]b [.a.b] [.a]b
182 19 a/b/ [.a.b] [.a.b] [.a]b.dir
183 20 a/b/c [.a.b]c [.a.b.c] [.a.b]c
184 21 a/b/c/ [.a.b.c] [.a.b.c] [.a.b]c.dir
186 22 a.b.c a_b.c [.a_b_c] a_b_c.dir
188 23 [x][y]z [x.y]z [x.y]z [x.y]z
189 24 [x][.y]z [x.y]z [x.y]z [x.y]z
191 25 filenames with '$' are left unchanged if they contain no '/'
192 25 filenames with ':' are left unchanged
193 26 filenames with a single pair of '[' ']' are left unchanged
195 the input string is not written to
209 #define MAXPATHLEN 512
211 enum namestate nstate
;
212 static char vmsname
[MAXPATHLEN
+1];
227 s
= strpbrk (name
, "$:");
235 s1
= strchr (s
+1, '[');
236 s2
= strchr (s
+1, ']');
241 if (strchr (name
, '/') == 0)
243 if ((type
== 1) && (s1
!= 0) && (s2
== 0))
245 strcpy (vmsname
, name
);
246 strcat (vmsname
, "]");
255 if ((type
== 1) && (s1
!= 0) && (s2
== 0))
257 strcpy (vmsname
, name
);
258 strcat (vmsname
, "]");
268 s
= strchr (name
, '[');
272 s1
= strchr (s
+1, '[');
276 && (strchr (s
+1, ']') == 0))
278 strcpy (vmsname
, name
);
279 strcat (vmsname
, "]");
283 return name
; /* single [, keep unchanged */
288 return name
; /* not ][, keep unchanged */
295 /* s -> starting char
300 strncpy (vptr
, s
, s1
-s
); /* copy up to but not including ']' */
304 s
= s1
+ 1; /* s -> char behind ']' */
305 if (*s
!= '[') /* was '][' ? */
306 break; /* no, last ] found, exit */
310 s1
= strchr (s
, ']');
311 if (s1
== 0) /* no closing ] */
322 else /* no [ in name */
327 int rooted
= 1; /* flag if logical is rooted, else insert [000000] */
336 case 0: /* start of loop */
342 else if (*fptr
== '.')
351 case 1: /* '/' at start */
361 case 2: /* no '/' at start */
362 s
= strchr (fptr
, '/');
363 if (s
== 0) /* no '/' (16) */
370 copyto (&vptr
, &fptr
, 0, (type
==1));
375 else /* found '/' (17..21) */
378 && (*(s
+1) == 0)) /* 17(2) */
380 copyto (&vptr
, &fptr
, '/', 1);
387 copyto (&vptr
, &fptr
, '/', 1);
394 case 3: /* '//' at start */
395 while (*fptr
== '/') /* collapse all '/' */
397 if (*fptr
== 0) /* just // */
399 char cwdbuf
[MAXPATHLEN
+1];
401 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
404 return ""; /* FIXME, err getcwd */
406 s
= strchr (s1
, ':');
409 return ""; /* FIXME, err no device */
411 strncpy (vptr
, s1
, s
-s1
+1);
419 if (copyto (&vptr
, &fptr
, '/', 1) == 0) /* copy device part */
427 if (*fptr
== 0) /* just '//a/' */
429 strcpy (vptr
+1, "[000000]");
435 /* check logical for [000000] insertion */
438 { /* found translation */
440 for (;;) /* loop over all nested logicals */
442 s2
= s1
+ strlen (s1
) - 1;
443 if (*s2
== ':') /* translation ends in ':' */
453 continue; /* next iteration */
455 if (*s2
== ']') /* translation ends in ']' */
457 if (*(s2
-1) == '.') /* ends in '.]' */
459 if (strncmp (fptr
, "000000", 6) != 0)
464 strcpy (vmsname
, s1
);
465 s
= strchr (vmsname
, ']');
488 strcpy (vptr
, "[000000.");
496 /* s1-> '.' after 000000 or NULL */
498 s
= strchr (fptr
, '/');
501 if (*(vptr
-1) == '.')
503 else if (rooted
== 0)
505 copyto (&vptr
, &fptr
, 0, (type
== 1));
511 while (*(s
+1) == '/') /* skip multiple '/' */
516 && (*(vptr
-1) != '.'))
522 if ((nstate
== N_DOT
)
535 case 4: /* single '/' at start (9..15) */
542 case 5: /* just '/' at start (9) */
548 strcpy (vptr
, "000000");
556 case 6: /* chars following '/' at start 10..15 */
559 s
= strchr (fptr
, '/');
564 strcpy (vptr
, "000000]");
567 copyto (&vptr
, &fptr
, 0, (type
== 1));
577 && (*(s
+1) == 0)) /* 11(2) */
579 strcpy (vptr
, "000000]");
583 copyto (&vptr
, &fptr
, '/', (*(vptr
-1) != ']'));
588 case 7: /* add '.dir' and exit */
589 if ((nstate
== N_OPEN
)
590 || (nstate
== N_DOT
))
607 strcpy (vptr
, ".dir");
612 case 8: /* add ']' and exit */
617 case 9: /* 17..21, fptr -> 1st '/' + 1 */
628 s
= strchr (fptr
, '/');
633 if (nstate
== N_OPEN
)
642 if (nstate
== N_OPEN
)
652 while (*(s
+1) == '/')
655 && (*(s
+1) == 0)) /* 19(2), 21(2)*/
657 if (nstate
!= N_CLOSED
)
666 if (nstate
== N_OPEN
)
674 if ( (*fptr
== '.') /* check for '..' or '../' */
675 && (*(fptr
+1) == '.')
676 && ((*(fptr
+2) == '/')
677 || (*(fptr
+2) == 0)) )
686 while (*fptr
== '/');
690 vptr
--; /* vptr -> '.' or ']' */
695 if (*s1
== '.') /* one back */
701 if (*s1
== '[') /* top level reached */
705 strcpy (s1
, "[000000]");
722 copyto (&vptr
, &fptr
, '/', as_dir
);
736 if (type
== 2) /* 19,21 */
749 case 10: /* 1,2 first is '.' */
759 case 11: /* 2, '..' at start */
763 if (*fptr
!= '/') /* got ..xxx */
770 while (*fptr
== '/') fptr
++;
773 if (*(fptr
+1) != '.')
780 while (*fptr
== '/');
782 { /* got '..' or '../' */
783 char cwdbuf
[MAXPATHLEN
+1];
785 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
788 return ""; /* FIXME, err getcwd */
791 s
= strchr (vptr
, ']');
801 strcpy (s
, "000000]");
809 if (*fptr
== 0) /* had '..' or '../' */
814 else /* had '../xxx' */
824 vptr
+= strlen (vptr
);
828 case 12: /* 1, '.' at start */
840 char cwdbuf
[MAXPATHLEN
+1];
842 s1
= getcwd(cwdbuf
, MAXPATHLEN
);
845 return ""; /*FIXME, err getcwd */
855 s
= strchr (vptr
, ']');
863 vptr
+= strlen (vptr
);
877 /* directory conversion done
878 fptr -> filename part of input string
879 vptr -> free space in vmsname
890 convert from vms-style to unix-style
892 dev:[dir1.dir2] //dev/dir1/dir2/
898 static char piece
[512];
901 if (strchr (name
, '/') != 0) /* already in unix style */
909 s
= strchr (name
, ':');
924 s
= strchr (name
, '[');
938 strcat (p
, "./"); /* [. */
958 if (*s
!= 0) /* more after ']' ?? */
962 strcpy (p
, s
); /* copy it anyway */
966 else /* no '[' anywhere */
972 /* force end with '/' */