gbranden pushed a commit to branch master
in repository groff.
commit 81afd4cf4339772fecd660a63f7ab9d6cb1fdcef
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sun Mar 15 16:53:41 2026 -0500
Refactor: migrate putenv(3) -> setenv(3) (2a/3).
* src/roff/groff/groff.cpp: Refactor: migrate from putenv(3) to
setenv(3) (1/4). Drop global pointer variable `groff_font_path`.
(xsetenv): New function wraps setenv(3) as `xputenv()` does putenv(3).
(main): Replace local string `e` containing shell-like environment
variable assignment with string `value` containing only the value to
be assigned. Call `xsetenv()` with `GROFF_FONT_PATH` string literal
and `value` as arguments. Because, unlike putenv(3), setenv(3) makes
its own copies of its C string operands, we no longer need to reserve
statically allocated storage for them to keep them from going out of
scope.
(xexit): Stop free(3)ing abandoned variable `groff_font_path`.
---
ChangeLog | 16 ++++++++++++++++
src/roff/groff/groff.cpp | 24 +++++++++++++-----------
2 files changed, 29 insertions(+), 11 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 0dd2c6d4f..8363c1046 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2026-03-15 G. Branden Robinson <[email protected]>
+
+ * src/roff/groff/groff.cpp: Refactor: migrate from putenv(3) to
+ setenv(3) (1/4). Drop global pointer variable
+ `groff_font_path`.
+ (xsetenv): New function wraps setenv(3) as `xputenv()` does
+ putenv(3).
+ (main): Replace local string `e` containing shell-like
+ environment variable assignment with string `value` containing
+ only the value to be assigned. Call `xsetenv()` with
+ `GROFF_FONT_PATH` string literal and `value` as arguments.
+ Because, unlike putenv(3), setenv(3) makes its own copies of its
+ C string operands, we no longer need to reserve statically
+ allocated storage for them to keep them from going out of scope.
+ (xexit): Stop free(3)ing abandoned variable `groff_font_path`.
+
2026-03-15 G. Branden Robinson <[email protected]>
Refactor: migrate from putenv(3) to setenv(3).
diff --git a/src/roff/groff/groff.cpp b/src/roff/groff/groff.cpp
index 9317e1c9e..5e478e0f5 100644
--- a/src/roff/groff/groff.cpp
+++ b/src/roff/groff/groff.cpp
@@ -26,7 +26,7 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
#include <assert.h>
#include <errno.h>
#include <stdio.h> // EOF, FILE, fflush(), setbuf(), stderr, stdout
-#include <stdlib.h> // exit(), EXIT_SUCCESS, free(), getenv(), putenv()
+#include <stdlib.h> // exit(), EXIT_SUCCESS, free(), getenv(), putenv(),
setenv()
#include <string.h> // strerror(), strsignal()
#include <getopt.h> // getopt_long()
@@ -110,7 +110,6 @@ char *predriver = 0 /* nullptr */;
bool need_postdriver = true;
char *saved_path = 0 /* nullptr */;
char *groff_bin_path = 0 /* nullptr */;
-char *groff_font_path = 0 /* nullptr */;
possible_command commands[NCOMMANDS];
@@ -138,13 +137,19 @@ static void xputenv(const char *s) {
return;
}
+static void xsetenv(const char *name, const char *value, int overwrite)
+{
+ if (setenv(name, value, overwrite) != 0)
+ fatal("cannot update process environment: %1", strerror(errno));
+ return;
+}
+
static void xexit(int status) {
free(spooler);
free(predriver);
free(postdriver);
free(saved_path);
free(groff_bin_path);
- free(groff_font_path);
exit(status);
}
@@ -521,17 +526,14 @@ int main(int argc, char **argv)
commands[first_index].append_arg("-");
}
if (Fargs.length() > 0) {
- string e = "GROFF_FONT_PATH";
- e += '=';
- e += Fargs;
+ string value = Fargs;
char *fontpath = getenv("GROFF_FONT_PATH");
if ((fontpath != 0 /* nullptr */) && (*fontpath != '\0')) {
- e += PATH_SEP_CHAR;
- e += fontpath;
+ value += PATH_SEP_CHAR;
+ value += fontpath;
}
- e += '\0';
- groff_font_path = xstrdup(e.contents());
- xputenv(groff_font_path);
+ value += '\0';
+ xsetenv("GROFF_FONT_PATH", value.contents(), 1 /* overwrite */);
}
{
// we save the original path in GROFF_PATH__ and put it into the
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit