1
/******************************************************************************
2
* $Id: utils.c 310 2006-06-09 19:53:35Z joshe $
4
* Copyright (c) 2005-2006 Transmission authors and contributors
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:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
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
*****************************************************************************/
25
#include "transmission.h"
27
void tr_msg( int level, char * msg, ... )
31
static int verboseLevel = 0;
36
env = getenv( "TR_DEBUG" );
37
verboseLevel = env ? atoi( env ) : -1;
38
verboseLevel = verboseLevel ? verboseLevel : -1;
41
if( verboseLevel < 1 && level > TR_MSG_ERR )
45
if( verboseLevel < 2 && level > TR_MSG_INF )
50
va_start( args, msg );
51
vsnprintf( string, sizeof( string ), msg, args );
53
fprintf( stderr, "%s\n", string );
56
int tr_rand( int sup )
67
void * tr_memmem( const void *vbig, size_t big_len,
68
const void *vlittle, size_t little_len )
70
const char *big = vbig;
71
const char *little = vlittle;
74
if( 0 == big_len || 0 == little_len )
79
for( ii = 0; ii + little_len <= big_len; ii++ )
81
for( jj = 0; jj < little_len; jj++ )
83
if( big[ii + jj] != little[jj] )
88
if( jj == little_len )
90
return (char*)big + ii;
97
int tr_mkdir( char * path )
108
while( ( p = strchr( pp, '/' ) ) || ( p = strchr( pp, '\0' ) ) )
118
if( stat( path, &sb ) )
120
/* Folder doesn't exist yet */
121
if( mkdir( path, 0777 ) )
123
tr_err( "Could not create directory %s (%s)", path,
129
else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
131
/* Node exists but isn't a folder */
132
tr_err( "Remove %s, it's in the way.", path );