2005-04-11 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / openfile.c
blobfaf9a2cd69c10cdb37c86f4a6f0d578aca9f8a84
1 /* openfile.c
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
32 #include "gpg.h"
33 #include "util.h"
34 #include "memory.h"
35 #include "ttyio.h"
36 #include "options.h"
37 #include "main.h"
38 #include "status.h"
39 #include "i18n.h"
41 #ifdef USE_ONLY_8DOT3
42 #define SKELEXT ".skl"
43 #else
44 #define SKELEXT EXTSEP_S "skel"
45 #endif
47 #if defined (HAVE_DRIVE_LETTERS) || defined (__riscos__)
48 #define CMP_FILENAME(a,b) ascii_strcasecmp( (a), (b) )
49 #else
50 #define CMP_FILENAME(a,b) strcmp( (a), (b) )
51 #endif
53 #ifdef MKDIR_TAKES_ONE_ARG
54 #undef mkdir
55 #define mkdir(a,b) mkdir(a)
56 #endif
58 /* FIXME: Implement opt.interactive. */
60 /****************
61 * Check whether FNAME exists and ask if it's okay to overwrite an
62 * existing one.
63 * Returns: True: it's okay to overwrite or the file does not exist
64 * False: Do not overwrite
66 int
67 overwrite_filep( const char *fname )
69 if( !fname || (*fname == '-' && !fname[1]) )
70 return 1; /* writing to stdout is always okay */
72 if( access( fname, F_OK ) )
73 return 1; /* does not exist */
75 #ifndef HAVE_DOSISH_SYSTEM
76 if ( !strcmp ( fname, "/dev/null" ) )
77 return 1; /* does not do any harm */
78 #endif
80 /* fixme: add some backup stuff in case of overwrite */
81 if( opt.answer_yes )
82 return 1;
83 if( opt.answer_no || opt.batch )
84 return 0; /* do not overwrite */
86 tty_printf(_("File `%s' exists. "), fname);
87 if( cpr_get_answer_is_yes("openfile.overwrite.okay",
88 _("Overwrite (y/N)? ")) )
89 return 1;
90 return 0;
94 /****************
95 * Strip know extensions from iname and return a newly allocated
96 * filename. Return NULL if we can't do that.
98 char *
99 make_outfile_name( const char *iname )
101 size_t n;
103 if( (!iname || (*iname=='-' && !iname[1]) ))
104 return xstrdup ("-");
106 n = strlen(iname);
107 if( n > 4 && ( !CMP_FILENAME(iname+n-4, EXTSEP_S "gpg")
108 || !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp")
109 || !CMP_FILENAME(iname+n-4, EXTSEP_S "sig")
110 || !CMP_FILENAME(iname+n-4, EXTSEP_S "asc") ) ) {
111 char *buf = xstrdup ( iname );
112 buf[n-4] = 0;
113 return buf;
115 else if( n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign") ) {
116 char *buf = xstrdup ( iname );
117 buf[n-5] = 0;
118 return buf;
121 log_info(_("%s: unknown suffix\n"), iname );
122 return NULL;
126 /****************
127 * Ask for a outputfilename and use the given one as default.
128 * Return NULL if no file has been given or it is not possible to
129 * ask the user.
131 char *
132 ask_outfile_name( const char *name, size_t namelen )
134 size_t n;
135 const char *s;
136 char *prompt;
137 char *fname;
138 char *defname;
140 if( opt.batch )
141 return NULL;
143 s = _("Enter new filename");
145 n = strlen(s) + namelen + 10;
146 defname = name && namelen? make_printable_string( name, namelen, 0): NULL;
147 prompt = xmalloc (n);
148 if( defname )
149 sprintf(prompt, "%s [%s]: ", s, defname );
150 else
151 sprintf(prompt, "%s: ", s );
152 fname = cpr_get("openfile.askoutname", prompt );
153 cpr_kill_prompt();
154 xfree (prompt);
155 if( !*fname ) {
156 xfree ( fname ); fname = NULL;
157 fname = defname; defname = NULL;
159 xfree (defname);
160 if (fname)
161 trim_spaces (fname);
162 return fname;
166 /****************
167 * Make an output filename for the inputfile INAME.
168 * Returns an iobuf_t and an errorcode
169 * Mode 0 = use ".gpg"
170 * 1 = use ".asc"
171 * 2 = use ".sig"
174 open_outfile( const char *iname, int mode, iobuf_t *a )
176 int rc = 0;
178 *a = NULL;
179 if( (!iname || (*iname=='-' && !iname[1])) && !opt.outfile ) {
180 if( !(*a = iobuf_create(NULL)) ) {
181 rc = gpg_error_from_errno (errno);
182 log_error(_("%s: can't open: %s\n"), "[stdout]", strerror(errno) );
184 else if( opt.verbose )
185 log_info(_("writing to stdout\n"));
187 else {
188 char *buf = NULL;
189 const char *name;
191 if( opt.dry_run )
192 name = "/dev/null";
193 else if( opt.outfile )
194 name = opt.outfile;
195 else {
196 #ifdef USE_ONLY_8DOT3
197 if (opt.mangle_dos_filenames)
199 /* It is quite common DOS system to have only one dot in a
200 * a filename So if we have something like this, we simple
201 * replace the suffix execpt in cases where the suffix is
202 * larger than 3 characters and not the same as.
203 * We should really map the filenames to 8.3 but this tends to
204 * be more complicated and is probaly a duty of the filesystem
206 char *dot;
207 const char *newsfx = mode==1 ? ".asc" :
208 mode==2 ? ".sig" : ".gpg";
210 buf = xmalloc (strlen(iname)+4+1);
211 strcpy(buf,iname);
212 dot = strchr(buf, '.' );
213 if ( dot && dot > buf && dot[1] && strlen(dot) <= 4
214 && CMP_FILENAME(newsfx, dot) )
216 strcpy(dot, newsfx );
218 else if ( dot && !dot[1] ) /* don't duplicate a dot */
219 strcpy( dot, newsfx+1 );
220 else
221 strcat ( buf, newsfx );
223 if (!buf)
224 #endif /* USE_ONLY_8DOT3 */
226 buf = xmalloc (strlen(iname)+4+1);
227 strcpy(stpcpy(buf,iname), mode==1 ? EXTSEP_S "asc" :
228 mode==2 ? EXTSEP_S "sig" : EXTSEP_S "gpg");
230 name = buf;
233 rc = 0;
234 while( !overwrite_filep (name) )
236 char *tmp = ask_outfile_name (NULL, 0);
237 if ( !tmp || !*tmp )
239 xfree (tmp);
240 rc = GPG_ERR_EEXIST;
241 break;
243 xfree (buf);
244 name = buf = tmp;
247 if( !rc )
249 if( !(*a = iobuf_create( name )) )
251 rc = gpg_error_from_errno (errno);
252 log_error(_("%s: can't create: %s\n"), name, strerror(errno) );
254 else if( opt.verbose )
255 log_info(_("writing to `%s'\n"), name );
257 xfree (buf);
260 return rc;
264 /****************
265 * Try to open a file without the extension ".sig" or ".asc"
266 * Return NULL if such a file is not available.
268 iobuf_t
269 open_sigfile( const char *iname, progress_filter_context_t *pfx )
271 iobuf_t a = NULL;
272 size_t len;
274 if( iname && !(*iname == '-' && !iname[1]) ) {
275 len = strlen(iname);
276 if( len > 4 && ( !strcmp(iname + len - 4, EXTSEP_S "sig")
277 || ( len > 5 && !strcmp(iname + len - 5, EXTSEP_S "sign") )
278 || !strcmp(iname + len - 4, EXTSEP_S "asc")) ) {
279 char *buf;
280 buf = xstrdup (iname);
281 buf[len-(buf[len-1]=='n'?5:4)] = 0 ;
282 a = iobuf_open( buf );
283 if( a && opt.verbose )
284 log_info(_("assuming signed data in `%s'\n"), buf );
285 if (a && pfx)
286 handle_progress (pfx, a, buf);
287 xfree (buf);
290 return a;
293 /****************
294 * Copy the option file skeleton to the given directory.
296 static void
297 copy_options_file( const char *destdir )
299 const char *datadir = GNUPG_DATADIR;
300 char *fname;
301 FILE *src, *dst;
302 int linefeeds=0;
303 int c;
304 mode_t oldmask;
305 int esc = 0;
306 int any_option = 0;
308 if( opt.dry_run )
309 return;
311 fname = xmalloc ( strlen(datadir) + strlen(destdir)
312 + strlen (SKELEXT) + 15 );
313 strcpy(stpcpy(fname, datadir), DIRSEP_S "gpg-conf" SKELEXT );
314 src = fopen( fname, "r" );
315 if( !src ) {
316 log_error(_("%s: can't open: %s\n"), fname, strerror(errno) );
317 xfree (fname);
318 return;
320 strcpy(stpcpy(fname, destdir), DIRSEP_S "gpg" EXTSEP_S "conf" );
321 oldmask=umask(077);
322 dst = fopen( fname, "w" );
323 umask(oldmask);
324 if( !dst ) {
325 log_error(_("%s: can't create: %s\n"), fname, strerror(errno) );
326 fclose( src );
327 xfree (fname);
328 return;
331 while( (c=getc(src)) != EOF ) {
332 if( linefeeds < 3 ) {
333 if( c == '\n' )
334 linefeeds++;
336 else {
337 putc( c, dst );
338 if (c== '\n')
339 esc = 1;
340 else if (esc == 1) {
341 if (c == ' ' || c == '\t')
343 else if (c == '#')
344 esc = 2;
345 else
346 any_option = 1;
350 fclose( dst );
351 fclose( src );
352 log_info(_("new configuration file `%s' created\n"), fname );
353 if (any_option)
354 log_info (_("WARNING: options in `%s'"
355 " are not yet active during this run\n"),
356 fname);
357 xfree (fname);
361 void
362 try_make_homedir( const char *fname )
364 const char *defhome = GNUPG_DEFAULT_HOMEDIR;
366 /* Create the directory only if the supplied directory name
367 * is the same as the default one. This way we avoid to create
368 * arbitrary directories when a non-default homedirectory is used.
369 * To cope with HOME, we do compare only the suffix if we see that
370 * the default homedir does start with a tilde.
372 if( opt.dry_run || opt.no_homedir_creation )
373 return;
375 if ( ( *defhome == '~'
376 && ( strlen(fname) >= strlen (defhome+1)
377 && !strcmp(fname+strlen(fname)-strlen(defhome+1),
378 defhome+1 ) ))
379 || ( *defhome != '~'
380 && !compare_filenames( fname, defhome ) )
382 if( mkdir( fname, S_IRUSR|S_IWUSR|S_IXUSR ) )
383 log_fatal( _("%s: can't create directory: %s\n"),
384 fname, strerror(errno) );
385 else if( !opt.quiet )
386 log_info( _("%s: directory created\n"), fname );
387 copy_options_file( fname );
388 /* log_info(_("you have to start GnuPG again, " */
389 /* "so it can read the new configuration file\n") ); */
390 /* g10_exit(1); */