1 /* fileutil.c - file utilities
2 * Copyright (C) 1998, 2003, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 #include <sys/types.h>
39 * Extract from a given path the filename component.
43 make_basename(const char *filepath
, const char *inputpath
)
46 return riscos_make_basename(filepath
, inputpath
);
51 if ( !(p
=strrchr(filepath
, DIRSEP_C
)) )
52 #ifdef HAVE_DRIVE_LETTERS
53 if ( !(p
=strrchr(filepath
, '\\')) )
54 if ( !(p
=strrchr(filepath
, ':')) )
57 return xstrdup(filepath
);
66 * Extract from a given filename the path prepended to it.
67 * If their isn't a path prepended to the filename, a dot
72 make_dirname(const char *filepath
)
78 if ( !(p
=strrchr(filepath
, DIRSEP_C
)) )
79 #ifdef HAVE_DRIVE_LETTERS
80 if ( !(p
=strrchr(filepath
, '\\')) )
81 if ( !(p
=strrchr(filepath
, ':')) )
84 return xstrdup(EXTSEP_S
);
87 dirname_length
= p
-filepath
;
88 dirname
= xmalloc(dirname_length
+1);
89 strncpy(dirname
, filepath
, dirname_length
);
90 dirname
[dirname_length
] = 0;
95 /* Expand tildes. Handles both the ~/foo and ~username/foo cases.
96 Returns what the tilde expands to. *name is advanced to be past
97 the tilde expansion. */
99 untilde(const char **name
)
103 assert((*name
)[0]=='~');
105 if((*name
)[1]==DIRSEP_C
|| (*name
)[1]=='\0')
107 /* This is the "~/foo" or "~" case. */
108 char *tmp
=getenv("HOME");
117 pwd
=getpwuid(getuid());
119 home
=xstrdup(pwd
->pw_dir
);
128 /* This is the "~username" case. */
132 user
=xstrdup((*name
)+1);
134 sep
=strchr(user
,DIRSEP_C
);
141 home
=xstrdup(pwd
->pw_dir
);
142 (*name
)+=1+strlen(user
);
153 Construct a filename from the NULL terminated list of parts. Tilde
154 expansion is done here. Note that FIRST_PART must never be NULL and
155 that this function is guaranteed to return an allocated string. */
157 make_filename( const char *first_part
, ... )
162 char *name
, *p
, *home
=NULL
;
164 va_start( arg_ptr
, first_part
) ;
165 n
= strlen(first_part
)+1;
166 while( (s
=va_arg(arg_ptr
, const char *)) )
173 home
=untilde(&first_part
);
179 p
= home
? stpcpy(stpcpy(name
,home
), first_part
)
180 : stpcpy(name
, first_part
);
181 va_start( arg_ptr
, first_part
) ;
182 while( (s
=va_arg(arg_ptr
, const char *)) )
183 p
= stpcpy(stpcpy(p
, DIRSEP_S
), s
);
189 #else /* __riscos__ */
190 p
= riscos_gstrans(name
);
193 #endif /* __riscos__ */
198 compare_filenames( const char *a
, const char *b
)
200 /* ? check whether this is an absolute filename and
204 #ifdef HAVE_DRIVE_LETTERS
205 return ascii_strcasecmp(a
,b
);
209 #else /* __riscos__ */
213 abuf
= riscos_gstrans(a
);
214 bbuf
= riscos_gstrans(b
);
216 c
= ascii_strcasecmp (abuf
, bbuf
);
222 #endif /* __riscos__ */
227 * A simple function to decide whether the filename is stdout
228 * or a real filename.
231 print_fname_stdout( const char *s
)
233 if( !s
|| (*s
== '-' && !s
[1]) )
240 print_fname_stdin( const char *s
)
242 if( !s
|| (*s
== '-' && !s
[1]) )
248 * Check if the file is compressed.
251 is_file_compressed( const char *s
, int *ret_rc
)
258 struct magic_compress_s
{
262 { 3, { 0x42, 0x5a, 0x68, 0x00 } }, /* bzip2 */
263 { 3, { 0x1f, 0x8b, 0x08, 0x00 } }, /* gzip */
264 { 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
267 if ( iobuf_is_pipe_filename (s
) || !ret_rc
)
268 return 0; /* We can't check stdin or no file was given */
272 *ret_rc
= G10ERR_OPEN_FILE
;
276 if ( iobuf_get_filelength( a
, &overflow
) < 4 && !overflow
) {
281 if ( iobuf_read( a
, buf
, 4 ) == -1 ) {
282 *ret_rc
= G10ERR_READ_FILE
;
286 for ( i
= 0; i
< DIM( magic
); i
++ ) {
287 if ( !memcmp( buf
, magic
[i
].magic
, magic
[i
].len
) ) {