source: roaraudio/libroar/env.c @ 5810:d44ff4605fa5

Last change on this file since 5810:d44ff4605fa5 was 5754:b23d79c13370, checked in by phi, 11 years ago

avoid getenv() and use more portable roar_env_get(). Also improved security as we enfore const now on bufferes obtained from the env.

File size: 4.0 KB
Line 
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
38int roar_env_set(struct roar_keyval * keyval) {
39#if defined(ROAR_HAVE_PUTENV) && !defined(ROAR_HAVE_SETENV)
40 size_t len;
41 char * str;
42#endif
43
44 if ( keyval == NULL || keyval->key == NULL ) {
45  roar_err_set(ROAR_ERROR_FAULT);
46  return -1;
47 }
48
49#ifdef ROAR_HAVE_SETENV
50 return setenv(keyval->key, keyval->value, 1);
51#elif defined(ROAR_HAVE_PUTENV)
52 len = strlen(keyval->key) + strlen(keyval->value) + 2;
53
54 // TODO: does this leak memory?
55 if ( (str = malloc(len)) == NULL ) {
56  return -1;
57 }
58
59 snprintf(str, len, "%s=%s", keyval->key, keyval->value);
60
61 return putenv(str) == 0 ? 0 : -1;
62#else
63 roar_strap(ROAR_TRAP_GROUP_LIBROAR, "env_set.not-implemented");
64 roar_err_set(ROAR_ERROR_NOSYS);
65 return -1;
66#endif
67}
68
69const char * roar_env_get(const char * key) {
70 const char * ret;
71
72 if ( key == NULL ) {
73  roar_err_set(ROAR_ERROR_FAULT);
74  return NULL;
75 }
76
77 roar_err_clear_all();
78 ret = getenv(key);
79 roar_err_update();
80
81 if ( ret == NULL && roar_error == ROAR_ERROR_NONE )
82  roar_err_set(ROAR_ERROR_NOENT);
83
84 return ret;
85}
86
87const char * roar_env_get_home(int level) {
88 const char * home = roar_env_get("HOME");
89
90 (void)level;
91
92 return home;
93}
94
95int roar_env_get_home_r(int level, char * str, size_t len) {
96 size_t homelen;
97 const char * home;
98
99 if ( len == 0 )
100  return 0;
101
102 if ( str == NULL )
103  return -1;
104
105 *str = 0; // write a single termination byte to be safe...
106
107 home = roar_env_get_home(level);
108
109 if ( home == NULL )
110  return -1;
111
112 homelen = strlen(home);
113
114 if ( (len - 1) < homelen )
115  return -1;
116
117 strncpy(str, home, len);
118 str[homelen] = 0;
119
120 return 0;
121}
122
123int roar_env_render_path_r(char * out, size_t len, const char * inpath) {
124 size_t inlen;
125 size_t needlen = 0;
126 size_t homelen = 0;
127 const char * home;
128 enum { UNKNOWN, COPY, HOME } mode = UNKNOWN;
129
130 if ( len == 0 && inpath == NULL )
131  return 0;
132
133 if ( inpath == NULL )
134  return -1;
135
136 inlen = strlen(inpath);
137
138 if ( len == 0 && inlen == 0 )
139  return 0;
140
141 if ( len == 0 )
142  return -1;
143
144 if ( out == NULL )
145  return -1;
146
147 home = roar_env_get_home(0);
148
149 if ( home != NULL )
150  homelen = strlen(home);
151
152 if ( inpath[0] == '/' ) {
153  needlen = inlen;
154  mode    = COPY;
155 } else if ( inpath[0] == '~' && inpath[1] == '/' ) {
156  if ( homelen == 0 ) /* we don't know our $HOME */
157   return -1;
158
159  needlen = inlen + homelen - 1;
160  mode    = HOME;
161 }
162
163 if ( len < (needlen + 1) )
164  return -1;
165
166 switch (mode) {
167  case COPY:
168    strncpy(out, inpath, len);
169    out[len-1] = 0;
170   break;
171  case HOME:
172    roar_mm_strlcpy(out, home, len);
173    roar_mm_strlcat(out, inpath+1, len); // strip only ~, so we have the / if home is not /-terminated.
174   break;
175  default:
176    return -1;
177   break;
178 }
179
180 return 0;
181}
182
183//ll
Note: See TracBrowser for help on using the repository browser.