aoc/src/advent_utility.c

72 lines
1.5 KiB
C
Raw Normal View History

#include <stdlib.h>
2021-09-08 12:12:11 -07:00
#include <string.h>
#include <stdio.h>
2021-09-18 10:59:09 -07:00
2022-09-09 11:13:53 -07:00
#if defined __linux__ || defined __APPLE__
2021-09-18 10:59:09 -07:00
#include <openssl/md5.h>
#else
2021-09-08 12:12:11 -07:00
#include <md5.h>
2021-09-18 10:59:09 -07:00
#endif
#include "advent_utility.h"
struct Point new_Point(int x, int y) {
struct Point p;
p.x = x;
p.y = y;
return p;
}
struct Point *new_Point_p(int x, int y) {
struct Point *p = malloc(sizeof(struct Point));
p->x = x;
p->y = y;
return p;
}
2021-09-18 10:59:09 -07:00
int same_Point(const struct Point *a, const struct Point *b) {
if (a->x == b->x && a->y == b->y) {
return 1;
}
return 0;
}
int same_Point_v(const void *a, const void *b) {
2021-09-18 10:59:09 -07:00
return same_Point((const struct Point *) a, (const struct Point *) b);
}
2021-09-08 12:12:11 -07:00
2021-09-18 10:59:09 -07:00
char *md5_str(const char *input) {
unsigned char digest[16];
2022-09-09 11:13:53 -07:00
#if defined __linux__ || defined __APPLE__
2021-09-18 10:59:09 -07:00
MD5_CTX context;
MD5_Init(&context);
MD5_Update(&context, input, strlen(input));
MD5_Final(digest, &context);
#else
struct MD5Context context;
MD5Init(&context);
MD5Update(&context, input, strlen(input));
MD5Final(digest, &context);
#endif
char *md5string = malloc(33);
for (int i = 0; i < 16; ++i) {
sprintf(&md5string[i * 2], "%02x", (unsigned int) digest[i]);
}
return md5string;
}
char *capture_system(const char *cmd) {
char *buf = malloc(256);
FILE *tmp = popen(cmd, "r");
if (tmp == NULL) {
printf("failed to open FILE *tmp\n");
exit(1);
}
fgets(buf, 256, tmp);
return buf;
2021-09-08 12:12:11 -07:00
}