1
/* fileutil.c - file utilities
2
* Copyright (C) 1998, 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
34
* Extract from a given path the filename component.
38
make_basename(const char *filepath, const char *inputpath)
41
return riscos_make_basename(filepath, inputpath);
46
if ( !(p=strrchr(filepath, DIRSEP_C)) )
47
#ifdef HAVE_DRIVE_LETTERS
48
if ( !(p=strrchr(filepath, '\\')) )
49
if ( !(p=strrchr(filepath, ':')) )
52
return m_strdup(filepath);
61
* Extract from a given filename the path prepended to it.
62
* If their isn't a path prepended to the filename, a dot
67
make_dirname(const char *filepath)
73
if ( !(p=strrchr(filepath, DIRSEP_C)) )
74
#ifdef HAVE_DRIVE_LETTERS
75
if ( !(p=strrchr(filepath, '\\')) )
76
if ( !(p=strrchr(filepath, ':')) )
79
return m_strdup(EXTSEP_S);
82
dirname_length = p-filepath;
83
dirname = m_alloc(dirname_length+1);
84
strncpy(dirname, filepath, dirname_length);
85
dirname[dirname_length] = 0;
93
Construct a filename from the NULL terminated list of parts. Tilde
94
expansion is done here. Note that FIRST_PART must never be NULL and
95
that this function is guaranteed to return an allocated string. */
97
make_filename( const char *first_part, ... )
102
char *name, *home, *p;
104
va_start( arg_ptr, first_part ) ;
105
n = strlen(first_part)+1;
106
while( (s=va_arg(arg_ptr, const char *)) )
112
if( *first_part == '~' && first_part[1] == DIRSEP_C
113
&& (home = getenv("HOME")) && *home )
117
p = home ? stpcpy(stpcpy(name,home), first_part+1)
118
: stpcpy(name, first_part);
119
va_start( arg_ptr, first_part ) ;
120
while( (s=va_arg(arg_ptr, const char *)) )
121
p = stpcpy(stpcpy(p, DIRSEP_S), s);
126
#else /* __riscos__ */
127
p = riscos_gstrans(name);
130
#endif /* __riscos__ */
135
compare_filenames( const char *a, const char *b )
137
/* ? check whether this is an absolute filename and
141
#ifdef HAVE_DRIVE_LETTERS
142
return ascii_strcasecmp(a,b);
146
#else /* __riscos__ */
150
abuf = riscos_gstrans(a);
151
bbuf = riscos_gstrans(b);
153
c = ascii_strcasecmp (abuf, bbuf);
159
#endif /* __riscos__ */
164
* A simple function to decide whether the filename is stdout
165
* or a real filename.
168
print_fname_stdout( const char *s )
170
if( !s || (*s == '-' && !s[1]) )
177
print_fname_stdin( const char *s )
179
if( !s || (*s == '-' && !s[1]) )
185
* Check if the file is compressed.
188
is_file_compressed( const char *s, int *ret_rc )
194
struct magic_compress_s {
198
{ 3, { 0x42, 0x5a, 0x68, 0x00 } }, /* bzip2 */
199
{ 3, { 0x1f, 0x8b, 0x08, 0x00 } }, /* gzip */
200
{ 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
203
if ( iobuf_is_pipe_filename (s) || !ret_rc )
204
return 0; /* We can't check stdin or no file was given */
208
*ret_rc = G10ERR_OPEN_FILE;
212
if ( iobuf_get_filelength( a ) < 4 ) {
217
if ( iobuf_read( a, buf, 4 ) == -1 ) {
218
*ret_rc = G10ERR_READ_FILE;
222
for ( i = 0; i < DIM( magic ); i++ ) {
223
if ( !memcmp( buf, magic[i].magic, magic[i].len ) ) {