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
27 #include <sys/types.h>
42 #define SKELEXT ".skl"
44 #define SKELEXT EXTSEP_S "skel"
47 #if defined (HAVE_DRIVE_LETTERS) || defined (__riscos__)
48 #define CMP_FILENAME(a,b) ascii_strcasecmp( (a), (b) )
50 #define CMP_FILENAME(a,b) strcmp( (a), (b) )
53 #ifdef MKDIR_TAKES_ONE_ARG
55 #define mkdir(a,b) mkdir(a)
58 /* FIXME: Implement opt.interactive. */
61 * Check whether FNAME exists and ask if it's okay to overwrite an
63 * Returns: True: it's okay to overwrite or the file does not exist
64 * False: Do not overwrite
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 */
80 /* fixme: add some backup stuff in case of overwrite */
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)? ")) )
95 * Strip know extensions from iname and return a newly allocated
96 * filename. Return NULL if we can't do that.
99 make_outfile_name( const char *iname
)
103 if( (!iname
|| (*iname
=='-' && !iname
[1]) ))
104 return xstrdup ("-");
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
);
115 else if( n
> 5 && !CMP_FILENAME(iname
+n
-5, EXTSEP_S
"sign") ) {
116 char *buf
= xstrdup ( iname
);
121 log_info(_("%s: unknown suffix\n"), iname
);
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
132 ask_outfile_name( const char *name
, size_t namelen
)
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
);
149 sprintf(prompt
, "%s [%s]: ", s
, defname
);
151 sprintf(prompt
, "%s: ", s
);
152 fname
= cpr_get("openfile.askoutname", prompt
);
156 xfree ( fname
); fname
= NULL
;
157 fname
= defname
; defname
= NULL
;
167 * Make an output filename for the inputfile INAME.
168 * Returns an iobuf_t and an errorcode
169 * Mode 0 = use ".gpg"
174 open_outfile( const char *iname
, int mode
, iobuf_t
*a
)
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"));
193 else if( opt
.outfile
)
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
207 const char *newsfx
= mode
==1 ? ".asc" :
208 mode
==2 ? ".sig" : ".gpg";
210 buf
= xmalloc (strlen(iname
)+4+1);
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 );
221 strcat ( buf
, newsfx
);
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");
234 while( !overwrite_filep (name
) )
236 char *tmp
= ask_outfile_name (NULL
, 0);
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
);
265 * Try to open a file without the extension ".sig" or ".asc"
266 * Return NULL if such a file is not available.
269 open_sigfile( const char *iname
, progress_filter_context_t
*pfx
)
274 if( iname
&& !(*iname
== '-' && !iname
[1]) ) {
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")) ) {
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
);
286 handle_progress (pfx
, a
, buf
);
294 * Copy the option file skeleton to the given directory.
297 copy_options_file( const char *destdir
)
299 const char *datadir
= GNUPG_DATADIR
;
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" );
316 log_error(_("%s: can't open: %s\n"), fname
, strerror(errno
) );
320 strcpy(stpcpy(fname
, destdir
), DIRSEP_S
"gpg" EXTSEP_S
"conf" );
322 dst
= fopen( fname
, "w" );
325 log_error(_("%s: can't create: %s\n"), fname
, strerror(errno
) );
331 while( (c
=getc(src
)) != EOF
) {
332 if( linefeeds
< 3 ) {
341 if (c
== ' ' || c
== '\t')
352 log_info(_("new configuration file `%s' created\n"), fname
);
354 log_info (_("WARNING: options in `%s'"
355 " are not yet active during this run\n"),
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
)
375 if ( ( *defhome
== '~'
376 && ( strlen(fname
) >= strlen (defhome
+1)
377 && !strcmp(fname
+strlen(fname
)-strlen(defhome
+1),
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") ); */