// cleana.c // Removes lines containing 'key' from 'file' with 'exception' #include #include #include #include #include main(int argc, char *argv[]) { FILE *fd; FILE *cleanfd; char buffer[200],filec[100]; if (argc < 3){ printf("Usage: ./cleana file key [exception]\n"); exit(1); } strcat(filec, argv[1]); strcat(filec,".cleaned"); // OPEN FILE if ((fd = fopen(argv[1], "r")) == NULL){ printf("Fatal: Cannot open file!\n"); exit(1); } if ((cleanfd = fopen(filec, "w+")) == NULL){ printf("Fatal: Cannot open/create clean file!\n"); exit(1); } // CLEAN FILE while((fgets(buffer, sizeof(buffer), fd)) != NULL){ if ((strstr(buffer, argv[2])) == NULL){ fputs(buffer, cleanfd); } else { if (argc == 4){ if ((strstr(buffer, argv[3])) != NULL){ fputs(buffer, cleanfd); } } } } fclose(cleanfd); fclose(fd); if ((execl("/bin/", "mv", filec, argv[1])) == -1) perror("execl"); printf("Operation completed!\n"); } // END MAIN