- Add missing comments;

- Reimplement a more safer dirname;
- Implement a simple and safe tokenize function;
This commit is contained in:
Enrico Fraccaroli (Galfurian)
2023-06-23 14:37:24 -04:00
parent 394695784b
commit 7972ddeac7
19 changed files with 252 additions and 205 deletions
+1 -1
View File
@@ -43,4 +43,4 @@ typedef struct termios {
#define IEXTEN 0x00000400 ///< Enables implementation-defined input processing.
/// @brief Mask for extracting control values.
#define CTRL(x) (x & 037)
#define CTRL(x) ((x) & 037)
+7 -14
View File
@@ -5,20 +5,13 @@
#include "stddef.h"
// TODO: doxygen comment.
/// @brief
/// @param out
/// @param cur
/// @param sep
/// @param max
/// @result
int parse_path(char *out, char **cur, char sep, size_t max);
// TODO: doxygen comment.
/// @brief
/// @param path
/// @result
char *dirname(const char *path);
/// @brief Extracts the parent directory of the given path and saves it inside
/// the given buffer, e.g., from "/home/user/test.txt" it extracts "/home/user".
/// @param path the path we are parsing.
/// @param buffer the buffer where we save the directory name.
/// @param buflen the length of the buffer.
/// @return 1 if succesfull, or 0 if the buffer cannot contain the path.
int dirname(const char *path, char *buffer, size_t buflen);
// TODO: doxygen comment.
/// @brief
+9
View File
@@ -170,6 +170,15 @@ char *strtok(char *str, const char *delim);
/// should be NULL, and saveptr should be unchanged since the previous call.
char *strtok_r(char *str, const char *delim, char **saveptr);
/// @brief Parses the string using the separator, and at each call it saves the
/// parsed token in buffer. The pointer `string` will be modified.
/// @param string cursor used to parse the string, it will be modified.
/// @param separator the separator we are using.
/// @param buffer the buffer where we save the parsed token.
/// @param buflen the length of the buffer.
/// @return 1 if we still have things to parse, 0 if we finished parsing.
int tokenize(char **string, char separator, char *buffer, ssize_t buflen);
/// @brief Copies the values of num bytes from the location pointed by source
/// to the memory block pointed by destination.
/// @param dst Pointer to the destination array where the content is to be
+8 -4
View File
@@ -28,9 +28,11 @@
/// @return the position of the first zero bit.
static inline int find_first_zero(unsigned long value)
{
for (int i = 0; i < 32; ++i)
if (!bit_check(value, i))
for (int i = 0; i < 32; ++i) {
if (!bit_check(value, i)) {
return i;
}
}
return 0;
}
@@ -39,8 +41,10 @@ static inline int find_first_zero(unsigned long value)
/// @return the position of the first non-zero bit.
static inline int find_first_non_zero(unsigned long value)
{
for (int i = 0; i < 32; ++i)
if (bit_check(value, i))
for (int i = 0; i < 32; ++i) {
if (bit_check(value, i)) {
return i;
}
}
return 0;
}
+4 -2
View File
@@ -74,8 +74,9 @@ static inline int list_head_empty(const list_head *head)
static inline unsigned list_head_size(const list_head *head)
{
unsigned size = 0;
if (!list_head_empty(head))
if (!list_head_empty(head)) {
list_for_each_decl(it, head) size += 1;
}
return size;
}
@@ -191,7 +192,8 @@ static inline void list_head_swap(list_head *entry1, list_head *entry2)
{
list_head *pos = entry2->prev;
list_head_replace(entry1, entry2);
if (pos == entry1)
if (pos == entry1) {
pos = entry2;
}
list_head_insert_after(entry1, pos);
}
+24 -25
View File
@@ -14,35 +14,34 @@ typedef int (*list_head_compare)(const list_head *, const list_head *);
/// @brief -
/// @param list
/// @param compare
void list_head_sort(list_head *list, list_head_compare compare)
static inline void list_head_sort(list_head *list, list_head_compare compare)
{
list_head *current = NULL, *index = NULL, *next = NULL;
// Check whether list is empty
if (list_head_empty(list)) {
return;
}
// Keeps track if we need to restart the outer loop.
int restart = 0;
// Current will point to head
for (current = list->next; current->next != list;) {
// Save pointer to next.
next = current->next;
// Reset restart flag.
restart = 0;
// Index will point to node next to current
for (index = current->next; index != list; index = index->next) {
// If current's data is greater than index's data, swap the data of
// current and index
if (compare(current, index)) {
list_head_swap(index, current);
restart = 1;
if (!list_head_empty(list)) {
// Keeps track if we need to restart the outer loop.
int restart = 0;
// Current will point to head
for (current = list->next; current->next != list;) {
// Save pointer to next.
next = current->next;
// Reset restart flag.
restart = 0;
// Index will point to node next to current
for (index = current->next; index != list; index = index->next) {
// If current's data is greater than index's data, swap the data of
// current and index
if (compare(current, index)) {
list_head_swap(index, current);
restart = 1;
}
}
// Check if we need to restart.
if (restart) {
current = list->next;
} else {
current = next;
}
}
// Check if we need to restart.
if (restart)
current = list->next;
else
current = next;
}
}
+1 -2
View File
@@ -6,9 +6,8 @@
#pragma once
#include "sys/ipc.h"
#include "sys/types.h"
#include "stddef.h"
#include "sys/types.h"
#include "time.h"
/// The maximum size for a message text.
+1 -2
View File
@@ -6,9 +6,8 @@
#pragma once
#include "sys/types.h"
#include "sys/ipc.h"
#include "stddef.h"
#include "sys/ipc.h"
#include "time.h"
#define SEM_UNDO 0x1000 ///< Undo the operation on exit.
+1 -2
View File
@@ -5,10 +5,9 @@
#pragma once
#include "sys/ipc.h"
#include "sys/types.h"
#include "stddef.h"
#include "sys/ipc.h"
#include "time.h"
/// Data type for storing the number of attaches.
+1 -1
View File
@@ -6,8 +6,8 @@
#pragma once
#include "sys/types.h"
#include "sys/dirent.h"
#include "stddef.h"
#include "sys/dirent.h"
#define STDIN_FILENO 0 ///< Standard input.
#define STDOUT_FILENO 1 ///< Standard output.