Include files:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int open (const char *pathname, int flag, mode_t mode);
Returns file descriptor of opened file on success, or -1 on failure.
First parameter is filename. Second is the "or" of any of these options:
O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, ...
Third parameter is only used when creating new files.
File descriptors: 0, 1 and 2 are standard input, standard output and standard error,
respectively. The STDIN_FILENO, STDOUT_FILENO and
STDERR_FILENO constants in the <unistd.h> file holds these values.
int creat(const char *pathname, mode_t mode);
Deprecated, equal to: open(pathname, O_WRONLY | O_CREAT
| O_TRUNC, mode);
int close(int fd);
Accessing Files
off_t lseek(int fd, off_t offset, int whence);
whence can be one of: SEEK_SET, SEEK_CUR, SEEK_END.
Returns -1 on error, on the new position on success:
my_pos = lseek(my_fd, 0, SEEK_CUR);
Files can have a negative offset, so check return value with "== -1" and not "< 0".
ssize_t read (int fd, void *buf, size_t nbytes);
Tries to read nbytes into buf. ssize_t is a signed integer, size_t is an unsigned one.
Returns -1 on error, 0 on end-of-file, otherwise number of bytes read. Should be nbytes
unless read from network.
Returns nbytes unless an error has occured. Write at current offset unless the file was
opened with O_APPEND.
int dup (int fd);
int dup2 (int fd_source, fd_dest);
int fcntl (int fd, int command, int some_argument);
Does one of many things, depending on the command parameter: Duplicate file descriptors,
get or set status flags, get or set file owner, get or set file or records locks, ...
int stat (const char *pathname, struct stat *buf);
int fstat (int fd, struct stat *buf);
int lstat (const char *pathname, struct stat *buf);
The 'struct stat' includes data about file type,
permissions, i-node number, device number, number of links, user and group IDs of owner,
size in bytes (for regular files), times of last access, last modification and last status
change, and a few others.
lstat() is the only variant that may return a
file type of 'symbolic link'.
File Status
File types: Regular, directory, symbolic link, character special, block special, fifo
(named pipe), and socket. Use the macros S_ISREG(),
S_ISDIR(), S_ISLNK() and so forth in <sys/stat.h>
to extract file type from a struct stat variable:
struct stat buf; if (lstat("my_file",
&buf) == -1) exit(1);
if (S_ISREG(buf.st_mode)) printf("regular file\n");
int chmod(const char *pathname, mode_t mode);
mode is a logical or of constants in <sys/stat.h>, for example:
chmod("my_file", S_IRUSR | S_IRGRP | S_IROTH);
int chown (const char *pathname, uid_t owner, gid_t group);
Change file's user and group ownership (used for permissions and quota).
'fchown()' and 'lchown()' usually exist as well but are non-standard.
Hard and Symbolic Links
int link (const char *existingpath, const char *newpath);
int unlink (const char *pathname);
link() does not copy a file - it creates a new hard link to an existing one.
It's impossible to create hard links to other file systems or to directories.
remove() does the same as unlink(), but works on
directories too (like rmdir() does).
int rename (const char *oldname, const char *newname);
Gives a new name to an existing hard link. Works on files and directories.
int symlink (const char *pathname, const char *sympath);
Creates a symbolic link named 'sympath' to the file in 'pathname'. May introduce
cycles, which will cause file I/O functions to return -1 and set the errno variable to ELOOP.
int readlink (const char *pathname, char *buf, int bufsize);
Read the contents of the symbolic link itself, not the file it points to. Returns
the number of bytes placed in buf.
Creating a hard link from the terminal: "ln
existingpath newpath". Creating a symbolic link: "ln -s pathname sympath".
Directories
int mkdir (const char *pathname, mode_t mode);
int rmdir (const char *pathname);
DIR *opendie (const char *pathname);
struct dirent *readdir (DIR *dp);
void rewinddir (DIR *dp);
int closedir (DIR *dp);
First two functions return NULL on error,
fourth one returns -1 on error.
Struct dirent contains two items: d_ino which a
file's i-node number, and d_name which is a
file's name.
int chdir (const char *pathname);
This changes the current directory of the current process (only).