2015-12-22 12:55:44 +01:00
|
|
|
/*
|
2017-01-19 00:58:43 +01:00
|
|
|
* This file is part of Jehanne.
|
2016-11-25 17:18:40 +01:00
|
|
|
*
|
2017-01-19 00:58:43 +01:00
|
|
|
* Copyright (C) 2016-2017 Giacomo Tesio <giacomo@tesio.it>
|
|
|
|
*
|
|
|
|
* Jehanne is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2 of the License.
|
|
|
|
*
|
|
|
|
* Jehanne is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
|
2015-12-22 12:55:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
|
|
|
|
int
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_putenv(const char *name, const char *value)
|
2015-12-22 12:55:44 +01:00
|
|
|
{
|
|
|
|
int f;
|
2017-01-19 00:58:43 +01:00
|
|
|
int32_t l;
|
|
|
|
char path[127+5+1];
|
2015-12-22 12:55:44 +01:00
|
|
|
|
2017-01-19 00:58:43 +01:00
|
|
|
assert(name != nil);
|
|
|
|
assert(value != nil);
|
|
|
|
if(name[0]=='\0')
|
|
|
|
goto BadName;
|
2017-04-19 23:33:14 +02:00
|
|
|
if(jehanne_strcmp(name, ".")==0 || jehanne_strcmp(name, "..")==0)
|
2017-01-19 00:58:43 +01:00
|
|
|
goto BadName;
|
2017-04-19 23:33:14 +02:00
|
|
|
if(jehanne_strchr(name, '/')!=nil)
|
2017-01-19 00:58:43 +01:00
|
|
|
goto BadName;
|
|
|
|
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_snprint(path, sizeof path, "/env/%s", name);
|
|
|
|
if(jehanne_strcmp(path+5, name) != 0)
|
2017-01-19 00:58:43 +01:00
|
|
|
goto BadName;
|
|
|
|
|
2017-04-19 23:33:14 +02:00
|
|
|
f = jehanne_ocreate(path, OWRITE, 0664);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(f < 0){
|
2019-11-26 02:25:23 +01:00
|
|
|
/* try with #e, in case of a previous sys_rfork(RFCNAMEG)
|
2016-11-25 17:18:40 +01:00
|
|
|
*/
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_snprint(path, sizeof path, "#e/%s", name);
|
|
|
|
f = jehanne_ocreate(path, OWRITE, 0664);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(f < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-19 23:33:14 +02:00
|
|
|
l = jehanne_strlen(value);
|
2019-11-26 02:25:23 +01:00
|
|
|
if(l > 0 && jehanne_write(f, value, l) != l){
|
|
|
|
sys_close(f);
|
2015-12-22 12:55:44 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2019-11-26 02:25:23 +01:00
|
|
|
sys_close(f);
|
2015-12-22 12:55:44 +01:00
|
|
|
return 0;
|
2017-01-19 00:58:43 +01:00
|
|
|
|
|
|
|
BadName:
|
2017-04-19 23:33:14 +02:00
|
|
|
jehanne_werrstr("bad env name: '%s'", name);
|
2017-01-19 00:58:43 +01:00
|
|
|
return -1;
|
2015-12-22 12:55:44 +01:00
|
|
|
}
|