Skip to content

Commit

Permalink
getenv from libc
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed Aug 11, 2024
1 parent 1f69760 commit 120df88
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 119 deletions.
5 changes: 5 additions & 0 deletions src/ccode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,8 @@ private extern int chown(char* path, int uid, int gid);
private extern int c_umask(int value);
private extern void exit(int status);

// value
private extern void save_env();
private extern void clear_env();
private extern void restore_env();

29 changes: 29 additions & 0 deletions src/ccode/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,32 @@ char** get_variable_names(int* len){
return ret;
}


extern char **environ;
void clear_env(){
char *path = strdup(getenv("PATH"));
char *env[1];
env[0] = NULL;
environ = env;
setenv("PATH", path, 1);
}

char** save_env(){
int len = 0;
while(environ[len]){
len++;
}
char **ret = malloc((len+1)*sizeof(char*));
len = 0;
while(environ[len]){
ret[len] = strdup(environ[len]);
len++;
}
ret[len] = NULL;
return ret;
}

void restore_env(char** envs){
environ = envs;
}

4 changes: 2 additions & 2 deletions src/data/code_runner.vala
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ public class code_runner {
print(colorize("Run operation:",blue)+job.name);
foreach(code_runner_plugin p in plugins) {
if(p.name == job.uses){
backup_env();
save_env();
clear_env();
foreach(string env in job.environs){
string var = ssplit(env,"=")[0];
string val = env[var.length+1:];
set_env(var, val);
setenv(var, val, 1);
}
print(colorize("Init plugin:",green)+p.name);
p.init(job.image, job.directory);
Expand Down
5 changes: 5 additions & 0 deletions src/interpreter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ private extern void no_stdin();
private extern void reset_std();


private class variable_integer {
public string name;
public int value;
}

public int ymp_run() {
variable_integer[] labels = {};
bool label_found = false;
Expand Down
4 changes: 2 additions & 2 deletions src/operations/package-manager/install.vala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
private static int install_main (string[] args) {
backup_env();
save_env();
clear_env();
file_cache_reset();
set_env("PATH","/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH","/sbin:/bin:/usr/sbin:/usr/bin", 1);

if (!is_root ()) {
error_add (_ ("You must be root!"));
Expand Down
8 changes: 4 additions & 4 deletions src/operations/package-manager/repository.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
private static int index_main (string[] args) {
backup_env();
save_env();
clear_env();
set_env("PATH","/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH","/sbin:/bin:/usr/sbin:/usr/bin", 1);
foreach (string arg in args) {
string path = srealpath (arg);
string index = create_index_data (path);
Expand All @@ -14,10 +14,10 @@ private static int index_main (string[] args) {
}

private static int update_main (string[] args) {
backup_env();
save_env();
clear_env();
file_cache_reset();
set_env("PATH","/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH","/sbin:/bin:/usr/sbin:/usr/bin", 1);
if (!is_root ()) {
error_add (_ ("You must be root!"));
error (1);
Expand Down
2 changes: 1 addition & 1 deletion src/operations/package-manager/template.vala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static int template_main (string[] args) {
}

private string get_gitconfig_variable (string variable) {
string gitconfig = srealpath (get_home () + "/.gitconfig");
string gitconfig = srealpath (getenv ("HOME") + "/.gitconfig");
if (isfile (gitconfig)) {
info (_ ("Reading gitconfig: %s").printf (gitconfig));
foreach (string line in readfile (gitconfig).split ("\n")) {
Expand Down
4 changes: 2 additions & 2 deletions src/operations/shell/chroot.vala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ private static int chroot_main (string[] args) {
cmd = "/bin/sh";
}
if(!get_bool("envs")){
backup_env();
save_env();
clear_env();
set_env("PATH", "/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
}
print_stderr (colorize (_ ("chroot: =>"), blue) + args[0]);
run_args_silent ( {"mount", "--bind", "/dev", args[0] + "/dev"});
Expand Down
2 changes: 1 addition & 1 deletion src/operations/shell/setget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static int cd_main (string[] args) {
}
private static int export_main (string[] args) {
foreach (string arg in args) {
set_env(arg,get_value (arg));
setenv(arg,get_value (arg), 1);
}
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/operations/utility/revdep-rebuild.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
private static int revdep_rebuild_main (string[] args) {
set_env ("LANG", "C");
set_env ("LC_ALL", "C");
setenv ("LANG", "C", 1);
setenv ("LC_ALL", "C", 1);
if (get_bool ("pkgconfig")) {
writefile ("/tmp/.empty.c", "");
foreach (string name in ssplit (getoutput ("pkg-config --list-package-names"), "\n")) {
Expand Down
4 changes: 2 additions & 2 deletions src/operations/utility/run-sandbox.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ private static int run_sandbox_main (string[] args) {
}
info (_ ("Execute sandbox :%s").printf (join (" ", args)));
if(!get_bool("envs")){
backup_env();
save_env();
clear_env();
set_env("PATH", "/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
}
int status = sandbox ("exec", args);
if(!get_bool("envs")){
Expand Down
6 changes: 3 additions & 3 deletions src/operations/utility/sysconf.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ private static int sysconf_main (string[] args) {
if (!is_root () || get_bool ("no-sysconf")) {
return 0;
}
backup_env();
save_env();
clear_env();
set_env("PATH","/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH","/sbin:/bin:/usr/sbin:/usr/bin", 1);

if (get_value ("OPERATION") != "install" && get_value ("OPERATION") != "remove") {
set_env ("OPERATION", get_value ("OPERATION"));
setenv ("OPERATION", get_value ("OPERATION"), 1);
}
if (DESTDIR != "/") {
run_args ( {"chroot", get_destdir (), "ldconfig"});
Expand Down
12 changes: 6 additions & 6 deletions src/settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ private static void settings_init () {
}else {
warning (_ ("Config file does not exists: %s").printf (CONFIG));
}
if (get_env ("USE") != null) {
set_value ("USE", get_env ("USE"));
if (getenv ("USE") != null) {
set_value ("USE", getenv ("USE"));
}else {
area = config_yaml.get_area (config_yaml.data, "ymp");
set_value ("USE", config_yaml.get_value (area, "use"));
}
#if DEBUG
if (get_bool ("debug")) {
set_env ("G_MESSAGES_DEBUG", "all");
set_env ("G_ENABLE_DIAGNOSTIC", "1");
set_env ("G_DEBUG", "fatal_warnings");
setenv ("G_MESSAGES_DEBUG", "all", 1);
setenv ("G_ENABLE_DIAGNOSTIC", "1", 1);
setenv ("G_DEBUG", "fatal_warnings", 1);
}
#endif
}
Expand Down Expand Up @@ -159,7 +159,7 @@ private static void parse_args (string[] args) {
}
}
}
if (get_env ("NO_COLOR") != null) {
if (getenv ("NO_COLOR") != null) {
set_bool ("no-color", true);
}
}
8 changes: 4 additions & 4 deletions src/util/build.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class builder {
public build_target build_target;
public string output_package_name;
public int build_single(string path) {
backup_env();
save_env();
clear_env();
set_env("PATH","/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH","/sbin:/bin:/usr/sbin:/usr/bin", 1);
bool unsafe = get_bool("unsafe");
string srcpath = srealpath(path);
string srcpkg = "";
Expand Down Expand Up @@ -197,9 +197,9 @@ public class builder {
warning(_("Dependency check disabled"));
return true;
}
backup_env();
save_env();
clear_env();
set_env("PATH","/sbin:/bin:/usr/sbin:/usr/bin");
setenv("PATH","/sbin:/bin:/usr/sbin:/usr/bin", 1);

string metadata = ymp_build.get_ympbuild_metadata();
var yaml = new yamlfile();
Expand Down
18 changes: 9 additions & 9 deletions src/util/code-runner.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ private static void code_runner_plugin_init(){
code_runner_plugin script = new code_runner_plugin();
script.name = plugin;
script.init.connect((image, directory)=>{
backup_env();
save_env();
clear_env();
set_env("IMAGE", image);
set_env("DIRECTORY", directory);
set_env("ACTION", "init");
setenv("IMAGE", image, 1);
setenv("DIRECTORY", directory, 1);
setenv("ACTION", "init", 1);
run("%s/%s".printf(plugin_directory ,plugin));
restore_env();

});
script.run.connect((command)=>{
backup_env();
save_env();
clear_env();
set_env("COMMAND", command);
set_env("ACTION", "run");
setenv("COMMAND", command, 1);
setenv("ACTION", "run", 1);
int status = run("%s/%s".printf(plugin_directory, plugin));
restore_env();
return status;
});
script.clean.connect(()=>{
backup_env();
save_env();
clear_env();
set_env("ACTION", "clean");
setenv("ACTION", "clean", 1);
run("%s/%s".printf(plugin_directory, plugin));
restore_env();
});
Expand Down
5 changes: 5 additions & 0 deletions src/util/file.vala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public string readfile (string path) {
return a.get_string();
}

private class variable {
public string name;
public string value;
}

private variable[] cached_files;

public string readfile_cached(string path){
Expand Down
81 changes: 0 additions & 81 deletions src/util/value.vala

This file was deleted.

2 changes: 2 additions & 0 deletions src/vapi/libc.vapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
private extern string getenv(string name);
private extern void setenv(string name, string value, int override);

0 comments on commit 120df88

Please sign in to comment.