From c07ea4108bf3c1b55c7f454c86a4b72e0c0f8bc6 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Tue, 26 Jan 2021 15:49:08 +0000 Subject: handle command line arguments --- parecord/parecord.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/parecord/parecord.c b/parecord/parecord.c index 5ce886d..6588d2e 100644 --- a/parecord/parecord.c +++ b/parecord/parecord.c @@ -1,14 +1,25 @@ #include #include #include +#include #include #include +#include #include #include #define BUFSIZE 1024 volatile sig_atomic_t flag_do = 1; +enum audio_format { + none, // = 0 + wav // = 1 +}; + +static const char *correct_formats[] = { + "wav", NULL +}; + void handler(int s) { signal(SIGINT, handler); @@ -43,10 +54,43 @@ int main(int argc, char *argv[]) pa_simple *connection; pa_sample_spec specification; int fd_output = STDOUT_FILENO; - - if(argv[1] != NULL) { + int result, i; + enum audio_format file_format = none; + const struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"format", required_argument, NULL, 'f'}, + {"formats", no_argument, NULL, 'F'}, + {NULL, 0, NULL, 0} + }; + + while((result = getopt_long(argc, argv, "hf:F", long_options, NULL)) != -1) { + switch(result) { + case 'h': { + // print help + break; + } + case 'f': { + for(i = 0; correct_formats[i] != NULL; i++) { + if(strcmp(optarg, correct_formats[i]) == 0) { + file_format = i+1; + break; + } + } + + if(file_format != none) break; + // else print formats (below) + } + case 'F': { + // print formats + break; + } + default: break; + } + } + + if(argv[optind] != NULL) { mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; - fd_output = open(argv[1], O_WRONLY | O_CREAT| O_TRUNC, mode); + fd_output = open(argv[optind], O_WRONLY | O_CREAT| O_TRUNC, mode); if(fd_output == -1) { perror("[Error] open"); exit(1); @@ -92,4 +136,4 @@ int main(int argc, char *argv[]) pa_simple_free(connection); return 0; -} \ No newline at end of file +} -- cgit v1.2.3-18-g5258