[3958] | 1 | //env.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
[3958] | 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 | |
---|
[3964] | 38 | int roar_env_set(struct roar_keyval * keyval) { |
---|
| 39 | #ifdef ROAR_HAVE_SETENV |
---|
| 40 | return setenv(keyval->key, keyval->value, 1); |
---|
[3965] | 41 | #elif defined(ROAR_HAVE_PUTENV) |
---|
[4917] | 42 | size_t len; |
---|
[3991] | 43 | char * str; |
---|
| 44 | |
---|
[4937] | 45 | len = strlen(keyval->key) + strlen(keyval->value) + 2; |
---|
[4917] | 46 | |
---|
[3964] | 47 | // TODO: does this leak memory? |
---|
[4917] | 48 | if ( (str = malloc(len)) == NULL ) { |
---|
[3964] | 49 | return -1; |
---|
| 50 | } |
---|
| 51 | |
---|
[4917] | 52 | snprintf(str, len, "%s=%s", keyval->key, keyval->value); |
---|
[3964] | 53 | |
---|
| 54 | return putenv(str) == 0 ? 0 : -1; |
---|
[3965] | 55 | #else |
---|
| 56 | return -1; |
---|
[3964] | 57 | #endif |
---|
| 58 | } |
---|
[3958] | 59 | |
---|
| 60 | const char * roar_env_get_home(int level) { |
---|
| 61 | const char * home = getenv("HOME"); |
---|
| 62 | |
---|
| 63 | return home; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | int 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 | |
---|
| 94 | int 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: |
---|
[5008] | 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. |
---|
[3958] | 145 | break; |
---|
| 146 | default: |
---|
| 147 | return -1; |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return 0; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | //ll |
---|