readlink

#include 
#include 

int main()
{
    ssize_t n;
    char buf[4096];
    
    if ((n = readlink("newfile.txt", buf, sizeof buf - 1)) < 0){
        perror("readlink");
        return 1;
    }
    
    buf[n] = '\0';
    printf("%s\n", buf);
    
    return 0;
}

rmdir

#include < unistd.h >
#include < stdio.h >

int main(){
    if (rmdir("work") < 0){
        perror("rmdir");
        return 1;
    }
    return 0;
}

lseekプログラム

#include < sys/types.h >
#include < unistd.h >

#include < sys/stat.h >
#include < fcntl.h >
#include < stdio.h >

int main()
{
    int fd;
    off_t offset;
    ssize_t n;
    char buf[4096];
    
    if((fd = open("file.txt", O_RDONLY)) < 0){
        perror("open");
        return 1;
    }
    
    if ((offset = lseek(fd, 10, SEEK_SET)) < 0){
        perror("lseek");
        return 1;
    }
    
    if ((n = read(fd, buf, sizeof buf)) < 0){
        perror("read");
        return 1;
    }
    write(1, buf, n);
    
    return 0;
}

write

#include < unistd.h >

#include < stdio.h >

int main()
{
    ssize_t n;
    
    if ((n = write(1, "Hello\n", 6)) < 0){
        perror("write");
        return 1;
    }
    return 0;
}

ファイルの読み込み・書き込み専用オープン

#include < sys/types.h >
#include < sys/stat.h >
#include < fcntl.h >

#include < stdio.h >

int main()
{
    int fd_r, fd_w;
    
    if ((fd_r = open("/etc/hosts", O_RDONLY)) < 0){
        perror("/etc/hosts");
        return 1;
    }
    
    if ((fd_w = open("file.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0){
        perror("file.txt");
        return 1;
    }
    return 0;
}