summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-28 15:02:39 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-28 15:02:39 -0600
commit6fe7f93ef05466477f4bc8f7f2e6ef10a881758e (patch)
tree9619eb0515506e2e8859710b31b04b617147cf7a
parenta1c0cabb837f179b70d0d7b22f4036894061d2c3 (diff)
downloadwhoson-6fe7f93ef05466477f4bc8f7f2e6ef10a881758e.tar.gz
Name the types with typedefs and keep type names unique
-rw-r--r--CONTRIBUTING.md2
-rw-r--r--whoson.c170
2 files changed, 86 insertions, 86 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 4984110..12b56ff 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -39,6 +39,8 @@ syntax (`?=` is out), and let `make -n` stay free of side effects.
`.clang-tidy`, gets fixed rather than silenced. `.clang-tidy` needs the z/OS
headers (`<sys/ps.h>`), so only a z/OS build environment can run it; the
Ubuntu CI job enforces clang-format, not clang-tidy.
+- Types are `typedef struct name { ... } name;`, and no variable, function
+ or field reuses a type name.
- `clang-format -i whoson.c` before committing; `.clang-format` is the
authority, and CI checks the result against clang-format 23.1.5.
- Comments say why, not what, and the file header says what the control-block
diff --git a/whoson.c b/whoson.c
index b1d4a93..66896a7 100644
--- a/whoson.c
+++ b/whoson.c
@@ -88,7 +88,7 @@
/* Field offsets are checked against these overlays (static_assert below).
* The overlays describe layout only; every read goes through the accessors,
* so the compiler never assumes the block is stable. */
-struct ascb {
+typedef struct ascb {
uint32_t ascbascb;
unsigned char pad0[0x24 - 0x04];
uint16_t ascbasid;
@@ -103,45 +103,45 @@ struct ascb {
uint64_t ascbsrbt;
unsigned char pad4[0x150 - 0xD0];
uint32_t ascbassb;
-};
+} ascb;
-struct cscb {
+typedef struct cscb {
unsigned char pad0[0x08];
char chkey[8];
char chcls[8];
unsigned char pad1[0x1C - 0x18];
unsigned char chtrkid;
-};
+} cscb;
-struct assb {
+typedef struct assb {
unsigned char pad[0x160];
uint64_t assbasst;
-};
+} assb;
-_Static_assert(offsetof(struct ascb, ascbasid) == 0x24, "ASCBASID");
-_Static_assert(offsetof(struct ascb, ascbcscb) == 0x38, "ASCBCSCB");
-_Static_assert(offsetof(struct ascb, ascbtsb) == 0x3C, "ASCBTSB");
-_Static_assert(offsetof(struct ascb, ascbejst) == 0x40, "ASCBEJST");
-_Static_assert(offsetof(struct ascb, ascbjbni) == 0xAC, "ASCBJBNI");
-_Static_assert(offsetof(struct ascb, ascbjbns) == 0xB0, "ASCBJBNS");
-_Static_assert(offsetof(struct ascb, ascbsrbt) == 0xC8, "ASCBSRBT");
-_Static_assert(offsetof(struct ascb, ascbassb) == 0x150, "ASCBASSB");
-_Static_assert(offsetof(struct cscb, chkey) == 0x08, "CHKEY");
-_Static_assert(offsetof(struct cscb, chcls) == 0x10, "CHCLS");
-_Static_assert(offsetof(struct cscb, chtrkid) == 0x1C, "CHTRKID");
-_Static_assert(offsetof(struct assb, assbasst) == 0x160, "ASSBASST");
+_Static_assert(offsetof(ascb, ascbasid) == 0x24, "ASCBASID");
+_Static_assert(offsetof(ascb, ascbcscb) == 0x38, "ASCBCSCB");
+_Static_assert(offsetof(ascb, ascbtsb) == 0x3C, "ASCBTSB");
+_Static_assert(offsetof(ascb, ascbejst) == 0x40, "ASCBEJST");
+_Static_assert(offsetof(ascb, ascbjbni) == 0xAC, "ASCBJBNI");
+_Static_assert(offsetof(ascb, ascbjbns) == 0xB0, "ASCBJBNS");
+_Static_assert(offsetof(ascb, ascbsrbt) == 0xC8, "ASCBSRBT");
+_Static_assert(offsetof(ascb, ascbassb) == 0x150, "ASCBASSB");
+_Static_assert(offsetof(cscb, chkey) == 0x08, "CHKEY");
+_Static_assert(offsetof(cscb, chcls) == 0x10, "CHCLS");
+_Static_assert(offsetof(cscb, chtrkid) == 0x1C, "CHTRKID");
+_Static_assert(offsetof(assb, assbasst) == 0x160, "ASSBASST");
/* One address space, as read from its ASCB, indexed by ASID - 1. */
-struct space {
+typedef struct space {
bool tso;
uint64_t us; /* microseconds of CPU */
char job[JOB_LEN];
char user[NAME_LEN];
char logon[JOB_LEN];
-};
+} space;
/* One output row. TSO rows are built from a space; SSH rows from a process. */
-struct row {
+typedef struct row {
unsigned asid;
uint64_t us;
pid_t pid;
@@ -149,19 +149,19 @@ struct row {
char job[JOB_LEN];
char logon[JOB_LEN];
char user[NAME_LEN];
-};
+} row;
-struct proc {
+typedef struct proc {
pid_t pid;
pid_t ppid;
uid_t euid;
char name[PATH_LEN];
-};
+} proc;
-struct pid_asid {
+typedef struct pid_asid {
pid_t pid;
unsigned asid;
-};
+} pid_asid;
static const char usage_text[] =
"usage: whoson [--help | --version]\n"
@@ -276,12 +276,12 @@ static int set_cvt(int fd, int cmd, int pccsid, int fccsid) {
/* The conversion setting belongs to the open file description, which the
* calling shell keeps after whoson exits. Save it and put it back, or the
* shell's own output would inherit ours. */
-struct cvt_saved {
+typedef struct cvt_saved {
int fd;
struct f_cnvrt v;
-};
+} cvt_saved;
-static struct cvt_saved saved_cvt[2];
+static cvt_saved saved_cvt[2];
static size_t nsaved;
static void restore_cvt(void) {
@@ -413,11 +413,11 @@ static void read_name(uint32_t ptr, char *dst, size_t dstlen) {
/* Read one ASCB. Returns 0 and fills *out on success; -1 skips it. */
static int scan_one(unsigned idx, uint32_t entry, uint32_t entries,
- struct space *out) {
- struct space tmp = {0};
- uint32_t ascb = entry & 0x7FFFFFFFu;
+ space *out) {
+ space tmp = {0};
+ uint32_t ascb_addr = entry & 0x7FFFFFFFu;
uint16_t asid;
- uint32_t cscb;
+ uint32_t cscb_addr;
uint64_t tod;
bool tso;
char key[NAME_LEN] = {0};
@@ -428,33 +428,33 @@ static int scan_one(unsigned idx, uint32_t entry, uint32_t entries,
fault_armed = 0;
return -1;
}
- if (read_u32(ascb) != ASCB_ACRONYM)
+ if (read_u32(ascb_addr) != ASCB_ACRONYM)
goto fail;
- asid = read_u16(ascb + (uint32_t)offsetof(struct ascb, ascbasid));
+ asid = read_u16(ascb_addr + (uint32_t)offsetof(ascb, ascbasid));
if (asid != idx + 1)
goto fail;
- tod = read_u64(ascb + (uint32_t)offsetof(struct ascb, ascbejst)) +
- read_u64(ascb + (uint32_t)offsetof(struct ascb, ascbsrbt));
- cscb = read_u32(ascb + (uint32_t)offsetof(struct ascb, ascbcscb));
- tso = read_u32(ascb + (uint32_t)offsetof(struct ascb, ascbtsb)) != 0;
+ tod = read_u64(ascb_addr + (uint32_t)offsetof(ascb, ascbejst)) +
+ read_u64(ascb_addr + (uint32_t)offsetof(ascb, ascbsrbt));
+ cscb_addr = read_u32(ascb_addr + (uint32_t)offsetof(ascb, ascbcscb));
+ tso = read_u32(ascb_addr + (uint32_t)offsetof(ascb, ascbtsb)) != 0;
{
- uint32_t assb =
- read_u32(ascb + (uint32_t)offsetof(struct ascb, ascbassb));
+ uint32_t assb_addr =
+ read_u32(ascb_addr + (uint32_t)offsetof(ascb, ascbassb));
- if (assb != 0)
- tod += read_u64(assb + (uint32_t)offsetof(struct assb, assbasst));
+ if (assb_addr != 0)
+ tod += read_u64(assb_addr + (uint32_t)offsetof(assb, assbasst));
}
- if (cscb != 0) {
- read_name(cscb + (uint32_t)offsetof(struct cscb, chkey), key,
+ if (cscb_addr != 0) {
+ read_name(cscb_addr + (uint32_t)offsetof(cscb, chkey), key,
sizeof(key));
- read_name(cscb + (uint32_t)offsetof(struct cscb, chcls), cls,
+ read_name(cscb_addr + (uint32_t)offsetof(cscb, chcls), cls,
sizeof(cls));
- if (read_u8(cscb + (uint32_t)offsetof(struct cscb, chtrkid)) == 0x01)
+ if (read_u8(cscb_addr + (uint32_t)offsetof(cscb, chtrkid)) == 0x01)
tso = true;
}
- read_name(read_u32(ascb + (uint32_t)offsetof(struct ascb, ascbjbni)),
- tmp.job, sizeof(tmp.job));
- read_name(read_u32(ascb + (uint32_t)offsetof(struct ascb, ascbjbns)),
+ read_name(read_u32(ascb_addr + (uint32_t)offsetof(ascb, ascbjbni)), tmp.job,
+ sizeof(tmp.job));
+ read_name(read_u32(ascb_addr + (uint32_t)offsetof(ascb, ascbjbns)),
tmp.logon, sizeof(tmp.logon));
/* The ASVT entry must still name the same ASCB: if the address space
* ended while we were reading it, the block may already be reused. */
@@ -508,7 +508,7 @@ fail:
return -1;
}
-static void ascb_scan(uint32_t entries, unsigned maxu, struct space *snap,
+static void ascb_scan(uint32_t entries, unsigned maxu, space *snap,
unsigned *found) {
unsigned i;
@@ -605,11 +605,11 @@ static int read_setup(int fd) {
return 0;
}
-static int add_pid_asid(struct pid_asid **map, size_t *n, size_t *cap,
- pid_t pid, unsigned asid) {
+static int add_pid_asid(pid_asid **map, size_t *n, size_t *cap, pid_t pid,
+ unsigned asid) {
if (*n == *cap) {
size_t ncap = *cap ? *cap * 2 : 64;
- struct pid_asid *grown = realloc(*map, ncap * sizeof(**map));
+ pid_asid *grown = realloc(*map, ncap * sizeof(**map));
if (grown == NULL)
return -1;
@@ -632,7 +632,7 @@ static bool is_header(const char *s) {
strstr(s, "ASID") != NULL;
}
-static int ps_scan(struct pid_asid **map, size_t *count) {
+static int ps_scan(pid_asid **map, size_t *count) {
FILE *fp;
char *buf = NULL;
size_t len = 0;
@@ -704,17 +704,16 @@ static int ps_scan(struct pid_asid **map, size_t *count) {
}
static int cmp_pid_asid(const void *a, const void *b) {
- const struct pid_asid *x = a;
- const struct pid_asid *y = b;
+ const pid_asid *x = a;
+ const pid_asid *y = b;
if (x->pid != y->pid)
return x->pid < y->pid ? -1 : 1;
return 0;
}
-static const struct pid_asid *find_asid(const struct pid_asid *map, size_t n,
- pid_t pid) {
- struct pid_asid key;
+static const pid_asid *find_asid(const pid_asid *map, size_t n, pid_t pid) {
+ pid_asid key;
key.pid = pid;
key.asid = 0;
@@ -722,17 +721,16 @@ static const struct pid_asid *find_asid(const struct pid_asid *map, size_t n,
}
static int cmp_proc(const void *a, const void *b) {
- const struct proc *x = a;
- const struct proc *y = b;
+ const proc *x = a;
+ const proc *y = b;
if (x->pid != y->pid)
return x->pid < y->pid ? -1 : 1;
return 0;
}
-static const struct proc *find_proc(const struct proc *procs, size_t n,
- pid_t pid) {
- struct proc key = {.pid = pid};
+static const proc *find_proc(const proc *procs, size_t n, pid_t pid) {
+ proc key = {.pid = pid};
return bsearch(&key, procs, n, sizeof(*procs), cmp_proc);
}
@@ -751,14 +749,14 @@ static bool is_sftp_server(const char *name) {
strcmp(name, "internal-sftp") == 0;
}
-static int append_proc(struct proc **procs, size_t *n, size_t *cap,
- const W_PSPROC *ps, const char *path) {
+static int append_proc(proc **procs, size_t *n, size_t *cap, const W_PSPROC *ps,
+ const char *path) {
const char *base;
size_t len;
if (*n == *cap) {
size_t ncap = *cap ? *cap * 2 : 64;
- struct proc *grown = realloc(*procs, ncap * sizeof(**procs));
+ proc *grown = realloc(*procs, ncap * sizeof(**procs));
if (grown == NULL)
return -1;
@@ -779,7 +777,7 @@ static int append_proc(struct proc **procs, size_t *n, size_t *cap,
return 0;
}
-static int proc_scan(struct proc **procs, size_t *count) {
+static int proc_scan(proc **procs, size_t *count) {
W_PSPROC ps;
char *path;
char *cmd;
@@ -906,8 +904,8 @@ static void user_name(uid_t uid, char *out, size_t outlen) {
}
static int cmp_rows(const void *a, const void *b) {
- const struct row *x = a;
- const struct row *y = b;
+ const row *x = a;
+ const row *y = b;
int cmp = strcmp(x->user, y->user);
if (cmp != 0)
@@ -927,18 +925,18 @@ static void fmt_cpu(uint64_t us, char *out, size_t outlen) {
secs % 3600u / 60u, secs % 60u);
}
-struct outbuf {
+typedef struct outbuf {
char buf[OUT_LEN];
size_t len;
-};
+} outbuf;
-static void out_flush(struct outbuf *o) {
+static void out_flush(outbuf *o) {
if (o->len != 0)
emit(STDOUT_FILENO, o->buf, o->len);
o->len = 0;
}
-static void out_add(struct outbuf *o, const char *fmt, ...) {
+static void out_add(outbuf *o, const char *fmt, ...) {
char line[MSG_LEN];
va_list ap;
int n;
@@ -956,8 +954,8 @@ static void out_add(struct outbuf *o, const char *fmt, ...) {
o->len += (size_t)n;
}
-static void print_rows(struct row *rows, size_t n) {
- struct outbuf out;
+static void print_rows(row *rows, size_t n) {
+ outbuf out;
char cpu[CPU_LEN];
char asid[ASID_LEN];
char pid[PID_LEN];
@@ -983,9 +981,9 @@ static void print_rows(struct row *rows, size_t n) {
out_flush(&out);
}
-static int collect(struct row *rows, size_t maxrows, const struct space *snap,
- unsigned maxu, const struct proc *procs, size_t nprocs,
- const struct pid_asid *map, size_t nmap) {
+static int collect(row *rows, size_t maxrows, const space *snap, unsigned maxu,
+ const proc *procs, size_t nprocs, const pid_asid *map,
+ size_t nmap) {
size_t n = 0;
unsigned i;
size_t k;
@@ -1005,8 +1003,8 @@ static int collect(struct row *rows, size_t maxrows, const struct space *snap,
n++;
}
for (k = 0; k < nprocs && n < maxrows; k++) {
- const struct proc *parent;
- const struct pid_asid *pa;
+ const proc *parent;
+ const pid_asid *pa;
unsigned asid = 0;
uint64_t us = 0;
const char *job = "-";
@@ -1035,10 +1033,10 @@ static int collect(struct row *rows, size_t maxrows, const struct space *snap,
}
int main(int argc, char **argv) {
- struct space *snap;
- struct row *rows;
- struct proc *procs;
- struct pid_asid *map;
+ space *snap;
+ row *rows;
+ proc *procs;
+ pid_asid *map;
size_t nmap;
size_t nprocs;
unsigned maxu;