xref: /openbsd/gnu/usr.bin/cvs/vms/misc.c (revision d415bd75)
1 /*
2  * Copyright � 1994 the Free Software Foundation, Inc.
3  *
4  * Author: Roland B. Roberts (roberts@nsrl.rochester.edu)
5  *
6  * This file is a part of GNU VMSLIB, the GNU library for porting GNU
7  * software to VMS.
8  *
9  * GNU VMSLIB is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNU VMSLIB is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 /*
21  * Miscellaneous utilities used by hackargv().
22  * Some of these are useful in their own right.
23  */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <lib$routines.h>
29 
30 /* See in misc.h why it is done like this.  */
31 void x_free (void *block)
32 {
33   free (block);
34 }
35 
36 /*
37  * Some string utilities.
38  */
39 char *downcase (char *s)
40 {
41   register char *t;
42   for (t = s ; *t; t++)
43     *t = tolower(*t);
44   return (s);
45 }
46 
47 char *strndup (char *src, int len) {
48   char *dst = (char *) xmalloc (len + 1);
49   strncpy (dst, src, len);
50   dst[len] = 0;
51   return (dst);
52 }
53 
54 #include <string.h>
55 
56 /*
57  * int fixpath (char *path)
58  *
59  * Synopsis:
60  *   `Fix' VMS pathnames, converting them to canonical form.
61  *
62  * Description:
63  *   The following conversions are performed
64  *     x:[y.][000000.z] --> x:[y.z]
65  *     x:[y.][z]        --> x:[y.z]
66  *     x:[000000.y]     --> x:[y]
67  *
68  * Author:
69  *   Roland B Roberts (roberts@nsrl.rochester.edu)
70  *   March 1994
71  */
72 int fixpath (char *path)
73 {
74   char *s, *d, *t;
75   int skip = 0;
76   d = s = path;
77   if (t = strstr(path ,".][000000"))
78     skip = 9;
79   else if (t = strstr(path,"]["))
80     skip = 2;
81   else if (t = strstr(path,"[000000."))
82     t++, skip = 7;
83   if (t) {
84     while (s < t)
85       *d++ = *s++;
86     s += skip;
87     while (*d++ = *s++);
88   }
89   return 0;
90 }
91 
92 
93 #include <ctype.h>
94 #include <string.h>
95 
96 #ifndef TRUE
97 #define TRUE 1
98 #define FALSE 0
99 #endif
100 
101 /*
102  * char *argvconcat (int argc, char **argv)
103  *
104  * Synopsis:
105  *   Concatenate all elements of argv into a single string suitable for
106  *   use as a command line.
107  *
108  * Description:
109  *   This is intended for use with hackargv() in order to build a command
110  *   line for background() or popen().  Each element of argv (except the
111  *   first) is surrounded by double quotes to insure the command line is
112  *   unaltered when DCL rereads it.
113  *
114  * Side Effect:
115  *   Space for the new string is allocated with xmalloc().
116  *
117  * Author:
118  *   Roland B Roberts (roberts@nsrl.rochester.edu)
119  *   March 1994
120  */
121 
122 char *argvconcat (int argc, char **argv)
123 {
124   int i, j, n, addquotes, flags, pid, status;
125   char *cmdline;
126   /*
127    * Allocate space
128    */
129   for (j = n = 0; j < argc; j++)
130     n += 3 + strlen(argv[j]);	/* Need 3 extra spaces, not 1; see below */
131   cmdline = (char *) xmalloc ((n + 1) * sizeof (char));
132   sprintf (cmdline, "%s ", argv[0]);
133   for (j = 1, addquotes = FALSE; j < argc; j++) {
134     /*
135      * Add double quotes to arg if it contains uppercase of spaces.
136      * Hence, the need to allocate three extra spaces for each argument.
137      */
138     for (i = 0; i < strlen(argv[j]); i++)
139       if (isupper(argv[j][i]) || isspace(argv[j][i])) {
140 	addquotes = TRUE;
141 	break;
142       }
143     if (addquotes) {
144       strcat (cmdline, argv[j]);
145       strcat (cmdline, " ");
146     }
147     else {
148       strcat (cmdline, "\"");	/* use quotes to preserve case */
149       strcat (cmdline, argv[j]);
150       strcat (cmdline, "\" ");
151     }
152   }
153   cmdline[strlen(cmdline)-1] = 0;
154   return (cmdline);
155 }
156