/************************************************************************* * File Name: ex0120.c * * ÆÄÀÏÁß¿¡ ÀÖ´Â tabÀ» °°Àº ±æÀÌÀÇ ºó Ä­À¸·Î ´ëÄ¡ÇÏ´Â ÇÁ·Î±×·¥ * ¸Å n¹ø° À§Ä¡¿¡ tabÁöÁ¡ÀÌ ÀÖ´Ù°í °¡Á¤ÇÑ´Ù. * tabsize´Â 8·Î ÁöÁ¤ ÇѶóÀÎÀº 1000¹®ÀÚ ±îÁö Çã¿ëÇÑ´Ù. *************************************************************************/ #include #include #include #include #include #include #define TABSIZE 8 #define MAXLINE 1000 int dtab(char **str); int entab(char **str); int write_file(const char *filename, const char *str); int read_file(const char *filename, char **str); int check_arg(int argc, char *argv[]); int main(int argc, char *argv[]) { char *result; if ( !check_arg(argc, argv) ) exit(EXIT_FAILURE); if ( !read_file(argv[1], &result) ) exit(EXIT_FAILURE); if ( !dtab(&result) ) exit(EXIT_FAILURE); if ( !write_file(argv[1], result) ) exit(EXIT_FAILURE); free(result); return 0; } /***************************************************************** * * Function Name: dtab * * Input : char **str * Output : int 1(success), 0(failure) * * Comment : fileÀÇ ³»¿ëÀ» ¸ðµÎ ´ýÇÁ½ÃŲ ¹®Àڹ迭 Æ÷ÀÎÅ͸¦ * ¹Þ¾Æ¿Í¼­(str) Åǹ®ÀÚ¸¦ ½ºÆäÀ̽º·Î º¯È¯½ÃŲ´Ù. * ´Ü ÅÇÀÇ À§Ä¡¿¡ ÇØ´çÇÏ´Â ¸¸Å­ÀÇ ½ºÆäÀ̽º °³¼ö·Î * ġȯÇÑ´Ù. * (*str)[i] Ç¥Çö¿¡ ÁÖÀÇÇÒ°Í, * str[i] ´Â ¿øÇÏÁö ¾Ê´Â °á°ú Ãâ·Â½ÃÅ´ * *****************************************************************/ int dtab(char **str) { int i, j, k, l; int n_tab, n_sp; char *new_str; /* tab °³¼ö üũ */ n_tab = 0; for ( i = 0; *str[i] != '\0'; ++i ) if ( *str[i] == '\t' ) ++n_tab; /* ÆĶó¹ÌÅÍ º¹»çº» - ¾à°£ÀÇ ¸Þ¸ð¸® ³¶ºñ Çã¿ë */ new_str = (char *)malloc(sizeof(char) * (strlen(*str) + (n_tab - 1) * TABSIZE + 1)); if ( new_str == NULL ) { fprintf(stderr, "[dtab: %d] malloc failure\n", __LINE__); return 0; } /* tab -> space!! */ for ( i = 0, j = 0; (*str)[i] != '\0'; ++i ) { new_str[j] = (*str)[i]; if ( new_str[j] == '\n' ) { k = 0; ++j; } else if ( new_str[j] == '\t' ) { n_sp = TABSIZE - (k % TABSIZE); for ( l = 0; l < n_sp; ++l ) new_str[j++] = ' '; } else { ++k; ++j; } } new_str[j] = '\0'; /* ¿øº» ÇØÁ¦ ÈÄ º¹»çº»À» ¿¬°á */ /* ¹®Á¦°¡ ÀÖ´Â ºÎºÐ */ free(*str); *str = new_str; /* DEBUG ÄÚµå */ printf("ori-str length: %d\n", strlen(*str)); printf("new-str length: %d\n", strlen(new_str)); printf("%s", new_str); return 1; } /***************************************************************** * * Function Name: entab * * Input : * Output : * * Comment : * *****************************************************************/ int entab(char **str) { return 0; } /***************************************************************** * * Function Name: write_file * * Input : const char *filename, const char *str * Output : int 1(success), 0(failure) * * Comment : ¹®ÀÚ¿­ ¹è¿­ÀÇ ³»¿ëÀ» ±×´ë·Î ÆÄÀÏ¿¡ ¿Å±ä´Ù. * ÆÄÀÏ ¿­±â, ±â·Ï, ´Ý±âµµ ÀÌ°÷¿¡¼­ ó¸®ÇÑ´Ù. * *****************************************************************/ int write_file(const char *filename, const char *str) { /* - ¹®Á¦°¡ ÀÖ´Ù! ½ºÆ®¸² ¹öÆÛ¸¦ ³Ñ¾î¼­Àΰ¡? * Á¦´ë·Î µÈ °á°ú°¡ ³ª¿ÀÁö ¾Ê´Â´Ù. * ½ÉÁö¾î strÀÇ ³»¿ëÁ¶Â÷µµ fprintf ÀÌÈÄ º¯°æµÈ´Ù. * openÀ» ÀÌ¿ëÇÑ Àú¼öÁØ ÆÄÀÏ ÇÔ¼ö´Â Á¤»ó ÀÛµ¿ * ½ºÆ®¸²ÂÊ ¹®Á¦°¡ ÀÖ´Â °ÍÀ¸·Î ÃßÁ¤ * ÃßÈÄ Á¶»çÇغ¼ °Í!! int fp; printf("%s", str); if ( (fp = fopen(filename, "w")) == NULL ) { fprintf(stderr, "Can't open %s to write\n", filename); return 0; } if ( fprintf(fp, "%s", str) < 0 ) { fprintf(stderr, "Can't write %s\n", filename); return 0; } fclose(fp); */ int fd; if ( (fd = open(filename, O_WRONLY | O_TRUNC, 0)) == -1 ) { fprintf(stderr, "[write_file: %d] Can't open file %s\n", __LINE__, filename); return 0; } printf("%s", str); printf("%d - %d\n", strlen(str), BUFSIZ); printf("fd=%d\n", fd); if ( write(fd, str, strlen(str)) != strlen(str) ) { fprintf(stderr, "[write_file: %d] Write error %s\n", __LINE__, filename); return 0; } close(fd); return 1; } /***************************************************************** * * Function Name: read_file * * Input : const char *filename, char **str * Output : int 1(success), 0(failure); * * Comment : ÅؽºÆ® ÆÄÀÏÀÇ ³»¿ëÀ» ¹®ÀÚ¿­ ¹è¿­¿¡ ´ýÇÁÇÑ´Ù. * ¹®ÀÚ¿­ ¹è¿­ Æ÷ÀÎÅ͸¦ ¹Þ¾Æ¿Í¼­ ±× °ªÀ» ±â·ÏÇÑ´Ù. * ¹®ÀÚ¿­ ¹è¿­Àº ÀÌ ÇÔ¼ö ³»¿¡¼­ ¸Þ¸ð¸® ÇÒ´çÀ̵Ǹç * ÀÌ ¹®ÀÚ¿­ ¹è¿­À» »ç¿ëÇÏ°í ³­ µÚ¿¡´Â ÇØÁ¦µÇ¾î¾ß¸¸ * ÇÑ´Ù. ÆĶó¹ÌÅÍ·Î ¿À´Â º¯¼ö´Â °ªÀÌ ÇÒ´çµÇÁö ¾ÊÀº * ¼ø¼ö Æ÷ÀÎÅÍ¿©¾ßÇÑ´Ù.(¹è¿­Àº ¾ÈµÊ!) * *****************************************************************/ int read_file(const char *filename, char **str) { int n_cnt; char buf[MAXLINE]; FILE *fp; if ( (fp = fopen(filename, "r")) == NULL ) { fprintf(stderr, "[read_file: %d] Can't open file: %s\n", __LINE__, filename); return 0; } /* ÆÄÀÏ ³»¿ë º¹»ç¸¦ À§ÇÑ ¿©À¯ °ø°£ ¸¶·Ã */ n_cnt = 0; while ( fgets(buf, MAXLINE, fp) != NULL ) n_cnt += strlen(buf); *str = (char *)malloc(sizeof(char) * (n_cnt + 1)); if ( *str == NULL ) { fprintf(stderr, "[read_file: %d] Mem alloc err\n", __LINE__); return 0; } /* ÆÄÀÏ Æ÷ÀÎÅÍ Ãʱâ À§Ä¡·Î */ if ( fseek(fp, 0, SEEK_SET) != 0 ) { fprintf(stderr, "[read_file: %d] file pointer seek err\n", __LINE__); return 0; } /* dump!! */ while ( fgets(buf, MAXLINE, fp) != NULL ) strcat(*str, buf); return 1; } /***************************************************************** * * Function Name: check_arg * * Input : int argc, char *argv[] * Output : int 1(success), 0(failure) * * Comment : ÇÁ·Î±×·¥ ½ÇÇà½ÃÀÇ Ä¿¸Çµå¶óÀÎ ¾Æ±Ô¸ÕÆ®¸¦ ó¸® * ÆÄÀϸíÀÌ ÀÖÁö¾Ê´Ù¸é ¿¡·¯¸¦ ¹Ýȯ * *****************************************************************/ int check_arg(int argc, char *argv[]) { if ( argc != 2 ) { fprintf(stderr, "Usage: %s \n", argv[0]); return 0; } return 1; }