| 1 |
/* |
| 2 |
Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
Written by: Gregory John Casamento <[email protected]> |
| 5 |
Date: 2022 |
| 6 |
|
| 7 |
This file is part of the GNUstep XCode Library |
| 8 |
|
| 9 |
This library is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 2 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
This library 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 GNU |
| 17 |
Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with this library; if not, write to the Free |
| 21 |
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 |
Boston, MA 02110 USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
#import "setenv.h" |
| 26 |
#import "GSXCBuildContext.h" |
| 27 |
|
| 28 |
int setenv(const char *key, const char *value, int overwrite) |
| 29 |
{ |
| 30 |
GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext]; |
| 31 |
int result = 255; // failure... |
| 32 |
char *current = getenv(key); |
| 33 |
|
| 34 |
if(overwrite && current) |
| 35 |
{ |
| 36 |
int key_len = strlen(key); |
| 37 |
int val_len = strlen(value); |
| 38 |
int len = key_len + 1 + val_len + 1; // key + "=" + value + '\0' |
| 39 |
char *buffer = malloc(len); |
| 40 |
|
| 41 |
memset(buffer, 0, len); |
| 42 |
sprintf(buffer,"%s=%s",key,value); |
| 43 |
result = putenv(buffer); |
| 44 |
|
| 45 |
[ctx setObject: [NSString stringWithCString: value] |
| 46 |
forKey: [NSString stringWithCString: key]]; |
| 47 |
|
| 48 |
free(buffer); |
| 49 |
} |
| 50 |
|
| 51 |
return result; |
| 52 |
} |
| 53 |
/* |
| 54 |
char *getenv(const char *key) |
| 55 |
{ |
| 56 |
GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext]; |
| 57 |
char *result = [[ctx objectForKey: [NSString stringWithCString: key]] cString]; |
| 58 |
return result; |
| 59 |
} |
| 60 |
*/ |