Advertisement
rmword

Untitled

Oct 4th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define MAXCHAR 1000
  9.  
  10. int main() {
  11. pid_t child_id;
  12. int status;
  13. char file_name[] = "access.log.2";
  14. child_id = fork();
  15.  
  16. if (child_id < 0) {
  17. exit(EXIT_FAILURE);
  18. }
  19.  
  20. if (child_id == 0) {
  21. // this is child
  22.  
  23. char *argv[] = {"unzip","access.log.2.zip", NULL};
  24. execv("/usr/bin/unzip", argv);
  25. }
  26.  
  27. else {
  28. // this is parent
  29. while ((wait(&status)) > 0);
  30. FILE *fo, *fw;
  31. char *line = NULL;
  32. size_t len = 0;
  33. ssize_t read;
  34.  
  35. fo = fopen("access.log.2","r");
  36. fw = fopen("output.txt","a");
  37.  
  38. while ((read = getline(&line, &len, fo)) != -1)
  39. {
  40. if(strstr(line,"Apache"))
  41. {
  42. fprintf(fw,"%s\n",line);
  43. }
  44. }
  45. fclose(fo);
  46. fclose(fw);
  47.  
  48. status = remove("access.log.2");
  49.  
  50. if(status == 0)
  51. printf("%s successfully deleted\n",file_name);
  52. else
  53. printf("Unable to delete file\n");
  54. }
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement