1 | //env.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | int roar_env_set(struct roar_keyval * keyval) { |
---|
39 | #ifdef ROAR_HAVE_SETENV |
---|
40 | return setenv(keyval->key, keyval->value, 1); |
---|
41 | #elif defined(ROAR_HAVE_PUTENV) |
---|
42 | size_t len; |
---|
43 | char * str; |
---|
44 | |
---|
45 | len = strlen(keyval->key) + strlen(keyval->value) + 2; |
---|
46 | |
---|
47 | // TODO: does this leak memory? |
---|
48 | if ( (str = malloc(len)) == NULL ) { |
---|
49 | return -1; |
---|
50 | } |
---|
51 | |
---|
52 | snprintf(str, len, "%s=%s", keyval->key, keyval->value); |
---|
53 | |
---|
54 | return putenv(str) == 0 ? 0 : -1; |
---|
55 | #else |
---|
56 | return -1; |
---|
57 | #endif |
---|
58 | } |
---|
59 | |
---|
60 | const char * roar_env_get_home(int level) { |
---|
61 | const char * home = getenv("HOME"); |
---|
62 | |
---|
63 | (void)level; |
---|
64 | |
---|
65 | return home; |
---|
66 | } |
---|
67 | |
---|
68 | int roar_env_get_home_r(int level, char * str, size_t len) { |
---|
69 | size_t homelen; |
---|
70 | const char * home; |
---|
71 | |
---|
72 | if ( len == 0 ) |
---|
73 | return 0; |
---|
74 | |
---|
75 | if ( str == NULL ) |
---|
76 | return -1; |
---|
77 | |
---|
78 | *str = 0; // write a single termination byte to be safe... |
---|
79 | |
---|
80 | home = roar_env_get_home(level); |
---|
81 | |
---|
82 | if ( home == NULL ) |
---|
83 | return -1; |
---|
84 | |
---|
85 | homelen = strlen(home); |
---|
86 | |
---|
87 | if ( (len - 1) < homelen ) |
---|
88 | return -1; |
---|
89 | |
---|
90 | strncpy(str, home, len); |
---|
91 | str[homelen] = 0; |
---|
92 | |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | int roar_env_render_path_r(char * out, size_t len, const char * inpath) { |
---|
97 | size_t inlen; |
---|
98 | size_t needlen = 0; |
---|
99 | size_t homelen = 0; |
---|
100 | const char * home; |
---|
101 | enum { UNKNOWN, COPY, HOME } mode = UNKNOWN; |
---|
102 | |
---|
103 | if ( len == 0 && inpath == NULL ) |
---|
104 | return 0; |
---|
105 | |
---|
106 | if ( inpath == NULL ) |
---|
107 | return -1; |
---|
108 | |
---|
109 | inlen = strlen(inpath); |
---|
110 | |
---|
111 | if ( len == 0 && inlen == 0 ) |
---|
112 | return 0; |
---|
113 | |
---|
114 | if ( len == 0 ) |
---|
115 | return -1; |
---|
116 | |
---|
117 | if ( out == NULL ) |
---|
118 | return -1; |
---|
119 | |
---|
120 | home = roar_env_get_home(0); |
---|
121 | |
---|
122 | if ( home != NULL ) |
---|
123 | homelen = strlen(home); |
---|
124 | |
---|
125 | if ( inpath[0] == '/' ) { |
---|
126 | needlen = inlen; |
---|
127 | mode = COPY; |
---|
128 | } else if ( inpath[0] == '~' && inpath[1] == '/' ) { |
---|
129 | if ( homelen == 0 ) /* we don't know our $HOME */ |
---|
130 | return -1; |
---|
131 | |
---|
132 | needlen = inlen + homelen - 1; |
---|
133 | mode = HOME; |
---|
134 | } |
---|
135 | |
---|
136 | if ( len < (needlen + 1) ) |
---|
137 | return -1; |
---|
138 | |
---|
139 | switch (mode) { |
---|
140 | case COPY: |
---|
141 | strncpy(out, inpath, len); |
---|
142 | out[len-1] = 0; |
---|
143 | break; |
---|
144 | case HOME: |
---|
145 | roar_mm_strlcpy(out, home, len); |
---|
146 | roar_mm_strlcat(out, inpath+1, len); // strip only ~, so we have the / if home is not /-terminated. |
---|
147 | break; |
---|
148 | default: |
---|
149 | return -1; |
---|
150 | break; |
---|
151 | } |
---|
152 | |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | //ll |
---|