Skip to content

Commit 7dcf109

Browse files
author
Michał Śledź
committed
Set default file name to unix timestamp. Set default time to 60 seconds.
1 parent 3613a1d commit 7dcf109

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

main.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string.h>
44
#include <unistd.h>
55
#include <argp.h>
6+
#include <time.h>
67

78
const char *argp_program_version = "system_info 0.1.0";
89
const char *argp_program_bug_address = "michalsledz34@gmail.com";
@@ -71,16 +72,21 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
7172
static struct argp argp = {options, parse_opt, 0, doc};
7273

7374
int main(int argc, char **argv) {
75+
unsigned long timestamp = (unsigned long)time(NULL);
76+
int timestamp_len = snprintf(NULL, 0, "%lu", timestamp);
77+
char *file_name = (char *)malloc(timestamp_len + 1 + 4); // +1 for NULL byte and +4 for .csv extenstion
78+
snprintf(file_name, timestamp_len + 1 + 4, "%lu.csv", timestamp);
79+
7480
struct arguments arguments;
75-
arguments.file = "cpu_load.csv";
81+
arguments.file = file_name;
7682
arguments.interval = 1;
77-
arguments.time = 10;
83+
arguments.time = 60;
7884
argp_parse(&argp, argc, argv, 0, 0, &arguments);
7985

8086
print_args(arguments);
8187

8288
unsigned int iterations = arguments.time / arguments.interval;
83-
printf("Iterations: %d\n", iterations);
89+
fprintf(stdout, "Iterations: %d\n", iterations);
8490
struct cpu_info last_cpu_info;
8591
struct cpu_info current_cpu_info;
8692
FILE *stat;
@@ -123,6 +129,7 @@ int main(int argc, char **argv) {
123129

124130
save_to_csv(arguments.file, cpu_loads, iterations);
125131
free(cpu_loads);
132+
free(file_name);
126133
return 0;
127134
}
128135

0 commit comments

Comments
 (0)