Replace concat3 with asprintf in set_env_prefix and set_env_suffix.

This commit is contained in:
Tobias Bergkvist 2021-12-01 23:27:26 +01:00
parent 3997e9de67
commit a1e62262bc
4 changed files with 71 additions and 94 deletions

View File

@ -64,7 +64,7 @@ makeDocumentedCWrapper() {
# ARGS: same as makeBinaryWrapper # ARGS: same as makeBinaryWrapper
makeCWrapper() { makeCWrapper() {
local argv0 n params cmd main flagsBefore flags executable params length local argv0 n params cmd main flagsBefore flags executable params length
local uses_prefix uses_suffix uses_assert uses_assert_success uses_concat3 local uses_prefix uses_suffix uses_assert uses_assert_success uses_stdio uses_asprintf
executable=$(escapeStringLiteral "$1") executable=$(escapeStringLiteral "$1")
params=("$@") params=("$@")
length=${#params[*]} length=${#params[*]}
@ -80,6 +80,7 @@ makeCWrapper() {
--set-default) --set-default)
cmd=$(setDefaultEnv "${params[n + 1]}" "${params[n + 2]}") cmd=$(setDefaultEnv "${params[n + 1]}" "${params[n + 2]}")
main="$main $cmd"$'\n' main="$main $cmd"$'\n'
uses_stdio=1
uses_assert_success=1 uses_assert_success=1
n=$((n + 2)) n=$((n + 2))
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 2 arguments"$'\n' [ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 2 arguments"$'\n'
@ -87,6 +88,7 @@ makeCWrapper() {
--unset) --unset)
cmd=$(unsetEnv "${params[n + 1]}") cmd=$(unsetEnv "${params[n + 1]}")
main="$main $cmd"$'\n' main="$main $cmd"$'\n'
uses_stdio=1
uses_assert_success=1 uses_assert_success=1
n=$((n + 1)) n=$((n + 1))
[ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 1 argument"$'\n' [ $n -ge "$length" ] && main="$main #error makeCWrapper: $p takes 1 argument"$'\n'
@ -95,7 +97,8 @@ makeCWrapper() {
cmd=$(setEnvPrefix "${params[n + 1]}" "${params[n + 2]}" "${params[n + 3]}") cmd=$(setEnvPrefix "${params[n + 1]}" "${params[n + 2]}" "${params[n + 3]}")
main="$main $cmd"$'\n' main="$main $cmd"$'\n'
uses_prefix=1 uses_prefix=1
uses_concat3=1 uses_asprintf=1
uses_stdio=1
uses_assert_success=1 uses_assert_success=1
uses_assert=1 uses_assert=1
n=$((n + 3)) n=$((n + 3))
@ -105,7 +108,8 @@ makeCWrapper() {
cmd=$(setEnvSuffix "${params[n + 1]}" "${params[n + 2]}" "${params[n + 3]}") cmd=$(setEnvSuffix "${params[n + 1]}" "${params[n + 2]}" "${params[n + 3]}")
main="$main $cmd"$'\n' main="$main $cmd"$'\n'
uses_suffix=1 uses_suffix=1
uses_concat3=1 uses_asprintf=1
uses_stdio=1
uses_assert_success=1 uses_assert_success=1
uses_assert=1 uses_assert=1
n=$((n + 3)) n=$((n + 3))
@ -133,15 +137,14 @@ makeCWrapper() {
main="$main argv[0] = \"${argv0:-${executable}}\";"$'\n' main="$main argv[0] = \"${argv0:-${executable}}\";"$'\n'
main="$main return execv(\"${executable}\", argv);"$'\n' main="$main return execv(\"${executable}\", argv);"$'\n'
[ -z "$uses_asprintf" ] || printf '%s\n' "#define _GNU_SOURCE /* See feature_test_macros(7) */"
printf '%s\n' "#include <unistd.h>" printf '%s\n' "#include <unistd.h>"
printf '%s\n' "#include <stdlib.h>" printf '%s\n' "#include <stdlib.h>"
[ -z "$uses_concat3" ] || printf '%s\n' "#include <string.h>" [ -z "$uses_assert" ] || printf '%s\n' "#include <assert.h>"
[ -z "$uses_assert" ] || printf '%s\n' "#include <assert.h>" [ -z "$uses_stdio" ] || printf '%s\n' "#include <stdio.h>"
[ -z "$uses_assert_success" ] || printf '%s\n' "#include <stdio.h>"
[ -z "$uses_assert_success" ] || printf '\n%s\n' "#define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0)" [ -z "$uses_assert_success" ] || printf '\n%s\n' "#define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0)"
[ -z "$uses_concat3" ] || printf '\n%s\n' "$(concat3Fn)" [ -z "$uses_prefix" ] || printf '\n%s\n' "$(setEnvPrefixFn)"
[ -z "$uses_prefix" ] || printf '\n%s\n' "$(setEnvPrefixFn)" [ -z "$uses_suffix" ] || printf '\n%s\n' "$(setEnvSuffixFn)"
[ -z "$uses_suffix" ] || printf '\n%s\n' "$(setEnvSuffixFn)"
printf '\n%s' "int main(int argc, char **argv) {" printf '\n%s' "int main(int argc, char **argv) {"
printf '\n%s' "$main" printf '\n%s' "$main"
printf '%s\n' "}" printf '%s\n' "}"
@ -238,41 +241,34 @@ assertValidEnvName() {
esac esac
} }
concat3Fn() {
printf '%s' "\
char *concat3(char *x, char *y, char *z) {
int xn = strlen(x);
int yn = strlen(y);
int zn = strlen(z);
char *res = malloc(sizeof(*res)*(xn + yn + zn + 1));
assert(res != NULL);
strncpy(res, x, xn);
strncpy(res + xn, y, yn);
strncpy(res + xn + yn, z, zn);
res[xn + yn + zn] = '\0';
return res;
}
"
}
setEnvPrefixFn() { setEnvPrefixFn() {
printf '%s' "\ printf '%s' "\
void set_env_prefix(char *env, char *sep, char *val) { void set_env_prefix(char *env, char *sep, char *prefix) {
char *existing = getenv(env); char *existing = getenv(env);
if (existing) val = concat3(val, sep, existing); if (existing) {
assert_success(setenv(env, val, 1)); char *val;
if (existing) free(val); assert_success(asprintf(&val, \"%s%s%s\", prefix, sep, existing));
assert_success(setenv(env, val, 1));
free(val);
} else {
assert_success(setenv(env, prefix, 1));
}
} }
" "
} }
setEnvSuffixFn() { setEnvSuffixFn() {
printf '%s' "\ printf '%s' "\
void set_env_suffix(char *env, char *sep, char *val) { void set_env_suffix(char *env, char *sep, char *suffix) {
char *existing = getenv(env); char *existing = getenv(env);
if (existing) val = concat3(existing, sep, val); if (existing) {
assert_success(setenv(env, val, 1)); char *val;
if (existing) free(val); assert_success(asprintf(&val, \"%s%s%s\", existing, sep, suffix));
assert_success(setenv(env, val, 1));
free(val);
} else {
assert_success(setenv(env, suffix, 1));
}
} }
" "
} }

View File

@ -6,39 +6,36 @@
--add-flags "-x -y -z" \ --add-flags "-x -y -z" \
--set MESSAGE2 WORLD --set MESSAGE2 WORLD
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0) #define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0)
char *concat3(char *x, char *y, char *z) { void set_env_prefix(char *env, char *sep, char *prefix) {
int xn = strlen(x); char *existing = getenv(env);
int yn = strlen(y); if (existing) {
int zn = strlen(z); char *val;
char *res = malloc(sizeof(*res)*(xn + yn + zn + 1)); assert_success(asprintf(&val, "%s%s%s", prefix, sep, existing));
assert(res != NULL); assert_success(setenv(env, val, 1));
strncpy(res, x, xn); free(val);
strncpy(res + xn, y, yn); } else {
strncpy(res + xn + yn, z, zn); assert_success(setenv(env, prefix, 1));
res[xn + yn + zn] = '\0'; }
return res;
} }
void set_env_prefix(char *env, char *sep, char *val) { void set_env_suffix(char *env, char *sep, char *suffix) {
char *existing = getenv(env); char *existing = getenv(env);
if (existing) val = concat3(val, sep, existing); if (existing) {
assert_success(setenv(env, val, 1)); char *val;
if (existing) free(val); assert_success(asprintf(&val, "%s%s%s", existing, sep, suffix));
} assert_success(setenv(env, val, 1));
free(val);
void set_env_suffix(char *env, char *sep, char *val) { } else {
char *existing = getenv(env); assert_success(setenv(env, suffix, 1));
if (existing) val = concat3(existing, sep, val); }
assert_success(setenv(env, val, 1));
if (existing) free(val);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {

View File

@ -2,32 +2,24 @@
--prefix PATH : /usr/bin/ \ --prefix PATH : /usr/bin/ \
--prefix PATH : /usr/local/bin/ --prefix PATH : /usr/local/bin/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0) #define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0)
char *concat3(char *x, char *y, char *z) { void set_env_prefix(char *env, char *sep, char *prefix) {
int xn = strlen(x);
int yn = strlen(y);
int zn = strlen(z);
char *res = malloc(sizeof(*res)*(xn + yn + zn + 1));
assert(res != NULL);
strncpy(res, x, xn);
strncpy(res + xn, y, yn);
strncpy(res + xn + yn, z, zn);
res[xn + yn + zn] = '\0';
return res;
}
void set_env_prefix(char *env, char *sep, char *val) {
char *existing = getenv(env); char *existing = getenv(env);
if (existing) val = concat3(val, sep, existing); if (existing) {
assert_success(setenv(env, val, 1)); char *val;
if (existing) free(val); assert_success(asprintf(&val, "%s%s%s", prefix, sep, existing));
assert_success(setenv(env, val, 1));
free(val);
} else {
assert_success(setenv(env, prefix, 1));
}
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {

View File

@ -2,32 +2,24 @@
--suffix PATH : /usr/bin/ \ --suffix PATH : /usr/bin/ \
--suffix PATH : /usr/local/bin/ --suffix PATH : /usr/local/bin/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0) #define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0)
char *concat3(char *x, char *y, char *z) { void set_env_suffix(char *env, char *sep, char *suffix) {
int xn = strlen(x);
int yn = strlen(y);
int zn = strlen(z);
char *res = malloc(sizeof(*res)*(xn + yn + zn + 1));
assert(res != NULL);
strncpy(res, x, xn);
strncpy(res + xn, y, yn);
strncpy(res + xn + yn, z, zn);
res[xn + yn + zn] = '\0';
return res;
}
void set_env_suffix(char *env, char *sep, char *val) {
char *existing = getenv(env); char *existing = getenv(env);
if (existing) val = concat3(existing, sep, val); if (existing) {
assert_success(setenv(env, val, 1)); char *val;
if (existing) free(val); assert_success(asprintf(&val, "%s%s%s", existing, sep, suffix));
assert_success(setenv(env, val, 1));
free(val);
} else {
assert_success(setenv(env, suffix, 1));
}
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -35,4 +27,4 @@ int main(int argc, char **argv) {
set_env_suffix("PATH", ":", "/usr/local/bin/"); set_env_suffix("PATH", ":", "/usr/local/bin/");
argv[0] = "/path/to/executable"; argv[0] = "/path/to/executable";
return execv("/path/to/executable", argv); return execv("/path/to/executable", argv);
} }