Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed __attributes __format__ #3

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clangd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CompileFlags:
Add: [ -std=gnu11 ]
Add: [ -std=c11 ]
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
docs/
a.out
main
.gdb_history
peda-session-main.txt
*/**/*.h.gch
*/*.h.gch
50 changes: 43 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
main_w: src/*
gcc -nostdlib -ggdb src/* -o main
main: src/*
gcc -w -nostdlib -ggdb src/* -o main
FLAGS= -std=c11 -ggdb -w
args=


# uses shared files
shared: lib src/main.c
@./scripts/check_bin.sh
@./scripts/check_lib.sh
gcc $(FLAGS) -w lib/* src/main.c -o bin/main

static: static_lib/* src/main.c
make static_lib
@./scripts/check_bin.sh
gcc -static -w -nostdlib src/main.c static_lib/no_libc.a -o bin/main

# uses .c files
main: include/**/* src/main.c
@./scripts/check_bin.sh
gcc $(FLAGS) -nostdlib include/src/* src/* -o bin/main

# runs every check
make_check:
make shared
@echo "=============================================="
make main
@echo "=============================================="
make static_lib_

# creates a static lib
static_lib_:
cd static_lib && make all

run:
@./main
@echo ===========================================
@echo "=============================================="
@./bin/main $(args)
@echo "=============================================="
make clean

clean:
rm main
@echo "Cleaning..."
@touch a.txt
@rm *.txt 2>/dev/null
@rm bin/* 2>/dev/null
@rm lib/* 2>/dev/null
@echo "Done"
160 changes: 52 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,128 +6,72 @@ this is not intended for any use

purely written out of fun

# FILES
---

## `_io.h`
# Compile
edit `src/main`

basic input / ouput functions

### Functions

| Name | purpose | definition |
| --------- | ----------------------------------------------- | --------------------------------------------------------- |
| _read | read from file descriptor | `void _read(int fd,char * buf,unsigned int count)` |
| _write | write into file descriptor | `void _write(int fd,const char * buf,unsigned int count)` |
| _print | write to standard output | `void _print(const char * str)` |
| _println | write into standard output but with a newline | `void _scanline(char * buf)` |
| _scanline | read from standard input into buffer | `void _scan(char * buf, const unsigned int size)` |
| _scan | read size count from standard input into buffer | `void _scan(char * buf, const unsigned int size)` |
| _printf | formatted string into standard output | `void _printf(const char * fmt,...)` |
| _scanf | formatted input from standard input | `void _scanf(const char * fmt,...)` |

### DEFINES

```c
STDIN_FILENO 0
STDOUT_FILENO 1
STDERR_FILENO 2
```sh
make
make run
```
### To test all

---

## `_int.h`

Holds int type definitions

### DEFINES

```c
signed long long int int64_t;
unsigned long long int uint64_t;
signed char int8_t;
unsigned char uint8_t;
signed short int16_t;
unsigned short uint16_t;
unsigned int size_t;
unsigned int uint_t;
```sh
make make_check
```

---
### Targets

## `_lib.h`

a version of the required basic standard lib stuff

### functions

| Name | purpose | definition |
| ------- | ------------------------------------ | ------------------------------------------- |
| _exit | exit _start() with value | `void _exit(int errcode)` |
| _malloc | allocate memory on the heap | `void * _malloc(size_t size)` |
| _memset | set a memory segment to desired data | `void * _memset(void * s, int c, size_t n)` |
| _brk | not implemented | `void * brk()` |
| sbrk | not implemented | `void * _mmap(size_t size)` |

### Defines

```c
_assert
null
NULL
_throw_assert
_assert_info
bool
_bool
true
false
_free
```
- `make_check` = run all ( for testing )
- `static` = creats a static lib in `static_lib` and links to `src/main.c`
- `static_lib_` = creates static lib `no_libc.a` in `static_lib/`
- `shared` = creates shared objects in `lib/` ( it is the default )
- `main` = uses raw c files in `include/src/`

---

## `_string.h`

basic funtions for string manipulation

### functions

| Name | purpose | definition |
| -------- | ------------------------- | ----------------------------------------- |
| _strlen | return lenght of a string | `size_t _strlen(const char * buf)` |
| _reverse | reverse a string | `int _reverse(char * str, size_t lenght)` |
| _itoa | convert int to string | `char * _itoa(int number)` |
## Note
- compiles to so files which are then used

---

## `_arg.h`
# DIR

definitions for variadic args

### Defines

```c
typedef __builtin_va_list _va_list;
#define _va_start(v, ...) __builtin_va_start(v, 0)
// #define _va_start(v,l) __builtin_va_start(v,l)
#define _va_end(v) __builtin_va_end(v)
#define _va_arg(v, l) __builtin_va_arg(v, l)
```

## `main.h`

for calling `int main(){}` in main.c
- `src/` = source files
- `include/` = header files & source files for the header files
- `include/src/` = source files for the header files
- `iib/` = compiled shared object files
- `scripts/` = scripts to compile shared files and check shared files
- `static_lib/` = contains `no_libc.a` for static linking
- `bin` = binary is placed

---

## `syscalls.h`

definitions for syscall numbers

```c
SYSREAD 0
SYSWRITE 1
SYSOPEN 2
SYSCLOSE 3
SYSMMAP 9
SYSEXIT 60
```
### Available Functions

- _read
- _write
- _print
- _println
- _scanline
- _scan
- _printf
- _scanf
- _exit
- _malloc
- _memset
- _brk
- sbrk
- _strlen
- _reverse
- _itoa
- _stoa
- _memcpy
- _memcmp
- _format
- _sprintf
- _snprintf
- _open
- _creat
- _close
Binary file added bin/main
Binary file not shown.
49 changes: 26 additions & 23 deletions include/_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,51 @@ void _print(const char* str);
/* write str into STDOUT with a newline */
void _println(const char* str);
/* write formmated output to STDOUT */
void _printf(const char* __restrict __fmt, ...);
__attribute__((__format__(__printf__, 1, 2)));
__attribute__((__format__(__printf__, 1, 2))) void _printf(
const char* __restrict __fmt, ...);

/* read until newline from STDIN into buf */
void _scanline(char* __restrict __buf);
/* read size bytes from STDIN to buf */
void _scan(char* __restrict __buf, const unsigned int size)
__attribute__((nonnull(1)));
__attribute__((nonnull(1))) void _scan(char* __restrict __buf,
const unsigned int size);

/* read from STDIN untill whitespace */
char* __readword(char* buf);

/* read formmated output from STDIN */
void _scanf(const char* __restrict __fmt, ...);
__attribute__((nonnull(1), __format__(__printf__, 1, 2)));
__attribute__((nonnull(1), __format__(__printf__, 1, 2))) void _scanf(
const char* __restrict __fmt, ...);

/* np */
int _sscanf(const char* __restrict __s, const char* __restrict __format, ...);
__attribute__((nonnull(1), __format__(__printf__, 2, 3)));
/* np [d*] */
__attribute__((nonnull(1), __format__(__printf__, 2, 3))) int _sscanf(
const char* __restrict __s, const char* __restrict __format, ...);

/* write formmated output to S */
int _sprintf(char* __restrict __s, char* __restrict __fmt, ...)
__attribute__((nonnull(1), __format__(__printf__, 2, 3)));
__attribute__((nonnull(1), __format__(__printf__, 2, 3))) int _sprintf(
char* __restrict __s, char* __restrict __fmt, ...);

/* return formmated string fmt */
char* _format(char* __restrict __fmt, ...)
__attribute__((nonnull(1), __format__(__printf__, 1, 2)));

int _snprintf(char* __restrict __s, size_t __maxlen,
const char* __restrict __format, ...)
__attribute__((nonnull(1), __format__(__printf__, 3, 4)));
[[nodiscard("returns a formatted string")]] __attribute__((
nonnull(1), __format__(__printf__, 1, 2))) char*
_format(char* __restrict __fmt, ...);

/* write formmated output to S */
__attribute__((nonnull(1), __format__(__printf__, 3, 4))) int _snprintf(
char* __restrict __s, size_t __maxlen, const char* __restrict __format,
...);

/* write character to stdout */
int _fputc(int __c, FILE* __stream) __attribute__((nonnull(2)));
int _putc(int __c, FILE* __stream) __attribute__((nonnull(2)));
__attribute__((nonnull(2))) int _fputc(int __c, FILE* __stream);
__attribute__((nonnull(2))) int _putc(int __c, FILE* __stream);
int _putchar(int __c);

/* Read a character from stdin. */
int _fgetc(FILE* __stream) __attribute__((nonnull(1)));
int _getc(FILE* __stream) __attribute__((nonnull(1)));
int _getchar(void);
int _fscanf(FILE* __restrict __stream, const char* __restrict __format, ...)
__attribute__((nonnull(1), __format__(__printf__, 2, 3)));
__attribute__((nonnull(1))) int _fgetc(FILE* __stream);
__attribute__((nonnull(1))) int _getc(FILE* __stream);
[[nodiscard("returns a char from stream")]] int _getchar(void);
__attribute__((__format__(__printf__, 2, 3))) int _fscanf(
FILE* __restrict __stream, const char* __restrict __format, ...);

#endif
10 changes: 5 additions & 5 deletions src/_file.c → include/src/_file.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "../include/_file.h"
#include "../_file.h"

#include <sys/cdefs.h>

#include "../include/_syscalls.h"
#include "../_syscalls.h"

int _open(const char *pathname, int flags) {
int fd = 0;
Expand Down Expand Up @@ -34,7 +32,9 @@ __attribute__((nonnull)) int _creat(const char *pathname, mode_t mode) {
}

int _openat(int dirfd, const char *pathname, int flags, ...
/* mode_t mode */);
/* mode_t mode */) {
return -1;
};

int _close(int fd) {
int ret = 0;
Expand Down
Loading
Loading