1 | /****************************************************************************** |
---|
2 | * $Id: misc.c 1617 2007-03-31 20:00:40Z joshe $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007 Joshua Elsasser |
---|
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 <sys/types.h> |
---|
26 | #include <assert.h> |
---|
27 | #include <string.h> |
---|
28 | #include <unistd.h> |
---|
29 | |
---|
30 | #include "errors.h" |
---|
31 | #include "misc.h" |
---|
32 | #include "transmission.h" |
---|
33 | |
---|
34 | static void pushdir ( char *, const char *, size_t ); |
---|
35 | |
---|
36 | static char gl_myname[256]; |
---|
37 | |
---|
38 | void |
---|
39 | setmyname( const char * argv0 ) |
---|
40 | { |
---|
41 | const char * name; |
---|
42 | |
---|
43 | name = strrchr( argv0, '/' ); |
---|
44 | if( NULL == name || '\0' == *(++name) ) |
---|
45 | { |
---|
46 | name = argv0; |
---|
47 | } |
---|
48 | strlcpy( gl_myname, name, sizeof gl_myname ); |
---|
49 | } |
---|
50 | |
---|
51 | const char * |
---|
52 | getmyname( void ) |
---|
53 | { |
---|
54 | return gl_myname; |
---|
55 | } |
---|
56 | |
---|
57 | void |
---|
58 | pushdir( char * path, const char * file, size_t size ) |
---|
59 | { |
---|
60 | size_t off; |
---|
61 | |
---|
62 | off = strlen( path ); |
---|
63 | if( 0 < off && off + 1 < size && '/' != path[off-1] ) |
---|
64 | { |
---|
65 | path[off] = '/'; |
---|
66 | path[off+1] = '\0'; |
---|
67 | } |
---|
68 | strlcat( path, file, size ); |
---|
69 | } |
---|
70 | |
---|
71 | void |
---|
72 | confpath( char * buf, size_t len, const char * file, enum confpathtype type ) |
---|
73 | { |
---|
74 | const char * typestr; |
---|
75 | |
---|
76 | switch( type ) |
---|
77 | { |
---|
78 | case CONF_PATH_TYPE_DAEMON: |
---|
79 | typestr = "daemon"; |
---|
80 | break; |
---|
81 | case CONF_PATH_TYPE_GTK: |
---|
82 | typestr = "gtk"; |
---|
83 | break; |
---|
84 | default: |
---|
85 | assert( 0 ); |
---|
86 | break; |
---|
87 | } |
---|
88 | |
---|
89 | strlcpy( buf, tr_getPrefsDirectory(), len ); |
---|
90 | pushdir( buf, typestr, len ); |
---|
91 | if( NULL != file ) |
---|
92 | { |
---|
93 | pushdir( buf, file, len ); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | void |
---|
98 | absolutify( char * buf, size_t len, const char * path ) |
---|
99 | { |
---|
100 | size_t off; |
---|
101 | |
---|
102 | if( '/' == path[0] ) |
---|
103 | { |
---|
104 | strlcpy( buf, path, len ); |
---|
105 | return; |
---|
106 | } |
---|
107 | |
---|
108 | getcwd( buf, len ); |
---|
109 | off = strlen( buf ); |
---|
110 | if( 0 < off && len > off + 1 && '/' != buf[off] ) |
---|
111 | { |
---|
112 | buf[off] = '/'; |
---|
113 | off++; |
---|
114 | buf[off] = '\0'; |
---|
115 | } |
---|
116 | strlcat( buf, path, len ); |
---|
117 | } |
---|