Implemented option -find alias -findi
This commit is contained in:
164
test/xorriso.c
164
test/xorriso.c
@ -2417,6 +2417,109 @@ int Linkitem_find(struct LinkiteM *stack, dev_t target_dev, ino_t target_ino,
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------- FindjoB -------------------------------- */
|
||||
|
||||
|
||||
struct FindjoB {
|
||||
char *start_path;
|
||||
|
||||
char *name_expr;
|
||||
#ifdef Xorriso_with_regeX
|
||||
regex_t name_re;
|
||||
regmatch_t name_match;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
int Findjob_destroy(struct FindjoB **job, int flag);
|
||||
|
||||
|
||||
int Findjob_new(struct FindjoB **o, char *start_path, int flag)
|
||||
{
|
||||
struct FindjoB *m;
|
||||
|
||||
m= *o= TSOB_FELD(struct FindjoB,1);
|
||||
if(m==NULL)
|
||||
return(-1);
|
||||
m->name_expr= NULL;
|
||||
m->start_path= strdup(start_path);
|
||||
if(m->start_path==NULL)
|
||||
goto failed;
|
||||
return(1);
|
||||
failed:;
|
||||
Findjob_destroy(o, 0);
|
||||
return(-1);
|
||||
}
|
||||
|
||||
|
||||
int Findjob_destroy(struct FindjoB **o, int flag)
|
||||
{
|
||||
struct FindjoB *m;
|
||||
|
||||
m= *o;
|
||||
if(m==NULL)
|
||||
return(0);
|
||||
if(m->start_path!=NULL)
|
||||
free(m->start_path);
|
||||
if(m->name_expr!=NULL) {
|
||||
#ifdef Xorriso_with_regeX
|
||||
regfree(&(m->name_re));
|
||||
#endif
|
||||
free(m->name_expr);
|
||||
}
|
||||
free((char *) m);
|
||||
*o= NULL;
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
int Findjob_set_name_expr(struct FindjoB *o, char *name_expr, int flag)
|
||||
{
|
||||
char regexpr[2*SfileadrL+2];
|
||||
|
||||
if(o->name_expr!=NULL) {
|
||||
#ifdef Xorriso_with_regeX
|
||||
regfree(&(o->name_re));
|
||||
#endif
|
||||
free(o->name_expr);
|
||||
o->name_expr= NULL;
|
||||
}
|
||||
if(strlen(name_expr)>=SfileadrL)
|
||||
return(0);
|
||||
o->name_expr= strdup(name_expr);
|
||||
if(o->name_expr==NULL)
|
||||
return(-1);
|
||||
Xorriso__bourne_to_reg(name_expr, regexpr, 0);
|
||||
if(regcomp(&(o->name_re), regexpr, 0)!=0)
|
||||
return(0);
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
/* @return 0=no match , 1=match , <0 = error
|
||||
*/
|
||||
int Findjob_test(struct FindjoB *o, char *name,
|
||||
struct stat *boss_stbuf, struct stat *stbuf,
|
||||
int depth, int flag)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if(o->name_expr!=NULL) {
|
||||
#ifdef Xorriso_with_regeX
|
||||
ret= regexec(&(o->name_re),name,1,&(o->name_match),0);
|
||||
#else
|
||||
ret= !(strcmp(name, o->name_expr)==0 || strcmp(o->name_expr, "*")==0);
|
||||
#endif
|
||||
if(ret!=0)
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* ??? >>> more tests to come ?*/;
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------- Xorriso -------------------------------- */
|
||||
|
||||
/** The list of startup file names */
|
||||
@ -3260,6 +3363,8 @@ cannot_compile:;
|
||||
}
|
||||
|
||||
|
||||
/* @return 0=match , else no match
|
||||
*/
|
||||
int Xorriso_regexec(struct XorrisO *xorriso, char *to_match, int *failed_at,
|
||||
int flag)
|
||||
/*
|
||||
@ -3860,7 +3965,7 @@ int Xorriso_check_for_root_pattern(struct XorrisO *xorriso,
|
||||
/* @param flag bit0= prepend wd only if name does not begin by '/'
|
||||
bit2= prepend wd (automatically done if wd[0]!=0)
|
||||
*/
|
||||
int Xorriso_make_pattern_adr(struct XorrisO *xorriso, char *wd, char *name,
|
||||
int Xorriso_make_abs_adr(struct XorrisO *xorriso, char *wd, char *name,
|
||||
char adr[], int flag)
|
||||
{
|
||||
if((wd[0]!=0 || (flag&4)) && !((flag&1) && name[0]=='/')) {
|
||||
@ -3972,7 +4077,7 @@ int Xorriso_obtain_pattern_files_x(
|
||||
{ret= -1; goto ex;}
|
||||
}
|
||||
|
||||
ret= Xorriso_make_pattern_adr(xorriso, wd, name, adr, flag&4);
|
||||
ret= Xorriso_make_abs_adr(xorriso, wd, name, adr, flag&4);
|
||||
if(ret<=0)
|
||||
goto ex;
|
||||
|
||||
@ -3983,7 +4088,7 @@ int Xorriso_obtain_pattern_files_x(
|
||||
path= adr;
|
||||
if(adr[0]!='/') {
|
||||
path= path_data;
|
||||
ret= Xorriso_make_pattern_adr(xorriso, xorriso->wdx, adr, path, 1|4);
|
||||
ret= Xorriso_make_abs_adr(xorriso, xorriso->wdx, adr, path, 1|4);
|
||||
if(ret<=0)
|
||||
goto ex;
|
||||
}
|
||||
@ -5059,7 +5164,7 @@ int Xorriso_lsx_filev(struct XorrisO *xorriso, char *wd,
|
||||
|
||||
/* Count valid nodes, warn of invalid ones */
|
||||
for(i= 0; i<filec; i++) {
|
||||
ret= Xorriso_make_pattern_adr(xorriso, wd, filev[i], path, 1|4);
|
||||
ret= Xorriso_make_abs_adr(xorriso, wd, filev[i], path, 1|4);
|
||||
if(ret<=0) {
|
||||
was_error++;
|
||||
continue;
|
||||
@ -5084,7 +5189,7 @@ int Xorriso_lsx_filev(struct XorrisO *xorriso, char *wd,
|
||||
passes= 1+!(flag&(4|8));
|
||||
for(pass= 0; pass<passes; pass++)
|
||||
for(i= 0; i<filec && !(xorriso->request_to_abort); i++) {
|
||||
ret= Xorriso_make_pattern_adr(xorriso, wd, filev[i], path, 1|4);
|
||||
ret= Xorriso_make_abs_adr(xorriso, wd, filev[i], path, 1|4);
|
||||
if(ret<=0)
|
||||
continue;
|
||||
ret= lstat(path, &stbuf);
|
||||
@ -6036,17 +6141,47 @@ sorry_ex:
|
||||
|
||||
|
||||
/* Option -find alias -findi */
|
||||
int Xorriso_option_findi(struct XorrisO *xorriso, char *pattern, int flag)
|
||||
int Xorriso_option_findi(struct XorrisO *xorriso, int argc, char **argv,
|
||||
int *idx, int flag)
|
||||
{
|
||||
char sfe[5*SfileadrL];
|
||||
int ret, i, end_idx;
|
||||
struct FindjoB *job= NULL;
|
||||
char *start_path, sfe[5*SfileadrL];
|
||||
struct stat dir_stbuf;
|
||||
|
||||
fprintf(stderr, ">>> XORRISO : filter by pattern %s\n", pattern);
|
||||
/* >>> prepare regex */
|
||||
end_idx= Xorriso_end_idx(xorriso, argc, argv, *idx, 1);
|
||||
start_path= xorriso->wdi;
|
||||
if(end_idx > *idx)
|
||||
start_path= argv[*idx];
|
||||
ret= Findjob_new(&job, start_path, 0);
|
||||
if(ret<=0) {
|
||||
sprintf(xorriso->info_text, "-find[ix]: cannot set create find job object");
|
||||
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
|
||||
{ret= -1; goto ex;}
|
||||
}
|
||||
|
||||
fprintf(stderr, ">>> LIBISOFS : traverse ISO tree beginning at %s\n",
|
||||
Text_shellsafe(xorriso->wdi, sfe, 0));
|
||||
|
||||
return(1);
|
||||
for(i= *idx+1; i<end_idx; i++) {
|
||||
if(strcmp(argv[i], "-name")==0) {
|
||||
i++;
|
||||
ret= Findjob_set_name_expr(job, argv[i], 0);
|
||||
if(ret<=0) {
|
||||
sprintf(xorriso->info_text, "-find[ix]: cannot set -name expression %s",
|
||||
Text_shellsafe(argv[i], sfe, 0));
|
||||
goto sorry_ex;
|
||||
}
|
||||
} else {
|
||||
sprintf(xorriso->info_text, "-find[ix]: unknown option %s",
|
||||
Text_shellsafe(argv[i], sfe, 0));
|
||||
sorry_ex:;
|
||||
Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "SORRY", 0);
|
||||
{ret= 0; goto ex;}
|
||||
}
|
||||
}
|
||||
ret= Xorriso_findi(xorriso, job, NULL, start_path, &dir_stbuf, 0, 0);
|
||||
ex:;
|
||||
Findjob_destroy(&job, 0);
|
||||
(*idx)= end_idx;
|
||||
return(ret);
|
||||
}
|
||||
|
||||
|
||||
@ -7486,8 +7621,7 @@ next_command:;
|
||||
ret= Xorriso_option_follow(xorriso, arg1, 0);
|
||||
|
||||
} else if(strcmp(cmd,"find")==0 || strcmp(cmd,"findi")==0) {
|
||||
(*idx)++;
|
||||
ret= Xorriso_option_findi(xorriso, arg1, 0);
|
||||
ret= Xorriso_option_findi(xorriso, argc, argv, idx, 0);
|
||||
|
||||
} else if(strcmp(cmd,"findx")==0) {
|
||||
(*idx)++;
|
||||
|
Reference in New Issue
Block a user