Skip to content

Commit

Permalink
Merge pull request #2 from punixcorn/io_dev
Browse files Browse the repository at this point in the history
SCANF ADDED & file manipulation Function added
  • Loading branch information
punixcorn authored Apr 17, 2024
2 parents 0d08e2b + 32df064 commit f7ac5c3
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
docs/
a.out
main
.gdb_history
peda-session-main.txt
56 changes: 26 additions & 30 deletions include/_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,42 @@
* ASSERTS ARE DEFINED UNDER _EXIT() & _ITOA()
* BECAUSE ASSERT DEPENDS ON ITOA & EXIT
*/
#define _assert(condition) \
if (!(condition)) { \
_print("Assertion failed!\n"); \
_print("IN : "); \
_print(__FILE__); \
_print(" : "); \
_print(__func__); \
_print("()\n"); \
_print("LINE : "); \
_print(_itoa(__LINE__)); \
_print("\n"); \
_exit(-1); \
#define _assert(condition) \
if (!(condition)) { \
_print(_itoa(__LINE__)); \
_print(" : "); \
_print(__FILE__); \
_print(" : "); \
_print(__func__); \
_print("()\n"); \
_print("\t[ "); \
_print(#condition); \
_print(" ] FAILED\n"); \
_exit(-1); \
}

#define _throw_assert(message) \
_print("Assertion Called!\n"); \
_print("IN : "); \
_print(__FILE__); \
_print(" : "); \
_print(__func__); \
_print("()\n"); \
_print("LINE : "); \
_print(_itoa(__LINE__)); \
_print("\n"); \
_print("MESSAGE: "); \
_print(message); \
_print("\n"); \
#define _throw_assert(message) \
_print(_itoa(__LINE__)); \
_print(" : "); \
_print(__FILE__); \
_print(" : "); \
_print(__func__); \
_print("()\n"); \
_print(message); \
_print("\n"); \
_exit(-1);

#define _static_assert(condition, message) \
if (!(condition)) { \
_print("Assertion failed!\n"); \
_print("IN : "); \
_print(_itoa(__LINE__)); \
_print(" : "); \
_print(__FILE__); \
_print(" : "); \
_print(__func__); \
_print("()\n"); \
_print("LINE : "); \
_print(_itoa(__LINE__)); \
_print("\n"); \
_print("\t[ "); \
_print(#condition); \
_print(" ] FAILED\n"); \
_print("MESSAGE: "); \
_print(message); \
_print("\n"); \
Expand Down
115 changes: 115 additions & 0 deletions include/_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#ifndef __FILE_H_
#define __FILE_H_

#include "_int.h"
typedef long long int __off64_t;
typedef int __off_t;

#define O_RDWR 02
#define O_CREAT 0100
#define O_RDONLY 00
#define O_APPEND 02000
#define O_WRONLY 01
#define O_TRUNC 01000

/* mode_t types */
typedef unsigned int mode_t;
#define S_IRWXU 00700 // user
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100
#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
#define S_ISUID 0004000
#define S_ISGID 0002000
#define S_ISVTX 0001000

/* return a file descriptor of a file at pathname with flags */
int _open(const char *pathname, int flags) __attribute__((nonnull(1)));

/* return a file descriptor of a created file at pathname with modes */
int _creat(const char *pathname, mode_t mode) __attribute__((nonnull(1)));
;

int _openat(int dirfd, const char *pathname, int flags, ...
/* mode_t mode */) __attribute__((nonnull(2)));

/* close fd */
int _close(int fd);

/* FILE* copied from libc */

struct _IO_FILE;

struct _IO_FILE;
struct _IO_marker;
struct _IO_codecvt;
struct _IO_wide_data;

/* During the build of glibc itself, _IO_lock_t will already have been
defined by internal headers. */
#ifndef _IO_lock_t_defined
typedef void _IO_lock_t;
#endif

/* The tag name of this struct is _IO_FILE to preserve historic
C++ mangled names for functions taking FILE* arguments.
That name should not be used in new code. */
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */

/* The following pointers correspond to the C++ streambuf protocol. */
char *_IO_read_ptr; /* Current read pointer */
char *_IO_read_end; /* End of get area. */
char *_IO_read_base; /* Start of putback+get area. */
char *_IO_write_base; /* Start of put area. */
char *_IO_write_ptr; /* Current put pointer. */
char *_IO_write_end; /* End of put area. */
char *_IO_buf_base; /* Start of reserve area. */
char *_IO_buf_end; /* End of reserve area. */

/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */

struct _IO_marker *_markers;

struct _IO_FILE *_chain;

int _fileno;
int _flags2;
__off_t _old_offset; /* This used to be _offset but it's too small. */

/* 1+column number of pbase(); 0 is unknown. */
unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];

_IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

struct _IO_FILE_complete {
struct _IO_FILE _file;
#endif
__off64_t _offset;
/* Wide character stream stuff. */
struct _IO_codecvt *_codecvt;
struct _IO_wide_data *_wide_data;
struct _IO_FILE *_freeres_list;
void *_freeres_buf;
size_t __pad5;
int _mode;
/* Make sure we don't get into trouble again. */
char _unused2[15 * sizeof(int) - 4 * sizeof(void *) - sizeof(size_t)];
};

#define FILE struct _IO_FILE
#endif
50 changes: 42 additions & 8 deletions include/_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/* base write / read wrappers */

/* INCLUDES */

#include "_file.h"
#include "_lib.h"
#include "_syscalls.h"

Expand All @@ -11,25 +13,57 @@
#define STDERR_FILENO 2

/* read count bytes from fd into buf */
void _read(int fd, char* buf, unsigned int count);
void _read(int fd, char* __restrict __buf, unsigned int count);
/* write count bytes of buf into fd */
void _write(int fd, const char* buf, unsigned int count);

/* write str into STDOUT */
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)));

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

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

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

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

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

/* return formmated string fmt */
char* _format(char* 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)));

/* write character to stdout */
int _fputc(int __c, FILE* __stream) __attribute__((nonnull(2)));
int _putc(int __c, FILE* __stream) __attribute__((nonnull(2)));
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)));

#endif
8 changes: 3 additions & 5 deletions include/_lib.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef __LIB_H
#define __LIB_H

#include "../include/_string.h"

/* DEFINE NULL */
Expand All @@ -11,20 +10,19 @@
#define _free(ptr) ptr = null

typedef uint8_t _bool;

#ifdef bool
#undef bool
#define bool _bool;
#define false 0
#define true 1
#else
#define bool _bool;
#define bool _bool
#define false 0
#define true 1
#endif

/* calls SYSEXIT syscall with exitcode as exit code */
void _exit(int exitcode);
[[noreturn]] void _exit(int exitcode);

/* Place holder for Assert macros */
#include "_assert.h"
Expand All @@ -35,7 +33,7 @@ void _free(void* ptr);
#endif

/* allocates memory of size (size) */
void* _malloc(size_t size);
[[nodiscard("returns pointer to heap memory")]] void* _malloc(size_t size);

/* not implemented */
void* brk();
Expand Down
16 changes: 11 additions & 5 deletions include/_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
#include "_int.h"

/* return size of buf */
size_t _strlen(const char* buf);
size_t _strlen(const char* buf) __attribute__((nonnull(1)));
;

/* reverse a string of length (length)*/
int _reverse(char* str, size_t length);
int _reverse(char* str, size_t length) __attribute__((nonnull(1)));

/* converts number into string */
char* _itoa(int number);

/* converts string to number */
int _stoi(const char* str);
int _stoi(const char* str) __attribute__((nonnull(1)));

/* sets n bytes of s to c */
void* _memset(void* s, int c, size_t n);
void* _memset(void* __restrict__ s, int c, size_t n);

/* copy n bytes of src to dest */
void* _memcpy(void* dst, const void* src, unsigned long n);
void* _memcpy(void* __restrict__ dst, const void* __restrict__ src,
unsigned long n) __attribute__((nonnull(1, 2)));

/* compare first n bytes for s1 to s2 */
int _memcmp(const void* __restrict__ s1, const void* __restrict__ s2, size_t n)
__attribute__((nonnull(1, 2)));

#endif
47 changes: 47 additions & 0 deletions src/_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "../include/_file.h"

#include <sys/cdefs.h>

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

int _open(const char *pathname, int flags) {
int fd = 0;
mode_t mode = S_IRWXU | S_IROTH | S_IRGRP;
__asm__ inline(
"syscall;"
"movl %0,%%eax;"
: "=r"(fd)
: "a"(SYSOPEN), "D"(pathname), "S"(flags), "d"(mode));
if (fd == -1 || fd == 0) {
return -1;
}
return fd;
};

__attribute__((nonnull)) int _creat(const char *pathname, mode_t mode) {
int fd = 0;
int flags = O_CREAT | O_WRONLY | O_TRUNC;
if (mode == 0) mode = S_IRWXU | S_IROTH | S_IRGRP;
__asm__ inline(
"syscall;"
"movl %0,%%eax;"
: "=r"(fd)
: "a"(SYSOPEN), "D"(pathname), "S"(flags), "d"(mode));
if (fd == -1 || fd == 0) {
return -1;
}
return fd;
}

int _openat(int dirfd, const char *pathname, int flags, ...
/* mode_t mode */);

int _close(int fd) {
int ret = 0;
__asm__ inline(
"syscall;"
"movl %%eax, %0;"
: "=r"(ret)
: "a"(SYSCLOSE), "D"(fd));
return ret;
}
Loading

0 comments on commit f7ac5c3

Please sign in to comment.