Wrote a new stdio.h, Implemented fcntl.h

This commit is contained in:
2024-10-25 12:32:22 +02:00
parent fe2dac1361
commit bd7e00e7e8
6 changed files with 76 additions and 252 deletions
+18
View File
@@ -0,0 +1,18 @@
#ifndef _FCNTL_H
#define _FCNTL_H
/* File access modes */
#define O_CREAT 0x0001 // Create if does not exist
#define O_RDONLY 0x0002 // Read only
#define O_WRONLY 0x0004 // Write only
#define O_RDWR 0x0008 // Read and write
#define O_APPEND 0x0010 // Always writes at the end (appends)
#define O_TRUNC 0x0020 // Truncate file size and position to 0 on Open
#define O_EXCL 0x0040 // Error if file exists (with O_CREAT)
#define O_BIN 0x0080 // Read bytes, not text
#define O_DIRECTORY 0x0100 // Error if file is not a directory
// TODO: more!
/* TODO: File lock */
#endif
+58 -15
View File
@@ -2,8 +2,27 @@
#define _STDIO_H
#include <stddef.h>
#include <stdarg.h>
// Nobody should have to interface with the internals of this structure
// End Of File
#define EOF (-1)
// File related macros
#define BUFSIZ 512
#define FILENAME_MAX 255
#define FOPEN_MAX 32
// Seek positions
#define SEEK_CUR 0
#define SEEK_END 1
#define SEEK_SET 2
// Standard file pointers
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
/* The ultimate FILE struct */
typedef struct _iobuf { // 24B
unsigned char* _ptr; // File buffer base
unsigned int _size; // Size of buffer
@@ -14,28 +33,52 @@ typedef struct _iobuf { // 24B
char __reserved[6];
} FILE;
// Standard file pointers
extern FILE* stdin;
extern FILE* stdout;
extern FILE* stderr;
/* File IO */
FILE* fopen(const char* filename, const char* mode);
/* File operations */
FILE* fopen(const char* path, const char* mode);
int fclose(FILE* stream);
size_t fread(void* ptr, size_t size, size_t count, FILE* stream);
size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream);
int fseek(FILE* stream, long int origin, int offset);
long int ftell(FILE* stream);
size_t fwrite(void* ptr, size_t size, size_t count, FILE* stream);
int fseek(FILE* stream, long offset, int whence);
long ftell(FILE* stream);
void rewind(FILE* stream);
int fflush(FILE* stream);
/* Terminal IO */
/* Character I/O */
int getchar(void);
int putchar(int c);
int getc(FILE* stream);
int putc(int c, FILE* stream);
int ungetc(int c, FILE* stream);
/* Line I/O */
char* fgets(char* str, int n, FILE* stream);
int fputs(const char* str, FILE* stream);
int puts(const char* str);
/* Error handling */
void perror(const char* s);
int feof(FILE* stream);
int ferror(FILE* stream);
void clearerr(FILE* stream);
/* Formatted I/O */
int printf(const char* fmt, ...);
int sprintf(char* str, const char* fmt, ...);
int snprintf(char* str, size_t n, const char* fmt, ...);
int vprintf(const char* fmt, va_list va);
int fprintf(FILE* stream, const char* fmt, ...);
void putc(char c);
char getc();
void print(const char* str);
void clear();
void reads(char* str);
int vfprintf(FILE* stream, const char* fmt, va_list va);
// INFO: (v)sprinf() Has been depricated and removed as it is unsafe (potencial buffer overflow). Please use (v)snprintf()
int snprintf(char* str, size_t size, const char* fmt, ...);
int vsnprintf(char* str, size_t size, const char* fmt, va_list va);
int scanf(const char* fmt, ...);
int vscanf(const char* fmt, va_list va);
int fscanf(FILE* stream, const char* fmt, ...);
int vfscanf(FILE* stream, const char* fmt, va_list va);
int sscanf(const char* str, const char* fmt, ...);
int vsscanf(const char* str, const char* fmt, va_list va);
#endif