source: trunk/libtransmission/utils.c @ 346

Last change on this file since 346 was 310, checked in by joshe, 15 years ago

Add support to libtransmission and the MacOS X GUI for saving private copies of

torrent files.

  • Property svn:keywords set to Date Rev Author Id
File size: 3.7 KB
Line 
1/******************************************************************************
2 * $Id: utils.c 310 2006-06-09 19:53:35Z titer $
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}
96
97int tr_mkdir( char * path )
98{
99    char      * p, * pp;
100    struct stat sb;
101    int done;
102
103    p = path;
104    while( '/' == *p )
105      p++;
106    pp = p;
107    done = 0;
108    while( ( p = strchr( pp, '/' ) ) || ( p = strchr( pp, '\0' ) ) )
109    {
110        if( '\0' == *p)
111        {
112            done = 1;
113        }
114        else
115        {
116            *p = '\0';
117        }
118        if( stat( path, &sb ) )
119        {
120            /* Folder doesn't exist yet */
121            if( mkdir( path, 0777 ) )
122            {
123                tr_err( "Could not create directory %s (%s)", path,
124                        strerror( errno ) );
125                *p = '/';
126                return 1;
127            }
128        }
129        else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
130        {
131            /* Node exists but isn't a folder */
132            tr_err( "Remove %s, it's in the way.", path );
133            *p = '/';
134            return 1;
135        }
136        if( done )
137        {
138            break;
139        }
140        *p = '/';
141        p++;
142        pp = p;
143    }
144
145    return 0;
146}
Note: See TracBrowser for help on using the repository browser.