From: <bo...@us...> - 2003-11-26 02:05:05
|
Update of /cvsroot/sharedaemon/core/src In directory sc8-pr-cvs1:/tmp/cvs-serv21992 Added Files: bttypes.h main-c.c main-c.h Log Message: Files for recursive directory scanning --- NEW FILE: bttypes.h --- /* * bttypes.h. Part of the bothie-utils. * * Copyright (C) 2001, 2002, 2003 Bodo Thiesen <bo...@gm...> * * 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 */ #ifndef BTTYPES_H #define BTTYPES_H typedef char * string; typedef const char * cstring; # ifndef __cplusplus # ifndef CURSES_H # ifndef bool # define bool int # endif # endif /* #ifndef CURSES_H */ # ifndef false # define false 0 # endif # ifndef true # define true -1 # endif # endif /* ifndef _cplusplus */ #endif // #ifndef BTTYPES_H --- NEW FILE: main-c.c --- /* * main-c.c. Part of the bothie-utils. * * Copyright (C) 2001, 2002, 2003 Bodo Thiesen <bo...@gm...> * * 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 */ #include "main-c.h" #include <dirent.h> //#include <errno.h> //#include <fcntl.h> //#include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> //#include <unistd.h> #ifndef S_IFMT # define S_IFMT 0170000 /* These bits determine file type. */ #endif #ifndef S_IFSOCK # define S_IFSOCK 0140000 /* Socket. */ #endif #ifndef S_ISSOCK # define S_ISSOCK(mode) (((mode) & (S_IFMT)) == (S_IFSOCK)) #endif int lstat(const char *file_name, struct stat *buf); /* * Function char* concat(int n,'n times:'char* str_i); * * Allocates memory and copies the contents of str_i, beginning with the * content of str_0 and finnishing with the content of str_(n-1) to the * newly allocated memory. * * The return value is the address of the new memory area. The latter * must be freed by the caller after use. */ string concat(int n, ...) { va_list ap; int i,string_offset,dest_strlen=1; int* sl; string dest_string; string* str; #ifdef DEBUG bprintf("-> concat\n"); #endif /* * Cache-Variables: Holds the address of the strings, passed as * arguments after 'int n', and their's sizes. * But first allocate them: */ sl =(int *)malloc(sizeof(int )*n); str=(string*)malloc(sizeof(string)*n); if (!sl || !str) { if (sl ) free(sl); if (str) free(str); return NULL; } /* * Walk throw the argument list, and summate the string sizes. * At this time, fill in the determined information in the caches. */ va_start(ap,n); for (i=0;i<n;i++) dest_strlen+=( sl[i]=strlen( str[i]=va_arg(ap,char*) ) ); va_end(ap); /* * Ok, let's allocate the memory for the destination string, ... */ dest_string=(char*)malloc(sizeof(char)*dest_strlen); if (!dest_string) { free(sl); free(str); return NULL; } /* * ... and copy the string's contents together. */ for (i=string_offset=0;i<n;i++) { strcpy(dest_string+string_offset,str[i]); string_offset+=sl[i]; } free(sl); free(str); #ifdef DEBUG bprintf("<- concat\n"); #endif return dest_string; } bool enum_special_inodes=false; void RealEnumFiles( cstring Base, cstring Relative, EnumFunction File, EnumFunction BlockDeviceFunction, EnumFunction CharacterDeviceFunction, EnumFunction PipeFunction, EnumFunction NamedSocketFunction, EnumFunction SymbolicLinkFunction, EnumFunction EnterDirectory, EnumFunction LeaveDirectory ) { DIR *Verzeichnis; struct dirent *Eintrag; struct stat DateiTyp; string SubDirectory,Temp,concat_tmp; Verzeichnis=opendir( concat_tmp=concat( 3, (Base && Base[0])?Base:".", "/", (Relative && Relative[0])?Relative:"." ) ); free(concat_tmp); if (Verzeichnis) { while ((Eintrag=readdir(Verzeichnis))!=0) { if (strcmp(Eintrag->d_name,".") && strcmp(Eintrag->d_name,"..")) { if (Base && Base [0]) { SubDirectory=concat(2,Base,"/"); } else { SubDirectory=concat(1,""); } if (Relative && Relative[0]) { Temp=concat(3,Relative,"/",Eintrag->d_name); } else { Temp=concat(1,Eintrag->d_name); } SubDirectory=concat(2,concat_tmp=SubDirectory,Temp); free(concat_tmp); lstat(SubDirectory,&DateiTyp); /* stat(SubDirectory,&DateiTyp); */ if (S_ISDIR(DateiTyp.st_mode)) { bool EnteringDirectory=true; #ifdef DEBUG bprintf("\tCalling callback-function EnterDirectory\n"); #endif if (EnterDirectory) { EnteringDirectory=EnterDirectory(Base,Relative,Eintrag->d_name); } if (EnteringDirectory) { RealEnumFiles( Base,Temp, File, BlockDeviceFunction, CharacterDeviceFunction, PipeFunction, NamedSocketFunction, SymbolicLinkFunction, EnterDirectory,LeaveDirectory ); if (LeaveDirectory) LeaveDirectory(Base,Relative,Eintrag->d_name); } } else if (S_ISREG(DateiTyp.st_mode)) { #ifdef DEBUG bprintf("\tCalling callback-function File\n"); #endif if (File) File(Base,Relative,Eintrag->d_name); } else if (S_ISBLK(DateiTyp.st_mode)) { #ifdef DEBUG bprintf("\tCalling callback-function BlockDeviceFunction\n"); #endif if (BlockDeviceFunction) BlockDeviceFunction(Base,Relative,Eintrag->d_name); } else if (S_ISCHR(DateiTyp.st_mode)) { #ifdef DEBUG bprintf("\tCalling callback-function CharacterDeviceFunction\n"); #endif if (CharacterDeviceFunction) CharacterDeviceFunction(Base,Relative,Eintrag->d_name); } else if (S_ISFIFO(DateiTyp.st_mode)) { #ifdef DEBUG bprintf("\tCalling callback-function PipeFunction\n"); #endif if (PipeFunction) PipeFunction(Base,Relative,Eintrag->d_name); } else if (S_ISSOCK(DateiTyp.st_mode)) { #ifdef DEBUG bprintf("\tCalling callback-function NamedSocketFunction\n"); #endif if (NamedSocketFunction) NamedSocketFunction(Base,Relative,Eintrag->d_name); } else if (S_ISLNK(DateiTyp.st_mode)) { #ifdef DEBUG bprintf("\tCalling callback-function SymbolicLinkFunction\n"); #endif if (SymbolicLinkFunction) SymbolicLinkFunction(Base,Relative,Eintrag->d_name); } free(SubDirectory); } } closedir(Verzeichnis); } } void EnumFiles(cstring BasePath,EnumFunction FileFunction,EnumFunction BeforeDirectoryFunction,EnumFunction AfterDirectoryFunction) { EnumFunction null=enum_special_inodes?FileFunction:NULL; RealEnumFiles( BasePath,"", FileFunction, null,null,null,null,null, BeforeDirectoryFunction, AfterDirectoryFunction ); } --- NEW FILE: main-c.h --- /* * main-c.h. Part of the bothie-utils. * * Copyright (C) 2001, 2002, 2003 Bodo Thiesen <bo...@gm...> * * 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 */ #ifndef MAIN_C_H #define MAIN_C_H #include "bttypes.h" typedef bool (*EnumFunction)(cstring,cstring,cstring); #ifdef __cplusplus extern "C" { #endif // #ifdef __cplusplus string concat(int n,...); void EnumFiles(cstring BasePath,EnumFunction FileFunction,EnumFunction BeforeDirectoryFunction,EnumFunction AfterDirectoryFunction); string concat_filename(cstring BasePath,cstring RelativePath,cstring FileName); string read_entire_file(int fd,int * size); #ifdef __cplusplus }; #endif // #ifdef __cplusplus #endif // #ifndef MAIN_C_H |