Unix System Calls for Files and I/O

Creating Files

  1. Include files:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
  2. 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.
  3. 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.
  4. int creat(const char *pathname, mode_t mode);
    Deprecated, equal to: open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
  5. int close(int fd);
  6. Accessing Files

  7. 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".
  8. 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.
  9. ssize_t write (int fd, const void *buf, size_t nbytes);
    Returns nbytes unless an error has occured. Write at current offset unless the file was opened with O_APPEND.
  10. int dup (int fd);
    int dup2 (int fd_source, fd_dest);
  11. 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, ...
  12. 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'.
  13. File Status

  14. 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");
  15. 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);
  16. 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.
  17. Hard and Symbolic Links

  18. 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).
  19. int rename (const char *oldname, const char *newname);
    Gives a new name to an existing hard link. Works on files and directories.
  20. 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.
  21. 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.
  22. Creating a hard link from the terminal: "ln existingpath newpath". Creating a symbolic link: "ln -s pathname sympath".
  23. Directories

  24. int mkdir (const char *pathname, mode_t mode);
    int rmdir (const char *pathname);
  25. 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.
  26. int chdir (const char *pathname);
    This changes the current directory of the current process (only).