source: trunk/libtransmission/utils.c @ 260

Last change on this file since 260 was 260, checked in by titer, 16 years ago

Added $Id$ keywords and updated a few headers

File size: 2.7 KB
Line 
1/******************************************************************************
2 * $Id$
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
27void tr_msg( int level, char * msg, ... )
28{
29    char         string[256];
30    va_list      args;
31    static int   verboseLevel = 0;
32
33    if( !verboseLevel )
34    {
35        char * env;
36        env          = getenv( "TR_DEBUG" );
37        verboseLevel = env ? atoi( env ) : -1;
38        verboseLevel = verboseLevel ? verboseLevel : -1;
39    }
40
41    if( verboseLevel < 1 && level > TR_MSG_ERR )
42    {
43        return;
44    }
45    if( verboseLevel < 2 && level > TR_MSG_INF )
46    {
47        return;
48    }
49
50    va_start( args, msg );
51    vsnprintf( string, sizeof( string ), msg, args );
52    va_end( args );
53    fprintf( stderr, "%s\n", string );
54}
55
56int tr_rand( int sup )
57{
58    static int init = 0;
59    if( !init )
60    {
61        srand( tr_date() );
62        init = 1;
63    }
64    return rand() % sup;
65}
66
67void * tr_memmem( const void *vbig, size_t big_len,
68                  const void *vlittle, size_t little_len )
69{
70    const char *big = vbig;
71    const char *little = vlittle;
72    size_t ii, jj;
73
74    if( 0 == big_len || 0 == little_len )
75    {
76        return NULL;
77    }
78
79    for( ii = 0; ii + little_len <= big_len; ii++ )
80    {
81        for( jj = 0; jj < little_len; jj++ )
82        {
83            if( big[ii + jj] != little[jj] )
84            {
85                break;
86            }
87        }
88        if( jj == little_len )
89        {
90            return (char*)big + ii;
91        }
92    }
93
94    return NULL;
95}
Note: See TracBrowser for help on using the repository browser.