시스템소프트웨어실험

4장 예제

report

조용환 교수님

손영환 조교님

2006057157하나래

Ex04-02

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

int main()

{   int filedes;

mode_t oldmask;

oldmask = umask(023);

filedes = open("test.txt", O_CREAT, 0777);

close(filedes);

}

Ex04-04

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main()

{

char *filename = "test04.txt";

if ( access(filename, R_OK) == -1 ) {

fprintf( stderr, "User cannot read file %s \n",

filename);

exit(1);

}

printf("%s readable, proceeding \n", filename);

/* rest of program ... */

}

Ex04-07

#include <unistd.h>

int main(int argc, char *argv[])

{

if(link(argv[1], argv[2]))

printf("hard-link failed\n");

}

Ex04-08

#include <unistd.h>

main(int argc, char *argv[])

{

if(symlink(argv[1], argv[2]))

printf("soft-link failed\n");

}

Ex04-09

#include <stdio.h>

#include <unistd.h>

int main()

{

char buffer[1024];

int nread

nread = readlink("test.txt", buffer, 1024);

write(1, buffer, nread);

}

Ex04-10

#include <stdio.h> 02

int main(int argc, char *argv[])

{

if(argc != 3)

exit(1);

if(rename(argv[1], argv[2]) == 0)

printf("성공!\n");

else

printf("실패!\n");

}

Ex04-11

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

int main(int argc, char *argv[])

{

struct stat finfo;

char fname[1024];

if(argc > 1)

strcpy(fname, argv[1]);

else

strcpy(fname, argv[0]);

if(stat(fname, &finfo) == -1) {

fprintf(stderr, "Couldn't stat %s \n", fname);

exit(1);

}

printf("%s \n", fname);

printf("ID of device: %d \n", finfo.st_dev);

printf("Inode number: %d \n", finfo.st_ino);

printf("File mode   : %o \n", finfo.st_mode);

printf("Num of links: %d \n", finfo.st_nlink);

printf("User ID     : %d \n", finfo.st_uid);

printf("Group ID    : %d \n", finfo.st_gid);

printf("Files size  : %d \n", finfo.st_size);

printf("Last access time : %u \n", finfo.st_atim);

printf("Last modify time : %u \n", finfo.st_mtim);

printf("Last stat change : %u \n", finfo.st_ctim);

printf("I/O Block size : %d \n", finfo.st_blksize);

printf("Num of blocks  : %d \n", finfo.st_blocks);

printf("File system : %s \n", finfo.st_fstype);

}