1 /* File: vms_progname.c
3 * This module provides a fixup of the program name.
5 * This module is designed to be a plug in replacement for the
6 * progname module used by many GNU utilities with a few enhancements
9 * It does not support the HAVE_DECL_PROGRAM_INVOCATION_* macros at this
12 * Make sure that the program_name string is set as close as possible to
13 * what the original command was given.
15 * When run from DCL, The argv[0] element is initialized with an absolute
16 * path name. The decc$ feature logical names can control the format
17 * of this pathname. In some cases it causes the UNIX format name to be
18 * formatted incorrectly.
20 * This DCL provided name is usually incompatible with what is expected to
21 * be provided by Unix programs and needs to be replaced.
23 * When run from an exec() call, the argv[0] element is initialized by the
24 * program. This name is compatible with what is expected to be provided
25 * by Unix programs and should be passed through unchanged.
27 * The DCL provided name can be detected because it always contains the
31 * devname:[dir]program.exe;1 Normal VMS - remove path and .EXE;n
32 * devname:[dir]facility$program.exe;1 Facility also needs removal.
33 * /devname/dir/program.exe
34 * /DISK$VOLUME/dir/program.exe.1 Bug version should not be there.
35 * /DISK$VOLUME/dir/program. Bug Period should not be there.
39 /* Copyright (C) 2014-2016 Free Software Foundation, Inc.
41 GNU Make is free software; you can redistribute it and/or modify it under the
42 terms of the GNU General Public License as published by the Free Software
43 Foundation; either version 3 of the License, or (at your option) any later
46 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
47 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
48 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
50 You should have received a copy of the GNU General Public License along with
51 this program. If not, see <http://www.gnu.org/licenses/>. */
54 /* Per copyright assignment agreement with the Free Software Foundation
55 this software may be available under under other license agreements
75 # include "progname.h"
78 #pragma member_alignment save
79 #pragma nomember_alignment longword
85 unsigned short * retlen;
88 struct filescan_itmlst_2
90 unsigned short length;
91 unsigned short itmcode;
95 #pragma member_alignment
98 SYS$GETDVIW (unsigned long efn,
100 const struct dsc$descriptor_s * devnam,
101 const struct item_list_3 * itmlst,
103 void (* astadr)(unsigned long),
104 unsigned long astprm,
108 SYS$FILESCAN (const struct dsc$descriptor_s * srcstr,
109 struct filescan_itmlst_2 * valuelist,
110 unsigned long * fldflags,
111 struct dsc$descriptor_s *auxout,
112 unsigned short * retlen);
114 /* String containing name the program is called with.
115 To be initialized by main(). */
117 const char *program_name = NULL;
119 static int internal_need_vms_symbol = 0;
121 static char vms_new_nam[256];
124 need_vms_symbol (void)
126 return internal_need_vms_symbol;
131 set_program_name (const char *argv0)
137 printf ("original argv0 = %s\n", argv0);
140 /* Posix requires non-NULL argv[0] */
143 fputs ("A NULL argv[0] was passed through an exec system call.\n",
148 program_name = argv0;
150 internal_need_vms_symbol = 0;
152 /* If the path name starts with a /, then it is an absolute path */
153 /* that may have been generated by the CRTL instead of the command name */
154 /* If it is the device name between the slashes, then this was likely */
155 /* from the run command and needs to be fixed up. */
156 /* If the DECC$POSIX_COMPLIANT_PATHNAMES is set to 2, then it is the */
157 /* DISK$VOLUME that will be present, and it will still need to be fixed. */
162 struct item_list_3 itemlist[3];
163 unsigned short dvi_iosb[4];
165 unsigned short alldevnam_len;
166 struct dsc$descriptor_s devname_dsc;
167 char diskvolnam[256];
168 unsigned short diskvolnam_len;
170 internal_need_vms_symbol = 1;
172 /* Get some information about the disk */
173 /*--------------------------------------*/
174 itemlist[0].len = (sizeof alldevnam) - 1;
175 itemlist[0].code = DVI$_ALLDEVNAM;
176 itemlist[0].bufadr = alldevnam;
177 itemlist[0].retlen = &alldevnam_len;
178 itemlist[1].len = (sizeof diskvolnam) - 1 - 5;
179 itemlist[1].code = DVI$_VOLNAM;
180 itemlist[1].bufadr = &diskvolnam[5];
181 itemlist[1].retlen = &diskvolnam_len;
183 itemlist[2].code = 0;
185 /* Add the prefix for the volume name. */
186 /* SYS$GETDVI will append the volume name to this */
187 strcpy (diskvolnam, "DISK$");
189 nextslash = strchr (&argv0[1], '/');
190 if (nextslash != NULL)
192 length = nextslash - argv0 - 1;
194 /* Cast needed for HP C compiler diagnostic */
195 devname_dsc.dsc$a_pointer = (char *)&argv0[1];
196 devname_dsc.dsc$w_length = length;
197 devname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
198 devname_dsc.dsc$b_class = DSC$K_CLASS_S;
200 status = SYS$GETDVIW (EFN$C_ENF, 0, &devname_dsc, itemlist,
201 dvi_iosb, NULL, 0, 0);
202 if (!$VMS_STATUS_SUCCESS (status))
204 /* If the sys$getdviw fails, then this path was passed by */
205 /* An exec() program and not from DCL, so do nothing */
206 /* An example is "/tmp/program" where tmp: does not exist */
208 printf ("sys$getdviw failed with status %d\n", status);
212 else if (!$VMS_STATUS_SUCCESS (dvi_iosb[0]))
215 printf ("sys$getdviw failed with iosb %d\n", dvi_iosb[0]);
225 /* Null terminate the returned alldevnam */
226 alldevnam[alldevnam_len] = 0;
228 devnam_len = alldevnam_len;
230 /* Need to skip past any leading underscore */
231 if (devnam[0] == '_')
237 /* And remove the trailing colon */
238 if (devnam[devnam_len - 1] == ':')
241 devnam[devnam_len] = 0;
244 /* Null terminate the returned volnam */
246 diskvolnam[diskvolnam_len] = 0;
248 /* Check first for normal CRTL behavior */
249 if (devnam_len == length)
251 strncpy (vms_new_nam, &argv0[1], length);
252 vms_new_nam[length] = 0;
253 result = (strcasecmp (devnam, vms_new_nam) == 0);
256 /* If we have not got a match, check for POSIX Compliant */
257 /* behavior. To be more accurate, we could also check */
258 /* to see if that feature is active. */
259 if ((result == 0) && (diskvolnam_len == length))
261 strncpy (vms_new_nam, &argv0[1], length);
262 vms_new_nam[length] = 0;
263 result = (strcasecmp (diskvolnam, vms_new_nam) == 0);
270 /* The path did not start with a slash, so it could be VMS format */
271 /* If it is vms format, it has a volume/device in it as it must */
272 /* be an absolute path */
273 struct dsc$descriptor_s path_desc;
275 unsigned long field_flags;
276 struct filescan_itmlst_2 item_list[5];
282 path_desc.dsc$a_pointer = (char *)argv0; /* cast ok */
283 path_desc.dsc$w_length = strlen (argv0);
284 path_desc.dsc$b_dtype = DSC$K_DTYPE_T;
285 path_desc.dsc$b_class = DSC$K_CLASS_S;
287 /* Don't actually need to initialize anything buf itmcode */
288 /* I just do not like uninitialized input values */
290 /* Sanity check, this must be the same length as input */
291 item_list[0].itmcode = FSCN$_FILESPEC;
292 item_list[0].length = 0;
293 item_list[0].component = NULL;
295 /* If the device is present, then it if a VMS spec */
296 item_list[1].itmcode = FSCN$_DEVICE;
297 item_list[1].length = 0;
298 item_list[1].component = NULL;
300 /* we need the program name and type */
301 item_list[2].itmcode = FSCN$_NAME;
302 item_list[2].length = 0;
303 item_list[2].component = NULL;
305 item_list[3].itmcode = FSCN$_TYPE;
306 item_list[3].length = 0;
307 item_list[3].component = NULL;
310 item_list[4].itmcode = 0;
311 item_list[4].length = 0;
312 item_list[4].component = NULL;
314 status = SYS$FILESCAN ((const struct dsc$descriptor_s *)&path_desc,
315 item_list, &field_flags, NULL, NULL);
318 if ($VMS_STATUS_SUCCESS (status) &&
319 (item_list[0].length == path_desc.dsc$w_length) &&
320 (item_list[1].length != 0))
327 /* We need the filescan to be successful, */
328 /* same length as input, and a volume to be present */
329 internal_need_vms_symbol = 1;
331 /* We will assume that we only get to this path on a version */
332 /* of VMS that does not support the EFS character set */
334 /* There may be a xxx$ prefix on the image name. Linux */
335 /* programs do not handle that well, so strip the prefix */
336 name = item_list[2].component;
337 name_len = item_list[2].length;
338 dollar = strrchr (name, '$');
342 name_len = name_len - (dollar - name);
346 strncpy (vms_new_nam, name, name_len);
347 vms_new_nam[name_len] = 0;
349 /* Commit to using the new name */
350 program_name = vms_new_nam;
352 /* We only keep the extension if it is not ".exe" */
354 ext = item_list[3].component;
356 if (item_list[3].length != 1)
359 if (item_list[3].length == 4)
361 if ((ext[1] == 'e' || ext[1] == 'E') &&
362 (ext[2] == 'x' || ext[2] == 'X') &&
363 (ext[3] == 'e' || ext[3] == 'E'))
369 strncpy (&vms_new_nam[name_len], ext, item_list[3].length);
381 /* This means it is probably the name from a DCL command */
382 /* Find the last slash which separates the file from the */
384 lastslash = strrchr (argv0, '/');
386 if (lastslash != NULL) {
391 /* There may be a xxx$ prefix on the image name. Linux */
392 /* programs do not handle that well, so strip the prefix */
393 dollar = strrchr (lastslash, '$');
395 if (dollar != NULL) {
400 strcpy (vms_new_nam, lastslash);
402 /* In UNIX mode + EFS character set, there should not be a */
403 /* version present, as it is not possible when parsing to */
404 /* tell if it is a version or part of the UNIX filename as */
405 /* UNIX programs use numeric extensions for many reasons. */
407 lastdot = strrchr (vms_new_nam, '.');
408 if (lastdot != NULL) {
412 while (isdigit (lastdot[i])) {
415 if (lastdot[i] == 0) {
420 /* Find the .exe on the name (case insenstive) and toss it */
421 dotexe = strrchr (vms_new_nam, '.');
422 if (dotexe != NULL) {
423 if ((dotexe[1] == 'e' || dotexe[1] == 'E') &&
424 (dotexe[2] == 'x' || dotexe[2] == 'X') &&
425 (dotexe[3] == 'e' || dotexe[3] == 'E') &&
430 /* Also need to handle a null extension because of a */
432 if (dotexe[1] == 0) {
438 /* Commit to new name */
439 program_name = vms_new_nam;
442 /* There is no way that the code should ever get here */
443 /* As we already verified that the '/' was present */
444 fprintf (stderr, "Sanity failure somewhere we lost a '/'\n");
452 main (int argc, char ** argv, char **env)
457 set_program_name (argv[0]);
459 printf ("modified argv[0] = %s\n", program_name);