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