fcntl, unistd: declarations for the Unix file API

This commit is contained in:
Lephenixnoir 2021-12-13 18:09:43 +01:00
parent 92ccd8b1db
commit c2feb94710
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
6 changed files with 80 additions and 73 deletions

View file

@ -9,9 +9,20 @@ extern "C" {
threads are supported. */
extern int errno;
#define EDOM 1
#define EILSEQ 2
#define ERANGE 3
#define EDOM 1 /* Outside of domain of math function. */
#define EILSEQ 2 /* Illegal multi-byte character sequence. */
#define ERANGE 3 /* Range error during text-to-numeric conversion. */
#define EACCES 4 /* File access denied. */
#define EEXIST 5 /* File already exists. */
#define EINVAL 6 /* Invalid argument in general; lots of uses. */
#define ENFILE 7 /* Out of file descriptors. */
#define ENOENT 8 /* File or folder does not exist. */
#define ENOMEM 9 /* System ran out of memory (RAM). */
#define EDQUOT 10 /* Out of inodes or memory space. */
#define ENOSPC 11 /* No space left on device. */
#define ENOTSUP 12 /* Operation not supported. */
#define EBADF 13 /* Invalid file descriptor. */
#define ESPIPE 14 /* File descriptor is unable to seek. */
#ifdef __cplusplus
}

View file

@ -5,21 +5,29 @@
extern "C" {
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
/* also defined in <stdio.h> */
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
/* Access mode. */
#define O_RDONLY 0x0000
#define O_WRONLY 0x0001
#define O_RDWR 0x0002
/* Create file if nonexistent. */
#define O_CREAT 0x0004
/* Fail if not a directory. */
#define O_DIRECTORY 0x0008
/* Append and truncate modes. */
#define O_APPEND 0x0010
#define O_TRUNC 0x0020
/* Exclusive open requiring creation. */
#define O_EXCL 0x0040
/*
** Open FILE and return a new file descriptor for it, or -1 on error.
** OFLAG determines the type of access used. If O_CREAT or O_TMPFILE is set
** in OFLAG, the third argument is taken as a `mode_t', the mode of the
** created file.
*/
extern int open(const char *pathname, int flags, ...);
/* Create, truncate and open a file. */
extern int creat(char const *__path, mode_t __mode);
/* Open file and return a file descriptor, -1 on error. */
extern int open(char const *__path, int __flags, ... /* mode_t __mode */);
#ifdef __cplusplus
}

View file

@ -10,7 +10,7 @@ extern "C" {
// Define properly off_t type.
# ifndef __off_t_defined
typedef uint32_t off_t;
typedef int32_t off_t;
# define __off_t_defined
# endif
@ -21,7 +21,6 @@ typedef int32_t ssize_t;
#endif
// Define alias
//FIXME: potential conflict with the real glibc(?)
typedef int32_t pid_t;
typedef int16_t mode_t;
typedef uint16_t dev_t;

View file

@ -1,9 +0,0 @@
#ifndef __BITS_FCNTL_H__
# define __BITS_FCNTL_H__
/* open/fcntl. */
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#endif /*__BITS_FCNTL_H__*/

View file

@ -5,11 +5,41 @@
extern "C" {
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
/* Standard file descriptors. */
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
/* Write data to a file descriptor; returns number of bytes written or -1. */
extern ssize_t write(int __fd, const void *__buf, size_t __nbytes);
/* Read data from a file descriptor; returns number of bytes read or -1. */
extern ssize_t read(int __fd, void *__buf, size_t __nbytes);
/* Read at a specific position from a file descriptor. */
extern ssize_t pread(int __fd, void *__buf, size_t __nbytes, off_t __offset);
/* Write at a specific position to a file descriptor. */
extern ssize_t pwrite(int __fd, const void *__buf, size_t __n, off_t __offset);
/* Seek at an offset from SEEK_SET/SEEK_CUR/SEEK_END; returns new position. */
extern off_t lseek(int __fd, off_t __offset, int __whence);
/* Close a file descriptor. */
extern int close(int __fd);
/* Remove a file. */
extern int unlink(const char *__path);
/* Kernel-style functions supported only by Vhex. */
#ifdef __SUPPORT_VHEX_KERNEL
//---
// Process part
///---
@ -29,51 +59,6 @@ extern pid_t getppid(void);
*/
extern int setpgid(pid_t __pid, pid_t __pgid);
//---
// File part
//---
/* Standard file descriptors. */
#define STDIN_FILENO 0 /* Standard input. */
#define STDOUT_FILENO 1 /* Standard output. */
#define STDERR_FILENO 2 /* Standard error output. */
/*
** Write N bytes of BUF to FD.
** Return the number written, or -1.
*/
extern ssize_t write(int __fd, const void *__buf, size_t __nbytes);
/*
** Read NBYTES into BUF from FD.
** Return the number read, -1 for errors or 0 for EOF.
*/
extern ssize_t read(int __fd, void *__buf, size_t __nbytes);
/*
** Read NBYTES into BUF from FD at the given position OFFSET without
** changing the file pointer.
** Return the number read, -1 for errors or 0 for EOF.
*/
extern ssize_t pread (int __fd, void *__buf, size_t __nbytes, off_t __offset);
/*
** Write N bytes of BUF to FD at the given position OFFSET without
** changing the file pointer.
** Return the number written, or -1.
*/
extern ssize_t pwrite (int __fd, const void *__buf, size_t __n, off_t __offset);
/*
** Move FD's file position to OFFSET bytes from the beginning of the file
** (if WHENCE is SEEK_SET), the current position (if WHENCE is SEEK_CUR),
** or the end of the file (if WHENCE is SEEK_END).
** Return the new file position.
*/
extern off_t lseek (int __fd, off_t __offset, int __whence);
/* Close the file descriptor FD */
extern int close(int __fd);
//---
// System part
//---
@ -87,6 +72,8 @@ extern int close(int __fd);
/* Get the value of the system variable NAME. */
extern long int sysconf(int __name);
#endif /*__SUPPORT_VHEX_KERNEL*/
#ifdef __cplusplus
}
#endif

View file

@ -6,6 +6,17 @@ static char *errno_strings [] = {
[EDOM] = "Numerical argument out of domain",
[EILSEQ] = "Invalid or incomplete multibyte or wide character",
[ERANGE] = "Numerical result out of range",
[EACCES] = "Permission denied",
[EEXIST] = "File exists",
[EINVAL] = "Invalid argument",
[ENFILE] = "Too many open files in system",
[ENOENT] = "No such file or directory",
[ENOMEM] = "Cannot allocate memory",
[EDQUOT] = "Disk quota exceeded",
[ENOSPC] = "No space left on device",
[ENOTSUP] = "Operation not supported",
[EBADF] = "Bad file descriptor",
[ESPIPE] = "Illegal seek",
};
char *strerror(int e)