reading tmp dirname

This commit is contained in:
bronku 2025-10-16 16:59:29 +02:00
parent dc96712a2d
commit c9f60d7ca0
3 changed files with 27 additions and 13 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
build build
tests tests
tmp

37
main.c
View file

@ -3,9 +3,13 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#define DEFAULT_FILE "tests/1.in"
#define DEFAULT_DIR "tmp"
#define DEFAULT_NN 100000 #define DEFAULT_NN 100000
#define DEFAULT_N 101 #define DEFAULT_N 101
#define DEFAULT_B 10 #define DEFAULT_B 10
@ -16,17 +20,20 @@
void print_usage() void print_usage()
{ {
printf("Options:\n"); printf("Options:\n");
printf(" -f [filename]\tRead/Generate records from [filename]\n"); printf(" -f [filename]\tRead/Generate records from [filename], default %s\n", DEFAULT_FILE);
printf(" -r\t randomly generate N records, and process them\n"); printf(" -d [dirname]\tSet the directory used for temporary files, default %s\n", DEFAULT_DIR);
printf(" -N [count]\tSet total number of records, default %d\n", DEFAULT_NN); printf(" -N [count]\tSet total number of records, default %d\n", DEFAULT_NN);
printf(" -n [count]\tSet blocking factor, default %d\n", DEFAULT_N); printf(" -n [count]\tSet blocking factor, default %d\n", DEFAULT_N);
printf(" -b [count]\tSet number of buffers, default %d\n", DEFAULT_B); printf(" -b [count]\tSet number of buffers, default %d\n", DEFAULT_B);
printf(" -r\t randomly generate N records\n");
printf(" -h\tShow this help message\n"); printf(" -h\tShow this help message\n");
} }
typedef struct typedef struct
{ {
FILE* input; FILE* input;
char* filename;
char* dirname;
bool generate_data; bool generate_data;
bool manual_input; bool manual_input;
int N; // number of records in a file int N; // number of records in a file
@ -41,17 +48,21 @@ int process_args(int argc, char** argv, options* opts)
return ERROR; return ERROR;
} }
opts->input = stdin; opts->input = stdin;
opts->filename = DEFAULT_FILE;
opts->dirname = DEFAULT_DIR;
opts->generate_data = false; opts->generate_data = false;
opts->manual_input = false; opts->manual_input = false;
opts->N = DEFAULT_NN; opts->N = DEFAULT_NN;
opts->n = DEFAULT_N; opts->n = DEFAULT_N;
opts->b = DEFAULT_B; opts->b = DEFAULT_B;
char* filename = NULL;
int opt; int opt;
while ((opt = getopt(argc, argv, "f:N:n:b:rhm")) != -1) { while ((opt = getopt(argc, argv, "f:d:N:n:b:rhm")) != -1) {
switch (opt) { switch (opt) {
case 'f': case 'f':
filename = optarg; opts->filename = optarg;
break;
case 'd':
opts->dirname = optarg;
break; break;
case 'r': case 'r':
opts->generate_data = true; opts->generate_data = true;
@ -67,6 +78,7 @@ int process_args(int argc, char** argv, options* opts)
break; break;
case 'm': case 'm':
opts->manual_input = true; opts->manual_input = true;
break;
case 'h': case 'h':
print_usage(); print_usage();
return END_PROGRAM; return END_PROGRAM;
@ -79,18 +91,18 @@ int process_args(int argc, char** argv, options* opts)
fprintf(stderr, "Incompatible arguments: mr, aborting\n"); fprintf(stderr, "Incompatible arguments: mr, aborting\n");
return ERROR; return ERROR;
} }
if (filename == NULL) { opts->input = fopen(opts->filename, "r+");
fprintf(stderr, "No file specified, aborting\n");
return ERROR;
}
opts->input = fopen(filename, "r+");
if (!opts->input) { if (!opts->input) {
opts->input = fopen(filename, "w+"); opts->input = fopen(opts->filename, "w+");
} }
if (!opts->input) { if (!opts->input) {
fprintf(stderr, "Error: Cannot open file '%s'\n", filename); fprintf(stderr, "Error: Cannot open file '%s'\n", opts->filename);
return ERROR; return ERROR;
} }
struct stat st = { 0 };
if (stat(opts->dirname, &st) == -1) {
mkdir(opts->dirname, 0777);
}
return SUCCESS; return SUCCESS;
} }
@ -159,6 +171,7 @@ int main(int argc, char** argv)
} }
if (opts.generate_data) { if (opts.generate_data) {
generate_file(&opts); generate_file(&opts);
return SUCCESS;
} }
if (opts.manual_input) { if (opts.manual_input) {
write_file(&opts); write_file(&opts);

View file

@ -16,7 +16,7 @@ tests:
mkdir -p tests mkdir -p tests
clean: clean:
rm -rf build tests rm -rf build tests tmp
run: build/main tests/1.in run: build/main tests/1.in
build/main -f tests/1.in | less build/main -f tests/1.in | less