source: trunk/libtransmission/utils.c @ 837

Last change on this file since 837 was 837, checked in by joshe, 17 years ago

Allow a couple of the message functions to be called before the first tr_init()

  • Property svn:keywords set to Date Rev Author Id
File size: 5.6 KB
Line 
1/******************************************************************************
2 * $Id: utils.c 837 2006-08-30 16:35:33Z joshe $
3 *
4 * Copyright (c) 2005-2006 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include "transmission.h"
26
27static tr_lock_t      * messageLock = NULL;
28static int              messageLevel = 0;
29static int              messageQueuing = 0;
30static tr_msg_list_t *  messageQueue = NULL;
31static tr_msg_list_t ** messageQueueTail = &messageQueue;
32
33void tr_msgInit( void )
34{
35    if( NULL == messageLock )
36    {
37        messageLock = calloc( 1, sizeof( *messageLock ) );
38        tr_lockInit( messageLock );
39    }
40}
41
42void tr_setMessageLevel( int level )
43{
44    tr_msgInit();
45    tr_lockLock( messageLock );
46    messageLevel = MAX( 0, level );
47    tr_lockUnlock( messageLock );
48}
49
50int tr_getMessageLevel( void )
51{
52    int ret;
53
54    tr_msgInit();
55    tr_lockLock( messageLock );
56    ret = messageLevel;
57    tr_lockUnlock( messageLock );
58
59    return ret;
60}
61
62void tr_setMessageQueuing( int enabled )
63{
64    tr_msgInit();
65    tr_lockLock( messageLock );
66    messageQueuing = enabled;
67    tr_lockUnlock( messageLock );
68}
69
70tr_msg_list_t * tr_getQueuedMessages( void )
71{
72    tr_msg_list_t * ret;
73
74    assert( NULL != messageLock );
75    tr_lockLock( messageLock );
76    ret = messageQueue;
77    messageQueue = NULL;
78    messageQueueTail = &messageQueue;
79    tr_lockUnlock( messageLock );
80
81    return ret;
82}
83
84void tr_freeMessageList( tr_msg_list_t * list )
85{
86    tr_msg_list_t * next;
87
88    while( NULL != list )
89    {
90        next = list->next;
91        free( list->message );
92        free( list );
93        list = next;
94    }
95}
96
97void tr_msg( int level, char * msg, ... )
98{
99    va_list          args;
100    tr_msg_list_t * newmsg;
101
102    assert( NULL != messageLock );
103    tr_lockLock( messageLock );
104
105    if( !messageLevel )
106    {
107        char * env;
108        env          = getenv( "TR_DEBUG" );
109        messageLevel = ( env ? atoi( env ) : 0 ) + 1;
110        messageLevel = MAX( 1, messageLevel );
111    }
112
113    if( messageLevel >= level )
114    {
115        va_start( args, msg );
116        if( messageQueuing )
117        {
118            newmsg = calloc( 1, sizeof( *newmsg ) );
119            if( NULL != newmsg )
120            {
121                newmsg->level = level;
122                newmsg->when = time( NULL );
123                vasprintf( &newmsg->message, msg, args );
124                if( NULL == newmsg->message )
125                {
126                    free( newmsg );
127                }
128                else
129                {
130                    *messageQueueTail = newmsg;
131                    messageQueueTail = &newmsg->next;
132                }
133            }
134        }
135        else
136        {
137            vfprintf( stderr, msg, args );
138            fputc( '\n', stderr );
139        }
140        va_end( args );
141    }
142
143    tr_lockUnlock( messageLock );
144}
145
146int tr_rand( int sup )
147{
148    static int init = 0;
149    if( !init )
150    {
151        srand( tr_date() );
152        init = 1;
153    }
154    return rand() % sup;
155}
156
157void * tr_memmem( const void *vbig, size_t big_len,
158                  const void *vlittle, size_t little_len )
159{
160    const char *big = vbig;
161    const char *little = vlittle;
162    size_t ii, jj;
163
164    if( 0 == big_len || 0 == little_len )
165    {
166        return NULL;
167    }
168
169    for( ii = 0; ii + little_len <= big_len; ii++ )
170    {
171        for( jj = 0; jj < little_len; jj++ )
172        {
173            if( big[ii + jj] != little[jj] )
174            {
175                break;
176            }
177        }
178        if( jj == little_len )
179        {
180            return (char*)big + ii;
181        }
182    }
183
184    return NULL;
185}
186
187int tr_mkdir( char * path )
188{
189    char      * p, * pp;
190    struct stat sb;
191    int done;
192
193    p = path;
194    while( '/' == *p )
195      p++;
196    pp = p;
197    done = 0;
198    while( ( p = strchr( pp, '/' ) ) || ( p = strchr( pp, '\0' ) ) )
199    {
200        if( '\0' == *p)
201        {
202            done = 1;
203        }
204        else
205        {
206            *p = '\0';
207        }
208        if( stat( path, &sb ) )
209        {
210            /* Folder doesn't exist yet */
211            if( mkdir( path, 0777 ) )
212            {
213                tr_err( "Could not create directory %s (%s)", path,
214                        strerror( errno ) );
215                *p = '/';
216                return 1;
217            }
218        }
219        else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
220        {
221            /* Node exists but isn't a folder */
222            tr_err( "Remove %s, it's in the way.", path );
223            *p = '/';
224            return 1;
225        }
226        if( done )
227        {
228            break;
229        }
230        *p = '/';
231        p++;
232        pp = p;
233    }
234
235    return 0;
236}
Note: See TracBrowser for help on using the repository browser.