diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-24 14:21:41 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-24 14:21:41 -0600 |
| commit | e770f9ca9ef8e9b44f9f34752a397f059f58eaae (patch) | |
| tree | 05bedcc98a47d4c8ab171806cfe1a6d1cc85a853 | |
| parent | 03042dfefee7b8e35b07db74c090eb5823be0265 (diff) | |
| parent | 24dc65f3343f114013dbfb0821d95f28d74f156d (diff) | |
| download | whoson-e770f9ca9ef8e9b44f9f34752a397f059f58eaae.tar.gz | |
Merge pull request #9 from skurtz97/fix/raw-stdout
whoson: write help, errors and the table for the destination's character set
| -rw-r--r-- | README.md | 9 | ||||
| -rw-r--r-- | whoson.c | 118 |
2 files changed, 98 insertions, 29 deletions
@@ -48,6 +48,15 @@ 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. +Output does not go through `printf`. `emit()` writes every message and the +table itself with `write(2)`, in the character set the destination expects: +plain ASCII when the descriptor is a terminal, EBCDIC when it is a pipe, a +socket or a file. CEE would convert stdio output from ASCII to EBCDIC on a +terminal, and this system's sshd converts the other way for a command it runs +without a tty, so neither `printf` nor a raw `write()` is correct on both +paths. Help and error text go through the same `emit()`, so `whoson -h` and +the usage message are readable whichever way they are run. + ## How the data is produced Both displays go through the ZOAU shared library vector (`vector->opercmd`), @@ -16,7 +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. + * 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 @@ -25,6 +27,7 @@ */ #include <inttypes.h> +#include <stdarg.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -41,6 +44,7 @@ #define NAME_LEN 16 #define ASID_LEN 8 #define TIME_LEN 16 +#define MSG_LEN 512 #define WUID_LEN 12 #define CPU_LEN 12 #define SCL_LEN 8 @@ -74,12 +78,57 @@ struct row { char pid[PID_LEN]; }; +static const char usage_text[] = "usage: whoson\n" + "\n" + "List logged-on users with session type (TSO/E or SSH).\n" + "\n" + "Options:\n" + " -h, --help show this help and exit\n"; + +/* Write a message in the character set its destination expects. CEE converts + * stdio output from ASCII to EBCDIC when the stream is a terminal, and sshd + * converts a pipe, socket or file the other way, so neither printf nor a raw + * write() is right on both: a terminal gets ASCII, everything else EBCDIC. */ +static void emit(int fd, char *buf, size_t len) +{ + if (!isatty(fd)) + __a2e_l(buf, len); + while (len > 0) { + ssize_t n = write(fd, buf, len); + + if (n <= 0) + return; + buf += n; + len -= (size_t)n; + } +} + +static void emit_str(int fd, const char *text) +{ + char buf[MSG_LEN]; + size_t len = strlen(text); + + if (len >= sizeof(buf)) + len = sizeof(buf) - 1; + memcpy(buf, text, len); + emit(fd, buf, len); +} + +/* Report an error on stderr; messages are short enough for one line buffer. */ +static void error_msg(const char *fmt, ...) +{ + char text[MSG_LEN]; + va_list ap; + + va_start(ap, fmt); + vsnprintf(text, sizeof(text), fmt, ap); + va_end(ap); + emit_str(STDERR_FILENO, text); +} + static int usage_error(const char *msg) { - fprintf(stderr, "whoson: %s\n", msg); - fprintf(stderr, "usage: whoson\n\n" - "List logged-on users with session type (TSO/E, " - "TSO/E OMVS, SSH).\n"); + error_msg("whoson: %s\n%s", msg, usage_text); return 2; } @@ -396,31 +445,35 @@ static int run_display(zoautil_vector4_t *vector, const char *args, len = strlen(parms); rc = vector->opercmd(parms, len, &response, options); if (rc != 0 || response == NULL) { - fprintf(stderr, "whoson: %s failed, rc = %d\n", args, rc); + error_msg("whoson: %s failed, rc = %d\n", args, rc); if (response != NULL) - fprintf(stderr, - "whoson: cmd_rc = %d, stdout_len = %d, " - "stderr_len = %d\n", response->cmd_rc, - response->stdout_len, response->stderr_len); + error_msg("whoson: cmd_rc = %d, stdout_len = %d, " + "stderr_len = %d\n", response->cmd_rc, + response->stdout_len, + response->stderr_len); if (response != NULL && response->stderr_response != NULL && response->stderr_len > 0) { + char text[MSG_LEN]; + __e2a_l(response->stderr_response, (size_t)response->stderr_len); - fprintf(stderr, "%.*s\n", response->stderr_len, - response->stderr_response); + snprintf(text, sizeof(text), "%.*s\n", + response->stderr_len, + response->stderr_response); + emit_str(STDERR_FILENO, text); } if (response != NULL) free_response(response); return -1; } if (response->stdout_response == NULL || response->stdout_len <= 0) { - fprintf(stderr, "whoson: %s returned no output\n", args); + error_msg("whoson: %s returned no output\n", args); free_response(response); return -1; } *out = malloc((size_t)response->stdout_len + 1); if (*out == NULL) { - fprintf(stderr, "whoson: out of memory\n"); + error_msg("whoson: out of memory\n"); free_response(response); return -1; } @@ -436,6 +489,8 @@ int main(int argc, char *argv[]) { static struct proc procs[MAX_PROCS]; static struct row rows[MAX_ROWS]; + static char out[(MAX_ROWS + 1) * 128]; + size_t used; zoautil_options_t options = { ZOAUTIL_EYECATCHER, VERSION_CURRENT, @@ -450,9 +505,7 @@ int main(int argc, char *argv[]) if (argc > 1) { if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { - printf("usage: whoson\n\n" - "List logged-on users with session type " - "(TSO/E, SSH).\n"); + emit_str(STDOUT_FILENO, usage_text); return 0; } return usage_error("unknown argument"); @@ -460,14 +513,13 @@ int main(int argc, char *argv[]) vrm = version(); if (vrm < VERSION_CURRENT) { - fprintf(stderr, - "whoson: ZOAU shared library is too old: 0x%x < 0x%x\n", - vrm, VERSION_CURRENT); + error_msg("whoson: ZOAU shared library is too old: 0x%x < 0x%x\n", + vrm, VERSION_CURRENT); return 1; } vector = init(vrm); if (vector == NULL) { - fprintf(stderr, "whoson: cannot get the ZOAU vector table\n"); + error_msg("whoson: cannot get the ZOAU vector table\n"); return 1; } if (run_display(vector, "D OMVS,A=ALL", options, &omvs_text) != 0) @@ -482,15 +534,23 @@ 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); - printf("%-8s %-12s %-6s %-4s %-11s %-10s %-8s %-10s %-4s %-4s %-6s\n", - "USER", "TYPE", "STATUS", "ASID", "PID", "WUID", "CPU", - "ELAPSED", "SCL", "PRIO", "RGP"); + /* 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"); for (int i = 0; i < nrows; i++) - printf("%-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); + 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); + + emit(STDOUT_FILENO, out, used); rc = 0; out: free(ts_text); |
