-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.c
More file actions
102 lines (86 loc) · 2.08 KB
/
helpers.c
File metadata and controls
102 lines (86 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include "helpers.h"
char** split_string(char* str, char* delimeter) {
char** strings = malloc(10 * sizeof(char*));
char* substr;
substr = strtok(str, delimeter);
int i = 0;
while (substr != NULL)
{
strings[i] = substr;
substr = strtok(NULL, delimeter);
i++;
}
return strings;
}
char* get_timestamp() {
char* timestamp = malloc(sizeof(char)*10);
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
sprintf(timestamp, "%d:%d:%d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
return timestamp;
}
bool parse_cmd_line_args(int argc, char* argv[]) {
int option;
bool verbose = 0;
while ((option = getopt (argc, argv, "hv")) != -1)
switch (option) {
case 'h':
print_usage();
break;
case 'v':
verbose = 1;
break;
default:
print_usage();
}
return verbose;
}
void print_usage() {
fprintf(stderr, "Usage: oss [-v for verbose mode]\n");
exit(0);
}
void set_timer(int duration) {
struct itimerval value;
value.it_interval.tv_sec = duration;
value.it_interval.tv_usec = 0;
value.it_value = value.it_interval;
if (setitimer(ITIMER_REAL, &value, NULL) == -1) {
perror("setitimer");
exit(1);
}
}
bool event_occured(unsigned int pct_chance) {
unsigned int percent = (rand() % 100) + 1;
if (percent <= pct_chance) {
return 1;
}
else {
return 0;
}
}
unsigned int** create_array(int m, int n) {
unsigned int* values = calloc(m*n, sizeof(unsigned int));
unsigned int** rows = malloc(n*sizeof(unsigned int*));
for (int i=0; i<n; ++i)
{
rows[i] = values + i*m;
}
return rows;
}
void destroy_array(unsigned int** arr) {
free(*arr);
free(arr);
}
void print_and_write(char* str, FILE* fp) {
fputs(str, stdout);
fputs(str, fp);
}