From 703feec3120f6ef63d61f9c00ee0c95c41c18eef Mon Sep 17 00:00:00 2001 From: root <alexandru.bologan> Date: Thu, 26 Oct 2023 21:32:06 +0300 Subject: [PATCH] src --- src/include/time.h | 8 +- src/io/close.c | 6 +- src/io/ftruncate.c | 6 +- src/io/lseek.c | 6 +- src/io/open.c | 6 +- src/io/puts.c | 5 + src/io/truncate.c | 6 +- src/mm/malloc.c | 29 ++++- src/mm/mmap.c | 10 +- src/process/nanosleep.c | 6 +- src/process/sleep.c | 6 +- src/stat/fstat.c | 6 +- src/stat/stat.c | 6 +- src/string/string.c | 258 +++++++++++++++++++++++++--------------- 14 files changed, 238 insertions(+), 126 deletions(-) diff --git a/src/include/time.h b/src/include/time.h index cf81c91..a35b9ec 100644 --- a/src/include/time.h +++ b/src/include/time.h @@ -5,14 +5,12 @@ extern "C" { #endif +// Define a structure to represent time intervals struct timespec { - long tv_sec; - long tv_nsec; + long tv_sec; // Seconds + long tv_nsec; // Nanoseconds }; -struct timespec it_interval; -struct timespec it_value; - int nanosleep(const struct timespec *req, struct timespec *rem); #ifdef __cplusplus diff --git a/src/io/close.c b/src/io/close.c index c18d1ab..94907ba 100644 --- a/src/io/close.c +++ b/src/io/close.c @@ -7,11 +7,13 @@ int close(int fd) { + // Call the system call for close int ret = syscall(__NR_close, fd); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/io/ftruncate.c b/src/io/ftruncate.c index f679184..57305e5 100644 --- a/src/io/ftruncate.c +++ b/src/io/ftruncate.c @@ -7,11 +7,13 @@ int ftruncate(int fd, off_t length) { + // Call the system call for ftruncate int ret = syscall(__NR_ftruncate, fd, length); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/io/lseek.c b/src/io/lseek.c index f0c0fa0..c06d5ae 100644 --- a/src/io/lseek.c +++ b/src/io/lseek.c @@ -7,11 +7,13 @@ off_t lseek(int fd, off_t offset, int whence) { + // Call the system call for lseek int ret = syscall(__NR_lseek, fd, offset, whence); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/io/open.c b/src/io/open.c index 437d892..9c5d6da 100644 --- a/src/io/open.c +++ b/src/io/open.c @@ -7,11 +7,13 @@ int open(const char *filename, int flags, ...) { + // Call the system call for open int ret = syscall(__NR_open, filename, flags); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/io/puts.c b/src/io/puts.c index 4be21cb..1104f14 100644 --- a/src/io/puts.c +++ b/src/io/puts.c @@ -2,13 +2,18 @@ #include <string.h> int puts(const char *str) { + // Calculate the length of the input string using strlen size_t length = strlen(str); + // Write the content of 'str' to the standard output write(1, str, length); // Write a newline character to move to the next line. char newline = '\n'; + + // Write a newline character to move to the next line write(1, &newline, 1); + // Return 0 to indicate successful execution return 0; } diff --git a/src/io/truncate.c b/src/io/truncate.c index 1e5d52a..92ca37c 100644 --- a/src/io/truncate.c +++ b/src/io/truncate.c @@ -7,11 +7,13 @@ int truncate(const char *path, off_t length) { + // Call the system call for truncate int ret = syscall(__NR_truncate, path, length); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/mm/malloc.c b/src/mm/malloc.c index a85e228..296e933 100644 --- a/src/mm/malloc.c +++ b/src/mm/malloc.c @@ -13,9 +13,10 @@ void *malloc(size_t size) // Use mmap to allocate memory size_t* block = (size_t*)mmap(NULL, size + 4, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); block[0] = size; + // Verifing if the memory was allocated if (block == MAP_FAILED) { - errno = ENOMEM; + errno = 12; // Set errno to indicate out of memory return NULL; } @@ -25,11 +26,14 @@ void *malloc(size_t size) void *calloc(size_t nmemb, size_t size) { + // Allocate memory using malloc void* block = malloc(nmemb); + // Check if 'malloc' returns NULL (an error indicator) if (block == MAP_FAILED) { return NULL; } + // Initialize the block with size information memset(block, 0, nmemb); @@ -38,10 +42,15 @@ void *calloc(size_t nmemb, size_t size) void free(void *ptr) { + // Cast the 'ptr' as a pointer to size_t size_t* ptr_new = (size_t*)(ptr); + + // Move the pointer back by 4 size_t elements to retrieve the size ptr_new -= 4; 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); } @@ -52,12 +61,15 @@ void *realloc(void *ptr, size_t size) // Verifing if the memory was allocated if (block == MAP_FAILED) { - errno = ENOMEM; + errno = 12; // Set errno to indicate out of memory return NULL; } + + // Free the existing memory pointed to by 'ptr' free(ptr); + + // Update the 'ptr' to point to the newly allocated memory block ptr = block; - // Initialize the block with size information return ptr; } @@ -68,13 +80,18 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size) // Verifing if the memory was allocated if (block == MAP_FAILED) { - errno = ENOMEM; + errno = 12; // Set errno to indicate out of memory return NULL; } + + // Free the existing memory pointed to by '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; - // Initialize the block with size information return ptr; } diff --git a/src/mm/mmap.c b/src/mm/mmap.c index ed0e501..3bf7432 100644 --- a/src/mm/mmap.c +++ b/src/mm/mmap.c @@ -6,11 +6,13 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { + // Check for bad files if (fd != -1) { errno = 9; // Set errno to indicate a bad file descriptor 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); if (ret == (void *)-22) { @@ -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) { + // 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); if (ret == MAP_FAILED) { @@ -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) { + // Call the syscall to unmap the specified memory region int ret = syscall(__NR_munmap, addr, length); + // Check if 'ret' is less than 0, which indicates an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } + return ret; } diff --git a/src/process/nanosleep.c b/src/process/nanosleep.c index 87b719b..61acd9c 100644 --- a/src/process/nanosleep.c +++ b/src/process/nanosleep.c @@ -3,11 +3,13 @@ #include <internal/syscall.h> int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { + // Call the system call for nanosleep int ret = syscall(__NR_nanosleep, rqtp, rmtp); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/process/sleep.c b/src/process/sleep.c index 8a336d6..c448419 100644 --- a/src/process/sleep.c +++ b/src/process/sleep.c @@ -4,16 +4,18 @@ #include <time.h> unsigned int sleep(unsigned int seconds){ + // Create a timespec structure 'req' to specify the requested sleep time struct timespec req, rem; req.tv_sec = (long)seconds; req.tv_nsec = 0; + // Perform nanosleep repeatedly until the requested sleep time is completed while (nanosleep(&req, &rem) == -1) { if (errno == EINTR) { - // Sleep was interrupted; update the remaining time and continue. + // Sleep was interrupted, update the remaining time and continue. req = rem; } else { - // An error occurred; return the remaining time. + // An error occurred, return the remaining time. return (unsigned int)rem.tv_sec; } } diff --git a/src/stat/fstat.c b/src/stat/fstat.c index 784ef3d..bab44d7 100644 --- a/src/stat/fstat.c +++ b/src/stat/fstat.c @@ -6,11 +6,13 @@ int fstat(int fd, struct stat *st) { + // Call the system call for fstat int ret = syscall(__NR_fstat, fd, st); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/stat/stat.c b/src/stat/stat.c index 3563b2c..8630965 100644 --- a/src/stat/stat.c +++ b/src/stat/stat.c @@ -7,11 +7,13 @@ int stat(const char *restrict path, struct stat *restrict buf) { + // Call the system call for stat int ret = syscall(__NR_stat, path, buf); + // Check if the return value is negative, indicating an error if (ret < 0) { - errno = -ret; - return -1; + errno = -ret; // Set errno to an error given by the syscall + return -1; // Return -1 to signal failure } return ret; diff --git a/src/string/string.c b/src/string/string.c index 01abd9a..80edb90 100755 --- a/src/string/string.c +++ b/src/string/string.c @@ -4,20 +4,25 @@ 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') { *dest = *source; dest++; source++; } - *dest = '\0'; // Null-terminate the destination string + + *dest = '\0'; // Null-terminate the string return destination; } char *strncpy(char *destination, const char *source, size_t len) { + // Copy at most 'len' characters from 'source' to 'destination' char *dest = destination; while (len-- && *source != '\0') { @@ -25,8 +30,10 @@ char *strncpy(char *destination, const char *source, size_t len) dest++; source++; } + if (len + 1 != 0) { while (len--) { + // Fill the remaining space with null characters *dest = '\0'; dest++; } @@ -37,7 +44,9 @@ char *strncpy(char *destination, const char *source, size_t len) char *strcat(char *destination, const char *source) { + // Append the characters from 'source' to the end of 'destination' char *dest = destination; + while (*dest != '\0') { dest++; } @@ -48,13 +57,16 @@ char *strcat(char *destination, const char *source) source++; } - *dest = '\0'; + *dest = '\0'; // Null-terminate the concatenated string + return destination; } 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; + while (*dest != '\0') { dest++; } @@ -65,11 +77,13 @@ char *strncat(char *destination, const char *source, size_t len) source++; } - *dest = '\0'; + *dest = '\0'; // Null-terminate the concatenated string + return destination; } int max(int a, int b) { + // Return the maximum of two integers 'a' and 'b' if (a > b) return a; return b; @@ -77,6 +91,7 @@ int max(int a, int b) { int strcmp(const char *str1, const char *str2) { + // Compare two strings 'str1' and 'str2' lexicographically int count = 0; char const *copystr1 = str1; char const *copystr2 = str2; @@ -108,6 +123,8 @@ int strcmp(const char *str1, const char *str2) 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; char const *copystr1 = str1; char const *copystr2 = str2; @@ -139,6 +156,7 @@ int strncmp(const char *str1, const char *str2, size_t len) size_t strlen(const char *str) { + // Calculate the length of a null-terminated string 'str' size_t i = 0; for (; *str != '\0'; str++, i++) @@ -149,6 +167,7 @@ size_t strlen(const char *str) char *strchr(const char *str, int c) { + // Find the first occurrence of character 'c' in the string 'str' while (*str != '\0') { if (*str == c) { return (char *)str; @@ -161,8 +180,10 @@ char *strchr(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; str += strlen(str); + while (len--) { if (*str == c) { return (char *)str; @@ -175,123 +196,170 @@ char *strrchr(const char *str, int c) char *strstr(const char *haystack, const char *needle) { - while (*haystack != '\0') { - if (*haystack == *needle) { - const char *copyhaystack = haystack; - copyhaystack++; - const char *copyneedle = needle; - copyneedle++; - while (*copyneedle != '\0') { - if (*copyhaystack != *copyneedle) { - break; - } - copyhaystack++; - copyneedle++; - } - if (*copyneedle == '\0') { - return (char *)haystack; - } - } - haystack++; - } - return NULL; + // Find the first occurrence of 'needle' in 'haystack' + // Loop through the 'haystack' string until the end is reached + while (*haystack != '\0') { + // Check if the current character in matches the first of 'needle' + if (*haystack == *needle) { + // Create a copy of 'haystack' pointer + const char *copyhaystack = haystack; + copyhaystack++; // Move the copy of 'haystack' one character ahead + + // Create a copy of 'needle' pointer + const char *copyneedle = needle; + copyneedle++; // Move the copy of 'needle' one character ahead + + // Start a loop to compare the characters + while (*copyneedle != '\0') { + // If the characters don't match, break out of the loop + if (*copyhaystack != *copyneedle) { + break; + } + 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) { - int len1 = strlen(haystack) - 1; - int len2 = strlen(needle) - 1; - haystack += strlen(haystack) - 1; - needle += strlen(needle) - 1; - - while (len1--) { - if (*haystack == *needle) { - int copylen2 = len2; - const char *copyhaystack = haystack; - copyhaystack--; - const char *copyneedle = needle; - copyneedle--; - - while (copylen2--) { - if (*copyhaystack != *copyneedle) { - break; - } - copyhaystack--; - copyneedle--; - } - - if (copylen2 == -1) { - return (char *)++copyhaystack; - } - } - haystack--; - } + // Find the last occurrence of 'needle' in 'haystack' + + // Get the lengths of 'haystack' and 'needle' + int len1 = strlen(haystack) - 1; + int len2 = strlen(needle) - 1; + + // Move the pointers to their respective ends + haystack += strlen(haystack) - 1; + needle += strlen(needle) - 1; + + // Start a loop to search for the last occurrence of 'needle' in 'haystack' + while (len1--) { + // Check if the current character matches the last of 'needle' + if (*haystack == *needle) { + // Create a copy of 'len2' + int copylen2 = len2; + + // Create a copy of 'haystack' pointer + const char *copyhaystack = haystack; + copyhaystack--; // Move the 'copyhaystack' one character back + + // Create a copy of 'needle' pointer + const char *copyneedle = needle; + copyneedle--; // Move the 'copyneedle' one character back + + // 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) { - char *dest = (char *)destination; + // Copy 'num' bytes from 'source' to 'destination' + char *dest = (char *)destination; const char *src = (const char *)source; - size_t i = 0; - while (i < num) { - dest[i] = src[i]; - i++; - } + size_t i = 0; - return destination; + while (i < num) { + dest[i] = src[i]; + i++; + } + + return destination; } 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; - size_t i = 0; - while (i < num) { - dest[i] = src[i]; - i++; - } + size_t i = 0; - return destination; + while (i < num) { + dest[i] = src[i]; + i++; + } + + return destination; } int memcmp(const void *ptr1, const void *ptr2, size_t num) { - int count = 0, copylen = num; - char const *copystr1 = (const char *)ptr1; - char const *copystr2 = (const char *)ptr2; - while (*copystr1 != '\0' && *copystr2 != '\0' && num--) { - if (*copystr1 != *copystr2) { - if (*copystr1 > *copystr2) { - return 1; - } else { - return -1; - } - break; - } - copystr1++; - copystr2++; - count++; - } + // Compare 'num' bytes in memory pointed to by 'ptr1' and 'ptr2' + int count = 0; + int copylen = num; + + // Create character pointers to 'ptr1' and 'ptr2' + const char *copystr1 = (const char *)ptr1; + const char *copystr2 = (const char *)ptr2; + + // Start a loop to compare 'num' bytes in memory + while (*copystr1 != '\0' && *copystr2 != '\0' && num--) { + // Compare the characters in 'copystr1' and 'copystr2' + if (*copystr1 != *copystr2) { + // If they don't match, return 1 if 'copystr1' is greater + // return -1 if 'copystr2' is greater + 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) { - return 0; - } + // If the loop completed without finding a difference, the blocks are equal + if (count == copylen) { + return 0; + } - if (strlen((const char *)ptr1) > strlen((const char *)ptr2)) { - return 1; - } - return -1; + // If the lengths of the two blocks are different + // 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; } void *memset(void *source, int value, size_t num) { - char *src = (char *)source; - while (num--) { - *src = value; - src++; - } + // Set 'num' bytes in memory pointed to by 'source' to the specified 'value' + char *src = (char *)source; + + while (num--) { + *src = value; + src++; + } - return source; + return source; } -- GitLab