Refactor user management functions and improve memory handling in addPersonToList
This commit is contained in:
+62
-1
@@ -1 +1,62 @@
|
||||
#include "users.h"
|
||||
#include "users.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <termios.h>
|
||||
#include <string.h>
|
||||
|
||||
int addPersonToList(
|
||||
node_t **_head,
|
||||
char *_name,
|
||||
char *_surname,
|
||||
uint32_t _department)
|
||||
{
|
||||
node_t *new_node = (node_t *)malloc(sizeof(node_t));
|
||||
if (new_node == NULL)
|
||||
return 0;
|
||||
|
||||
if (!strlen(_name) || !strlen(_surname))
|
||||
{
|
||||
free(new_node);
|
||||
new_node = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
new_node->user.name = (char *)malloc(strlen(_name) + 1);
|
||||
new_node->user.surname = (char *)malloc(strlen(_surname) + 1);
|
||||
|
||||
if (new_node->user.name == NULL || new_node->user.surname == NULL)
|
||||
{
|
||||
free(new_node->user.name);
|
||||
free(new_node->user.surname);
|
||||
free(new_node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
strcpy(new_node->user.name, _name);
|
||||
strcpy(new_node->user.surname, _surname);
|
||||
new_node->user.department = _department;
|
||||
new_node->user.last_time_event = -255;
|
||||
new_node->user.total = 0;
|
||||
|
||||
new_node->next = NULL;
|
||||
|
||||
if (*_head == NULL)
|
||||
{
|
||||
*_head = new_node;
|
||||
return;
|
||||
}
|
||||
|
||||
node_t *current = *_head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
current->next = new_node;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user