BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1#include "utils.h"
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <stdarg.h>
6#include <stddef.h>
7#include <stdint.h>
8#include <time.h>
9#include <string.h>
10#include <unistd.h>
11#include <sys/types.h>
12#include <sys/file.h>
13#include <sys/stat.h>
14#include <arpa/inet.h>
15#include <netinet/in.h>
16
17void get_timestamp(char* buffer, size_t buffer_size)
18{
19 time_t now = time(NULL);
20 struct tm tm_now;
21 localtime_r(&now, &tm_now);
22 strftime(buffer, buffer_size, "%Y%m%d%H%M%S", &tm_now);
23}
24
25void get_formatted_timestamp(char* buffer, size_t buffer_size)
26{
27 time_t now = time(NULL);
28 struct tm tm_now;
29 localtime_r(&now, &tm_now);
30 strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", &tm_now);
31}
32
34{
35 guarded_print("\033[H\033[J");
36}
37
39{
40 const char* home = getenv("HOME");
41 if (home == NULL)
42 return 1;
43 const char* suffix = "/.bitlab";
44 char* init_dir = malloc(strlen(home) + strlen(suffix) + 1);
45 if (init_dir == NULL)
46 return 1;
47 strcpy(init_dir, home);
48 strcat(init_dir, suffix);
49
50 mkdir(init_dir, 0700);
51 free(init_dir);
52 return 0;
53}
54
55void guarded_print(const char* format, ...)
56{
57 flockfile(stdout);
58 va_list args;
59 va_start(args, format);
60 vprintf(format, args);
61 va_end(args);
62 funlockfile(stdout);
63}
64
65void guarded_print_line(const char* format, ...)
66{
67 flockfile(stdout);
68 va_list args;
69 va_start(args, format);
70 vprintf(format, args);
71 va_end(args);
72 printf("\n");
73 funlockfile(stdout);
74}
75
76uint64_t ntohll(uint64_t value)
77{
78 if (__BYTE_ORDER == __LITTLE_ENDIAN)
79 {
80 return ((uint64_t)ntohl(value & 0xFFFFFFFF) << 32) | ntohl(value >> 32);
81 }
82 else
83 {
84 return value;
85 }
86}
87
88uint64_t read_var_int(const unsigned char* data, size_t* offset)
89{
90 uint64_t result = 0;
91 unsigned char first_byte = data[*offset];
92 *offset += 1;
93
94 if (first_byte < 0xfd)
95 {
96 result = first_byte;
97 }
98 else if (first_byte == 0xfd)
99 {
100 result = *(uint16_t*)(data + *offset);
101 *offset += 2;
102 }
103 else if (first_byte == 0xfe)
104 {
105 result = *(uint32_t*)(data + *offset);
106 *offset += 4;
107 }
108 else if (first_byte == 0xff)
109 {
110 result = *(uint64_t*)(data + *offset);
111 *offset += 8;
112 }
113 return result;
114}
115
116int is_valid_ipv4(const char* ip_str)
117{
118 struct sockaddr_in sa;
119 return inet_pton(AF_INET, ip_str, &(sa.sin_addr)) == 1;
120}
uint64_t ntohll(uint64_t value)
Convert a 64-bit integer from host byte order to network byte order.
Definition utils.c:76
void get_formatted_timestamp(char *buffer, size_t buffer_size)
Get the formatted timestamp.
Definition utils.c:25
void clear_cli()
Clear the CLI window.
Definition utils.c:33
int is_valid_ipv4(const char *ip_str)
Check if the IP address is valid.
Definition utils.c:116
void get_timestamp(char *buffer, size_t buffer_size)
Get the timestamp.
Definition utils.c:17
uint64_t read_var_int(const unsigned char *data, size_t *offset)
Definition utils.c:88
void guarded_print_line(const char *format,...)
Guarded print line function.
Definition utils.c:65
void guarded_print(const char *format,...)
Guarded print function.
Definition utils.c:55
int init_config_dir()
Initialize the configuration directory.
Definition utils.c:38
struct tm * localtime_r(const time_t *timer, struct tm *buf)
void flockfile(FILE *filehandle)
void funlockfile(FILE *file)