Created
August 5, 2024 06:03
-
-
Save honki12345/4ad33907caee440f55be3c531ddaaa84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* ************************************************************************** */ | |
| /* */ | |
| /* ::: :::::::: */ | |
| /* hookim_cat.c :+: :+: :+: */ | |
| /* +:+ +:+ +:+ */ | |
| /* By: hookim <[email protected]> +#+ +:+ +#+ */ | |
| /* +#+#+#+#+#+ +#+ */ | |
| /* Created: 2024/08/05 11:39:14 by hookim #+# #+# */ | |
| /* Updated: 2024/08/05 14:30:31 by hookim ### ########.fr */ | |
| /* */ | |
| /* ************************************************************************** */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| static void ft_do_cat_recursive(FILE *f); | |
| static void ft_process_do_cat(int c); | |
| void ft_process_argument(char *path_name) | |
| { | |
| FILE *f; | |
| f = fopen(path_name, "r"); | |
| if (!f) | |
| { | |
| perror(path_name); | |
| exit(1); | |
| } | |
| ft_do_cat_recursive(f); | |
| fclose(f); | |
| } | |
| void ft_argument_recursive(int argc, char **argv, int idx) | |
| { | |
| if (idx >= argc) | |
| return ; | |
| ft_process_argument(argv[idx]); | |
| ft_argument_recursive(argc, argv, idx + 1); | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| int i; | |
| FILE *f; | |
| if (argc == 1) | |
| ft_do_cat_recursive(stdin); | |
| else | |
| ft_argument_recursive(argc, argv, 1); | |
| exit(0); | |
| } | |
| static void ft_do_cat_recursive(FILE *f) | |
| { | |
| int c; | |
| c = fgetc(f); | |
| if (c == EOF) | |
| return ; | |
| ft_process_do_cat(c); | |
| ft_do_cat_recursive(f); | |
| } | |
| static void ft_process_do_cat(int c) | |
| { | |
| if ((char) c == '\t') | |
| { | |
| if (fputs("\\t", stdout) == EOF) | |
| exit(1); | |
| } | |
| if ((char) c == '\n') | |
| { | |
| if (fputs("$\n", stdout) == EOF) | |
| exit(1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment