Skip to content
Snippets Groups Projects
Commit 703feec3 authored by root's avatar root
Browse files

src

parent 23392df4
No related branches found
No related tags found
No related merge requests found
Pipeline #28164 passed
...@@ -5,14 +5,12 @@ ...@@ -5,14 +5,12 @@
extern "C" { extern "C" {
#endif #endif
// Define a structure to represent time intervals
struct timespec { struct timespec {
long tv_sec; long tv_sec; // Seconds
long tv_nsec; long tv_nsec; // Nanoseconds
}; };
struct timespec it_interval;
struct timespec it_value;
int nanosleep(const struct timespec *req, struct timespec *rem); int nanosleep(const struct timespec *req, struct timespec *rem);
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
int close(int fd) int close(int fd)
{ {
// Call the system call for close
int ret = syscall(__NR_close, fd); int ret = syscall(__NR_close, fd);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
int ftruncate(int fd, off_t length) int ftruncate(int fd, off_t length)
{ {
// Call the system call for ftruncate
int ret = syscall(__NR_ftruncate, fd, length); int ret = syscall(__NR_ftruncate, fd, length);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
off_t lseek(int fd, off_t offset, int whence) off_t lseek(int fd, off_t offset, int whence)
{ {
// Call the system call for lseek
int ret = syscall(__NR_lseek, fd, offset, whence); int ret = syscall(__NR_lseek, fd, offset, whence);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
int open(const char *filename, int flags, ...) int open(const char *filename, int flags, ...)
{ {
// Call the system call for open
int ret = syscall(__NR_open, filename, flags); int ret = syscall(__NR_open, filename, flags);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -2,13 +2,18 @@ ...@@ -2,13 +2,18 @@
#include <string.h> #include <string.h>
int puts(const char *str) { int puts(const char *str) {
// Calculate the length of the input string using strlen
size_t length = strlen(str); size_t length = strlen(str);
// Write the content of 'str' to the standard output
write(1, str, length); write(1, str, length);
// Write a newline character to move to the next line. // Write a newline character to move to the next line.
char newline = '\n'; char newline = '\n';
// Write a newline character to move to the next line
write(1, &newline, 1); write(1, &newline, 1);
// Return 0 to indicate successful execution
return 0; return 0;
} }
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
int truncate(const char *path, off_t length) int truncate(const char *path, off_t length)
{ {
// Call the system call for truncate
int ret = syscall(__NR_truncate, path, length); int ret = syscall(__NR_truncate, path, length);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -13,9 +13,10 @@ void *malloc(size_t size) ...@@ -13,9 +13,10 @@ void *malloc(size_t size)
// Use mmap to allocate memory // Use mmap to allocate memory
size_t* block = (size_t*)mmap(NULL, size + 4, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); size_t* block = (size_t*)mmap(NULL, size + 4, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
block[0] = size; block[0] = size;
// Verifing if the memory was allocated // Verifing if the memory was allocated
if (block == MAP_FAILED) { if (block == MAP_FAILED) {
errno = ENOMEM; errno = 12; // Set errno to indicate out of memory
return NULL; return NULL;
} }
...@@ -25,11 +26,14 @@ void *malloc(size_t size) ...@@ -25,11 +26,14 @@ void *malloc(size_t size)
void *calloc(size_t nmemb, size_t size) void *calloc(size_t nmemb, size_t size)
{ {
// Allocate memory using malloc
void* block = malloc(nmemb); void* block = malloc(nmemb);
// Check if 'malloc' returns NULL (an error indicator)
if (block == MAP_FAILED) { if (block == MAP_FAILED) {
return NULL; return NULL;
} }
// Initialize the block with size information // Initialize the block with size information
memset(block, 0, nmemb); memset(block, 0, nmemb);
...@@ -38,10 +42,15 @@ void *calloc(size_t nmemb, size_t size) ...@@ -38,10 +42,15 @@ void *calloc(size_t nmemb, size_t size)
void free(void *ptr) void free(void *ptr)
{ {
// Cast the 'ptr' as a pointer to size_t
size_t* ptr_new = (size_t*)(ptr); size_t* ptr_new = (size_t*)(ptr);
// Move the pointer back by 4 size_t elements to retrieve the size
ptr_new -= 4; ptr_new -= 4;
size_t size = ptr_new[0]; size_t size = ptr_new[0];
// Call 'munmap' to deallocate the memory block and the header
// Add 4 to the size to account for the memory allocated for the size
munmap((void*)ptr_new, size + 4); munmap((void*)ptr_new, size + 4);
} }
...@@ -52,12 +61,15 @@ void *realloc(void *ptr, size_t size) ...@@ -52,12 +61,15 @@ void *realloc(void *ptr, size_t size)
// Verifing if the memory was allocated // Verifing if the memory was allocated
if (block == MAP_FAILED) { if (block == MAP_FAILED) {
errno = ENOMEM; errno = 12; // Set errno to indicate out of memory
return NULL; return NULL;
} }
// Free the existing memory pointed to by 'ptr'
free(ptr); free(ptr);
// Update the 'ptr' to point to the newly allocated memory block
ptr = block; ptr = block;
// Initialize the block with size information
return ptr; return ptr;
} }
...@@ -68,13 +80,18 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size) ...@@ -68,13 +80,18 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size)
// Verifing if the memory was allocated // Verifing if the memory was allocated
if (block == MAP_FAILED) { if (block == MAP_FAILED) {
errno = ENOMEM; errno = 12; // Set errno to indicate out of memory
return NULL; return NULL;
} }
// Free the existing memory pointed to by 'ptr'
free(ptr); free(ptr);
memset(block, 0, size);
// Initialize the newly allocated memory block with zeros
memset(block, size, nmemb);
// Update the 'ptr' to point to the newly allocated memory block
ptr = block; ptr = block;
// Initialize the block with size information
return ptr; return ptr;
} }
...@@ -6,11 +6,13 @@ ...@@ -6,11 +6,13 @@
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{ {
// Check for bad files
if (fd != -1) { if (fd != -1) {
errno = 9; // Set errno to indicate a bad file descriptor errno = 9; // Set errno to indicate a bad file descriptor
return MAP_FAILED; return MAP_FAILED;
} }
// Call the syscall to map memory with the specified parameters
void *ret = (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); void *ret = (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
if (ret == (void *)-22) { if (ret == (void *)-22) {
...@@ -27,6 +29,7 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) ...@@ -27,6 +29,7 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
void *mremap(void *old_address, size_t old_size, size_t new_size, int flags) void *mremap(void *old_address, size_t old_size, size_t new_size, int flags)
{ {
// Declare a void pointer 'ret' to store the return value of the syscall
void *ret = (void *)syscall(__NR_mremap, old_address, old_size, new_size, flags); void *ret = (void *)syscall(__NR_mremap, old_address, old_size, new_size, flags);
if (ret == MAP_FAILED) { if (ret == MAP_FAILED) {
...@@ -38,11 +41,14 @@ void *mremap(void *old_address, size_t old_size, size_t new_size, int flags) ...@@ -38,11 +41,14 @@ void *mremap(void *old_address, size_t old_size, size_t new_size, int flags)
int munmap(void *addr, size_t length) int munmap(void *addr, size_t length)
{ {
// Call the syscall to unmap the specified memory region
int ret = syscall(__NR_munmap, addr, length); int ret = syscall(__NR_munmap, addr, length);
// Check if 'ret' is less than 0, which indicates an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
} }
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
#include <internal/syscall.h> #include <internal/syscall.h>
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) {
// Call the system call for nanosleep
int ret = syscall(__NR_nanosleep, rqtp, rmtp); int ret = syscall(__NR_nanosleep, rqtp, rmtp);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -4,16 +4,18 @@ ...@@ -4,16 +4,18 @@
#include <time.h> #include <time.h>
unsigned int sleep(unsigned int seconds){ unsigned int sleep(unsigned int seconds){
// Create a timespec structure 'req' to specify the requested sleep time
struct timespec req, rem; struct timespec req, rem;
req.tv_sec = (long)seconds; req.tv_sec = (long)seconds;
req.tv_nsec = 0; req.tv_nsec = 0;
// Perform nanosleep repeatedly until the requested sleep time is completed
while (nanosleep(&req, &rem) == -1) { while (nanosleep(&req, &rem) == -1) {
if (errno == EINTR) { if (errno == EINTR) {
// Sleep was interrupted; update the remaining time and continue. // Sleep was interrupted, update the remaining time and continue.
req = rem; req = rem;
} else { } else {
// An error occurred; return the remaining time. // An error occurred, return the remaining time.
return (unsigned int)rem.tv_sec; return (unsigned int)rem.tv_sec;
} }
} }
......
...@@ -6,11 +6,13 @@ ...@@ -6,11 +6,13 @@
int fstat(int fd, struct stat *st) int fstat(int fd, struct stat *st)
{ {
// Call the system call for fstat
int ret = syscall(__NR_fstat, fd, st); int ret = syscall(__NR_fstat, fd, st);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
int stat(const char *restrict path, struct stat *restrict buf) int stat(const char *restrict path, struct stat *restrict buf)
{ {
// Call the system call for stat
int ret = syscall(__NR_stat, path, buf); int ret = syscall(__NR_stat, path, buf);
// Check if the return value is negative, indicating an error
if (ret < 0) { if (ret < 0) {
errno = -ret; errno = -ret; // Set errno to an error given by the syscall
return -1; return -1; // Return -1 to signal failure
} }
return ret; return ret;
......
...@@ -4,20 +4,25 @@ ...@@ -4,20 +4,25 @@
char *strcpy(char *destination, const char *source) char *strcpy(char *destination, const char *source)
{ {
char *dest = destination; // Create a copy of the destination pointer for returning // Create a copy of the destination pointer forreturning
char *dest = destination;
// Copy the characters from 'source' to 'destination' until a null
// terminator is encountered
while (*source != '\0') { while (*source != '\0') {
*dest = *source; *dest = *source;
dest++; dest++;
source++; source++;
} }
*dest = '\0'; // Null-terminate the destination string
*dest = '\0'; // Null-terminate the string
return destination; return destination;
} }
char *strncpy(char *destination, const char *source, size_t len) char *strncpy(char *destination, const char *source, size_t len)
{ {
// Copy at most 'len' characters from 'source' to 'destination'
char *dest = destination; char *dest = destination;
while (len-- && *source != '\0') { while (len-- && *source != '\0') {
...@@ -25,8 +30,10 @@ char *strncpy(char *destination, const char *source, size_t len) ...@@ -25,8 +30,10 @@ char *strncpy(char *destination, const char *source, size_t len)
dest++; dest++;
source++; source++;
} }
if (len + 1 != 0) { if (len + 1 != 0) {
while (len--) { while (len--) {
// Fill the remaining space with null characters
*dest = '\0'; *dest = '\0';
dest++; dest++;
} }
...@@ -37,7 +44,9 @@ char *strncpy(char *destination, const char *source, size_t len) ...@@ -37,7 +44,9 @@ char *strncpy(char *destination, const char *source, size_t len)
char *strcat(char *destination, const char *source) char *strcat(char *destination, const char *source)
{ {
// Append the characters from 'source' to the end of 'destination'
char *dest = destination; char *dest = destination;
while (*dest != '\0') { while (*dest != '\0') {
dest++; dest++;
} }
...@@ -48,13 +57,16 @@ char *strcat(char *destination, const char *source) ...@@ -48,13 +57,16 @@ char *strcat(char *destination, const char *source)
source++; source++;
} }
*dest = '\0'; *dest = '\0'; // Null-terminate the concatenated string
return destination; return destination;
} }
char *strncat(char *destination, const char *source, size_t len) char *strncat(char *destination, const char *source, size_t len)
{ {
// Append at most 'len' characters from 'source' to the end of 'destination'
char *dest = destination; char *dest = destination;
while (*dest != '\0') { while (*dest != '\0') {
dest++; dest++;
} }
...@@ -65,11 +77,13 @@ char *strncat(char *destination, const char *source, size_t len) ...@@ -65,11 +77,13 @@ char *strncat(char *destination, const char *source, size_t len)
source++; source++;
} }
*dest = '\0'; *dest = '\0'; // Null-terminate the concatenated string
return destination; return destination;
} }
int max(int a, int b) { int max(int a, int b) {
// Return the maximum of two integers 'a' and 'b'
if (a > b) if (a > b)
return a; return a;
return b; return b;
...@@ -77,6 +91,7 @@ int max(int a, int b) { ...@@ -77,6 +91,7 @@ int max(int a, int b) {
int strcmp(const char *str1, const char *str2) int strcmp(const char *str1, const char *str2)
{ {
// Compare two strings 'str1' and 'str2' lexicographically
int count = 0; int count = 0;
char const *copystr1 = str1; char const *copystr1 = str1;
char const *copystr2 = str2; char const *copystr2 = str2;
...@@ -108,6 +123,8 @@ int strcmp(const char *str1, const char *str2) ...@@ -108,6 +123,8 @@ int strcmp(const char *str1, const char *str2)
int strncmp(const char *str1, const char *str2, size_t len) int strncmp(const char *str1, const char *str2, size_t len)
{ {
// Compare two strings 'str1' and 'str2' lexicographically
// considering only the first 'len' characters
int count = 0, copylen = len; int count = 0, copylen = len;
char const *copystr1 = str1; char const *copystr1 = str1;
char const *copystr2 = str2; char const *copystr2 = str2;
...@@ -139,6 +156,7 @@ int strncmp(const char *str1, const char *str2, size_t len) ...@@ -139,6 +156,7 @@ int strncmp(const char *str1, const char *str2, size_t len)
size_t strlen(const char *str) size_t strlen(const char *str)
{ {
// Calculate the length of a null-terminated string 'str'
size_t i = 0; size_t i = 0;
for (; *str != '\0'; str++, i++) for (; *str != '\0'; str++, i++)
...@@ -149,6 +167,7 @@ size_t strlen(const char *str) ...@@ -149,6 +167,7 @@ size_t strlen(const char *str)
char *strchr(const char *str, int c) char *strchr(const char *str, int c)
{ {
// Find the first occurrence of character 'c' in the string 'str'
while (*str != '\0') { while (*str != '\0') {
if (*str == c) { if (*str == c) {
return (char *)str; return (char *)str;
...@@ -161,8 +180,10 @@ char *strchr(const char *str, int c) ...@@ -161,8 +180,10 @@ char *strchr(const char *str, int c)
char *strrchr(const char *str, int c) char *strrchr(const char *str, int c)
{ {
// Find the last occurrence of character 'c' in the string 'str'
int len = strlen(str) - 1; int len = strlen(str) - 1;
str += strlen(str); str += strlen(str);
while (len--) { while (len--) {
if (*str == c) { if (*str == c) {
return (char *)str; return (char *)str;
...@@ -175,123 +196,170 @@ char *strrchr(const char *str, int c) ...@@ -175,123 +196,170 @@ char *strrchr(const char *str, int c)
char *strstr(const char *haystack, const char *needle) char *strstr(const char *haystack, const char *needle)
{ {
while (*haystack != '\0') { // Find the first occurrence of 'needle' in 'haystack'
if (*haystack == *needle) { // Loop through the 'haystack' string until the end is reached
const char *copyhaystack = haystack; while (*haystack != '\0') {
copyhaystack++; // Check if the current character in matches the first of 'needle'
const char *copyneedle = needle; if (*haystack == *needle) {
copyneedle++; // Create a copy of 'haystack' pointer
while (*copyneedle != '\0') { const char *copyhaystack = haystack;
if (*copyhaystack != *copyneedle) { copyhaystack++; // Move the copy of 'haystack' one character ahead
break;
} // Create a copy of 'needle' pointer
copyhaystack++; const char *copyneedle = needle;
copyneedle++; copyneedle++; // Move the copy of 'needle' one character ahead
}
if (*copyneedle == '\0') { // Start a loop to compare the characters
return (char *)haystack; while (*copyneedle != '\0') {
} // If the characters don't match, break out of the loop
} if (*copyhaystack != *copyneedle) {
haystack++; break;
} }
return NULL; copyhaystack++; // Move the 'copyhaystack' pointer
copyneedle++; // Move the 'copyneedle' pointer
}
// If we reached the end, return the starting position
if (*copyneedle == '\0') {
return (char *)haystack;
}
}
haystack++; // Move the 'haystack' pointer to the next character
}
// If no match was found, return NULL
return NULL;
} }
char *strrstr(const char *haystack, const char *needle) char *strrstr(const char *haystack, const char *needle)
{ {
int len1 = strlen(haystack) - 1; // Find the last occurrence of 'needle' in 'haystack'
int len2 = strlen(needle) - 1;
haystack += strlen(haystack) - 1; // Get the lengths of 'haystack' and 'needle'
needle += strlen(needle) - 1; int len1 = strlen(haystack) - 1;
int len2 = strlen(needle) - 1;
while (len1--) {
if (*haystack == *needle) { // Move the pointers to their respective ends
int copylen2 = len2; haystack += strlen(haystack) - 1;
const char *copyhaystack = haystack; needle += strlen(needle) - 1;
copyhaystack--;
const char *copyneedle = needle; // Start a loop to search for the last occurrence of 'needle' in 'haystack'
copyneedle--; while (len1--) {
// Check if the current character matches the last of 'needle'
while (copylen2--) { if (*haystack == *needle) {
if (*copyhaystack != *copyneedle) { // Create a copy of 'len2'
break; int copylen2 = len2;
}
copyhaystack--; // Create a copy of 'haystack' pointer
copyneedle--; const char *copyhaystack = haystack;
} copyhaystack--; // Move the 'copyhaystack' one character back
if (copylen2 == -1) { // Create a copy of 'needle' pointer
return (char *)++copyhaystack; const char *copyneedle = needle;
} copyneedle--; // Move the 'copyneedle' one character back
}
haystack--; // Start a loop to compare the characters
} while (copylen2--) {
// If the characters don't match, break out of the loop
if (*copyhaystack != *copyneedle) {
break;
}
copyhaystack--; // Move the 'copyhaystack' pointer back
copyneedle--; // Move the 'copyneedle' pointer back
}
// If we reached the beginning, return the starting position
if (copylen2 == -1) {
return (char *)++copyhaystack;
}
}
haystack--; // Move the 'haystack' pointer one character back
}
return NULL; // If no match was found, return NULL
return NULL;
} }
void *memcpy(void *destination, const void *source, size_t num) void *memcpy(void *destination, const void *source, size_t num)
{ {
char *dest = (char *)destination; // Copy 'num' bytes from 'source' to 'destination'
char *dest = (char *)destination;
const char *src = (const char *)source; const char *src = (const char *)source;
size_t i = 0; size_t i = 0;
while (i < num) {
dest[i] = src[i];
i++;
}
return destination; while (i < num) {
dest[i] = src[i];
i++;
}
return destination;
} }
void *memmove(void *destination, const void *source, size_t num) void *memmove(void *destination, const void *source, size_t num)
{ {
char *dest = (char *)destination; // Move 'num' bytes from 'source' to 'destination', handling potential overlap
char *dest = (char *)destination;
const char *src = (const char *)source; const char *src = (const char *)source;
size_t i = 0; size_t i = 0;
while (i < num) {
dest[i] = src[i];
i++;
}
return destination; while (i < num) {
dest[i] = src[i];
i++;
}
return destination;
} }
int memcmp(const void *ptr1, const void *ptr2, size_t num) int memcmp(const void *ptr1, const void *ptr2, size_t num)
{ {
int count = 0, copylen = num; // Compare 'num' bytes in memory pointed to by 'ptr1' and 'ptr2'
char const *copystr1 = (const char *)ptr1; int count = 0;
char const *copystr2 = (const char *)ptr2; int copylen = num;
while (*copystr1 != '\0' && *copystr2 != '\0' && num--) {
if (*copystr1 != *copystr2) { // Create character pointers to 'ptr1' and 'ptr2'
if (*copystr1 > *copystr2) { const char *copystr1 = (const char *)ptr1;
return 1; const char *copystr2 = (const char *)ptr2;
} else {
return -1; // Start a loop to compare 'num' bytes in memory
} while (*copystr1 != '\0' && *copystr2 != '\0' && num--) {
break; // Compare the characters in 'copystr1' and 'copystr2'
} if (*copystr1 != *copystr2) {
copystr1++; // If they don't match, return 1 if 'copystr1' is greater
copystr2++; // return -1 if 'copystr2' is greater
count++; if (*copystr1 > *copystr2) {
} return 1;
} else {
return -1;
}
break; // Break the loop as soon as a difference is found
}
copystr1++;
copystr2++;
count++;
}
if (count == copylen) { // If the loop completed without finding a difference, the blocks are equal
return 0; if (count == copylen) {
} return 0;
}
if (strlen((const char *)ptr1) > strlen((const char *)ptr2)) { // If the lengths of the two blocks are different
return 1; // return 1 if the first block is longer, -1 if the second block is longer
} if (strlen((const char *)ptr1) > strlen((const char *)ptr2)) {
return -1; return 1;
}
return -1;
} }
void *memset(void *source, int value, size_t num) void *memset(void *source, int value, size_t num)
{ {
char *src = (char *)source; // Set 'num' bytes in memory pointed to by 'source' to the specified 'value'
while (num--) { char *src = (char *)source;
*src = value;
src++; while (num--) {
} *src = value;
src++;
}
return source; return source;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment