/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Copyright 2007 mibo@student.math.hr */ #include #include #include #include #include /* opis: bom (Bez Ostalog Molim) je program za automatizirano brisanje bespotrebnih datototeka u ~ direktoriju verzija: 1.13.10.2007. autor: mibo@student.math.hr licenca: GNU GPL compile: gcc -o bom bom.c bugovi: bugove saljite na mibo@student.math.hr; popraviti cu cim uhvatim vremena napomena: ovaj je program pisan za HP-UX (OS na studentu) i vjerojatno ne funkcionira na windoz */ #define PATH_MAX 256 #define OK ".okbom" long int velicina(FILE* datoteka); int postoji(char* naziv, int la, char* nazivi, int lb); long int velicina(FILE* datoteka) { fseek(datoteka, 0, SEEK_END); long r = ftell(datoteka); fseek(datoteka, 0, SEEK_SET); return r; } int postoji(char* naziv, int la, char* nazivi, int lb) { int i, k, a; for(i = 0; i < lb; i += 1) { a = 0; if((lb - i - la) < -1) return 0; if(nazivi[i] == naziv[0]) { for(k = 0; k < la; k += 1) if(nazivi[i + k] == naziv[k]) a += 1; if(a == la) return 1; } } return 0; } int main() { int z, i, d = -1, j, b = 0, v; char *trenutni = malloc(sizeof(char) * PATH_MAX), *okdatoteke, *m, *naredba; char o; struct dirent **nazivi; FILE *datotekaok; if((datotekaok = fopen(OK, "r")) == NULL) { printf("Kreirajte datoteku '%s' u istom direktoriju s ovim programom. ", OK); printf("Datoteka mora sadrzavati niz zarezom odjeljenih datoteka/direktorija "); printf("koje program ignorira. SVE ostale datoteke ce program pobrisati!i\n"); printf("Primjer '%s' datoteke:\nMail,.kshrc,.profile,Dokumenti,skripta.pdf,\n", OK); printf("Napomena: pisati popis bez razmaka, te iza zadnje datoteke popisa "); printf("takoder napisati zarez."); free(trenutni); free(nazivi); return 91; } v = z = velicina(datotekaok); okdatoteke = malloc(sizeof(char) * z); for(i = 0; i < z; i += 1) okdatoteke[i] = fgetc(datotekaok); fclose(datotekaok); printf("\n**********************\nDatoteka %s glasi: \n%s**********************\n", OK, okdatoteke); getcwd(trenutni, PATH_MAX); printf("Jeste li sigurni da zelite pobrisati SVE datoteke (OSIM NAVEDENIH u '%s') iz %s\n", OK, trenutni); printf("d/n : "); scanf("%c", &o); if(o != 'd') return 0; z = scandir(trenutni, &nazivi, 0, alphasort); if(z < 0) { printf(":-(\n"); return 9; } if(z > 0) { for(i = 0; i < z; i += 1) { if(!((strcmp(".", nazivi[i]->d_name) == 0) || (strcmp("..", nazivi[i]->d_name) == 0) || (strcmp(OK, nazivi[i]->d_name) == 0) || (strcmp("bom", nazivi[i]->d_name) == 0) || (strcmp("bom.c", nazivi[i]->d_name) == 0) )) { d = strlen(nazivi[i]->d_name); m = malloc((d + 1) * sizeof(char)); for(j = 0; j < d ; j += 1) m[j] = nazivi[i]->d_name[j]; m[d] = ','; m[d + 1] = '\0'; if(postoji(m, d + 1, okdatoteke, v) == 0) { naredba = malloc(sizeof(char) * (PATH_MAX + 6)); sprintf(naredba, "rm -rf %s", nazivi[i]->d_name); system(naredba); } free(m); free(nazivi[i]); } } } free(trenutni); free(nazivi); return 0; }