파일 종류 검색

---------------------------

S_IFMT : 0xF000 : st_mode 값에서 파일의 종류를 정의한 부분을 가져옴 

S_FIFO : 0x1000 : FIFO 파일

S_IFCHR : 0x2000 : 문자 장치 특수 파일

S_IFDIR : 0x4000 : 디렉토리 

S_IFBLK : 0x6000 : 블록 장치 특수 파일

S_IFREG : 0x8000 : 일반 파일

S_IFLNK : 0xA000 : 심볼릭 링크 파일

---------------------------------------------------------------

# include <stdio.h>

# include <sys/stat.h>


int main(void) {

struct stat buf; 

int kind;


stat("s_dir", &buf);

printf("mode => (16진수 => %x) \n", (unsigned int)buf.st_mode);


kind = buf.st_mode & S_IFMT;

printf("kind => %x\n", kind);


switch(kind) {

case S_IFDIR: // IF DIR 

printf ("s_dir : directory \n");

break;

case S_IFREG: // IF REG

printf ("s_dir : Regular file \n");

break;

case S_IFLNK: // IF LNK 

printf ("s_dir : symbolic link file \n");

break;

}


return 0;

}



'System programming' 카테고리의 다른 글

시스템 프로그래밍_01  (0) 2018.03.09
시스템 프로그래밍  (0) 2018.03.07
strcpy  (0) 2017.11.08
시스템 프로그래밍_1  (0) 2017.10.09

파일의 종류 검색 : 127슬라이드

S_IFMT : 

S_IFDIR : 디렉토리 파일 : 0x4000

S_IFREG : 일반 파일 : 0x8000

S_IFLNK : 심볼릭 링크 파일 : 0xA000

---------------------------------------------------------------------

# include <stdio.h>

# include <sys/stat.h>


int main(void) {

struct stat buf;

int kind;


stat("s_dir", &buf);


printf("mode => (16진수 => %x)\n", (unsigned int)buf.st_mode);


kind = buf.st_mode & S_IFMT;

printf("Kind => %x\n", kind);


switch(kind) {

case S_IFDIR:

printf("s_dir : Directory\n");

break;

case S_IFREG:

printf("s_dir : Regular file\n");

break;

case S_IFLNK:

printf("s_dir : symbolic link file \n");

break;

}


return 0;

}

'System programming' 카테고리의 다른 글

시스템 프로그래밍 파일종류 검색  (0) 2018.03.09
시스템 프로그래밍  (0) 2018.03.07
strcpy  (0) 2017.11.08
시스템 프로그래밍_1  (0) 2017.10.09

심볼릭 ( 아침 시작 ) 

// 하드링크 (hard link), 소프트 링크 (soft link)


[1] >>> man -s 3 link 

******************************************************************

# include <stdio.h>

# include <unistd.h>


int main(void) {

        if (link("./s", "./hardlink_file") == -1) {

                printf("error ... !!!\n");

        }


        printf("success ... \n");

        return 0;

}

# ls -il


[2] >>> man -s 3 symlink

******************************************************************

# include <stdio.h>

# include <unistd.h>


int main(void) {

        if (symlink("s", "symlink_file") == -1) {

                printf("error ... !!!\n");

        }

        else {

                printf("success ... !!!\n");

        }

        return 0;

}


[++] >> 파일 정보 검색 : stat

******************************************************************

# include <sys/stat.h>

# include <stdio.h> // standard input output


int main(void) {

        struct stat buf;

        stat("file1", &buf);


        printf("buf.st_nlink => %4d\n", buf.st_nlink);

        printf("buf.st_uid   => %4d\n", buf.st_uid); // User Identification Number 

        printf("buf.st_gid   => %4d\n", buf.st_gid); // Group Identification Number


        return 0;

}


[3] >>> 심볼릭 링크의 정보 검색 : lstat

******************************************************************

# include <sys/stat.h>

# include <stdio.h>


int main(void) {

struct stat buf;

lstat("syml", &buf);


printf("Link count => %d\n", (int)buf.st_nlink);

printf("Inode      => %d\n", (int)buf.st_ino);

return 0;

}


[4] >>> 심볼릭 링크의 내용 읽기 : readlink

******************************************************************

man -s3 readlink

# include <sys/stat.h>

# include <unistd.h>

# include <stdio.h>

# include <stdlib.h> // exit

# include <string.h> // strlen

# define BUFSIZE 100

int main(void) {

        char buf[BUFSIZE] = {0,};

        int result = 0;


        result = readlink("./syml", buf, BUFSIZE);

        if (result == -1) {

                printf("error ... \n");

                exit(1);

        }

        printf("result > %d\n", result);

        buf[strlen(buf)] = '\0';

        printf("file.sym : %s\n", buf);


        return 0;

}


[5] >>> 원본 파일의 경로 읽기 : realpath

******************************************************************

man -s3 realpath

[-] 필요한 헤더 정보

# include <stdio.h>

# include <limits.h>

# include <stdlib.h>

# define BUFSIZE 100

int main(void) {

        char buf[BUFSIZE] = {0, };


        realpath("syml", buf);

        printf("syml : REALPATH => %s\n", buf);

        return 0;

}



'System programming' 카테고리의 다른 글

