source: roaraudio/libroar/env.c @ 3965:6369fe4ad739

Last change on this file since 3965:6369fe4ad739 was 3965:6369fe4ad739, checked in by phi, 14 years ago

corrected test macros a bit

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