source: roaraudio/libroar/env.c @ 3958:ec9754f367c0

Last change on this file since 3958:ec9754f367c0 was 3958:ec9754f367c0, checked in by phi, 14 years ago

added some functios for env handling and path resolution

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