diff --git a/userspace/programs/dirname/main.cpp b/userspace/programs/dirname/main.cpp index 1fe01ab7..a174da63 100644 --- a/userspace/programs/dirname/main.cpp +++ b/userspace/programs/dirname/main.cpp @@ -1,18 +1,56 @@ +#include #include #include -int usage(const char* argv0, int ret) +int main(int argc, char* argv[]) { - FILE* fout = ret ? stderr : stdout; - fprintf(fout, "usage: %s STRING\n", argv0); - return ret; -} + bool zero = false; + + for (;;) + { + static option long_options[] { + { "zero", no_argument, nullptr, 'z' }, + { "help", no_argument, nullptr, 'h' }, + }; + + int ch = getopt_long(argc, argv, "zh", long_options, nullptr); + if (ch == -1) + break; + + switch (ch) + { + case 'z': + zero = true; + break; + case 'h': + fprintf(stderr, "usage: %s [OPTIONS]...\n", argv[0]); + fprintf(stderr, " control the audio server\n"); + fprintf(stderr, "OPTIONS:\n"); + fprintf(stderr, " -l, --list list devices and their pins\n"); + fprintf(stderr, " -d, --device N set device index N as the current one\n"); + fprintf(stderr, " -p, --pin N set pin N as the current one\n"); + fprintf(stderr, " -v, --volume N set volume to N%%. if + or - is given, volume is relative to the current volume\n"); + fprintf(stderr, " -h, --help show this message and exit\n"); + return 0; + case '?': + fprintf(stderr, "invalid option %c\n", optopt); + fprintf(stderr, "see '%s --help' for usage\n", argv[0]); + return 1; + } + } + + if (optind >= argc) + { + fprintf(stderr, "missing operand\n"); + fprintf(stderr, "see '%s --help' for usage\n", argv[0]); + return 1; + } + + for (int i = optind; i < argc; i++) + { + printf("%s", dirname(argv[i])); + putchar(zero ? '\0' : '\n'); + } -int main(int argc, const char* argv[]) -{ - if (argc != 2) - return usage(argv[0], 1); - const char* result = dirname(const_cast(argv[1])); - printf("%s\n", result); return 0; }