home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
upperdir.lzh
/
upperdir.c
< prev
Wrap
C/C++ Source or Header
|
1994-09-11
|
7KB
|
203 lines
/* UpperDir.c */
/* This programme converts all filenames to lowercase */
/* and all directory names to uppercase */
/* Suitable for use on OS9 level 1 and 2 */
/* CopyRight (c) 1989 by Bob Devries */
/* This programme may be freely distributed provided */
/* that this message is not removed from the source code */
#include <stdio.h>
#ifndef OSK
#include <os9.h>
#else
#include <errno.h>
#endif
#include <direct.h>
#include <modes.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
int path,dirpath;
static struct dirent dir;
main(argc,argv)
int argc;
char *argv[];
{
char name[29];
char newname[29];
char temp[81];
char dirname[256]; /* allow for complete pathname */
static char spaces[] = " ";
int changed;
int i,attr;
setbuf(stdout,NULL);
puts(" ");
if (argc > 1)
{
if((argv[1][0] == '-') && (argv[1][1] == '?'))
{
help(); /* argument '-?' prints help message */
exit(0); /* and quits. so try again */
}
strcpy(dirname,argv[1]);
if(chdir(dirname) == EOF)
{
puts("Can't open that directory.");
help();
exit(errno);
}
}
if ((dirpath=open(".",S_IREAD+S_IFDIR+S_IWRITE))==EOF) /* open dir */
{
puts("Can't open working directory.");
exit(errno); /* exit if opendir failed */
}
read(dirpath,&dir,sizeof(dir)); /* read past '..' and '.' */
read(dirpath,&dir,sizeof(dir)); /* entries in dir */
while((read(dirpath,&dir,sizeof(dir))) != NULL) /* keep reading */
{ /* until end of dir */
if (dir.dir_name[0] != '\0') /* skip killed files */
{
extname(dir.dir_name,name); /* extract filename */
if((path=open(name,S_IREAD))==EOF) /* open file */
{ /* if that fails */
if((path=open(name,S_IREAD+S_IFDIR))==EOF) /* open as dir */
continue;
}
attr = readfd(path); /* get file descriptor */
if (attr == -1) /* to make sure of */
{ /* file type */
strcpy(temp,"Error in reading file descriptor for ");
strcat(temp,name); /* if error tell user */
puts(temp);
continue; /* but keep going */
}
if ((attr & 0x80) == 0x80) /* if file is dir */
{
changed = upper(name,newname); /* convert to uppercase */
}
else
{
changed = lower(name,newname); /* else to lowercase */
}
strcpy(temp,newname); /* print names out neatly in columns */
if (strlen(newname) >= 16)
strncat(temp,spaces,32-strlen(newname));
else
strncat(temp,spaces,16-strlen(newname));
for (i=0;i<=(strlen(temp));i++)
putchar(temp[i]);
if (changed) /* if filename needed changing */
rename(dirpath,newname); /* go rename it */
close(path); /* close the file */
}
} /* endwhile */ /* loop around again */
close(dirpath); /* close the directory */
} /* end */ /* and quit */
extname(entry,name) /* extracts filename from struct member */
char *entry; /* and resets the last char high bit */
char *name;
{
while(isascii(*entry)) /* using macro from ctype.h */
{
*name++ = *entry++;
}
*name++ = toascii(*entry++);
*name = '\0';
}
readfd(path) /* using SS.FD GetStat call, find the */
int path; /* file attribute */
{
int attr;
struct fildes fd;
#ifndef OSK
struct registers regs;
regs.rg_a = path;
regs.rg_b = SS_FD;
regs.rg_x = &fd;
if(_os9(I_GETSTT,®s)==0)
#else
if(_gs_gfd(path,&fd,sizeof(fd)) != -1)
#endif
{
attr = (int)fd.fd_att; /* if no error,attr = attribute of file */
}
else
{
attr = -1; /* if error, attr = -1 */
}
return(attr); /* send it back to calling function */
}
upper(name,newname) /* convert filename to uppercase */
char *name;
char *newname;
{
int flag=FALSE; /* preload change flag */
while(*name)
{
if(!(isupper(*name) || ispunct(*name))) /* char upper or punct ?*/
{
*newname++ = toupper(*name++); /* no so change it */
flag=TRUE; /* and set the flag */
}
else
{
*newname++=*name++; /* otherwise just move it */
}
}
*newname ='\0'; /* and terminate it with null */
return(flag); /* and return the flag */
}
lower(name,newname) /* convert filename to lowercase */
char *name;
char *newname;
{
int flag = FALSE; /* prelaoad change flag */
while(*name)
{
if(!(islower(*name) || ispunct(*name))) /* char lower or punct?*/
{
*newname++=tolower(*name++); /* no so change it */
flag = TRUE; /* and set flag */
}
else
{
*newname++ = *name++; /* otherwise just move it */
}
}
*newname='\0'; /* terminate with null */
return(flag); /* and return flag */
}
rename(dirpath,newname) /* rename file to new name */
int dirpath;
char *newname;
{
lseek(dirpath,-32l,1); /* seek back 32 chars (1 dir entry) */
strcpy(dir.dir_name,newname); /* put new name into struct */
dir.dir_name[strlen(dir.dir_name)-1] += 128; /*set high bit on last char*/
write(dirpath,&dir,sizeof(dir)); /* write the new dir entry */
} /* and return */
help()
{
puts("Usage: UpperDir [directory name]");
puts(" Changes filenames to lowercase,");
puts(" and directories to uppercase.");
}