~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to util/stringhelp.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
 
/* stringhelp.c -  standard string helper functions
2
 
 *      Copyright (C) 1998, 1999 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 <stdlib.h>
23
 
#include <string.h>
24
 
#include <ctype.h>
25
 
 
26
 
#include "libutil-config.h"
27
 
#include "stringhelp.h"
28
 
 
29
 
 
30
 
/****************
31
 
 * look for the substring SUB in buffer and return a pointer to that
32
 
 * substring in BUF or NULL if not found.
33
 
 * Comparison is case-insensitive.
34
 
 */
35
 
const char *
36
 
memistr( const char *buf, size_t buflen, const char *sub )
37
 
{
38
 
    const byte *t, *s ;
39
 
    size_t n;
40
 
 
41
 
    for( t=buf, n=buflen, s=sub ; n ; t++, n-- )
42
 
        if( toupper(*t) == toupper(*s) ) {
43
 
            for( buf=t++, buflen = n--, s++;
44
 
                 n && toupper(*t) == toupper(*s); t++, s++, n-- )
45
 
                ;
46
 
            if( !*s )
47
 
                return buf;
48
 
            t = buf; n = buflen; s = sub ;
49
 
        }
50
 
 
51
 
    return NULL ;
52
 
}
53
 
 
54
 
/****************
55
 
 * Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein
56
 
 * '\0' angeh�ngt. Ist n = 0, so geschieht nichts, ist Destination
57
 
 * gleich NULL, so wird via libutil_malloc Speicher besorgt, ist dann nicht
58
 
 * gen�gend Speicher vorhanden, so bricht die funktion ab.
59
 
 */
60
 
char *
61
 
mem2str( char *dest , const void *src , size_t n )
62
 
{
63
 
    char *d;
64
 
    const char *s;
65
 
 
66
 
    if( n ) {
67
 
        if( !dest )
68
 
            dest = libutil_xmalloc( n ) ;
69
 
        d = dest;
70
 
        s = src ;
71
 
        for(n--; n && *s; n-- )
72
 
            *d++ = *s++;
73
 
        *d = '\0' ;
74
 
    }
75
 
 
76
 
    return dest ;
77
 
}
78
 
 
79
 
 
80
 
/****************
81
 
 * remove leading and trailing white spaces
82
 
 */
83
 
char *
84
 
trim_spaces( char *str )
85
 
{
86
 
    char *string, *p, *mark;
87
 
 
88
 
    string = str;
89
 
    /* find first non space character */
90
 
    for( p=string; *p && isspace( *(byte*)p ) ; p++ )
91
 
        ;
92
 
    /* move characters */
93
 
    for( (mark = NULL); (*string = *p); string++, p++ )
94
 
        if( isspace( *(byte*)p ) ) {
95
 
            if( !mark )
96
 
                mark = string ;
97
 
        }
98
 
        else
99
 
            mark = NULL ;
100
 
    if( mark )
101
 
        *mark = '\0' ;  /* remove trailing spaces */
102
 
 
103
 
    return str ;
104
 
}
105
 
 
106
 
/****************
107
 
 * remove trailing white spaces
108
 
 */
109
 
char *
110
 
trim_trailing_spaces( char *string )
111
 
{
112
 
    char *p, *mark;
113
 
 
114
 
    for( mark = NULL, p = string; *p; p++ ) {
115
 
        if( isspace( *(byte*)p ) ) {
116
 
            if( !mark )
117
 
                mark = p;
118
 
        }
119
 
        else
120
 
            mark = NULL;
121
 
    }
122
 
    if( mark )
123
 
        *mark = '\0' ;
124
 
 
125
 
    return string ;
126
 
}
127
 
 
128
 
 
129
 
 
130
 
unsigned
131
 
trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
132
 
{
133
 
    byte *p, *mark;
134
 
    unsigned n;
135
 
 
136
 
    for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
137
 
        if( strchr(trimchars, *p ) ) {
138
 
            if( !mark )
139
 
                mark = p;
140
 
        }
141
 
        else
142
 
            mark = NULL;
143
 
    }
144
 
 
145
 
    if( mark ) {
146
 
        *mark = 0;
147
 
        return mark - line;
148
 
    }
149
 
    return len;
150
 
}
151
 
 
152
 
/****************
153
 
 * remove trailing white spaces and return the length of the buffer
154
 
 */
155
 
unsigned
156
 
trim_trailing_ws( byte *line, unsigned len )
157
 
{
158
 
    return trim_trailing_chars( line, len, " \t\r\n" );
159
 
}
160
 
 
161
 
 
162
 
 
163
 
/*********************************************
164
 
 ********** missing string functions *********
165
 
 *********************************************/
166
 
 
167
 
#ifndef HAVE_STPCPY
168
 
char *
169
 
stpcpy(char *a,const char *b)
170
 
{
171
 
    while( *b )
172
 
        *a++ = *b++;
173
 
    *a = 0;
174
 
 
175
 
    return (char*)a;
176
 
}
177
 
#endif
178
 
 
179
 
#ifndef HAVE_STRLWR
180
 
char *
181
 
strlwr(char *s)
182
 
{
183
 
    char *p;
184
 
    for(p=s; *p; p++ )
185
 
        *p = tolower(*p);
186
 
    return s;
187
 
}
188
 
#endif
189
 
 
190
 
 
191
 
#ifndef HAVE_STRCASECMP
192
 
int
193
 
strcasecmp( const char *a, const char *b )
194
 
{
195
 
    for( ; *a && *b; a++, b++ ) {
196
 
        if( *a != *b && toupper(*a) != toupper(*b) )
197
 
            break;
198
 
    }
199
 
    return *(const byte*)a - *(const byte*)b;
200
 
}
201
 
#endif
202
 
 
203
 
 
204
 
/****************
205
 
 * mingw32/cpd has a memicmp()
206
 
 */
207
 
#ifndef HAVE_MEMICMP
208
 
int
209
 
memicmp( const char *a, const char *b, size_t n )
210
 
{
211
 
    for( ; n; n--, a++, b++ )
212
 
        if( *a != *b  && toupper(*(const byte*)a) != toupper(*(const byte*)b) )
213
 
            return *(const byte *)a - *(const byte*)b;
214
 
    return 0;
215
 
}
216
 
#endif
217
 
 
218
 
 
219