home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define ShowError(a) ShowErrorFunc(__LINE__,a)
- extern int errno;
-
- char *__ver = "$VER: Aren 1.4$";
-
-
- void ShowErrorFunc (int line,char *s);
- int (*StrNCmp)();
-
- void ShowErrorFunc(int line,char *s)
- {
- fprintf(stderr,"%s: Error on line %d, ",s,line);
- perror("");
- exit(errno);
- }
-
- void usage(char *s)
- {
- fprintf(stderr,"%s by MENDEZ Marc. (%s %s) All right reserved. Copyright 1994\n",s,__DATE__,__TIME__);
- fprintf(stderr,"Usage: %s [-i] oldpattern newpattern file [file...]\n",s);
- exit(1);
- }
-
- char * basename(char *f)
- {
- char *p,*t ;
-
- if ((p = strrchr(f,'/')) == NULL)
- p = strrchr(f,':');
-
- p = (p == NULL ? f : p+1);
-
- if ((t = strdup(p)) == NULL)
- ShowError("malloc");
-
- return t;
- }
-
- char * dirname(char *f)
- {
- char *p;
-
- if ((p = (char *)malloc(strlen(f)-strlen(basename(f))+1)) == NULL)
- ShowError("malloc");
-
- strncpy(p,f,strlen(f)-strlen(basename(f)));
- p[strlen(f)-strlen(basename(f))]='\0';
-
-
- return p;
- }
-
-
-
- int main(int arc, char *arv[])
- {
- int i,j;
- char *OldPattern, *NewPattern;
-
- char Found=0; /* Flag. 1 if a filename matches the OldPattern */
- char *NewName;
- int argc;
- char **argv;
- char CaseSensitive=1;
- char *directory;
-
-
-
- if (arc <4 || (!strcmp(arv[1],"-i") && arc < 5) )
- usage(arv[0]);
-
-
- #ifdef _DCC /* Compatibility with UNIX. */
- if (expand_args(arc, arv, &argc, &argv)!=0)
- ShowError("expand_args");
- #else
- argv=arv;
- argc=arc;
- #endif
-
- if (!strcmp(argv[1],"-i"))
- {
- StrNCmp = strncmp;
- OldPattern=argv[2];
- NewPattern=argv[3];
- }
- else
- {
- StrNCmp = strnicmp;
- OldPattern=argv[1];
- NewPattern=argv[2];
- }
-
-
-
- for (i=3;i<argc; i++)
- {
- for(j=0;!Found && j<(strlen(basename(argv[i]))-strlen(OldPattern)+1);j++)
- {
- if (!StrNCmp(basename(argv[i])+j,
- OldPattern,
- strlen(OldPattern))
- )
- {
- Found=1;
-
- if ((NewName = (char*)malloc(strlen(argv[i]) -
- strlen(OldPattern) +
- strlen(NewPattern) + 1)) ==
- NULL)
- ShowError("malloc");
-
- directory = dirname(argv[i]);
-
-
- strcpy(NewName,directory);
- if (j!=0)
- strncat(NewName,basename(argv[i]),j);
- NewName[j+strlen(directory)]='\0';
- /* Important. If you don't add a '\0', */
- /* the 'strcat' function will add the */
- /* text anywhere, just after the first */
- /* '\0' it finds. */
-
- strcat(NewName,NewPattern);
- strcat(NewName,argv[i]+strlen(directory)+j+strlen(OldPattern));
- if (rename(argv[i],NewName)<0)
- {
- fprintf(stderr,"Error while renaming '%s' as '%s':",argv[i],NewName);
- perror("");
- }
- else
- fprintf(stderr,"Renaming '%s' as '%s'\n",argv[i],NewName);
- }
- }
- Found=0;
- }
-
-
- return 0;
-
- }
-