-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.c
66 lines (49 loc) · 1005 Bytes
/
users.c
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
/*
* Copyright (c) 2024, <>()
*/
#include "users.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char *db_path = "users.db";
static char **users;
static uint16_t users_number;
void init_users(void)
{
FILE *users_db = fopen(db_path, "r");
if (!users_db) {
perror("Error reading users.db");
return;
}
fscanf(users_db, "%hu", &users_number);
users = malloc(users_number * sizeof(char *));
char temp[32];
for (uint16_t i = 0; i < users_number; i++) {
fscanf(users_db, "%s", temp);
int size = strlen(temp);
users[i] = malloc(size + 1);
strcpy(users[i], temp);
}
fclose(users_db);
}
uint16_t get_user_id(char *name)
{
if (!users)
return -1;
for (uint16_t i = 0; i < users_number; i++)
if (!strcmp(users[i], name))
return i;
return -1;
}
char *get_user_name(uint16_t id)
{
if (id >= users_number)
return NULL;
return users[id];
}
void free_users(void)
{
for (size_t i = 0; i < users_number; i++)
free(users[i]);
free(users);
}