시스템 프로그래밍_1
# 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 |