sin: add version and quiet flags

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I53f8463f4926cbd6aeaf31447cdb5a7c6a6a6964
This commit is contained in:
raf 2025-11-27 19:04:03 +03:00
commit faef5bccdc
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

28
main.c
View file

@ -15,6 +15,8 @@
#include <systemd/sd-bus.h>
#define VERSION "1.0.0"
#define EXIT_OK 0
#define EXIT_ARGS 1
#define EXIT_INIT 2
@ -42,6 +44,7 @@ static const char *valid_mode[] = {"block", "delay", NULL};
static volatile sig_atomic_t running = 1;
static volatile sig_atomic_t cleanup_requested = 0;
static int quiet = 0;
static void handle_sig(int sig) {
(void)sig;
@ -344,7 +347,7 @@ static void usage(FILE *f, const char *prog) {
fprintf(f,
"Usage: %s -n <pattern> [-n <pattern> ...] "
"[--what=WHAT] [--mode=MODE]\n"
" [--who=WHO] [--why=WHY] [--poll=SECONDS]\n"
" [--who=WHO] [--why=WHY] [--poll=SECONDS] [-q]\n"
"\n"
"Options:\n"
" -n, --name PATTERN Process command-line substring to watch "
@ -362,7 +365,9 @@ static void usage(FILE *f, const char *prog) {
"system idle...)\n"
" --poll SECONDS Poll interval in seconds (%.1f-%.1f, "
"default: %.1f)\n"
" -h, --help Show this help\n",
" -q, --quiet Suppress informational output\n"
" -h, --help Show this help\n"
" -v, --version Show version\n",
prog, MIN_POLL_INTERVAL, MAX_POLL_INTERVAL, DEFAULT_POLL_INTERVAL);
}
@ -483,6 +488,15 @@ int main(int argc, char **argv) {
free(patterns);
return EXIT_OK;
} else if (strcmp(argv[i], "--version") == 0 ||
strcmp(argv[i], "-v") == 0) {
printf("sin %s\n", VERSION);
free(patterns);
return EXIT_OK;
} else if (strcmp(argv[i], "--quiet") == 0 || strcmp(argv[i], "-q") == 0) {
quiet = 1;
} else if (argv[i][0] == '-' && argv[i][1] == 'n' && argv[i][2] != '\0') {
// Support "-npattern" form
if (npats >= MAX_PATTERNS) {
@ -548,13 +562,14 @@ int main(int argc, char **argv) {
if (found) {
if (inh.fd < 0) {
fprintf(stderr, "Target process found - acquiring inhibitor\n");
if (!quiet)
fprintf(stderr, "Target process found - acquiring inhibitor\n");
if (inhibitor_acquire(&inh) < 0) {
fprintf(
stderr,
"Failed to acquire inhibitor; retrying after poll interval\n");
exit_code = EXIT_RUNTIME;
} else {
} else if (!quiet) {
fprintf(stderr, "Inhibitor acquired (fd=%d)\n", inh.fd);
}
}
@ -573,7 +588,8 @@ int main(int argc, char **argv) {
}
if (inh.fd >= 0 && running) {
fprintf(stderr, "Target exited - releasing inhibitor\n");
if (!quiet)
fprintf(stderr, "Target exited - releasing inhibitor\n");
inhibitor_release(&inh);
}
} else {
@ -590,7 +606,7 @@ int main(int argc, char **argv) {
}
// Cleanup
if (cleanup_requested) {
if (cleanup_requested && !quiet) {
fprintf(stderr, "Shutting down gracefully...\n");
}