source: roaraudio/libroar/env.c @ 5008:f66a2be5b974

Last change on this file since 5008:f66a2be5b974 was 5008:f66a2be5b974, checked in by phi, 13 years ago

use roar_mm_str[ls]{cat,cpy}()

File size: 3.4 KB
Line 
1//env.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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#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
60const char * roar_env_get_home(int level) {
61 const char * home = getenv("HOME");
62
63 return home;
64}
65
66int roar_env_get_home_r(int level, char * str, size_t len) {
67 size_t homelen;
68 const char * home;
69
70 if ( len == 0 )
71  return 0;
72
73 if ( str == NULL )
74  return -1;
75
76 *str = 0; // write a single termination byte to be safe...
77
78 home = roar_env_get_home(level);
79
80 if ( home == NULL )
81  return -1;
82
83 homelen = strlen(home);
84
85 if ( (len - 1) < homelen )
86  return -1;
87
88 strncpy(str, home, len);
89 str[homelen] = 0;
90
91 return 0;
92}
93
94int roar_env_render_path_r(char * out, size_t len, const char * inpath) {
95 size_t inlen;
96 size_t needlen = 0;
97 size_t homelen = 0;
98 const char * home;
99 enum { UNKNOWN, COPY, HOME } mode = UNKNOWN;
100
101 if ( len == 0 && inpath == NULL )
102  return 0;
103
104 if ( inpath == NULL )
105  return -1;
106
107 inlen = strlen(inpath);
108
109 if ( len == 0 && inlen == 0 )
110  return 0;
111
112 if ( len == 0 )
113  return -1;
114
115 if ( out == NULL )
116  return -1;
117
118 home = roar_env_get_home(0);
119
120 if ( home != NULL )
121  homelen = strlen(home);
122
123 if ( inpath[0] == '/' ) {
124  needlen = inlen;
125  mode    = COPY;
126 } else if ( inpath[0] == '~' && inpath[1] == '/' ) {
127  if ( homelen == 0 ) /* we don't know our $HOME */
128   return -1;
129
130  needlen = inlen + homelen - 1;
131  mode    = HOME;
132 }
133
134 if ( len < (needlen + 1) )
135  return -1;
136
137 switch (mode) {
138  case COPY:
139    strncpy(out, inpath, len);
140    out[len-1] = 0;
141   break;
142  case HOME:
143    roar_mm_strlcpy(out, home, len);
144    roar_mm_strlcat(out, inpath+1, len); // strip only ~, so we have the / if home is not /-terminated.
145   break;
146  default:
147    return -1;
148   break;
149 }
150
151 return 0;
152}
153
154//ll
Note: See TracBrowser for help on using the repository browser.