Neat utility to convert uppercase filenames

Alexander Dupuy dupuy at cs.columbia.edu
Wed Dec 12 07:34:02 AEST 1990


Well, I didn't write this, and if I did it now, I'd do it in perl, but it's
pretty cute, since it not only downcases files, it will strip directory names
and Twenex/VMS extensions and attributes.

: This is a shar archive.  Extract with sh, not csh.
: The rest of this file will extract: 
:
:	xxu.c
:
echo x - xxu.c
sed 's/^X//' > xxu.c << '//go.sysin dd *'
X/*  X X U  --  20-to-Unix filename converter  */
X
X/*
X Change DEC-20 or VAX/VMS style filenames into normal Unix names.
X Handy for use after ftp MGETs, when you find your directory full of
X files with names like LIB:<KERMIT>CKUFIO.C.2 or FRED::[ETHEL]A.B;37
X when all you really wanted was ckufio.c and a.b.
X
X Usage: xxu file(s)
X
X Action: Renames argument files as follows:
X   strips Unix path name from front (up to rightmost '/') if present
X   strips DEC device:, node:: names from front (up to rightmost ':') if present
X   strips DEC-20 <directory> or VMS [directory] name if present
X   strips DEC-20 version number from end (everything after 2nd dot) if present
X   strips VMS generation number from end (everything after ';') if present
X   lowercases any uppercase letters
X   honors DEC-20 CTRL-V quote for special characters
X   discards unquoted unprintable characters
X   if result is null, file is renamed to xxfile-n, where n is a number.
X   if result would write over an existing file, file also renamed to xxfile-n.
X
X Recommended procedure: make a new directory, cd to it, then FTP files
X from DEC-20 or VMS system, then do "xxu *".
X
X Author:  F. da Cruz, CUCCA, July 85
X*/
X#include <stdio.h>
X#include <ctype.h>
X#include <sys/file.h>			/* For access() */
X
Xchar name[500];				/* File name buffer */
Xchar *pp, *cp, *xp;			/* Character pointers */
Xchar delim;				/* Directory Delimiter */
Xint dc = 0, n = 0;			/* Counters */
Xint quote = 0, indir = 0; done = 0;	/* Flags */
X
Xmain(argc,argv) int argc; char **argv; {
X
X    if (argc < 2) {			/* Give message if no args */
X	fprintf(stderr,"Usage: xxu file(s)\n");
X	exit(1);
X    }
X    n = 0;				/* Unfixable filename counter */
X    while (--argc > 0) {		/* For all files on command line... */
X	argv++;
X	xp = *argv;			/* Copy pointer for simplicity */
X	printf("%s ",*argv);		/* Echo name of this file */
X
X	pp = name;			/* Point to translation buffer */
X	*name = '\0';			/* Initialize buffer */
X	dc = 0;				/* Filename dot counter */
X	done = 0;			/* Flag for early completion */
X
X	for (cp = xp; (*cp != '\0') && !done; cp++) { /* Loop thru chars... */
X
X	    if (quote) {		/* If this char quoted, */
X		*pp++ = *cp;		/*  include it literally. */
X		quote = 0;
X	    }
X	    else if (indir) {		/* If in directory name, */
X		if (*cp == delim) indir = 0; /* look for end delimiter. */
X	    }
X	    else switch (*cp) {
X		case '<':		/* Discard DEC-20 directory name */
X		    indir = 1;
X		    delim = '>';
X		    break;
X		case '[':		/* Discard VMS directory name */
X		    indir = 1;
X		    delim = ']';
X		    break;
X		case '/':		/* Discard Unix path name */
X		case '\\':		/*  or messy-dos path name */
X		case ':':   	    	/*  or DEC dev: or node:: name */
X		    pp = name; 
X		    break;
X		case '.':		/* DEC -20 generation number */
X	    	    if (++dc == 1 && cp[1] != '.' && cp[1] != '\0')
X		    	*pp++ = *cp;	/* Keep first dot if nonempty .ext */
X		    else		/* Discard everything starting */
X		    	done = 1;	/* with second dot. */
X		    break;
X		case ';':		/* VMS generation or DEC-20 attrib */
X		    done = 1;		/* Discard everything starting with */
X		    break;		/* semicolon */
X	    	case '\026':		/* Control-V quote for special chars */
X		    quote = 1;		/* Set flag for next time. */
X		    break;
X		default:
X		    if (isupper(*cp))  	/* Uppercase letter to lowercase */
X	    	    	*pp++ = tolower(*cp);
X		    else if (isprint(*cp)) /* Other printable, just keep */
X		    	*pp++ = *cp;
X	    }
X	}
X	*pp = '\0';			/* Done with name, terminate it */
X	if (strcmp(name,xp) == 0) {	/* If no renaming necessary, */
X	    printf("(ok)\n");		/*  just give message. */
X	    continue;
X        }
X	while (*name == '\0' || access(name,0) == 0) { /* Find unique name */
X	    sprintf(name,"xxfile-%d",n++);
X	}
X	printf("=> %s ",name);		/* Tell what new name will be */
X	if (rename(xp,name) == 0)	/* Try to rename it */
X	    printf("(ok)\n");		/* Say what happened */
X	else
X	    perror("failed");
X    }
X    exit(0);				/* Done. */
X}
//go.sysin dd *
exit
--
-- 
inet: dupuy at cs.columbia.edu
uucp: ...!rutgers!cs.columbia.edu!dupuy



More information about the Alt.sources mailing list