이름 | 설명 |
---|---|
open | 파일을 설정값을 부여하여 열 수 있다. |
create | 빈 파일을 생성한다. |
close | 열려있던 파일을 닫는다. |
read | 파일 읽는다. |
write | 파일에 내용을 입력한다. |
lseek | file의 특정 byte로 이동한다. |
unlink | 파일을 제거한다. |
remove | 파일을 제거한다. |
fcntl | 파일을 제어한다. |
file descriptor | 의미 |
---|---|
0 | 표준 입력 |
1 | 표준 출력 |
2 | 표준 에러 |
#include <fcntl.h>
int open(const char *pathname, int flags, [mode_t mode]);
/*성공시 fd, 실패시 -1을 반환한다.*/
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
/*성공시 fd, 실패시 -1을 반환한다.*/
fd1 = creat(“/tmp/newfile”, 0644);
fd2 = open(“/tmp/newfile”, O_WRONLY|O_CREAT|O_TRUNC, 0644);
//fd1 = fd2
$ chmod g+w 파일명
$ chmod 764 파일명
이름 | 설명 |
---|---|
S_IRUSR | 유저 읽기 허용 |
S_IWUSR | 유저 쓰기 허용 |
S_IXUSR | 유저 실행 허용 |
S_IRWXU | 유저 읽기, 쓰기, 실행 허용 |
S_IRGRP | 그룹 읽기 허용 |
S_IROTH | 외부 읽기 허용 |
S_ISUID | set user ID on execution |
S_ISGID | set group ID on execution |
int fd;
mode_t fdmode = { S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
fd = open("file", O_RDWR | O_CREAT, fdmode);
#include <unistd.h>
int close(int filedes);
/*성공시 0, 실패시 -1 반환*/
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t bytes);
/*성공시 읽어드린 byte수, EOF을 만나면 0, 실패시 -1을 반환*/
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t byte);
/*성공시 쓰여진 byte수, 실패시 -1 반환*/
int copyfile(const char *name1, const char *name2){
int infile, outfile;
ssize_t nread;
char buf[BUFSIZE]; /*BUFSIZE 512*/
if((infile = open(name1, O_RDONLY)) == --1)
return -1;
if((outfile = open(name2, FMODE, PERM) == -1){
close(infile);
return -2;
}
/*buf에 infile의 내용을 적고 그 값을 outfile에 적어넣음*/
while(nread = read(infile, buf, BUFSIZE) > 0){
if(write(outfile, buf, nread) < nread){
close(infile);
close(outfile);
return -3;
{
}
close(infile)
close(outfile)
if(nread == -1) return -4;
else return 0;
}
int main(){
infile("test.in", "test.out");
}
#include <unistd.h>
off_t lseek(int fd, off_t offset, int start_flag);
/*성공 new fiel offset, 실패시 -1 반환*/
fd = open(fname, O_RDWR);
lseek(fd, (off_t)0, SEEK_END); /* file position을 끝으로 설정*/
write(fd, outbuf, OBSIZE);
==
fd = open(fname, O_WRONLY | O_APPEND);
write(fd, outbuf, OBSIZE);
/*filesize 반환*/
off_t filesize;
filesize = lseek(fd, (off_t)0, SEEK_END);
#include <unistd.h>
int dup(int filedes);
int dup2(int filedes, int filedes2);
/*성공시 새 파일의 fd값, 실패시 -1 반환*/
#include <unistd.h>
fd3 = dup(1);
fd4 = open("test", O_RDONLY); //fd4 = 4
dup2(3, fd4);
#include <fcntl.h>
int fcntl(int filedes, int cmd, ...);
/*성공시 cmd에 따른 값이, 실패시 -1이 반환된다.*/
#include <fcntl.h>
int filestatus(int filedes)
{
int arg1;
/*fd에 해당하는 file의 status 값을 가져와 저장한다.*/
if (( arg1 = fcntl (filedes, F_GETFL)) == -1)
{
printf ("filestatus failed\n");
return (-1);
}
printf ("File descriptor %d: ",filedes);
/*
access mode bit를 이용하여 접근 권한을 확인할 수 있다.
*/
switch ( arg1 & O_ACCMODE){
case O_WRONLY: printf ("write-only"); break;
case O_RDWR: printf ("read-write"); break;
case O_RDONLY: printf ("read-only"); break;
default: printf("No such mode");
}
/* 특정 flag가 설정되어있는지 확인하는 법 */
if (arg1 & O_APPEND)
printf (" -append flag set");
printf ("\n");
return (0);
}
$ prog_name
$ prog_name < infile
dup2(infile, 0);
prog_name > outfile
dup2(outfile, 1);
fd 1의 file table의 v-node ptr은 outfile's v-node를 가르키게된다.
입력을 표준 입력으로 받고 출력을 표준 출력으로 내보낸다.
#incldue <stdlib.h>
#include <unistd.h>
#define SIZE 512
main(){
ssize_t nread;
char buf[SIZE];
while((nread = read(0, buf, SIZE)) > 0)
write(1, buf, nread);
exit(0);
}
$ io
This is line 1 (enter)
This is line 1
<Ctrl-D>
$
UNIX I/O (system call)
Standard I/O
Standard는 File* 이용하고 UNIX는 file desciptor를 이용하여 file에 접근한다.
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *stream;
if ( ( stream = fopen ("junk", "r")) == NULL)
{
printf ("Could not open file junk\n");
exit (1);
}
}
#include <stdio.h>
FILE *fopen(const char *restrict pathname, const char *restrict type);
/* 성공시 file pointer, 실패시 error */
#include <stdio.h>
int getc(FILE *istream);
/* 성공시 다음 character를, EOF on end of file */
int putc(int c, FILE *ostream);
/* 성공시 c, 실패시 EOF */
int c;
FILE *istream, *ostream;
while((c=getc(istream)) != EOF)
putc(c, ostream);
#include <stdio.h>
int fprintf(FILE *restrict fp, const char *restrict format, ...);
/* 성공시 output된 char의 갯수를, 실패시 음수를 반환한다. */
#include <stdio.h>
#include <stdlib.h>
int notfound(const char *progname, const char *filename)
{
fprintf (stderr, "%s: file %s not found\n", progname, filename);
exit (1);
}
#include <string.h>
#include <stdio.h>
char *strerror(int errnum);
void perror(count char *msg);
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
void main(int argc, char** argv){
int fd;
if((fd = open("nonesuch", O_RDONLY)) == -1)
fprintf(stderr, "ENOENT: %s\n", stderror(errno));
errno = EACESS; //permission denied.
/* errono의 값과 연관된 메세지가 출력된 다음 줄바꾸기 문자가 이어진다. */
perror(argv[0]);
ENOENT: No such files or directory
a.out: Permission denied
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
main() {
int fd;
fd = open(“nonesuch”, O_RDONLY); //errno = ENOENT
fprintf(stderr, "error %d\n", errno);
perror(“first position”);
fd = open(“existfile”, O_RDONLY); //errno = EACESS
fprintf(stderr, "error %d\n", errno);
perror(“second Position”);
}
error 2
First position : There is no such files
error 3
Second position : There is no access permission