int mpt_scan(char* file) { char cwd[NAME_MAX+1]; spod_path_splited* sp; struct stat stats; res = XMALLOCD0(results, "ignore"); if(stat(file, &stats) == -1) { printf("Could not get stats for %s\n", file); perror(file); exit(1); } printf("Going to check recursivly %s\n", file); if(access(file, F_OK | R_OK)) { perror(file); return -1; } stats.st_mode &= S_IFMT; if((stats.st_mode & S_IFREG) == S_IFREG) { puts("Target is a regular file. Doing expr. and perish."); sp = spod_split_path(file); do_expr(sp->head, sp->tail); exit(0); } if(access(file, F_OK | X_OK)) { perror(file); return -1; } /* go for it */ desc_dir(file, 0); //xprint_malloc_stat(); return 0; failed: return 1; }
static int do_paren( void ) { int op; if( 1 > nparens-- ) { return( not_ok ); } do { op = do_expr(); if( op < ok ) { break; } } while( get_prio( (char)op ) ); return( op ); }
static int evaluate( char * * line, long *val ) { long arg; char * ptr; char * str; char * endptr; int ercode; operator * op; int expr_oper; // looking for term or operator expr_oper = 0; coper = 0; cvalue = 0; nparens = 0; ptr = *line; while( *ptr ) { if( *ptr == ' ' ) { if( ignore_blanks ) { ptr++; continue; } else { break; } } switch( expr_oper ) { case 0: // look for term str = get_exp( ptr ); if( str == NULL ) { // nothing is error return( not_ok ); } op = get_op( str ); if( *(str +1) == NULC ) { if( NULL != op ) { push_op( op->operc ); ptr++; break; } if( (*str == '-' ) || (*str == '+' ) ) { push_op(*str); ++ptr; break; } } arg = strtol( str, &endptr, 10 ); if( (((arg == LONG_MIN) || (arg == LONG_MAX)) && errno == ERANGE) || (str == endptr) ) { return( not_ok ); } push_val( arg ); ptr += endptr - str; // to the next unprocessed char expr_oper = 1; // look for operator next break; case 1: // look for operator op = get_op( ptr ); if( NULL == op ) { return( not_ok ); } if( ')' == *ptr ) { ercode = do_paren(); if( ok > ercode ) { return( ercode ); } } else { while( coper && op->priority <= get_prio_m1() ) { do_expr(); } push_op( op->operc ); expr_oper = 0; // look for term next } ptr++; break; } } while( 1 < cvalue ) { ercode = do_expr(); if( ok > ercode ) return ercode; } if( !coper ) { *line = ptr; // next scan position return( pop_val( val ) ); // no operations left return result } else { return( not_ok ); } }
/* This function is recursivly scaning a directory and all file in it for a match */ void desc_dir(char *dir, unsigned int parent_depth) { DIR *dir_struct = NULL; struct dirent *entry; #ifdef DEBUG assert(dir && parent_depth >= 0); #endif /* Check max depth */ if(MAXDEPTH > 0 && parent_depth > MAXDEPTH) { #ifdef DEBUG printf("DEBUG: Max depth prevents mpfind descending into level %d (trying to scan %s)\n", parent_depth, dir); #endif return; } ++parent_depth; /* Change dir */ if(chdir(dir) == -1) { #ifdef DEBUG puts("DEBUG: Could not change directory"); #endif perror(dir); return; } dir_struct = opendir(dir); if(!dir_struct) perror(dir); /* Looping through every dir-entry and examine file or descend down further */ while(1) { struct stat stats; entry = readdir(dir_struct); if(!entry) { #ifdef DEBUG puts("DEBUG: EOF"); #endif /* perror(dir);*/ break; } /* skipping . and .. */ if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } #ifdef DEBUG printf("DEBUG: Checking dir entry %s\n", entry->d_name); #endif /* Get stats for entry */ if(stat(entry->d_name, &stats) == -1) { char *fullfilename = xmallocd(NAME_MAX + 1, "ignore"); #ifdef DEBUG printf("DEBUG: Could not get stats for %s\n", entry->d_name); #endif if(dir[strlen(dir)-1] == '/') snprintf(fullfilename, NAME_MAX, "%s%s", dir, entry->d_name); else snprintf(fullfilename, NAME_MAX, "%s/%s", dir, entry->d_name); perror(fullfilename); xfree(fullfilename); continue; } /* init inode list if necessary and check if inode was allready scanned */ if(!first_elem) { /* First run, eh? */ #ifdef DEBUG puts("DEBUG: Setting up list of scanned inodes"); #endif last_elem = xmallocd(sizeof(proc_list), "ignore"); last_elem->inode = stats.st_ino; last_elem->next = NULL; first_elem = last_elem; } else { proc_list *ptr; int skip = 0; #ifdef DEBUG assert(last_elem && first_elem); #endif ptr = first_elem; while(ptr) { if(stats.st_ino == ptr->inode) { #ifdef DEBUG puts("DEBUG: Inode allready scanned, skipping"); #endif skip = 1; break; } ptr = (proc_list*)ptr->next; } if(skip) continue; /* Add this inode to the end of the list */ last_elem->next = xmallocd(sizeof(proc_list), "ignore"); last_elem = (proc_list*)last_elem->next; last_elem->inode = stats.st_ino; last_elem->next = NULL; } if(access(entry->d_name, F_OK | R_OK) == -1) { char *fullfilename = xmallocd(NAME_MAX + 1, "ignore"); if(dir[strlen(dir)-1] == '/') snprintf(fullfilename, NAME_MAX, "%s%s", dir, entry->d_name); else snprintf(fullfilename, NAME_MAX, "%s/%s", dir, entry->d_name); perror(fullfilename); continue; } #ifdef DEBUG printf("DEBUG: Scanning inode %d\n", stats.st_ino); #endif /* Checking stats and decide what to do */ stats.st_mode &= S_IFMT; if((stats.st_mode & S_IFLNK) == S_IFLNK) { #ifdef DEBUG puts("DEBUG: inode is symbolic link, going to ignore for now.."); #endif continue; } if((stats.st_mode & S_IFREG) == S_IFREG) { #ifdef DEBUG puts("DEBUG: inode is a regular file."); #endif if(stats.st_size > 128) { do_expr(dir, entry->d_name); } #ifdef DEBUG else { puts("DEBUG: File has invalid size to have a tag"); } #endif } else if((stats.st_mode & S_IFDIR) == S_IFDIR) { char *todesc; #ifdef DEBUG puts("DEBUG: inode is a directory"); #endif if(access(entry->d_name, R_OK | X_OK) == -1) { perror(entry->d_name); } else { todesc = xmallocd(NAME_MAX + 1, "ignore"); memset(todesc, 0, NAME_MAX+1); if(dir[strlen(dir)-1] == '/') snprintf(todesc, NAME_MAX+1, "%s%s", dir, entry->d_name); else snprintf(todesc, NAME_MAX+1, "%s/%s", dir, entry->d_name); desc_dir(todesc, parent_depth) ; /* Change back to our dir */ if(chdir(dir) == -1) { #ifdef DEBUG puts("DEBUG: Could not change directory"); #endif perror(dir); return; } } } } closedir(dir_struct); }