Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jdarge authored Apr 4, 2023
0 parents commit 69a9e69
Show file tree
Hide file tree
Showing 14 changed files with 1,112 additions and 0 deletions.
50 changes: 50 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
cmake_minimum_required(VERSION 3.24.2)

# Setting a variable to use for both the project name and the resulting binary executable
set(REPO schizo_diary)
project(${REPO})
set(PROJECT_NAME ${REPO})


# Set the C standard to C99
set(CMAKE_C_STANDARD 99)

# Compiler flags
set(CPP_FLAGS "-Wall -Wextra -pedantic")


# Add the src directory to the include path
include_directories(${PROJECT_SOURCE_DIR}/include)

# Add all the source files in the src directory to the project
file(GLOB SOURCES ${PROJECT_SOURCE_DIR}/src/*.c)

# Create the executable from the source files
add_executable(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/driver.c ${SOURCES} include/string.h src/string.c)


# Set the output directory to the build/bin directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/bin)
set_target_properties(${PROJECT_NAME}
PROPERTIES
COMPILE_FLAGS ${CPP_FLAGS}
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/bin
)

# Set the object directory to the build/obj directory
set_property(
TARGET ${PROJECT_NAME}
PROPERTY
CMAKE_OBJECT_PATH_PREFIX ${PROJECT_SOURCE_DIR}/build/obj/
)


# Set the cmake binary directory to the build/cmake directory
set(CMAKE_BINARY_DIR ${PROJECT_SOURCE_DIR}/build/cmake)

# Add the build/cmake directory to the include path
include_directories(${PROJECT_SOURCE_DIR}/build/cmake)


# Add any other dependencies here, such as libraries or additional build options

67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# schizo_diary (lol)

## Structure

```console
.
├── build
│   ├── bin
│   └── cmake
├── CMakeLists.txt
├── driver.c
├── include
│   └── foo.h
├── run.sh
└── src
└── foo.c
```

## Commands

```console
$ ./run [build/make]
$ ./run rebuild
$ ./run clean
$ ./run
```

Make sure to set the <ins>run</ins> file to be executable.

```console
$ chmod +x run
```

### ./run make/build :

Both do the same thing.
<br>They run CMakeList.txt and then the Makefile generated by CMake.

```console
$ ./run make
$ ./run build
```

### ./run rebuild :

Deletes previously generated files and then runs CMake and Make

```console
$ ./run rebuild
```

### ./run clean :

Removes the <ins>build/</ins> directory.

```console
$ ./run clean
```

### ./run :

If there is binary executable inside of <ins>build/bin</ins> it will run it.
<br>If there isn't, it will run the build commands for you...

```console
$ ./run
```
124 changes: 124 additions & 0 deletions driver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "def.h"
#include "print.h"
#include "sbrk.h"
#include "string.h"

//Structs
typedef struct node {
int value;
struct node *next;
} node_t;

// Tests
void node_test(void); //
void dynamic_array(void); //
void string(void); //

int main(void) {

node_test();
dynamic_array();
string();

return 0;
}

void node_test(void) {
// Create an empty linked list
node_t *head = NULL;

// Allocate the first node
node_t *new_node = (node_t *) my_sbrk(sizeof(node_t));

if (new_node == NULL) {
my_printf("Error: out of memory\n");
return;
}

new_node->value = 69;
new_node->next = NULL;
head = new_node;

// Allocate a second node
new_node = my_sbrk(sizeof(node_t));
if (new_node == NULL) {
my_printf("Error: out of memory\n");
return;
}

new_node->value = 420;
new_node->next = NULL;
head->next = new_node;

// Print the contents of the linked list
node_t *current_node = head;
while (current_node != NULL) {
my_printf("%d ", current_node->value);
current_node = current_node->next;
}

my_printf("\n");
}

void dynamic_array(void) {
// Allocate an array of 10 integers
int *array = my_sbrk(10 * sizeof(int));
if (array == NULL) {
my_printf("Error: out of memory\n");
return;
}

// Initialize the array
for (int i = 0; i < 10; i++) {
array[i] = i;
}

// Print the contents of the array
for (int i = 0; i < 10; i++) {
my_printf("%d ", array[i]);
}
my_printf("\n");

// Resize the array to hold 20 integers
int *new_array = my_sbrk(20 * sizeof(int));
if (new_array == NULL) {
my_printf("Error: out of memory\n");
return;
}

// Copy the contents of the old array to the new array
for (int i = 0; i < 10; i++) {
new_array[i] = array[i];
}

// Initialize the remaining elements of the new array
for (int i = 10; i < 20; i++) {
new_array[i] = i;
}

// Print the contents of the new array
for (int i = 0; i < 20; i++) {
my_printf("%d ", new_array[i]);
}
my_printf("\n");
}

void string(void) {
// Allocate memory for two strings
char *str1 = (char *) my_sbrk(1024);
char *str2 = (char *) my_sbrk(1024);

// Copy some text into the first string
my_strncpy(str1, "Hello, world!", 1024);

// Copy the first string into the second string
my_strncpy(str2, str1, 1024);

// Print the two strings
my_printf("str1: %s\n", str1);
my_printf("str2: %s\n", str2);

// Free the memory allocated for the strings
my_brk(str2);
my_brk(str1);
}
12 changes: 12 additions & 0 deletions include/builtin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#ifndef SBRK_BUILTIN_H
#define SBRK_BUILTIN_H

typedef __builtin_va_list va_list;

#define my_va_start(v, l) __builtin_va_start(v,l)
#define my_va_end(v) __builtin_va_end(v)
#define my_va_arg(v, l) __builtin_va_arg(v,l)

#endif //SBRK_BUILTIN_H
27 changes: 27 additions & 0 deletions include/def.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#ifndef SBRK_DEF_H
#define SBRK_DEF_H

#undef NULL
#define NULL 0

#undef size_t
typedef unsigned long size_t;

#undef uintptr_t
typedef long int intptr_t;

#undef uintptr_t
typedef unsigned long int uintptr_t;

#undef ptrdiff_t
typedef signed long int ptrdiff_t;

#undef wchar_t
typedef int wchar_t;

#undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *) 0) -> MEMBER)

#endif //SBRK_DEF_H
21 changes: 21 additions & 0 deletions include/print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#ifndef SBRK_PRINT_H
#define SBRK_PRINT_H

#include "builtin.h"
#include "def.h"

void my_putchar(char c);

void my_puts(const char *str);

void my_putint(long n);

void my_putfloat(double num);

void my_printf(const char *format, ...);

void my_vprintf(const char *format, va_list args);

#endif //SBRK_PRINT_H
12 changes: 12 additions & 0 deletions include/sbrk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#ifndef SBRK_SBRK_H
#define SBRK_SBRK_H

#include "def.h"

void *my_brk(void *end_data_segment);

void *my_sbrk(intptr_t increment);

#endif //SBRK_SBRK_H
75 changes: 75 additions & 0 deletions include/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma once

#ifndef SBRK_STRING_H
#define SBRK_STRING_H

#include "def.h"

void *my_memccpy(void *restrict dest, const void *restrict src, int c, size_t n);

void *my_memchr(const void *dest, int i, size_t n);

void *my_memrchr(const void *dest, int i, size_t n);

int my_memcmp(const void *dest, const void *src, size_t n);

void *my_memcpy(void *restrict dest, const void *restrict src, size_t n);

void *my_memmove(void *dest, const void *src, size_t n);

void *my_memset(void *dest, int i, size_t n);

char *my_stpcpy(char *restrict dest, const char *restrict src);

char *my_stpncpy(char *restrict dest, const char *restrict src, size_t n);

char *my_strcat(char *restrict dest, const char *restrict src);

char *my_strchr(const char *str, int c);

int my_strcmp(const char *str1, const char *str2);

char *my_strcpy(char *restrict dest, const char *restrict src);

size_t my_strcspn(const char *str, const char *reject);

char *my_strdup(const char *str);

size_t my_strlen(const char *str);

char *my_strncat(char *restrict dest, const char *restrict src, size_t n);

int my_strncmp(const char *str1, const char *str2, size_t n);

char *my_strncpy(char *restrict dest, const char *restrict src, size_t n);

char *my_strndup(const char *str, size_t n);

size_t my_strnlen(const char *str, size_t maxlen);

char *my_strpbrk(const char *str, const char *accept);

char *my_strrchr(const char *str, int c);

void my_strrev(char *str, int len);

size_t my_strspn(const char *str, const char *accept);

char *my_strtok(char *restrict str, const char *restrict delim);

char *my_strtok_r(char *restrict str, const char *restrict delim, char **restrict saveptr);

// TODO
//char *xstrerror(int errnum);
//int xstrerror_r(int errnum, char *buf, size_t buflen);
//char *xstrsignal(int sig);
//char *xstrstr(const char *haystack, const char *needle);v
//size_t xstrxfrm(char *restrict dest, const char *restrict src, size_t n);

// TO.. LEARN...?
//int xstrcoll(const char *dest, const char *src);
//int xstrcoll_l(const char *dest, const char *src, locale_t loc);
//char *xstrerror_l(int i, locale_t loc);
//size_t xstrxfrm_l(char *restrict dest, const char *restrict src, size_t n, locale_t loc);

#endif //SBRK_STRING_H
Loading

0 comments on commit 69a9e69

Please sign in to comment.