Add searchUUIDByName function to retrieve UUID by name and surname; improve formatting in users.c

This commit is contained in:
2025-04-25 16:32:18 +02:00
parent e23ed280d8
commit b9a4b77a40
2 changed files with 64 additions and 21 deletions
+50 -19
View File
@@ -1,7 +1,6 @@
#include "users.h"
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
@@ -11,10 +10,8 @@
#include <termios.h>
#include <string.h>
uint16_t generateUUID
(
node_t *_head
)
uint16_t generateUUID(
node_t *_head)
{
uint16_t uuid = rand() % (UINT16_MAX - 1 + 1) + 1;
node_t *current = _head;
@@ -33,11 +30,9 @@ uint16_t generateUUID
return uuid;
}
node_t *searchPersonByUUID
(
node_t *searchPersonByUUID(
node_t *_head,
uint16_t _uuid
)
uint16_t _uuid)
{
node_t *current = _head;
while (current != NULL)
@@ -51,13 +46,51 @@ node_t *searchPersonByUUID
return NULL;
};
int addPersonToList
(
uint16_t *searchUUIDByName(
node_t *_head,
char *_name,
char *_surname)
{
int sum = 0;
uint16_t *uuids = malloc(sizeof(uint16_t));
node_t *current = _head;
if (_head == NULL)
{
return 0;
}
if (uuids == NULL)
{
puts("Fatal error: free memory is not sufficient for this operation");
exit(EXIT_FAILURE);
}
while (current != NULL)
{
if (strcmp(_name, current->user.name) == 0 && strcmp(_surname, current->user.surname) == 0)
{
sum++;
realloc(uuids, sum * sizeof(uint16_t));
if (uuids == NULL)
{
puts("Fatal error: free memory is not sufficient for this operation");
exit(EXIT_FAILURE);
}
uuids[sum - 1] = current->user.uuid;
}
}
if (sum == 0) return NULL;
return uuids;
}
int addPersonToList(
node_t **_head,
char *_name,
char *_surname,
uint32_t _department
)
uint32_t _department)
{
node_t *new_node = (node_t *)malloc(sizeof(node_t));
if (new_node == NULL)
@@ -107,10 +140,8 @@ int addPersonToList
return 1;
}
int addTimeEvent
(
node_t *_person
)
int addTimeEvent(
node_t *_person)
{
time_t current_time = time(0);
@@ -124,8 +155,8 @@ int addTimeEvent
_person->user.total += (current_time - _person->user.last_time_event);
}
_person->user.available = !_person->user.available;
_person->user.last_time_event = current_time;
_person->user.available = !_person->user.available;
_person->user.last_time_event = current_time;
return 1;
}