Code:
/* log cleaner utility :o) *
* It is often used ./logclean /var/log *
* signoff predator *
* preedator(at)sendmail(dot)ru *
**********************************************************************/
/* include files */
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
/* path to /dev/null */
char *linkovano="/dev/null";
/* Name of dir which is passed as 2nd argument */
char *dir_name;
/* some usefull variables */
struct DIR * dir;
struct dirent *open_dir;
/* Declaration of functions */
int is_dir(const char *);
int check_for_dev_null();
int cleanlog(const char *);
int yes_dir(const char *);
int main(int argc,char **argv){
printf(" log cleaner by predator <
[email protected]>\n");
if (!check_for_dev_null()){
printf(" Aborting,aborting.../dev/null isn't char device\n");
printf(" Logs didn't chang,please remove all /var/log \n");
exit(1);
}
if (argc!=2){
printf(" Usage %s <log dir> \n",argv[0]);
exit(1);
}
dir_name=argv[1];
dir=(struct DIR *)opendir(dir_name);
chdir(dir_name);
while ((open_dir=readdir((DIR *)dir))!=NULL){
if (!is_dir(open_dir->d_name)){
chdir(dir_name);
cleanlog(open_dir->d_name);
}else{
/* if dir is '.' or '..' ignore it *
* I got SIGSEGV with this :o) */
if (strcmp(open_dir->d_name,".")!=0 && strcmp(open_dir->d_name,"..")!=0){
yes_dir(open_dir->d_name);
}
}
}
closedir((DIR *)dir);
return 0;
}
/* This function will check our file it is called after readdir */
int is_dir(const char *path){
struct stat buf;
lstat(path,&buf);
if (buf.st_mode & S_IFDIR)
return 1; //1 if it is dir
return 0; //0 if it is file
}
/* This function will check is /dev/null char device *
* If it isn't log cleaner will abort */
int check_for_dev_null(){
struct stat buf;
lstat(linkovano,&buf);
if (buf.st_mode & S_IFCHR)
return 1;
return 0;
}
/* This function is mother of all :o) *
* if file is directory is is called *
* This one use recursion function call */
int yes_dir(const char *path){ //funny name yes_dir :o))))
struct DIR *tmp;
struct dirent *tmp_dir;
/* Opening our dir path */
tmp=(struct DIR *)opendir(path);
if ( tmp==NULL )
return 1;
/* No chdir() to that dir */
chdir(path);
/* Show may begin */
while ((tmp_dir=readdir((DIR *)tmp))!=NULL){
/* Now chceck if file is dir */
if (!is_dir(tmp_dir->d_name)){
/* If it isn't call cleanlog() */
cleanlog(tmp_dir->d_name);
}else{
/* if dir is '.' or '..' ignore it *
* I got SIGSEGV with this :o) */
if (strcmp(tmp_dir->d_name,".")!=0 && strcmp(tmp_dir->d_name,"..")!=0){
/* Call again this function */
yes_dir(tmp_dir->d_name);
}
}
}
/* When we clean our subdir we must go one dir up */
closedir((DIR *)tmp);
chdir("..");
return 0;
}
/* This functio will delete file and link it to /dev/null */
int cleanlog(const char *path){
unlink(path);
symlink(linkovano,path);
return 0;
}