tools

various tools I have been using throughout the years
Log | Files | Refs | README | LICENSE

mpd_add_to_queue.c (1450B)


      1 #define _POSIX_C_SOURCE 200809L
      2 
      3 #include "mpd_common.h"
      4 
      5 #include <string.h>
      6 #include <unistd.h>
      7 
      8 static void usage(FILE *file, const char *program) {
      9   fprintf(file, "Usage: %s [--verbose] [FILE|-]\n", program);
     10 }
     11 
     12 int main(int argc, char **argv) {
     13   bool verbose = false;
     14   const char *path = NULL;
     15   for (int i = 1; i < argc; i++) {
     16     if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
     17       usage(stdout, argv[0]);
     18       return 0;
     19     }
     20     if (strcmp(argv[i], "--verbose") == 0) {
     21       verbose = true;
     22     } else if (argv[i][0] == '-' && strcmp(argv[i], "-") != 0) {
     23       usage(stderr, argv[0]);
     24       return 2;
     25     } else if (path == NULL) {
     26       path = argv[i];
     27     } else {
     28       usage(stderr, argv[0]);
     29       return 2;
     30     }
     31   }
     32 
     33   bool close_input;
     34   FILE *input = mt_open_input(path, &close_input);
     35   if (input == NULL)
     36     return 1;
     37   if (path == NULL && isatty(STDIN_FILENO))
     38     fprintf(stderr, "Reading tracks from stdin; press Ctrl-D to finish.\n");
     39 
     40   struct mpd_connection *connection = mt_connect();
     41   if (connection == NULL) {
     42     if (close_input)
     43       fclose(input);
     44     return 1;
     45   }
     46   size_t count = 0U;
     47   bool ok = mt_add_queue_stream(connection, input, verbose, &count);
     48   if (close_input && fclose(input) != 0) {
     49     perror("mpd: close input");
     50     ok = false;
     51   }
     52   if (ok)
     53     printf("Added %zu track%s.\n", count, count == 1U ? "" : "s");
     54   mpd_connection_free(connection);
     55   return ok ? 0 : 1;
     56 }