add cmd_env_unset to remove environment variables

This commit is contained in:
slonkazoid 2024-09-12 15:04:14 +03:00
parent 8ca45f2538
commit ca53b3e4eb
No known key found for this signature in database
5 changed files with 41 additions and 5 deletions

View file

@ -19,4 +19,10 @@ char **env_create();
*/ */
char **env_setenv(char **envp, char *name, char *value); char **env_setenv(char **envp, char *name, char *value);
/**
* Unsets an environment variable in the given environment.
* If successful, this will reallocate the entire array.
*/
char **env_unsetenv(char **envp, char *name);
#endif #endif

View file

@ -126,6 +126,7 @@ sway_cmd cmd_default_border;
sway_cmd cmd_default_floating_border; sway_cmd cmd_default_floating_border;
sway_cmd cmd_default_orientation; sway_cmd cmd_default_orientation;
sway_cmd cmd_env; sway_cmd cmd_env;
sway_cmd cmd_env_unset;
sway_cmd cmd_exec; sway_cmd cmd_exec;
sway_cmd cmd_exec_always; sway_cmd cmd_exec_always;
sway_cmd cmd_exit; sway_cmd cmd_exit;

View file

@ -58,6 +58,7 @@ static const struct cmd_handler handlers[] = {
{ "default_border", cmd_default_border }, { "default_border", cmd_default_border },
{ "default_floating_border", cmd_default_floating_border }, { "default_floating_border", cmd_default_floating_border },
{ "env", cmd_env }, { "env", cmd_env },
{ "env_unset", cmd_env_unset },
{ "exec", cmd_exec }, { "exec", cmd_exec },
{ "exec_always", cmd_exec_always }, { "exec_always", cmd_exec_always },
{ "floating_maximum_size", cmd_floating_maximum_size }, { "floating_maximum_size", cmd_floating_maximum_size },

View file

@ -15,3 +15,14 @@ struct cmd_results *cmd_env(int argc, char **argv) {
return cmd_results_new(CMD_SUCCESS, NULL); return cmd_results_new(CMD_SUCCESS, NULL);
} }
struct cmd_results *cmd_env_unset(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "env_unset", EXPECTED_EQUAL_TO, 1))) {
return error;
}
child_envp = env_unsetenv(child_envp, argv[0]);
return cmd_results_new(CMD_SUCCESS, NULL);
}

View file

@ -51,18 +51,25 @@ size_t env_len(char **envp) {
return i; return i;
} }
char **env_clone(char **envp, size_t reserve) { char **env_clone(char **envp, size_t reserve, env_info exclude) {
char **new_envp = calloc(env_len(envp) + 1 + reserve, sizeof(char *)); size_t elem_count = env_len(envp) + 1 + reserve - (exclude.ptr != NULL);
char **new_envp = calloc(elem_count, sizeof(char *));
char *strp; char *strp;
size_t i = 0; size_t i = 0;
size_t new_i = 0;
while ((strp = envp[i]) != NULL) { while ((strp = envp[i]) != NULL) {
if (exclude.ptr == strp) {
i++;
continue;
}
size_t n = strlen(strp) + 1; size_t n = strlen(strp) + 1;
char *new_strp = malloc(n); char *new_strp = malloc(n);
memcpy(new_strp, strp, n); memcpy(new_strp, strp, n);
new_envp[i] = new_strp; new_envp[new_i] = new_strp;
i++; i++;
new_i++;
} }
return new_envp; return new_envp;
@ -81,7 +88,7 @@ void env_destroy(char **envp) {
// copy the global environment array into a newly-allocated one // copy the global environment array into a newly-allocated one
// you are responsible for deallocating it after use // you are responsible for deallocating it after use
char **env_create() { return env_clone(environ, 0); } char **env_create() { return env_clone(environ, 0, (env_info){NULL, 0}); }
// use env_get_envp() to acquire an envp // use env_get_envp() to acquire an envp
// might clone and deallocate the given envp // might clone and deallocate the given envp
@ -100,9 +107,19 @@ char **env_setenv(char **envp, char *name, char *value) {
envp[existing.idx] = newp; envp[existing.idx] = newp;
return envp; return envp;
} else { } else {
char **new_envp = env_clone(envp, 1); char **new_envp = env_clone(envp, 1, (env_info){NULL, 0});
new_envp[env_len(envp)] = newp; new_envp[env_len(envp)] = newp;
env_destroy(envp); env_destroy(envp);
return new_envp; return new_envp;
} }
} }
char **env_unsetenv(char **envp, char *name) {
env_info existing = env_get(envp, name);
if (existing.ptr == NULL) // dont do anything if
return envp; // the variable is not set
char **new_envp = env_clone(envp, 0, existing);
env_destroy(envp);
return new_envp;
}