Skip to content

Commit

Permalink
Merge pull request #10 from gyptazy/feature/9-add-default-path
Browse files Browse the repository at this point in the history
feature: Add default path of /etc/rc.conf when not using any arg
  • Loading branch information
scovl authored Dec 28, 2024
2 parents cc88ad0 + 9e54cfa commit e72f53c
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "config.h"
#include "validation_utils.h"
Expand All @@ -10,18 +11,30 @@

int process_line(char* line, int line_number);

int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s /path/to/rc.conf\n", argv[0]);
return EXIT_FAILURE;
}
int main(int argc, char *argv[]) {
const char *default_file = "/etc/rc.conf";
const char *file_path;

FILE* file = fopen(argv[1], "r");
if (!file) {
fprintf(stderr, "Failed to open file: %s\n", argv[1]);
perror("Error");
return EXIT_FAILURE;
}
if (argc == 1) {
file_path = default_file;
if (access(file_path, R_OK) != 0) {
fprintf(stderr, "Default file %s does not exist or is not readable.\n", file_path);
perror("Error");
return EXIT_FAILURE;
}
} else if (argc == 2) {
file_path = argv[1];
} else {
fprintf(stderr, "Usage: %s [optional:/path/to/rc.conf]\n", argv[0]);
return EXIT_FAILURE;
}

FILE *file = fopen(file_path, "r");
if (!file) {
fprintf(stderr, "Failed to open file: %s\n", file_path);
perror("Error");
return EXIT_FAILURE;
}

char line[MAX_LINE_LENGTH];
int line_number = 0;
Expand Down Expand Up @@ -69,3 +82,4 @@ int process_line(char* line, int line_number) {

return EXIT_FAILURE;
}

0 comments on commit e72f53c

Please sign in to comment.