Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <string.h>
- #define MAXCHAR 1000
- int main() {
- pid_t child_id;
- int status;
- char file_name[] = "access.log.2";
- child_id = fork();
- if (child_id < 0) {
- exit(EXIT_FAILURE);
- }
- if (child_id == 0) {
- // this is child
- char *argv[] = {"unzip","access.log.2.zip", NULL};
- execv("/usr/bin/unzip", argv);
- }
- else {
- // this is parent
- while ((wait(&status)) > 0);
- FILE *fo, *fw;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- fo = fopen("access.log.2","r");
- fw = fopen("output.txt","a");
- while ((read = getline(&line, &len, fo)) != -1)
- {
- if(strstr(line,"Apache"))
- {
- fprintf(fw,"%s\n",line);
- }
- }
- fclose(fo);
- fclose(fw);
- status = remove("access.log.2");
- if(status == 0)
- printf("%s successfully deleted\n",file_name);
- else
- printf("Unable to delete file\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement