/*--------------------------------------------------------------------------*\ * PROGRAM: mvp - move with pattern, version 1.01 * * PURPOSE: Rename a set of files according to a given pattern. * * BLURB: Are you tired of running into a situation where you have a * bunch of files that you want to rename in the same way, but * you have no tool to do that for you? For instance, you might * have a bunch of files 'data01.dat' to 'data99.dat' and you * want to rename them to use four digits for the numbers. Well, * now you can. This program will allow you to make simple * changes to groups of filenames by allowing you to insert a * common substring or cut one from a certain point in all of the * filenames given. * * WEB PAGE: http://www.pobox.com/~csbruce/unix/ * * HISTORY: * DATE PROGRAMMER DESCRIPTION * 03-Oct-1997 Craig Bruce Initial creation \*--------------------------------------------------------------------------*/ #include #include #include #define VERSION "mvp version 1.01 by Craig Bruce, 03-Oct-1996" #define NAME_LEN 1000 #define TRUE (!0) #define FALSE (!1) #define ERR_PATMATCH -1 #define ERR_PATINVAL -2 #define ERR_TOOCOMPL -3 int main( int argc, char *argv[] ); void UsageAbort( void ); int GetNewName( char *filename, char *pattern, char *newname ); int MoveFile( char *filename, char *newname ); int CopyFile( char *filename, char *newname ); char *progname; int verbose; int debug; int copyFiles; int preserveFiles; /****************************************************************************/ int main( int argc, char *argv[] ) { int i, j, err; char *pattern; char *filename; char newname[NAME_LEN]; verbose = FALSE; debug = FALSE; copyFiles = FALSE; preserveFiles = FALSE; progname = argv[0]; if (argc < 3) UsageAbort(); pattern = argv[argc-1]; for (i=1; i inDex) return( ERR_PATMATCH ); for (backMatchDex-=1,inDex-=1; backMatchDex>=0; backMatchDex-=1,inDex-=1) { if (backMatchBuf[backMatchDex] != filename[inDex] && backMatchBuf[backMatchDex]!='?') { return( ERR_PATMATCH ); } if (state==STATE_LITERAL) { outDex -= 1; } } inDex += 1; } matchForward = TRUE; if (pch=='\0') break; } else { if (pch=='*') return( ERR_TOOCOMPL ); backMatchBuf[backMatchDex++] = pch; if (debug) { backMatchBuf[backMatchDex] = '\0'; fprintf(stderr, " backMatchBuf=\"%s\"\n", backMatchBuf); } pch = -1; } } switch( pch ) { case -1: break; case '+': if (state==STATE_INSERT) { state = STATE_LITERAL; } else { if (state != STATE_LITERAL) return( ERR_PATINVAL ); state = STATE_INSERT; } break; case '%': if (state==STATE_SKIP) { state = STATE_LITERAL; } else { if (state != STATE_LITERAL) return( ERR_PATINVAL ); state = STATE_SKIP; } break; case '^': if (state==STATE_INSERT) return( ERR_PATINVAL ); inDex = 0; break; case '*': if (state==STATE_INSERT) return( ERR_PATINVAL ); if (state==STATE_SKIP) { inDex = strlen( filename ); } else { while (filename[inDex] != '\0') { newname[outDex++] = filename[inDex++]; } } matchForward = FALSE; backMatchDex = 0; break; case '?': pch = filename[inDex]; /*fall through*/ default: newname[outDex] = pch; switch( state ) { case STATE_LITERAL: if (filename[inDex] != pch) return( ERR_PATMATCH ); inDex += 1; outDex += 1; break; case STATE_INSERT: outDex += 1; break; case STATE_SKIP: if (filename[inDex] != pch) return( ERR_PATMATCH ); inDex += 1; break; } break; } patDex += 1; if (debug) { newname[outDex] = '\0'; printf("patDex=%d, inDex=%d, outDex=%d, newname=\"%s\"\n", patDex, inDex, outDex, newname); } } newname[outDex] = '\0'; if (state != STATE_LITERAL) return( ERR_PATINVAL ); return( 0 ); } /*--------------------------------------------------------------------------*\ * END OF PROGRAM: mvp.c \*--------------------------------------------------------------------------*/