~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty-security

« back to all changes in this revision

Viewing changes to util/fileutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* fileutil.c -  file utilities
2
 
 *      Copyright (C) 1998, 2003 Free Software Foundation, Inc.
3
 
 *
4
 
 * This file is part of GnuPG.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#include <config.h>
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <stdarg.h>
25
 
#include <string.h>
26
 
#include <assert.h>
27
 
#include <unistd.h>
28
 
#include "util.h"
29
 
#include "memory.h"
30
 
#include "ttyio.h"
31
 
 
32
 
 
33
 
/***************
34
 
 * Extract from a given path the filename component.
35
 
 *
36
 
 */
37
 
char *
38
 
make_basename(const char *filepath, const char *inputpath)
39
 
{
40
 
#ifdef __riscos__
41
 
    return riscos_make_basename(filepath, inputpath);
42
 
#endif
43
 
 
44
 
    char *p;
45
 
 
46
 
    if ( !(p=strrchr(filepath, DIRSEP_C)) )
47
 
#ifdef HAVE_DRIVE_LETTERS
48
 
        if ( !(p=strrchr(filepath, '\\')) )
49
 
            if ( !(p=strrchr(filepath, ':')) )
50
 
#endif
51
 
              {
52
 
                return m_strdup(filepath);
53
 
              }
54
 
 
55
 
    return m_strdup(p+1);
56
 
}
57
 
 
58
 
 
59
 
 
60
 
/***************
61
 
 * Extract from a given filename the path prepended to it.
62
 
 * If their isn't a path prepended to the filename, a dot
63
 
 * is returned ('.').
64
 
 *
65
 
 */
66
 
char *
67
 
make_dirname(const char *filepath)
68
 
{
69
 
    char *dirname;
70
 
    int  dirname_length;
71
 
    char *p;
72
 
 
73
 
    if ( !(p=strrchr(filepath, DIRSEP_C)) )
74
 
#ifdef HAVE_DRIVE_LETTERS
75
 
        if ( !(p=strrchr(filepath, '\\')) )
76
 
            if ( !(p=strrchr(filepath, ':')) )
77
 
#endif
78
 
              {
79
 
                return m_strdup(EXTSEP_S);
80
 
              }
81
 
 
82
 
    dirname_length = p-filepath;
83
 
    dirname = m_alloc(dirname_length+1);
84
 
    strncpy(dirname, filepath, dirname_length);
85
 
    dirname[dirname_length] = 0;
86
 
 
87
 
    return dirname;
88
 
}
89
 
 
90
 
 
91
 
 
92
 
/*
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.  */
96
 
char *
97
 
make_filename( const char *first_part, ... )
98
 
{
99
 
    va_list arg_ptr ;
100
 
    size_t n;
101
 
    const char *s;
102
 
    char *name, *home, *p;
103
 
 
104
 
    va_start( arg_ptr, first_part ) ;
105
 
    n = strlen(first_part)+1;
106
 
    while( (s=va_arg(arg_ptr, const char *)) )
107
 
        n += strlen(s) + 1;
108
 
    va_end(arg_ptr);
109
 
 
110
 
    home = NULL;
111
 
#ifndef __riscos__
112
 
    if( *first_part == '~' && first_part[1] == DIRSEP_C
113
 
                           && (home = getenv("HOME")) && *home )
114
 
        n += strlen(home);
115
 
#endif
116
 
    name = m_alloc(n);
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);
122
 
    va_end(arg_ptr);
123
 
 
124
 
#ifndef __riscos__
125
 
    return name;
126
 
#else /* __riscos__ */
127
 
    p = riscos_gstrans(name);
128
 
    m_free(name);
129
 
    return p;
130
 
#endif /* __riscos__ */
131
 
}
132
 
 
133
 
 
134
 
int
135
 
compare_filenames( const char *a, const char *b )
136
 
{
137
 
    /* ? check whether this is an absolute filename and
138
 
     * resolve symlinks?
139
 
     */
140
 
#ifndef __riscos__
141
 
#ifdef HAVE_DRIVE_LETTERS
142
 
    return ascii_strcasecmp(a,b);
143
 
#else
144
 
    return strcmp(a,b);
145
 
#endif
146
 
#else /* __riscos__ */
147
 
    int c = 0;
148
 
    char *abuf, *bbuf;
149
 
 
150
 
    abuf = riscos_gstrans(a);
151
 
    bbuf = riscos_gstrans(b);
152
 
 
153
 
    c = ascii_strcasecmp (abuf, bbuf);
154
 
 
155
 
    m_free(abuf);
156
 
    m_free(bbuf);
157
 
 
158
 
    return c;
159
 
#endif /* __riscos__ */
160
 
}
161
 
 
162
 
 
163
 
/****************
164
 
 * A simple function to decide whether the filename is stdout
165
 
 * or a real filename.
166
 
 */
167
 
const char *
168
 
print_fname_stdout( const char *s )
169
 
{
170
 
    if( !s || (*s == '-' && !s[1]) )
171
 
        return "[stdout]";
172
 
    return s;
173
 
}
174
 
 
175
 
 
176
 
const char *
177
 
print_fname_stdin( const char *s )
178
 
{
179
 
    if( !s || (*s == '-' && !s[1]) )
180
 
        return "[stdin]";
181
 
    return s;
182
 
}
183
 
 
184
 
/****************
185
 
 * Check if the file is compressed.
186
 
 **/
187
 
int
188
 
is_file_compressed( const char *s, int *ret_rc )
189
 
{
190
 
    IOBUF a;
191
 
    byte buf[4];
192
 
    int i, rc = 0;
193
 
 
194
 
    struct magic_compress_s {
195
 
        size_t len;
196
 
        byte magic[4];
197
 
    } magic[] = {
198
 
        { 3, { 0x42, 0x5a, 0x68, 0x00 } }, /* bzip2 */
199
 
        { 3, { 0x1f, 0x8b, 0x08, 0x00 } }, /* gzip */
200
 
        { 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
201
 
    };
202
 
    
203
 
    if ( iobuf_is_pipe_filename (s) || !ret_rc )
204
 
        return 0; /* We can't check stdin or no file was given */
205
 
 
206
 
    a = iobuf_open( s );
207
 
    if ( a == NULL ) {
208
 
        *ret_rc = G10ERR_OPEN_FILE;
209
 
        return 0;
210
 
    }
211
 
 
212
 
    if ( iobuf_get_filelength( a ) < 4 ) {
213
 
        *ret_rc = 0;
214
 
        goto leave;
215
 
    }
216
 
 
217
 
    if ( iobuf_read( a, buf, 4 ) == -1 ) {
218
 
        *ret_rc = G10ERR_READ_FILE;
219
 
        goto leave;
220
 
    }
221
 
 
222
 
    for ( i = 0; i < DIM( magic ); i++ ) {
223
 
        if ( !memcmp( buf, magic[i].magic, magic[i].len ) ) {
224
 
            *ret_rc = 0;
225
 
            rc = 1;
226
 
            break;
227
 
        }
228
 
    }
229
 
 
230
 
leave:    
231
 
    iobuf_close( a );
232
 
    return rc;
233
 
}