home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 2: PC
/
frozenfish_august_1995.bin
/
bbs
/
d09xx
/
d0996.lha
/
Aren
/
aren.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-05
|
3KB
|
156 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ShowError(a) ShowErrorFunc(__LINE__,a)
extern int errno;
char *__ver = "$VER: Aren 1.2$";
void ShowErrorFunc (int line,char *s);
char *StringUpcase(char *s);
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=(char *)malloc(strlen(p)+1))==NULL)
ShowError("malloc");
strcpy(t,p);
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;
}
char *StringUpcase(char *s)
{
char *t,*d;
if ((t=(char*)strdup(s))==NULL)
ShowError("strdup");
d=t;
while(*t)
{
*t=toupper(*t);
t++;
}
return d;
}
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"))
{
CaseSensitive=0;
OldPattern=argv[2];
NewPattern=argv[3];
}
else
{
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((CaseSensitive ? basename(argv[i])+j : StringUpcase(basename(argv[i])+j)),
(CaseSensitive ? OldPattern : StringUpcase(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);
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' to '%s':",argv[i],NewName);
perror("");
}
else
fprintf(stderr,"Renaming '%s' to '%s'\n",argv[i],NewName);
}
}
Found=0;
}
return 0;
}