commit 7fd8efc3907fb2d73b5cdf8686daa1724903d9d8 from: Benjamin Stürz date: Thu Jan 04 20:48:34 2024 UTC implement command-line argument parsing commit - f6ed57b5b3d22754d407d377759b1e6ec349b808 commit + 7fd8efc3907fb2d73b5cdf8686daa1724903d9d8 blob - 58c2e41aa282594470482a9727a5ac2f119db777 blob + a947cb97390e8ab7c334c3689e7494440aa521fd --- Makefile +++ Makefile @@ -1,4 +1,5 @@ -CFLAGS = -Wall -g -Og +VERSION = 0.1 +MY_CFLAGS = -Wall -Wextra -DVERSION=\"${VERSION}\" ${CFLAGS} LIBS = -lutil -lncurses all: apmtop @@ -10,4 +11,4 @@ clean: rm -f apmtop *.core apmtop: apmtop.c - ${CC} -o $@ apmtop.c ${CFLAGS} ${LIBS} + ${CC} -o $@ apmtop.c ${MY_CFLAGS} ${LIBS} blob - 05c21d3f7f856376af565647df3e82fb3cad55b2 blob + 2d10930a7072ada31106b513e634ad08535b6e59 --- README.md +++ README.md @@ -8,7 +8,7 @@ $ make ``` ## TODO -- getopt(3) +- apmtop.1 - Better error handling - Code looks a little ugly - Bonus: calculate the cpuspeed() multiple times, between calls to update(), blob - e0c501285c7406403fbf39f254a8a6231b7125a9 blob + da783449c0beaccea9b9c9b817e13b5bb1fb4d12 --- apmtop.c +++ apmtop.c @@ -338,21 +338,51 @@ update (struct display *dpy) entries = w; } +static int +usage (void) +{ + fputs ("usage: apmtop [-V] [-d delay]\n", stderr); + return 1; +} + int main (int argc, char *argv[]) { struct display dpy; + int option; // pledge(2) doesn't work, because apmtop(1) needs sysctl(2). unveil ("/usr/share/terminfo", "r"); unveil (NULL, NULL); - // Configure the "display". memset (&dpy, 0, sizeof (dpy)); + dpy.delay = 10; + + while ((option = getopt (argc, argv, "d:V")) != -1) { + const char *errstr; + switch (option) { + case 'd': + dpy.delay = (int)strtonum (optarg, 1, 100, &errstr); + if (errstr != NULL) + errx (1, "invalid delay '%s': %s", optarg, errstr); + break; + case 'V': + puts ("apmtop-" VERSION); + return 1; + default: + return usage (); + } + } + + argc -= optind; + //argv += optind; + if (argc > 0) + return usage (); + + // Configure the "display". dpy.ncpu = num_cpu (); dpy.cpus = calloc (dpy.ncpu, sizeof (struct cpu_usage)); dpy.fd_apm = open ("/dev/apm", O_RDONLY); - dpy.delay = 10; dpy.running = true; find_sensors ();