From: leilabd@syma.sussex.ac.uk (Leila Burrell-Davis)
Newsgroups: comp.unix.questions
Subject: Re: need script to rename uppercase filenames
Date: 8 Sep 92 12:14:56 GMT
Organization: Computing Service, University of Sussex, UK
X-Newsreader: Tin 1.1 PL5

Bill Miller (slix@svcs1.UUCP) wrote:
: 
: I'm fairly new to unix, and I need a script or procedure to do the following:
:  
: I have some source code in DOS (many separate files) that I tarred under
: DOS and untarred under 386BSD.  The big problem is that all the files
: came through in UPPERCASE and I need a script to mv (rename) them all
: to lowercase quickly.
:  
: Since they're in DOS text format, I realize I also need to strip the 
: extra carriage returns on each line.  I've been successful in doing this
: with:
:  
:   cat (file) | tr -d '\015' > (newfile)
:  
: It would be nice to combine both of these so that I could rename the
: files to uppercase and strip the extra newlines all in one fell swoop
: instead of doing it one file at a time.

Not exactly the answer to your question, but there are two well tried
and tested programs to do this. To do the upper case to lower case
conversion a program called xxu from those nice Kermit people. I'll
append the source as it's short. For the end of line conversion in an
intelligent and reliable way and in either direction, a program called
flip by Rahul Dhesi which is very portable. I think it's been posted
to comp.sources.misc before now.

Leila
--
/*  X X U  --  20-to-Unix filename converter  */

/*
 Change DEC-20 or VAX/VMS style filenames into normal Unix names.
 Handy for use after ftp MGETs, when you find your directory full of
 files with names like LIB:<KERMIT>CKUFIO.C.2 or FRED::[ETHEL]A.B;37
 when all you really wanted was ckufio.c and a.b.

 Usage: xxu file(s)

 Action: Renames argument files as follows:
   strips Unix path name from front (up to rightmost '/') if present
   strips DEC device:, node:: names from front (up to rightmost ':') if present
   strips DEC-20 <directory> or VMS [directory] name if present
   strips DEC-20 version number from end (everything after 2nd dot) if present
   strips VMS generation number from end (everything after ';') if present
   lowercases any uppercase letters
   honors DEC-20 CTRL-V quote for special characters
   discards unquoted unprintable characters
   if result is null, file is renamed to xxfile-n, where n is a number.
   if result would write over an existing file, file also renamed to xxfile-n.

 Recommended procedure: make a new directory, cd to it, then FTP files
 from DEC-20 or VMS system, then do "xxu *".

 Author:  F. da Cruz, CUCCA, July 85
*/
#include <stdio.h>
#if	SYSV | M_XENIX
#include <sys/types.h>
#endif
#include <ctype.h>
#include <sys/file.h>			/* For access() */
/* <<<<<<<< define NO_RENAME on cc line if missing >>>>>>>> */

char name[500];				/* File name buffer */
char *pp, *cp, *xp;			/* Character pointers */
char delim;				/* Directory Delimiter */
int dc = 0, n = 0;			/* Counters */
int quote = 0, indir = 0; done = 0;	/* Flags */

main(argc,argv) int argc; char **argv; {

    if (argc < 2) {			/* Give message if no args */
	fprintf(stderr,"Usage: xxu file(s)\n");
	exit(1);
    }
    n = 0;				/* Unfixable filename counter */
    while (--argc > 0) {		/* For all files on command line... */
	argv++;
	xp = *argv;			/* Copy pointer for simplicity */
	printf("%s ",*argv);		/* Echo name of this file */

	pp = name;			/* Point to translation buffer */
	*name = '\0';			/* Initialize buffer */
	dc = 0;				/* Filename dot counter */
	done = 0;			/* Flag for early completion */

	for (cp = xp; (*cp != '\0') && !done; cp++) { /* Loop thru chars... */

	    if (quote) {		/* If this char quoted, */
		*pp++ = *cp;		/*  include it literally. */
		quote = 0;
	    }
	    else if (indir) {		/* If in directory name, */
		if (*cp == delim) indir = 0; /* look for end delimiter. */
	    }
	    else switch (*cp) {
		case '<':		/* Discard DEC-20 directory name */
		    indir = 1;
		    delim = '>';
		    break;
		case '[':		/* Discard VMS directory name */
		    indir = 1;
		    delim = ']';
		    break;
		case '/':		/* Discard Unix path name */
		case ':':   	    	/*  or DEC dev: or node:: name */
		    pp = name; 
		    break;
		case '.':		/* DEC -20 generation number */
	    	    if (++dc == 1)	/* Keep first dot */
		    	*pp++ = *cp;
		    else		/* Discard everything starting */
		    	done = 1;	/* with second dot. */
		    break;
		case ';':		/* VMS generation or DEC-20 attrib */
		    done = 1;		/* Discard everything starting with */
		    break;		/* semicolon */
	    	case '\026':		/* Control-V quote for special chars */
		    quote = 1;		/* Set flag for next time. */
		    break;
		default:
		    if (isupper(*cp))  	/* Uppercase letter to lowercase */
	    	    	*pp++ = tolower(*cp);
		    else if (*cp == ' ')/* change blanks to underscore */
		        *pp++ = '_';
		    else if (isprint(*cp)) /* Other printable, just keep */
		    	*pp++ = *cp;
	    }
	}
	*pp = '\0';			/* Done with name, terminate it */
	if (strcmp(name,xp) == 0) {	/* If no renaming necessary, */
	    printf("(ok)\n");		/*  just give message. */
	    continue;
        }
	while (*name == '\0' || access(name,0) == 0) { /* Find unique name */
	    sprintf(name,"xxfile-%d",n++);
	}
	printf("=> %s ",name);		/* Tell what new name will be */
	if (rename(xp,name) == 0)	/* Try to rename it */
	    printf("(ok)\n");		/* Say what happened */
	else
	    perror("failed");
    }
    exit(0);				/* Done. */
}
/*****************************************************************
 |  rename - for systems lacking the rename system call
 |----------------------------------------------------------------
 |  Arguments:
 |   1) string - current filename
 |   2) string - new filename
 ****************************************************************/

#if	NO_RENAME
rename(oldname, newname)
    char *oldname, *newname;
{
    char cmdline[133];
    
    sprintf(cmdline, "mv \"%s\" %s", oldname, newname);
    return system(cmdline);
}
#endif
-- 
Leila Burrell-Davis, Computing Service, University of Sussex, Brighton, UK
Tel:   +44 273 678390              Fax:   +44 273 678470
Email: leilabd@syma.sussex.ac.uk  (JANET: leilabd@uk.ac.sussex.syma)
Newsgroups: comp.unix.questions