fix typo
[openssl.git] / apps / apps.c
1 /* apps/apps.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #define NON_MAIN
65 #include "apps.h"
66 #undef NON_MAIN
67
68 #ifdef WINDOWS
69 #  include "bss_file.c"
70 #endif
71
72 #ifndef NOPROTO
73 int app_init(long mesgwin);
74 #else
75 int app_init();
76 #endif
77
78 #ifdef undef /* never finished - probably never will be :-) */
79 int args_from_file(file,argc,argv)
80 char *file;
81 int *argc;
82 char **argv[];
83         {
84         FILE *fp;
85         int num,i;
86         unsigned int len;
87         static char *buf=NULL;
88         static char **arg=NULL;
89         char *p;
90         struct stat stbuf;
91
92         if (stat(file,&stbuf) < 0) return(0);
93
94         fp=fopen(file,"r");
95         if (fp == NULL)
96                 return(0);
97
98         *argc=0;
99         *argv=NULL;
100
101         len=(unsigned int)stbuf.st_size;
102         if (buf != NULL) Free(buf);
103         buf=(char *)Malloc(len+1);
104         if (buf == NULL) return(0);
105
106         len=fread(buf,1,len,fp);
107         if (len <= 1) return(0);
108         buf[len]='\0';
109
110         i=0;
111         for (p=buf; *p; p++)
112                 if (*p == '\n') i++;
113         if (arg != NULL) Free(arg);
114         arg=(char **)Malloc(sizeof(char *)*(i*2));
115
116         *argv=arg;
117         num=0;
118         p=buf;
119         for (;;)
120                 {
121                 if (!*p) break;
122                 if (*p == '#') /* comment line */
123                         {
124                         while (*p && (*p != '\n')) p++;
125                         continue;
126                         }
127                 /* else we have a line */
128                 *(arg++)=p;
129                 num++;
130                 while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
131                         p++;
132                 if (!*p) break;
133                 if (*p == '\n')
134                         {
135                         *(p++)='\0';
136                         continue;
137                         }
138                 /* else it is a tab or space */
139                 p++;
140                 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
141                         p++;
142                 if (!*p) break;
143                 if (*p == '\n')
144                         {
145                         p++;
146                         continue;
147                         }
148                 *(arg++)=p++;
149                 num++;
150                 while (*p && (*p != '\n')) p++;
151                 if (!*p) break;
152                 /* else *p == '\n' */
153                 *(p++)='\0';
154                 }
155         *argc=num;
156         return(1);
157         }
158 #endif
159
160 int str2fmt(s)
161 char *s;
162         {
163         if      ((*s == 'D') || (*s == 'd'))
164                 return(FORMAT_ASN1);
165         else if ((*s == 'T') || (*s == 't'))
166                 return(FORMAT_TEXT);
167         else if ((*s == 'P') || (*s == 'p'))
168                 return(FORMAT_PEM);
169         else if ((*s == 'N') || (*s == 'n'))
170                 return(FORMAT_NETSCAPE);
171         else
172                 return(FORMAT_UNDEF);
173         }
174
175 #if defined(MSDOS) || defined(WIN32) || defined(WIN16)
176 void program_name(in,out,size)
177 char *in;
178 char *out;
179 int size;
180         {
181         int i,n;
182         char *p=NULL;
183
184         n=strlen(in);
185         /* find the last '/', '\' or ':' */
186         for (i=n-1; i>0; i--)
187                 {
188                 if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
189                         {
190                         p= &(in[i+1]);
191                         break;
192                         }
193                 }
194         if (p == NULL)
195                 p=in;
196         n=strlen(p);
197         /* strip off trailing .exe if present. */
198         if ((n > 4) && (p[n-4] == '.') &&
199                 ((p[n-3] == 'e') || (p[n-3] == 'E')) &&
200                 ((p[n-2] == 'x') || (p[n-2] == 'X')) &&
201                 ((p[n-1] == 'e') || (p[n-1] == 'E')))
202                 n-=4;
203         if (n > size-1)
204                 n=size-1;
205
206         for (i=0; i<n; i++)
207                 {
208                 if ((p[i] >= 'A') && (p[i] <= 'Z'))
209                         out[i]=p[i]-'A'+'a';
210                 else
211                         out[i]=p[i];
212                 }
213         out[n]='\0';
214         }
215 #else
216 void program_name(in,out,size)
217 char *in;
218 char *out;
219 int size;
220         {
221         char *p;
222
223         p=strrchr(in,'/');
224         if (p != NULL)
225                 p++;
226         else
227                 p=in;
228         strncpy(out,p,size-1);
229         out[size-1]='\0';
230         }
231 #endif
232
233 #ifdef WIN32
234 int WIN32_rename(from,to)
235 char *from;
236 char *to;
237         {
238         int ret;
239
240         ret=MoveFileEx(from,to,MOVEFILE_REPLACE_EXISTING|MOVEFILE_COPY_ALLOWED);
241         return(ret?0:-1);
242         }
243 #endif
244
245 int chopup_args(arg,buf,argc,argv)
246 ARGS *arg;
247 char *buf;
248 int *argc;
249 char **argv[];
250         {
251         int num,len,i;
252         char *p;
253
254         *argc=0;
255         *argv=NULL;
256
257         len=strlen(buf);
258         i=0;
259         if (arg->count == 0)
260                 {
261                 arg->count=20;
262                 arg->data=(char **)Malloc(sizeof(char *)*arg->count);
263                 }
264         for (i=0; i<arg->count; i++)
265                 arg->data[i]=NULL;
266
267         num=0;
268         p=buf;
269         for (;;)
270                 {
271                 /* first scan over white space */
272                 if (!*p) break;
273                 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
274                         p++;
275                 if (!*p) break;
276
277                 /* The start of something good :-) */
278                 if (num >= arg->count)
279                         {
280                         arg->count+=20;
281                         arg->data=(char **)Realloc(arg->data,
282                                 sizeof(char *)*arg->count);
283                         if (argc == 0) return(0);
284                         }
285                 arg->data[num++]=p;
286
287                 /* now look for the end of this */
288                 if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
289                         {
290                         i= *(p++);
291                         arg->data[num-1]++; /* jump over quote */
292                         while (*p && (*p != i))
293                                 p++;
294                         *p='\0';
295                         }
296                 else
297                         {
298                         while (*p && ((*p != ' ') &&
299                                 (*p != '\t') && (*p != '\n')))
300                                 p++;
301
302                         if (*p == '\0')
303                                 p--;
304                         else
305                                 *p='\0';
306                         }
307                 p++;
308                 }
309         *argc=num;
310         *argv=arg->data;
311         return(1);
312         }
313
314 #ifndef APP_INIT
315 int app_init(mesgwin)
316 long mesgwin;
317         {
318         return(1);
319         }
320 #endif