summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-24 13:12:07 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-24 13:12:07 -0600
commite3776b1a6c06f07bfb6a422d34b5d7e3fdfaf446 (patch)
treee9bd660cb4ec22e45f01d47f154a9f886c556c13
parent041f4637e8a1ab804fd47361ced6ff76c361a3f0 (diff)
downloadwhoson-e3776b1a6c06f07bfb6a422d34b5d7e3fdfaf446.tar.gz
whoson: write the table with write(2) so CEE leaves it ASCII
CEE converts stdio output from ASCII to EBCDIC when stdout is a terminal, and sshd forwards that pty to the client unchanged, so printf put EBCDIC on the screen. A single buffered write(2) keeps the bytes ASCII on a terminal, a pipe and a redirected file.
-rw-r--r--README.md10
-rw-r--r--whoson.c45
2 files changed, 33 insertions, 22 deletions
diff --git a/README.md b/README.md
index 125e94d..eccaf55 100644
--- a/README.md
+++ b/README.md
@@ -46,9 +46,13 @@ make PREFIX=/usr/local install # -> /usr/local/bin/whoson
The sources are ASCII and stay that way; the build is in ASCII character mode
(`-fzos-le-char-mode=ascii`). ZOAU hands its display back in EBCDIC, so
-`run_display` converts each response in place (`__e2a_l`) before it is parsed,
-and the finished table is converted back (`__a2e_l`) before it is printed,
-because the console and the ssh exec channel both expect EBCDIC.
+`run_display` converts each response in place (`__e2a_l`) before it is parsed.
+
+The table itself is written with `write(2)` rather than `printf`. CEE converts
+stdio output from ASCII to EBCDIC when stdout is a terminal, and the pty sshd
+hands the client gets no converting of its own, so `printf` would put EBCDIC
+on the screen while `write(2)` leaves the ASCII bytes alone. Keep the output
+on `write(2)` (or `fwrite` on a stream you have taken out of CEE's hands).
## How the data is produced
diff --git a/whoson.c b/whoson.c
index f3e74f5..1895b19 100644
--- a/whoson.c
+++ b/whoson.c
@@ -16,9 +16,9 @@
* double quotes, "D TS,A". Unquoted, the D is taken for an option letter and
* the call fails with opercmd's usage text (rc 2). The program is built in
* ASCII character mode and passes the command in that mode; opercmd returns
- * the display itself in EBCDIC, so run_display converts each response, and
- * the finished table is converted back before printing: the console and the
- * ssh exec channel both hand this program's output to the user as EBCDIC.
+ * the display itself in EBCDIC, so run_display converts each response. The
+ * table leaves with write(2): CEE turns stdio output into EBCDIC on a
+ * terminal, and sshd forwards that pty to the client untouched.
*
* A TSO/E session reports its own CPU time (CT), elapsed time (ET), job id
* (WUID/SCL/P) and region (RGP), so those columns need no second source. An
@@ -78,8 +78,6 @@ struct row {
static int usage_error(const char *msg)
{
- /* shortcut: this text goes out as ASCII, so it is mangled when piped,
- * unlike the table; convert it if the usage ever has to be readable. */
fprintf(stderr, "whoson: %s\n", msg);
fprintf(stderr, "usage: whoson\n\n"
"List logged-on users with session type (TSO/E, "
@@ -440,8 +438,6 @@ int main(int argc, char *argv[])
{
static struct proc procs[MAX_PROCS];
static struct row rows[MAX_ROWS];
- /* One output line is far under 128 bytes, and this holds the header plus
- * one per row, so the snprintf calls below cannot truncate. */
static char out[(MAX_ROWS + 1) * 128];
size_t used;
zoautil_options_t options = {
@@ -490,21 +486,32 @@ int main(int argc, char *argv[])
nrows = add_ssh_rows(procs, nprocs, rows, nrows, MAX_ROWS);
qsort(rows, (size_t)nrows, sizeof(rows[0]), compare_rows);
+ /* One line is far under 128 bytes, and the buffer holds the header plus
+ * one per row, so these snprintf calls cannot truncate. */
used = (size_t)snprintf(out, sizeof(out),
- "%-8s %-12s %-6s %-4s %-11s %-10s %-8s %-10s "
- "%-4s %-4s %-6s\n",
- "USER", "TYPE", "STATUS", "ASID", "PID", "WUID",
- "CPU", "ELAPSED", "SCL", "PRIO", "RGP");
+ "%-8s %-12s %-6s %-4s %-11s %-10s %-8s %-10s "
+ "%-4s %-4s %-6s\n",
+ "USER", "TYPE", "STATUS", "ASID", "PID", "WUID",
+ "CPU", "ELAPSED", "SCL", "PRIO", "RGP");
for (int i = 0; i < nrows; i++)
used += (size_t)snprintf(out + used, sizeof(out) - used,
- "%-8s %-12s %-6s %-4s %-11s %-10s %-8s "
- "%-10s %-4s %-4s %-6s\n",
- rows[i].user, rows[i].session, rows[i].status,
- rows[i].asid, rows[i].pid, rows[i].wuid,
- rows[i].cpu, rows[i].elapsed, rows[i].scl,
- rows[i].prio, rows[i].rgp);
- __a2e_l(out, used);
- fwrite(out, 1, used, stdout);
+ "%-8s %-12s %-6s %-4s %-11s %-10s "
+ "%-8s %-10s %-4s %-4s %-6s\n",
+ rows[i].user, rows[i].session,
+ rows[i].status, rows[i].asid, rows[i].pid,
+ rows[i].wuid, rows[i].cpu, rows[i].elapsed,
+ rows[i].scl, rows[i].prio, rows[i].rgp);
+
+ /* write(2), not printf: CEE converts stdio output from ASCII to EBCDIC
+ * when stdout is a terminal, and sshd passes the pty to the client as is. */
+ for (const char *p = out; used > 0;) {
+ ssize_t n = write(STDOUT_FILENO, p, used);
+
+ if (n <= 0)
+ break;
+ p += n;
+ used -= (size_t)n;
+ }
rc = 0;
out:
free(ts_text);