Skip to content
Snippets Groups Projects
Commit 63c2201d authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Added argument parser

parent 419e0e73
No related branches found
No related tags found
No related merge requests found
#include "parser/parser.h" #include "parser/parser.h"
#include "ultra-cp/ultra-cp.h" #include "ultra-cp/ultra-cp.h"
#include <ctype.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
printf("aflag = %d\n", get_opt(argc, argv, "af")); // Arguments parsing
int source_count = 0;
uint8_t opt = get_opt(argc, argv, "af", &source_count);
char source_path[PATH_MAX]; // Destination parsing
char *destination_path = argv[argc - 1];
// Source parsing
char **source_paths = malloc(source_count * sizeof(char *));
for (int i = 0; i < source_count; i++) {
source_paths[i] = malloc(PATH_MAX * sizeof(char));
strcpy(source_paths[i], argv[optind - source_count + i]);
// Trim ending slash if present
if (source_paths[i][strlen(source_paths[i]) - 1] == '/')
source_paths[i][strlen(source_paths[i]) - 1] = '\0';
}
// TODO: Copy files form sources to destination
// Free sources
for (int i = 0; i < source_count; i++)
free(source_paths[i]);
free(source_paths);
list_dir("/home/toguy/Documents/source");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
#include "parser.h" #include "parser.h"
uint8_t get_opt(int argc, char **argv, char *args) { uint8_t get_opt(int argc, char **argv, char *args, int *source_count) {
uint8_t opt = 0b00; uint8_t opt = 0b00;
// Parse optional arguments
int c; int c;
while ((c = getopt(argc, argv, args)) != -1) { while ((c = getopt(argc, argv, args)) != -1) {
switch (c) { switch (c) {
...@@ -19,5 +20,9 @@ uint8_t get_opt(int argc, char **argv, char *args) { ...@@ -19,5 +20,9 @@ uint8_t get_opt(int argc, char **argv, char *args) {
} }
} }
// Arguments which was not parsed by getopt
for (; optind < argc - 1; optind++)
(*source_count)++;
return opt; return opt;
} }
...@@ -10,9 +10,10 @@ ...@@ -10,9 +10,10 @@
* @param argc Arg count * @param argc Arg count
* @param argv Arg values * @param argv Arg values
* @param args Arg expected * @param args Arg expected
* @param source_count Number of source files
* *
* @returns Options * @returns Options
*/ */
uint8_t get_opt(int argc, char **argv, char *args); uint8_t get_opt(int argc, char **argv, char *args, int *source_count);
#endif // !PARSER_H_ #endif // !PARSER_H_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment