Revert last. It is still wrong.
[gnupg.git] / util / fileutil.c
blob5834e3d89ca0ddec37d776d53e4cc205e1efa1aa
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,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #ifdef HAVE_PWD_H
31 #include <pwd.h>
32 #endif
33 #include "util.h"
34 #include "memory.h"
35 #include "ttyio.h"
38 /***************
39 * Extract from a given path the filename component.
42 char *
43 make_basename(const char *filepath, const char *inputpath)
45 #ifdef __riscos__
46 return riscos_make_basename(filepath, inputpath);
47 #endif
49 char *p;
51 if ( !(p=strrchr(filepath, DIRSEP_C)) )
52 #ifdef HAVE_DRIVE_LETTERS
53 if ( !(p=strrchr(filepath, '\\')) )
54 if ( !(p=strrchr(filepath, ':')) )
55 #endif
57 return xstrdup(filepath);
60 return xstrdup(p+1);
65 /***************
66 * Extract from a given filename the path prepended to it.
67 * If their isn't a path prepended to the filename, a dot
68 * is returned ('.').
71 char *
72 make_dirname(const char *filepath)
74 char *dirname;
75 int dirname_length;
76 char *p;
78 if ( !(p=strrchr(filepath, DIRSEP_C)) )
79 #ifdef HAVE_DRIVE_LETTERS
80 if ( !(p=strrchr(filepath, '\\')) )
81 if ( !(p=strrchr(filepath, ':')) )
82 #endif
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;
92 return dirname;
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. */
98 static char *
99 untilde(const char **name)
101 char *home=NULL;
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");
109 if(tmp)
110 home=xstrdup(tmp);
112 #ifdef HAVE_GETPWUID
113 if(!home)
115 struct passwd *pwd;
117 pwd=getpwuid(getuid());
118 if(pwd)
119 home=xstrdup(pwd->pw_dir);
121 #endif
122 if(home)
123 (*name)++;
125 #ifdef HAVE_GETPWNAM
126 else
128 /* This is the "~username" case. */
129 char *user,*sep;
130 struct passwd *pwd;
132 user=xstrdup((*name)+1);
134 sep=strchr(user,DIRSEP_C);
135 if(sep)
136 *sep='\0';
138 pwd=getpwnam(user);
139 if(pwd)
141 home=xstrdup(pwd->pw_dir);
142 (*name)+=1+strlen(user);
145 xfree(user);
147 #endif
149 return home;
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. */
156 char *
157 make_filename( const char *first_part, ... )
159 va_list arg_ptr ;
160 size_t n;
161 const char *s;
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 *)) )
167 n += strlen(s) + 1;
168 va_end(arg_ptr);
170 #ifndef __riscos__
171 if(*first_part=='~')
173 home=untilde(&first_part);
174 if(home)
175 n+=strlen(home);
177 #endif
178 name = xmalloc(n);
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);
184 va_end(arg_ptr);
185 xfree(home);
187 #ifndef __riscos__
188 return name;
189 #else /* __riscos__ */
190 p = riscos_gstrans(name);
191 xfree(name);
192 return p;
193 #endif /* __riscos__ */
198 compare_filenames( const char *a, const char *b )
200 /* ? check whether this is an absolute filename and
201 * resolve symlinks?
203 #ifndef __riscos__
204 #ifdef HAVE_DRIVE_LETTERS
205 return ascii_strcasecmp(a,b);
206 #else
207 return strcmp(a,b);
208 #endif
209 #else /* __riscos__ */
210 int c = 0;
211 char *abuf, *bbuf;
213 abuf = riscos_gstrans(a);
214 bbuf = riscos_gstrans(b);
216 c = ascii_strcasecmp (abuf, bbuf);
218 xfree(abuf);
219 xfree(bbuf);
221 return c;
222 #endif /* __riscos__ */
226 /****************
227 * A simple function to decide whether the filename is stdout
228 * or a real filename.
230 const char *
231 print_fname_stdout( const char *s )
233 if( !s || (*s == '-' && !s[1]) )
234 return "[stdout]";
235 return s;
239 const char *
240 print_fname_stdin( const char *s )
242 if( !s || (*s == '-' && !s[1]) )
243 return "[stdin]";
244 return s;
247 /****************
248 * Check if the file is compressed.
251 is_file_compressed( const char *s, int *ret_rc )
253 IOBUF a;
254 byte buf[4];
255 int i, rc = 0;
256 int overflow;
258 struct magic_compress_s {
259 size_t len;
260 byte magic[4];
261 } magic[] = {
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 */
270 a = iobuf_open( s );
271 if ( a == NULL ) {
272 *ret_rc = G10ERR_OPEN_FILE;
273 return 0;
276 if ( iobuf_get_filelength( a, &overflow ) < 4 && !overflow) {
277 *ret_rc = 0;
278 goto leave;
281 if ( iobuf_read( a, buf, 4 ) == -1 ) {
282 *ret_rc = G10ERR_READ_FILE;
283 goto leave;
286 for ( i = 0; i < DIM( magic ); i++ ) {
287 if ( !memcmp( buf, magic[i].magic, magic[i].len ) ) {
288 *ret_rc = 0;
289 rc = 1;
290 break;
294 leave:
295 iobuf_close( a );
296 return rc;