diff options
author | Joursoir <chat@joursoir.net> | 2021-01-26 15:49:08 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-01-26 15:49:08 +0000 |
commit | c07ea4108bf3c1b55c7f454c86a4b72e0c0f8bc6 (patch) | |
tree | 0319782df78e8ca2fbdbfa3c1bbc2ea23c5903e4 | |
parent | 6f3da5401c971ab5c0def425dfaf3a4f9c8230fd (diff) | |
download | audio-tools-c07ea4108bf3c1b55c7f454c86a4b72e0c0f8bc6.tar.gz audio-tools-c07ea4108bf3c1b55c7f454c86a4b72e0c0f8bc6.tar.bz2 audio-tools-c07ea4108bf3c1b55c7f454c86a4b72e0c0f8bc6.zip |
handle command line arguments
-rw-r--r-- | parecord/parecord.c | 52 |
1 files 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 <stdio.h> #include <unistd.h> #include <stdlib.h> +#include <string.h> #include <fcntl.h> #include <signal.h> +#include <getopt.h> #include <errno.h> #include <pulse/simple.h> #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 +} |