source: roaraudio/libroar/env.c @ 3964:725e89d19b5f

Last change on this file since 3964:725e89d19b5f was 3964:725e89d19b5f, checked in by phi, 14 years ago

added support for roar_env_set()

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