]> granicus.if.org Git - transmission/commitdiff
make MAX_PATH_LENGTH private to libtransmission. add tr_dirname() and tr_basename...
authorCharles Kerr <charles@transmissionbt.com>
Tue, 14 Oct 2008 03:39:16 +0000 (03:39 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Tue, 14 Oct 2008 03:39:16 +0000 (03:39 +0000)
13 files changed:
cli/cli.c
daemon/daemon.c
daemon/remote.c
gtk/makemeta-ui.c
libtransmission/blocklist.c
libtransmission/fdlimit.c
libtransmission/makemeta.c
libtransmission/peer-msgs.c
libtransmission/platform.h
libtransmission/torrent-ctor.c
libtransmission/transmission.h
libtransmission/utils.c
libtransmission/utils.h

index 7f25eaa574fe77b05931b7774eeb1f98559ab037..560f68229110c9bacb3f3116ceb188d22fb1a036 100644 (file)
--- a/cli/cli.c
+++ b/cli/cli.c
@@ -308,7 +308,6 @@ main( int     argc,
     tr_handle *  h;
     tr_ctor *    ctor;
     tr_torrent * tor = NULL;
-    char         cwd[MAX_PATH_LENGTH];
 
     printf( "Transmission %s - http://www.transmissionbt.com/\n",
             LONG_VERSION_STRING );
@@ -352,11 +351,7 @@ main( int     argc,
 
     /* if no download directory specified, use cwd instead */
     if( !downloadDir )
-    {
-        tr_getcwd( cwd, sizeof( cwd ) );
-        downloadDir = cwd;
-    }
-
+        downloadDir = tr_strdup( tr_getcwd( ) );
 
     /* Initialize libtransmission */
     h = tr_sessionInitFull(
index 48b624890a6830c1b38116855ac5b6b5b42beece..b0409f78d2cf7ce38355cfffa596c91c3ccf7de5 100644 (file)
@@ -167,7 +167,7 @@ session_init( const char * configDir,
               const char * password,
               int          blocklistEnabled )
 {
-    char          mycwd[MAX_PATH_LENGTH];
+    char        * mycwd;
     tr_benc       state, *dict = NULL;
     int           peerPort = -1, peers = -1;
     int           whitelistEnabled = -1;
@@ -190,7 +190,7 @@ session_init( const char * configDir,
     ****  If neither of those can be found, the TR_DEFAULT fields are used .
     ***/
 
-    tr_getcwd( mycwd, sizeof( mycwd ) );
+    mycwd = tr_getcwd( );
     getConfigStr( dict, KEY_DOWNLOAD_DIR,    &downloadDir,       mycwd );
     getConfigInt( dict, KEY_PEX_ENABLED,     &pexEnabled,
                   TR_DEFAULT_PEX_ENABLED );
@@ -258,6 +258,8 @@ session_init( const char * configDir,
 
     if( dict )
         tr_bencFree( &state );
+
+    tr_free( mycwd );
 }
 
 static const char *
index a34af5c9c9a01884d483be31697b0758674ae641..c9ef2e3a6ac16aa043b8bc4a3606f0a8b0f91d8f 100644 (file)
@@ -151,9 +151,9 @@ absolutify( const char * path )
     if( *path == '/' )
         buf = tr_strdup( path );
     else {
-        char cwd[MAX_PATH_LENGTH];
-        tr_getcwd( cwd, sizeof( cwd ) );
+        char * cwd = tr_getcwd( );
         buf = tr_buildPath( cwd, path, NULL );
+        tr_free( cwd );
     }
 
     return buf;
index b6f7912a6bbaadc052e746f2ac985c82fb464c77..e0d9d42543c9e373dca4037523bc38f239f1f6af 100644 (file)
@@ -248,50 +248,50 @@ static void
 refreshFromBuilder( MakeMetaUI * ui )
 {
     char                  sizeStr[128];
-    char                  buf[MAX_PATH_LENGTH];
+    char                * buf;
     tr_metainfo_builder * builder = ui->builder;
     const char *          filename = builder ? builder->top : NULL;
 
+
+    /* update the progressbar */
     if( !filename )
-        g_snprintf( buf, sizeof( buf ), _( "No source selected" ) );
+        buf = g_strdup( _( "No source selected" ) );
     else
-        g_snprintf( buf, sizeof( buf ), "%s.torrent (%d%%)", filename, 0 );
+        buf = g_strdup_printf( "%s.torrent (%d%%)", filename, 0 );
     gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar ), buf );
     refreshButtons( ui );
+    g_free( buf );
 
+    /* update the size label */
     if( !filename )
-        g_snprintf( buf, sizeof( buf ), _( "<i>No source selected</i>" ) );
-    else
-    {
+        buf = g_strdup( "<i>No source selected</i>" );
+    else {
         tr_strlsize( sizeStr, builder->totalSize, sizeof( sizeStr ) );
-        g_snprintf( buf, sizeof( buf ),
-                    /* %1$s is the torrent size
-                       %2$'d is its number of files */
-                    ngettext( "<i>%1$s; %2$'d File</i>",
-                              "<i>%1$s; %2$'d Files</i>",
-                              builder->fileCount ),
-                    sizeStr, builder->fileCount );
+        buf = g_strdup_printf( /* %1$s is the torrent size
+                                  %2$'d is its number of files */
+                               ngettext( "<i>%1$s; %2$'d File</i>",
+                                         "<i>%1$s; %2$'d Files</i>",
+                                         builder->fileCount ),
+                               sizeStr, builder->fileCount );
     }
     gtk_label_set_markup ( GTK_LABEL( ui->size_lb ), buf );
+    g_free( buf );
 
+    /* update the pieces label */
     if( !filename )
-        *buf = '\0';
-    else
-    {
-        char countStr[512];
-        g_snprintf( countStr, sizeof( countStr ),
-                    ngettext( "%'d Piece", "%'d Pieces",
-                              builder->pieceCount ),
-                    builder->pieceCount );
+        buf = g_strdup( "" );
+    else {
+        char * countStr = g_strdup_printf( ngettext( "%'d Piece", "%'d Pieces",
+                                                     builder->pieceCount ),
+                                           builder->pieceCount );
         tr_strlsize( sizeStr, builder->pieceSize, sizeof( sizeStr ) );
-        g_snprintf( buf, sizeof( buf ),
-                    /* %1$s is number of pieces;
-                       %2$s is how big each piece is */
-                    _( "%1$s @ %2$s" ),
-                    countStr,
-                    sizeStr );
+        buf = g_strdup_printf( /* %1$s is number of pieces;
+                                  %2$s is how big each piece is */
+                               _( "%1$s @ %2$s" ), countStr, sizeStr );
+        g_free( countStr );
     }
     gtk_label_set_markup ( GTK_LABEL( ui->pieces_lb ), buf );
+    g_free( buf );
 }
 
 static void
index 1259e0cebbdfbe5d69299a5c5f60d7b8a3e0dcdd..842d38cb6c8f76ece7d3aaaf20bc341b80a8b140 100644 (file)
@@ -18,7 +18,6 @@
  #include <windows.h>
 #endif
 
-#include <libgen.h> /* basename */
 #ifndef WIN32
  #include <sys/mman.h>
 #endif
@@ -105,12 +104,9 @@ blocklistLoad( tr_blocklist * b )
     b->fd = fd;
 
     {
-        char * name;
-        char   buf[MAX_PATH_LENGTH];
-        tr_strlcpy( buf, b->filename, sizeof( buf ) );
-        name = basename( buf );
-        tr_inf( _( "Blocklist \"%s\" contains %'u entries" ), name,
-                (unsigned int)b->ruleCount );
+        char * base = tr_basename( b->filename );
+        tr_inf( _( "Blocklist \"%s\" contains %'zu entries" ), base, b->ruleCount );
+        tr_free( base );
     }
 }
 
@@ -296,13 +292,9 @@ _tr_blocklistSetContent( tr_blocklist * b,
     }
 
     {
-        char * name;
-        char   buf[MAX_PATH_LENGTH];
-        tr_strlcpy( buf, b->filename, sizeof( buf ) );
-        name = basename( buf );
-        tr_inf( _(
-                    "Blocklist \"%1$s\" updated with %2$'d entries" ), name,
-                lineCount );
+        char * base = tr_basename( b->filename );
+        tr_inf( _( "Blocklist \"%1$s\" updated with %2$'d entries" ), base, lineCount );
+        tr_free( base );
     }
 
 
index 5d35eb42a4cb8607ab83e2956f64fe5b97988ce1..97f0c289e33e4aba3ec1b63869f618bf42f4e627 100644 (file)
@@ -40,7 +40,6 @@
  #include <sys/resource.h> /* getrlimit */
 #endif
 #include <unistd.h>
-#include <libgen.h> /* dirname */
 #include <fcntl.h> /* O_LARGEFILE */
 
 #include <event.h>
@@ -124,8 +123,8 @@ TrOpenFile( int          i,
     filename = tr_buildPath( folder, torrentFile, NULL );
     if( write )
     {
-        char *    tmp = tr_strdup( filename );
-        const int err = tr_mkdirp( dirname( tmp ), 0777 ) ? errno : 0;
+        char * tmp = tr_dirname( filename );
+        const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
         tr_free( tmp );
         tr_free( filename );
         if( err )
index f7afeafe91ab3ae4367f772222d8e4d3e8f1ae56..1b9efc82393db2e8b89a819b0119136a179ddc13 100644 (file)
@@ -19,7 +19,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
-#include <libgen.h> /* dirname, basename */
 #include <dirent.h>
 
 #include "crypto.h" /* tr_sha1 */
@@ -133,14 +132,11 @@ tr_metaInfoBuilderCreate( tr_handle *  handle,
     /* build a list of files containing topFile and,
        if it's a directory, all of its children */
     {
-        char *dir, *base;
-        char  dirbuf[MAX_PATH_LENGTH];
-        char  basebuf[MAX_PATH_LENGTH];
-        tr_strlcpy( dirbuf, topFile, sizeof( dirbuf ) );
-        tr_strlcpy( basebuf, topFile, sizeof( basebuf ) );
-        dir = dirname( dirbuf );
-        base = basename( basebuf );
+        char * dir = tr_dirname( topFile );
+        char * base = tr_basename( topFile );
         files = getFiles( dir, base, NULL );
+        tr_free( base );
+        tr_free( dir );
     }
 
     for( walk = files; walk != NULL; walk = walk->next )
@@ -333,7 +329,7 @@ makeInfoDict( tr_benc *             dict,
               tr_metainfo_builder * builder )
 {
     uint8_t * pch;
-    char      base[MAX_PATH_LENGTH];
+    char    * base;
 
     tr_bencDictReserve( dict, 5 );
 
@@ -353,8 +349,9 @@ makeInfoDict( tr_benc *             dict,
         }
     }
 
-    tr_strlcpy( base, builder->top, sizeof( base ) );
-    tr_bencDictAddStr( dict, "name", basename( base ) );
+    base = tr_basename( builder->top );
+    tr_bencDictAddStr( dict, "name", base );
+    tr_free( base );
 
     tr_bencDictAddInt( dict, "piece length", builder->pieceSize );
 
@@ -519,11 +516,7 @@ tr_makeMetaInfo( tr_metainfo_builder *   builder,
     if( outputFile && *outputFile )
         builder->outputFile = tr_strdup( outputFile );
     else
-    {
-        char out[MAX_PATH_LENGTH];
-        tr_snprintf( out, sizeof( out ), "%s.torrent", builder->top );
-        builder->outputFile = tr_strdup( out );
-    }
+        builder->outputFile = tr_strdup_printf( "%s.torrent", builder->top );
 
     /* enqueue the builder */
     lock = getQueueLock ( builder->handle );
index 7c6ced4566b75147a78daed65c3cc82aa6857249..ca29e5d228509f002508ca4e84a7a50c70936700 100644 (file)
@@ -16,7 +16,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <libgen.h> /* basename */
 
 #include <event.h>
 
@@ -327,7 +326,7 @@ myDebug( const char *               file,
         va_list           args;
         char              timestr[64];
         struct evbuffer * buf = evbuffer_new( );
-        char *            myfile = tr_strdup( file );
+        char *            base = tr_basename( file );
 
         evbuffer_add_printf( buf, "[%s] %s - %s [%s]: ",
                              tr_getLogTimeStr( timestr, sizeof( timestr ) ),
@@ -337,10 +336,10 @@ myDebug( const char *               file,
         va_start( args, fmt );
         evbuffer_add_vprintf( buf, fmt, args );
         va_end( args );
-        evbuffer_add_printf( buf, " (%s:%d)\n", basename( myfile ), line );
+        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
         fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
 
-        tr_free( myfile );
+        tr_free( base );
         evbuffer_free( buf );
     }
 }
index 82c5e960370734c0ec2513f943dcf84f7cd20839..71522f4362b444e4cd9b3a9711711eeb518e43cf 100644 (file)
  #define TR_PATH_DELIMITER_STR "/"
 #endif
 
+#ifdef __BEOS__
+ #include <StorageDefs.h>
+ #define MAX_PATH_LENGTH  B_FILE_NAME_LENGTH
+#else
+ #define MAX_PATH_LENGTH  1024
+#endif
+
 typedef struct tr_lock   tr_lock;
 typedef struct tr_thread tr_thread;
 
index fbb7e73aba83f1ea39a80c9439ab91a50e2b568b..d3e9da668fd06b566d7d94fa9b6cc067d3901981 100644 (file)
@@ -11,7 +11,6 @@
  */
 
 #include <errno.h>
-#include <libgen.h> /* basename */
 #include "transmission.h"
 #include "bencode.h"
 #include "platform.h"
@@ -121,9 +120,9 @@ tr_ctorSetMetainfoFromFile( tr_ctor *    ctor,
                     name = NULL;
             if( !name || !*name )
             {
-                char * tmp = tr_strdup( filename );
-                tr_bencDictAddStr( info, "name", basename( tmp ) );
-                tr_free( tmp );
+                char * base = tr_basename( filename );
+                tr_bencDictAddStr( info, "name", base );
+                tr_free( base );
             }
         }
     }
index 958b97ef743cffb5062211c2d0b6799524e79c92..5e4f5a71e2f34b03fc95c568b38346bbbc7cd3de 100644 (file)
@@ -49,13 +49,6 @@ extern "C" {
 
 #define SHA_DIGEST_LENGTH 20
 
-#ifdef __BEOS__
- #include <StorageDefs.h>
- #define MAX_PATH_LENGTH  B_FILE_NAME_LENGTH
-#else
- #define MAX_PATH_LENGTH  1024
-#endif
-
 typedef uint32_t tr_file_index_t;
 typedef uint32_t tr_piece_index_t;
 typedef uint64_t tr_block_index_t;
index a50e0a4c56659060f2d25314d07694b5d6dea7d3..c93e878290e2fc41932339f54190217621b31011 100644 (file)
@@ -195,7 +195,7 @@ tr_deepLog( const char * file,
         va_list           args;
         char              timestr[64];
         struct evbuffer * buf = evbuffer_new( );
-        char *            myfile = tr_strdup( file );
+        char *            base = tr_basename( file );
 
         evbuffer_add_printf( buf, "[%s] ",
                             tr_getLogTimeStr( timestr, sizeof( timestr ) ) );
@@ -204,10 +204,10 @@ tr_deepLog( const char * file,
         va_start( args, fmt );
         evbuffer_add_vprintf( buf, fmt, args );
         va_end( args );
-        evbuffer_add_printf( buf, " (%s:%d)\n", basename( myfile ), line );
+        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
         (void) fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
 
-        tr_free( myfile );
+        tr_free( base );
         evbuffer_free( buf );
     }
 }
@@ -456,14 +456,34 @@ tr_loadFile( const char * path,
 }
 
 char*
-tr_getcwd( char  * buffer,
-           int     maxlen )
+tr_getcwd( void )
 {
+    char buf[2048];
+    *buf = '\0';
 #ifdef WIN32
-    return _getcwd( buffer, maxlen );
+    _getcwd( buf, sizeof( buf ) );
 #else
-    return getcwd( buffer, maxlen );
+    getcwd( buf, sizeof( buf ) );
 #endif
+    return tr_strdup( buf );
+}
+
+char*
+tr_basename( const char * path )
+{
+    char * tmp = tr_strdup( path );
+    char * ret = tr_strdup( basename( tmp ) );
+    tr_free( tmp );
+    return ret;
+}
+
+char*
+tr_dirname( const char * path )
+{
+    char * tmp = tr_strdup( path );
+    char * ret = tr_strdup( dirname( tmp ) );
+    tr_free( tmp );
+    return ret;
 }
 
 int
@@ -524,10 +544,9 @@ tr_mkdirp( const char * path_in,
         else if( ( sb.st_mode & S_IFMT ) != S_IFDIR )
         {
             /* Node exists but isn't a folder */
-            char buf[MAX_PATH_LENGTH];
-            tr_snprintf( buf, sizeof( buf ), _(
-                             "File \"%s\" is in the way" ), path );
+            char * buf = tr_strdup_printf( _( "File \"%s\" is in the way" ), path );
             tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path_in, buf );
+            tr_free( buf );
             tr_free( path );
             errno = ENOTDIR;
             return -1;
index b0c695ddac05cc6ceff34903d60e22007fbcc6e0..ef51b820257e802a74eb94993214f982ac954ec4 100644 (file)
@@ -140,11 +140,14 @@ void           tr_deepLog( const char * file,
 char*          tr_getLogTimeStr( char * buf,
                                  int    buflen );
 
-/**
- * a portability wrapper around getcwd().
- */
-char*          tr_getcwd( char  * buffer,
-                          int     maxlen );
+/** a portability wrapper for getcwd(). */
+char*          tr_getcwd( void ) TR_GNUC_MALLOC;
+
+/** a portability wrapper for basename(). */
+char*          tr_basename( const char * path ) TR_GNUC_MALLOC;
+
+/** a portability wrapper for dirname(). */
+char*          tr_dirname( const char * path ) TR_GNUC_MALLOC;
 
 /**
  * a portability wrapper around mkdir().