source: roaraudio/libroar/env.c @ 3991:41dd190c83cc

Last change on this file since 3991:41dd190c83cc was 3991:41dd190c83cc, checked in by phi, 14 years ago

dekl. missing var

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