Skip to content
Snippets Groups Projects
Commit 940daeac authored by 강한결's avatar 강한결
Browse files

Initial Commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 182 additions and 0 deletions
File added
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv){
int fd = 0;
if(argc != 3)
{
fprintf(stderr, "usage: %s <file name> <access mode\n>", argv[0]);
fprintf(stderr, "access mode is octet number, ex:0775\n");
return 1;
}
mode_t mode = 0;
sscanf(argv[2], "0%o", &mode);
fd = open(argv[1], O_WRONLY|O_CREAT|O_EXCL, mode);
if(fd == -1)
{
perror("failed open");
return 1;
}
printf("success open %s\n", argv[1]);
close(fd);
return 0;
}
File added
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv){
int fd;
int rd;
char buf[256];
if(argc != 3)
{
fprintf(stderr, "usage: %s <file name> <data to add>\n", argv[0]);
}
fd = open(argv[1], O_WRONLY|O_CREAT|O_APPEND, 0777);
if(fd == -1)
{
perror("failed open for write");
return 1;
}
write(fd, argv[2], strlen(argv[2]));
write(fd, "\n", -1);
close(fd);
fd = open(argv[1], O_RDONLY, 0777);
if(fd == -1)
{
perror("failed open for read");
return 1;
}
memset(buf, 0, sizeof(buf));
while((rd = read(fd, buf, sizeof(buf)))>0)
{
printf("%s\n", buf);
}
close(fd);
return 0;
}
System Programming is fun
\ No newline at end of file
File added
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
int fd;
int i, buf;
fd = open("num.dat", O_RDWR|O_CREAT);
if(fd < 0)
{
perror("error : ");
exit(0);
}
for (i=1000; i< 1010; i++)
{
write(fd, (void*)&i, sizeof(i));
}
lseek(fd, 0, SEEK_SET);
read(fd, (void*)&buf, sizeof(i));
printf("%d\n", buf);
lseek(fd, 4*sizeof(i), SEEK_SET);
read(fd, (void*)&buf, sizeof(i));
printf("%d\n", buf);
}
File added
File added
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
int main()
{
int fd = open("./dup2_example_data", O_CREAT|O_WRONLY, 0755);
if(dup2(fd, STDOUT_FILENO) == -1)
{
perror("failed dup2");
return 1;
}
printf("Hello World\n");
close(fd);
return 0;
}
Hello World
File added
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
int main()
{
int fd1 = open("./dup_example_data", O_CREAT|O_WRONLY, 0755);
write(fd1, "Hello\n", strlen("Hello\n"));
int fd2 = dup(fd1);
write(fd2, "Hi\n", strlen("Hi\n"));
close(fd1);
write(fd2, "nihao\n", strlen("nihao\n"));
close(fd2);
return 0;
}
Hello
Hi
nihao
File added
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
int fd1 = 0, fd2 = 0, fd3 = 0;
fd1 = open("./fcntl_example_data", O_WRONLY|O_CREAT|O_TRUNC);
printf("fcntl(F_DUPFD) TEST\n");
fd2 = fcntl(fd1, F_DUPFD, 30);
printf("fd2: %d\n", fd2);
fd3 = fcntl(fd1, F_DUPFD, 30);
printf("fd2: %d\n", fd3);
close(fd2);
close(fd3);
printf("dup2() TEST\n");
fd2 = dup2(fd1, 30);
printf("fd2: %d\n", fd2);
fd3 = dup2(fd1, 30);
printf("fd3: %d\n", fd3);
close(fd2);
close(fd3);
close(fd1);
return 0;
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment