●int open(const char *pathname, int flags);
●int open(const char *pathname, int flags, mode_t mode);
●int close(int fd);
[root@rhel74 c]# cat openclose.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
static void die(const char *s);
int
main(int argc, char *args[])
{
int fd;
fd = open(args[1], O_RDWR);
if (fd < 0) die(args[1]);
if (close(fd) < 0) die(args[1]);
exit(0);
}
static void
die(const char *s)
{
perror(s);
exit(1);
}
[root@rhel74 c]# ./openclose ./openclose.c
[root@rhel74 c]# echo $?
0
●ssize_t read(int fd, void *buf size_t bufsize);
●ssize_t write(int fd, const void *buf, size_t bufsize);
[root@rhel74 c]# cat read.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
static void die(const char *s);
int
main(int argc, char *args[])
{
unsigned char buf[1024];
int n;
n = read(STDIN_FILENO, buf, sizeof buf);
if (n < 0) die("read");
if (write(STDOUT_FILENO, buf, n) < 0) die("write");
exit(0);
}
static void
die(const char *s)
{
perror(s);
exit(1);
}
[root@rhel74 c]# echo asdf | ./read
asdf
catを作る
[root@rhel74 c]# cat mycat.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
static void do_cat(const char *path);
static void die(const char *s);
int
main(int argc, char *argv[]){
int i;
if (argc < 2){
fprintf(stderr, "%s: file name not given\n", argv[0]);
exit(1);
}
for (i = 1; i < argc; i++) {
do_cat(argv[i]);
}
exit(0);
}
#define BUFFER_SIZE 2048
static void
do_cat(const char *path){
int fd;
unsigned char buf[BUFFER_SIZE];
int n;
fd = open(path, O_RDONLY);
if(fd < 0) die(path);
for (;;){
n = read(fd, buf, sizeof buf);
if(n < 0) die(path);
if(n == 0) break;
if(write(STDOUT_FILENO, buf, n) < 0) die(path);
}
if(close(fd) < 0) die(path);
}
static void
die(const char *s){
perror(s);
printf("%s\n",strerror(errno));
exit(1);
}
●off_t lseek(int fd, off_t offset, int whence);
[root@rhel74 c]# cat lseek.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static void die(const char *s);
int
main(int argc, char *argv[])
{
int n;
unsigned char buf[100];
int fd;
char path[100] = "/etc/passwd";
fd = open(path, O_RDWR);
if (fd < 0) die(path);
/* 先頭から5バイト先へ */
if (lseek(fd, 5, SEEK_SET) < 0) die("lseek");
n = read(fd, buf, sizeof buf);
if (n < 0) die("read");
if (write(STDOUT_FILENO, buf, n) < 0) die("write");
printf("\n");
/* カレントから15バイト前へ */
if (lseek(fd, -15, SEEK_CUR) < 0) die("lseek");
n = read(fd, buf, sizeof buf);
if (n < 0) die("read");
if (write(STDOUT_FILENO, buf, n) < 0) die("write");
printf("\n");
/* EOFから5バイト前へ */
if (lseek(fd, -5, SEEK_END) < 0) die("lseek");
n = read(fd, buf, sizeof buf);
if (n < 0) die("read");
if (write(STDOUT_FILENO, buf, n) < 0) die("write");
if (close(fd) < 0) die(path);
exit(0);
}
static void die(const char *s)
{
perror(s);
exit(1);
}
[root@rhel74 c]# ./lseek
x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:
ogin