writev

#include < sys/uio.h >
#include < stdio.h >

int main()
{
    ssize_t n;
    struct iovec vector[3];
    
    vector[0].iov_base = "Hello";
    vector[0].iov_len = 5;
    
    vector[1].iov_base = " ";
    vector[1].iov_len = 1;
    vector[2].iov_base = "World\n";
    vector[2].iov_len = 6;
    
    if ((n = writev(1, vector, 3))< 0){
        perror("writev");
        return 1;
    }
    return 0;
}

readv

長さが違う3個の読み込み用バッファを用意して、readvで読み込む。

#include < sys/uio.h >
#include < stdio.h >

int main()
{
    int i;
    ssize_t n;
    char buf1[5], buf2[10], buf3[15];
    struct iovec vector[3];
    
    vector[0].iov_base = buf1;
    vector[0].iov_len = sizeof buf1;
    
    vector[1].iov_base = buf2;
    vector[1].iov_len = sizeof buf2;
    vector[2].iov_base = buf3;
    vector[2].iov_len = sizeof buf3;
    
    if ((n = readv(0,vector, 3)) < 0){
        perror("readv");
        return 1;
    }
    
    for (i = 0; i < 3; i++){
        if (n < vector[i].iov_len){
            vector[i].iov_len = n;
        }
        n -= vector[i].iov_len;
            
            printf("buf%d[] = %.*s\n",
                   i, (int)vector[i].iov_len, (char *)vector[i].iov_base);
        }
        return 0;
}

fstatvfs

#include < sys/statvfs.h >

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

int main()
{
    int fd;
    struct statvfs buf;
    
    if((fd = open(".", O_RDONLY)) < 0){
        perror("open");
        return 1;
    }
    
    if (fstatvfs(fd, &buf) < 0){
        perror("fstatvfs");
        return 1;
    }
    
    printf(
    (sizeof(fsblkcnt_t) == 8) ?(
           "f_bsize = %lu, f_frsize = %lu,\n"
            "f_blocks = %llu, f_bfree = %llu, f_bavail = %llu, \n"
                                "f_files = %llu, f_ffree = %llu, f_favail = %llu,\n"
                                "f_fsid = %lx, f_flag= %lx, f_namemax = %lu\n"
                                ) : (
                                     "f_bsize = %lu, f_frsize = %lu,\n"
                                     "f_blocks = %llu, f_bfree = %lu, f_bavail = %lu, \n"
                                     "f_files = %lu, f_ffree = %lu, f_favail = %lu,\n"
                                     "f_fsid = %lx, f_flag= %lx, f_namemax = %lu\n"
           ),
           buf.f_bsize, buf.f_frsize,
           buf.f_blocks, buf.f_bfree, buf.f_bavail,
           buf.f_files, buf.f_ffree, buf.f_favail,
           buf.f_fsid, buf.f_flag, buf.f_namemax
           );
    return 0;
}

statvfs

カレントディレクトリが属するファイルシステムの情報取得

#include < sys/statvfs.h >
#include < stdio.h >

int main()
{
    struct statvfs buf;
    
    if(statvfs(".", &buf) < 0){
        perror("statvfs");
        return 1;
    }
    
    printf(
    (sizeof(fsblkcnt_t)== 8)?(
           "f_bsize = %lu, f_frsize = %lu,\n"
            "f_blocks = %llu, f_bfree = %llu, f_bavail = %llu,\n"
                              "f_files = %llu, f_ffree = %llu, f_favail = %llu,\n"
                              "f_fsid = %lx, f_flag = %lx, f_namemax = %lu\n")
           : (
           "f_bsize = %lu, f_frsize = %lu, \n"
              "f_blocks = %lu, f_bfree = %lu, f_bavail = %lu, \n"
              "f_fsid = %lx, f_flag = %lx, f_namemax = %lu\n"),
           buf.f_bsize, buf.f_frsize,
           buf.f_blocks, buf.f_bfree, buf.f_bavail,
           buf.f_files, buf.f_ffree, buf.f_favail,
           buf.f_fsid, buf.f_flag, buf.f_namemax);
    return 0;
}

複数のファイル記述子を同時に監視

#include < sys/select.h >

#include < stdio.h >

int main(){
    int n;
    fd_set readfds;
    struct timeval tv;
    
    FD_ZERO(&readfds);
    FD_SET(0, &readfds);
    FD_SET(4, &readfds);
    
    tv.tv_sec = 2;
    tv.tv_usec = 500000;
    
    n = select(5, &readfds, NULL, NULL, &tv);
    
    if (n < 0){
        perror("select");
        return 1;
    } else if (n == 0){
        printf("time out \n");
    } else {
        if (FD_ISSET(0, &readfds)){
            printf("input from fd = 0\n");
        }
        if (FD_ISSET(4, &readfds)){
            printf("input from fd = 4\n");
        }
    }
    return 0;
}

utimes

#include < sys/time.h >

#include < time.h >
#include < stdio.h >

int main(){
    struct timeval times[2];
    struct tm t;
    
    t.tm_sec = 7;
    t.tm_min = 14;
    t.tm_hour = 12;
    t.tm_mday = 19;
    t.tm_mon = 0;
    t.tm_year = 138;
    t.tm_isdst = 0;
    times[0].tv_sec = mktime(&t);
    times[0].tv_usec = 0;
    times[1] = times[0];
    
    if (utimes("file.txt",times) < 0){
        perror("utimes");
        return 1;
    }
    return 0;
}

utime

#include 
#include 

#include 
#include 

int main()
{
    struct utimbuf buf;
    struct tm t;
    
    t.tm_sec = 7;
    t.tm_min = 14;
    t.tm_hour = 12;
    t.tm_mday = 19;
    t.tm_mon = 0;
    t.tm_year = 138;
    t.tm_isdst = 0;
    buf.actime =mktime(&t);
    buf.modtime = buf.actime;
    
    if (utime("file.txt", &buf) < 0){
        perror("utime");
        return 1;
    }
    return 0;
}

lstat

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

#include < time.h >
#include < stdio.h >

int main()
{
    struct stat buf;
    
    if(lstat("file.txt", &buf) < 0){
        perror("lstat");
        return 1;
    }
    
    printf("%s", ctime(&buf.st_mtime));
    return 0;
}

stat

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

#include 
#include 

int main(){
    struct stat buf;
    
    if (stat("file.txt", &buf) < 0){
        perror("stat");
        return 1;
    }
    printf("%s", ctime(&buf.st_mtime));
    return 0;
}