home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2256 < prev    next >
Internet Message Format  |  1990-12-28  |  5KB

  1. From: bobmon@iuvax.cs.indiana.edu (RAMontante)
  2. Newsgroups: alt.sources
  3. Subject: Re: Neat utility to convert uppercase filenames
  4. Message-ID: <77676@iuvax.cs.indiana.edu>
  5. Date: 11 Dec 90 18:04:50 GMT
  6.  
  7. Here's some source, gotten from Keith Petersen/Bill Davidsen/Frank da Cruz;
  8. does casing and other neat stuff too.  Originally from comp.binaries.ibm.pc
  9. (I think).
  10.  
  11. One-liners... HMMMPH!
  12.  
  13.  
  14.  
  15. /*  X X U  --  20-to-Unix filename converter  */
  16.  
  17. /**
  18.  ** This can be very handy for fixing uppercase case and illegal filenames
  19.  ** on Unix.  Why is it posted here?  Because it's a tool for getting
  20.  ** MSDOS files from comp.binaries.ibm.pc and SIMTEL20.
  21.  ** 
  22.  ** Thanks to Bill Davidsen for the update.
  23.  ** 
  24.  ** Keith Petersen
  25.  ** Maintainer of the CP/M & MSDOS archives at
  26.  **     WSMR-SIMTEL20.ARMY.MIL [26.0.0.74]
  27.  ** DDN: W8SDZ@WSMR-SIMTEL20.ARMY.MIL
  28.  ** Uucp: {ames,decwrl,harvard,rutgers,ucbvax,uunet}
  29.  **     !wsmr-simtel20.army.mil!w8sdz
  30.  **/
  31.  
  32. /*
  33.  Change DEC-20 or VAX/VMS style filenames into normal Unix names.
  34.  Handy for use after ftp MGETs, when you find your directory full of
  35.  files with names like LIB:<KERMIT>CKUFIO.C.2 or FRED::[ETHEL]A.B;37
  36.  when all you really wanted was ckufio.c and a.b.
  37.  
  38.  Usage: xxu file(s)
  39.  
  40.  Action: Renames argument files as follows:
  41.    strips Unix path name from front (up to rightmost '/') if present
  42.    strips DEC device:, node:: names from front (up to rightmost ':') if present
  43.    strips DEC-20 <directory> or VMS [directory] name if present
  44.    strips DEC-20 version number from end (everything after 2nd dot) if present
  45.    strips VMS generation number from end (everything after ';') if present
  46.    lowercases any uppercase letters
  47.    honors DEC-20 CTRL-V quote for special characters
  48.    discards unquoted unprintable characters
  49.    if result is null, file is renamed to xxfile-n, where n is a number.
  50.    if result would write over an existing file, file also renamed to xxfile-n.
  51.  
  52.  Recommended procedure: make a new directory, cd to it, then FTP files
  53.  from DEC-20 or VMS system, then do "xxu *".
  54.  
  55.  Author:  F. da Cruz, CUCCA, July 85
  56. */
  57.  
  58. #include <stdio.h>
  59. #if    SYSV | M_XENIX
  60. #include <sys/types.h>
  61. #endif
  62. #include <ctype.h>
  63. #include <sys/file.h>            /* For access() */
  64. /* <<<<<<<< define NO_RENAME on cc line if missing >>>>>>>> */
  65.  
  66. char name[500];                /* File name buffer */
  67. char *pp, *cp, *xp;            /* Character pointers */
  68. char delim;                /* Directory Delimiter */
  69. int dc = 0, n = 0;            /* Counters */
  70. int quote = 0, indir = 0, done = 0;    /* Flags */
  71.  
  72. int main(argc,argv) int argc; char **argv; {
  73.  
  74.     if (argc < 2) {            /* Give message if no args */
  75.     fprintf(stderr,"Usage: xxu file(s)\n");
  76.     exit(1);
  77.     }
  78.     n = 0;                /* Unfixable filename counter */
  79.     while (--argc > 0) {        /* For all files on command line... */
  80.     argv++;
  81.     xp = *argv;            /* Copy pointer for simplicity */
  82.     printf("%s ",*argv);        /* Echo name of this file */
  83.  
  84.     pp = name;            /* Point to translation buffer */
  85.     *name = '\0';            /* Initialize buffer */
  86.     dc = 0;                /* Filename dot counter */
  87.     done = 0;            /* Flag for early completion */
  88.  
  89.     for (cp = xp; (*cp != '\0') && !done; cp++) { /* Loop thru chars... */
  90.  
  91.         if (quote) {        /* If this char quoted, */
  92.         *pp++ = *cp;        /*  include it literally. */
  93.         quote = 0;
  94.         }
  95.         else if (indir) {        /* If in directory name, */
  96.         if (*cp == delim) indir = 0; /* look for end delimiter. */
  97.         }
  98.         else switch (*cp) {
  99.         case '<':        /* Discard DEC-20 directory name */
  100.             indir = 1;
  101.             delim = '>';
  102.             break;
  103.         case '[':        /* Discard VMS directory name */
  104.             indir = 1;
  105.             delim = ']';
  106.             break;
  107.         case '/':        /* Discard Unix path name */
  108.         case ':':               /*  or DEC dev: or node:: name */
  109.             pp = name; 
  110.             break;
  111.         case '.':        /* DEC -20 generation number */
  112.                 if (++dc == 1)    /* Keep first dot */
  113.                 *pp++ = *cp;
  114.             else        /* Discard everything starting */
  115.                 done = 1;    /* with second dot. */
  116.             break;
  117.         case ';':        /* VMS generation or DEC-20 attrib */
  118.             done = 1;        /* Discard everything starting with */
  119.             break;        /* semicolon */
  120.             case '\026':        /* Control-V quote for special chars */
  121.             quote = 1;        /* Set flag for next time. */
  122.             break;
  123.         default:
  124.             if (isupper(*cp))      /* Uppercase letter to lowercase */
  125.                     *pp++ = tolower(*cp);
  126.             else if (*cp == ' ')/* change blanks to underscore */
  127.                 *pp++ = '_';
  128.             else if (isprint(*cp)) /* Other printable, just keep */
  129.                 *pp++ = *cp;
  130.         }
  131.     }
  132.     *pp = '\0';            /* Done with name, terminate it */
  133.     if (strcmp(name,xp) == 0) {    /* If no renaming necessary, */
  134.         printf("(ok)\n");        /*  just give message. */
  135.         continue;
  136.         }
  137.     while (*name == '\0' || access(name,0) == 0) { /* Find unique name */
  138.         sprintf(name,"xxfile-%d",n++);
  139.     }
  140.     printf("=> %s ",name);        /* Tell what new name will be */
  141.     if (rename(xp,name) == 0)    /* Try to rename it */
  142.         printf("(ok)\n");        /* Say what happened */
  143.     else
  144.         perror("failed");
  145.     }
  146.     return 0;                /* Done. */
  147. }
  148.  
  149. /*****************************************************************
  150.  |  rename - for systems lacking the rename system call
  151.  |----------------------------------------------------------------
  152.  |  Arguments:
  153.  |   1) string - current filename
  154.  |   2) string - new filename
  155.  ****************************************************************/
  156.  
  157. #if    NO_RENAME
  158. rename(oldname, newname)
  159.     char *oldname, *newname;
  160. {
  161.     char cmdline[133];
  162.     
  163.     sprintf(cmdline, "mv \"%s\" %s", oldname, newname);
  164.     return system(cmdline);
  165. }
  166. #endif
  167.