시스템 프로그래밍 파일종류 검색  (0) 2018.03.09
시스템 프로그래밍_01  (0) 2018.03.09
strcpy  (0) 2017.11.08
시스템 프로그래밍_1  (0) 2017.10.09

strcpy

System programming2017. 11. 8. 21:41

# include <stdio.h>

# include <Windows.h>


int main(void) {

char s[40] = { 0, };

lstrcpyA(s, "hello world"); // STRCPY

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

return 0;

}

'System programming' 카테고리의 다른 글

시스템 프로그래밍 파일종류 검색  (0) 2018.03.09
시스템 프로그래밍_01  (0) 2018.03.09
시스템 프로그래밍  (0) 2018.03.07
시스템 프로그래밍_1  (0) 2017.10.09

# include <stdio.h>

# include <stdlib.h>

# include <string.h>

# include <sys/types.h>

# include <sys/stat.h>

# include <fcntl.h>

# include <unistd.h> // getopt

# define ERROR -1

typedef struct _FILE_WRITE

{

    int fd1; // file descriptor 

    int fd2; // file descriptor

    int sumSize;

    char buf[1];

}_FILE_WRITE, *ptr_FILE_WRITE;

// function prototype ============================================

ptr_FILE_WRITE creatNode(); // [1]

void Init(_FILE_WRITE** pParam); // [2] 

void Step1(_FILE_WRITE** pParam, int* countNumber, char** pArgu); // [3]

// ============================================================== 

extern int optind;

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

{

    ptr_FILE_WRITE Fp = creatNode();

    if (argc != 4)

    {

        fprintf(stderr, "Usage : ./OriginalFile \"srcFile\" \"option\" \"dstFile\" ");

        exit(1);

    }

    else 

    { // argc == 4

        Fp->fd1 = open(argv[1], O_RDWR); // File only read and write

        if (Fp->fd1 == ERROR) 

        {

            // file not exist

            perror("file not exist");

            exit(1);

        }

        else

        {

            // file exist

            Step1(&Fp, &argc, argv);

        }

    }

    return 0;

} // end of main function 

// ==============================================================

ptr_FILE_WRITE creatNode() // [1]

{

    ptr_FILE_WRITE pNode = (ptr_FILE_WRITE)malloc(sizeof(_FILE_WRITE));

    return pNode;

} // end of creatNode function 

// ==============================================================

void Init(_FILE_WRITE** pParam) // [2] 

{

    if ((*pParam) == NULL)

    {

        fprintf(stderr, "malloc error ... !!!");

        exit(1);

    }

    else

    {

        memset( (*pParam), 0, sizeof(_FILE_WRITE) );

    }

} // end of Init function 

// ==============================================================

void Step1(_FILE_WRITE** pParam, int* countNumber, char** pArgu) // [3] 

    int Fnumber;

    int temp = 0;

    Fnumber = getopt((*countNumber), pArgu ,"ac");

    printf("test => %c %d\n", Fnumber, Fnumber);

    // question ???????????

    optind = 2;

    switch(Fnumber)

    {

        case 'a': //printf("test1\n");

                  (** pParam).fd2 = open(pArgu[3], O_RDWR | O_CREAT | O_APPEND); // FILE create 

                  // file create fail error

                  if ( (** pParam).fd2 == ERROR)

                  {

                      fprintf(stderr, "file create error ... !!");

                      exit(1);

                  }

                  else // (** pParam).fd2 != error 

                  {

                      while ((temp = read((** pParam).fd1, (** pParam).buf, 1)) > 0)

                      {

                          (** pParam).buf[1] = '\0'; // NULL character

                          printf("temp => %d\n", temp);

                          (** pParam).sumSize += temp; // data size

                          if ( write((** pParam).fd2, (** pParam).buf, temp) != temp )

                          {

                              fprintf(stderr, "Write error ... !!!\n");

                              exit(1);

                          }

                      }

                  }

                  break;

        case 'c': //printf("test2\n");

                  (** pParam).fd2 = open(pArgu[3], O_WRONLY | O_CREAT | O_TRUNC, 0777);

                  if ((** pParam).fd2 == ERROR)

                  {

                      fprintf(stderr, "file create fail ... !!!\n");

                      exit(1);

                  }

                  while ((temp = read((** pParam).fd1, (** pParam).buf, 1)) > 0)

                  {

                      (** pParam).buf[1] = '\0'; // NULL character

                      printf("temp => %d\n", temp);

                      (** pParam).sumSize += temp; // data size

                      if ( write((** pParam).fd2, (** pParam).buf, temp) != temp )

                      {

                          fprintf(stderr, "Write error ... !!!\n");

                          exit(1);

                      }

                  }

                  break;

        case '?': printf("error1...\n");

                  break;

        default : printf("error2...\n");

                  break;

    }

    printf("byte size => %d byte\n", (** pParam).sumSize);

    close((** pParam).fd2);

    close((** pParam).fd1);

} // end of Step1 function 

// ==============================================================

'System programming' 카테고리의 다른 글

시스템 프로그래밍 파일종류 검색  (0) 2018.03.09
시스템 프로그래밍_01  (0) 2018.03.09
시스템 프로그래밍  (0) 2018.03.07
strcpy  (0) 2017.11.08