commit: b497b852db5622d81e6cf2964f37f79a55411e86
parent e4d1d4005ade9f2d881864589fe4df64e4bc6281
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 29 Mar 2025 16:55:31 +0100
cmd/getconf: Align -a option with Austin Group Issue 1808
Diffstat:
3 files changed, 62 insertions(+), 64 deletions(-)
diff --git a/cmd/getconf.1 b/cmd/getconf.1
@@ -11,13 +11,11 @@
.Nm
.Ar system_var
.Nm
-.Fl a
-.Nm
.Ar path_var
.Ar path
.Nm
.Fl a
-.Ar path
+.Op Ar path
.Sh DESCRIPTION
In the first synopsis form,
.Nm
@@ -31,10 +29,6 @@ Which are obtained, in order, by:
.Pp
In the second synopsis form,
.Nm
-prints all the known system variables.
-.Pp
-In the third synopsis form,
-.Nm
prints the variable specified by the
.Ar path_var
operand for the path specified by the
@@ -43,20 +37,28 @@ operand.
Which are obtained via
.Xr pathconf 3 .
.Pp
-In the fourth synopsis form,
+When
+.Fl a
+is set
.Nm
-prints all the known path variables for the path specified by the
+prints all the known system and path variables.
+If
.Ar path
-operand.
+is unspecified,
+.Nm
+behaves as if it was set to
+.Ql \&.
.Sh STDOUT
If the specified variable is found, it is printed out as-is,
when it isn't found but valid it is printed as "undefined".
.Pp
-When using the
+When
.Fl a
-form,
-each variable is printed on a line in "name = value" format when found,
-and only as "name" when not found.
+is used,
+each variable is printed on a line in "name: value" format,
+with "value" being
+.Ql undefined
+when not found.
.Sh EXIT STATUS
.Ex -std
.Sh EXAMPLES
@@ -79,5 +81,10 @@ argument,
should be compliant with the
IEEE Std 1003.1-2024 (“POSIX.1”)
specification.
+.Pp
+The
+.Fl a
+option was added in
+.Lk https://www.austingroupbugs.net/view.php?id=1808 "0001808: Add option -a to getconf utility - Austin Group Issue Tracker"
.Sh AUTHORS
.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me
diff --git a/cmd/getconf.c b/cmd/getconf.c
@@ -156,7 +156,34 @@ main(int argc, char *argv[])
{
return print_system_var(argv[0]);
}
- else if(o_all && argc == 0)
+ else if(!o_all && argc == 2)
+ {
+ for(size_t i = 0; i < (sizeof(pathconf_vars) / sizeof(*pathconf_vars)); i++)
+ {
+ if(strcmp(pathconf_vars[i].name, argv[0]) != 0) continue;
+
+ errno = 0;
+ long ret = pathconf(argv[1], (int)(pathconf_vars[i].value));
+ if(ret == -1 && errno != 0)
+ {
+ fprintf(stderr,
+ "getconf: error: pathconf(\"%s\", %d /* \"%s\" */): %s\n",
+ argv[1],
+ pathconf_vars[i].value,
+ argv[0],
+ strerror(errno));
+ return 1;
+ }
+ if(ret == -1) ret = sysconf_vars[i].limit_h;
+
+ printf("%ld\n", ret);
+ return 0;
+ }
+
+ fprintf(stderr, "getconf: error: unknown path_var \"%s\"\n", argv[0]);
+ return 1;
+ }
+ else if(o_all && (argc == 0 || argc == 1))
{
int err = 0;
char *buf = NULL;
@@ -179,8 +206,7 @@ main(int argc, char *argv[])
continue;
}
- /* undefined */
- printf("%s\n", confstr_vars[i].name);
+ printf("%s: undefined\n", confstr_vars[i].name);
continue;
}
@@ -219,12 +245,11 @@ main(int argc, char *argv[])
continue;
}
- /* undefined */
- printf("%s\n", confstr_vars[i].name);
+ printf("%s: undefined\n", confstr_vars[i].name);
continue;
}
- printf("%s = ", confstr_vars[i].name);
+ printf("%s: ", confstr_vars[i].name);
fwrite(buf, buflen_ret, 1, stdout);
printf("\n");
}
@@ -250,12 +275,11 @@ main(int argc, char *argv[])
if(ret == -1)
{
- /* undefined */
- printf("%s\n", sysconf_vars[i].name);
+ printf("%s: undefined\n", sysconf_vars[i].name);
continue;
}
- printf("%s = %ld\n", sysconf_vars[i].name, ret);
+ printf("%s: %ld\n", sysconf_vars[i].name, ret);
}
for(size_t i = 0; i < (sizeof(limits_vars) / sizeof(*limits_vars)); i++)
@@ -264,55 +288,23 @@ main(int argc, char *argv[])
if(ret == -1)
{
- /* undefined */
- printf("%s\n", limits_vars[i].name);
+ printf("%s: undefined\n", limits_vars[i].name);
continue;
}
- printf("%s = %ld\n", limits_vars[i].name, ret);
+ printf("%s: %ld\n", limits_vars[i].name, ret);
}
- return err;
- }
- else if(!o_all && argc == 2)
- {
+ const char *path = argc == 1 ? argv[0] : ".";
for(size_t i = 0; i < (sizeof(pathconf_vars) / sizeof(*pathconf_vars)); i++)
{
- if(strcmp(pathconf_vars[i].name, argv[0]) != 0) continue;
-
errno = 0;
- long ret = pathconf(argv[1], (int)(pathconf_vars[i].value));
+ long ret = pathconf(path, (int)(pathconf_vars[i].value));
if(ret == -1 && errno != 0)
{
fprintf(stderr,
"getconf: error: pathconf(\"%s\", %d /* \"%s\" */): %s\n",
- argv[1],
- pathconf_vars[i].value,
- argv[0],
- strerror(errno));
- return 1;
- }
- if(ret == -1) ret = sysconf_vars[i].limit_h;
-
- printf("%ld\n", ret);
- return 0;
- }
-
- fprintf(stderr, "getconf: error: unknown path_var \"%s\"\n", argv[0]);
- return 1;
- }
- else if(o_all && argc == 1)
- {
- int err = 0;
- for(size_t i = 0; i < (sizeof(pathconf_vars) / sizeof(*pathconf_vars)); i++)
- {
- errno = 0;
- long ret = pathconf(argv[0], (int)(pathconf_vars[i].value));
- if(ret == -1 && errno != 0)
- {
- fprintf(stderr,
- "getconf: error: pathconf(\"%s\", %d /* \"%s\" */): %s\n",
- argv[0],
+ path,
pathconf_vars[i].value,
pathconf_vars[i].name,
strerror(errno));
@@ -321,7 +313,7 @@ main(int argc, char *argv[])
}
if(ret == -1) ret = sysconf_vars[i].limit_h;
- printf("%s = %ld\n", pathconf_vars[i].name, ret);
+ printf("%s: %ld\n", pathconf_vars[i].name, ret);
}
return err;
@@ -331,9 +323,8 @@ main(int argc, char *argv[])
fprintf(stderr, "getconf: error: wrong number of arguments\n");
fprintf(stderr, "\
Usage: getconf <system_var>\n\
- getconf -a\n\
getconf <path_var> <path>\n\
- getconf -a <path>\n\
+ getconf -a [path]\n\
");
return 1;
}
diff --git a/test-cmd/getconf.sh b/test-cmd/getconf.sh
@@ -7,7 +7,7 @@ target="${WD}/../cmd/getconf"
plans=3
. "${WD}/tap.sh"
-getconf_dupes() { "$target" -a 2>/dev/null | cut -d= -f1 | sort | uniq -d; }
+getconf_dupes() { "$target" -a 2>/dev/null | cut -d: -f1 | sort | uniq -d; }
# utils-std doesn't have sort yet
if command -v sort >/dev/null 2>/dev/null; then