home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / question / 10829 < prev    next >
Encoding:
Text File  |  1992-09-08  |  6.3 KB  |  180 lines

  1. Path: sparky!uunet!mcsun!uknet!cam-cl!doc.ic.ac.uk!syma!leilabd
  2. From: leilabd@syma.sussex.ac.uk (Leila Burrell-Davis)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: need script to rename uppercase filenames
  5. Message-ID: <1992Sep8.121456.18394@syma.sussex.ac.uk>
  6. Date: 8 Sep 92 12:14:56 GMT
  7. References: <699@svcs1.UUCP>
  8. Organization: Computing Service, University of Sussex, UK
  9. Lines: 168
  10. X-Newsreader: Tin 1.1 PL5
  11.  
  12. Bill Miller (slix@svcs1.UUCP) wrote:
  13. : I'm fairly new to unix, and I need a script or procedure to do the following:
  14. :  
  15. : I have some source code in DOS (many separate files) that I tarred under
  16. : DOS and untarred under 386BSD.  The big problem is that all the files
  17. : came through in UPPERCASE and I need a script to mv (rename) them all
  18. : to lowercase quickly.
  19. :  
  20. : Since they're in DOS text format, I realize I also need to strip the 
  21. : extra carriage returns on each line.  I've been successful in doing this
  22. : with:
  23. :  
  24. :   cat (file) | tr -d '\015' > (newfile)
  25. :  
  26. : It would be nice to combine both of these so that I could rename the
  27. : files to uppercase and strip the extra newlines all in one fell swoop
  28. : instead of doing it one file at a time.
  29.  
  30. Not exactly the answer to your question, but there are two well tried
  31. and tested programs to do this. To do the upper case to lower case
  32. conversion a program called xxu from those nice Kermit people. I'll
  33. append the source as it's short. For the end of line conversion in an
  34. intelligent and reliable way and in either direction, a program called
  35. flip by Rahul Dhesi which is very portable. I think it's been posted
  36. to comp.sources.misc before now.
  37.  
  38. Leila
  39. --
  40. /*  X X U  --  20-to-Unix filename converter  */
  41.  
  42. /*
  43.  Change DEC-20 or VAX/VMS style filenames into normal Unix names.
  44.  Handy for use after ftp MGETs, when you find your directory full of
  45.  files with names like LIB:<KERMIT>CKUFIO.C.2 or FRED::[ETHEL]A.B;37
  46.  when all you really wanted was ckufio.c and a.b.
  47.  
  48.  Usage: xxu file(s)
  49.  
  50.  Action: Renames argument files as follows:
  51.    strips Unix path name from front (up to rightmost '/') if present
  52.    strips DEC device:, node:: names from front (up to rightmost ':') if present
  53.    strips DEC-20 <directory> or VMS [directory] name if present
  54.    strips DEC-20 version number from end (everything after 2nd dot) if present
  55.    strips VMS generation number from end (everything after ';') if present
  56.    lowercases any uppercase letters
  57.    honors DEC-20 CTRL-V quote for special characters
  58.    discards unquoted unprintable characters
  59.    if result is null, file is renamed to xxfile-n, where n is a number.
  60.    if result would write over an existing file, file also renamed to xxfile-n.
  61.  
  62.  Recommended procedure: make a new directory, cd to it, then FTP files
  63.  from DEC-20 or VMS system, then do "xxu *".
  64.  
  65.  Author:  F. da Cruz, CUCCA, July 85
  66. */
  67. #include <stdio.h>
  68. #if    SYSV | M_XENIX
  69. #include <sys/types.h>
  70. #endif
  71. #include <ctype.h>
  72. #include <sys/file.h>            /* For access() */
  73. /* <<<<<<<< define NO_RENAME on cc line if missing >>>>>>>> */
  74.  
  75. char name[500];                /* File name buffer */
  76. char *pp, *cp, *xp;            /* Character pointers */
  77. char delim;                /* Directory Delimiter */
  78. int dc = 0, n = 0;            /* Counters */
  79. int quote = 0, indir = 0; done = 0;    /* Flags */
  80.  
  81. main(argc,argv) int argc; char **argv; {
  82.  
  83.     if (argc < 2) {            /* Give message if no args */
  84.     fprintf(stderr,"Usage: xxu file(s)\n");
  85.     exit(1);
  86.     }
  87.     n = 0;                /* Unfixable filename counter */
  88.     while (--argc > 0) {        /* For all files on command line... */
  89.     argv++;
  90.     xp = *argv;            /* Copy pointer for simplicity */
  91.     printf("%s ",*argv);        /* Echo name of this file */
  92.  
  93.     pp = name;            /* Point to translation buffer */
  94.     *name = '\0';            /* Initialize buffer */
  95.     dc = 0;                /* Filename dot counter */
  96.     done = 0;            /* Flag for early completion */
  97.  
  98.     for (cp = xp; (*cp != '\0') && !done; cp++) { /* Loop thru chars... */
  99.  
  100.         if (quote) {        /* If this char quoted, */
  101.         *pp++ = *cp;        /*  include it literally. */
  102.         quote = 0;
  103.         }
  104.         else if (indir) {        /* If in directory name, */
  105.         if (*cp == delim) indir = 0; /* look for end delimiter. */
  106.         }
  107.         else switch (*cp) {
  108.         case '<':        /* Discard DEC-20 directory name */
  109.             indir = 1;
  110.             delim = '>';
  111.             break;
  112.         case '[':        /* Discard VMS directory name */
  113.             indir = 1;
  114.             delim = ']';
  115.             break;
  116.         case '/':        /* Discard Unix path name */
  117.         case ':':               /*  or DEC dev: or node:: name */
  118.             pp = name; 
  119.             break;
  120.         case '.':        /* DEC -20 generation number */
  121.                 if (++dc == 1)    /* Keep first dot */
  122.                 *pp++ = *cp;
  123.             else        /* Discard everything starting */
  124.                 done = 1;    /* with second dot. */
  125.             break;
  126.         case ';':        /* VMS generation or DEC-20 attrib */
  127.             done = 1;        /* Discard everything starting with */
  128.             break;        /* semicolon */
  129.             case '\026':        /* Control-V quote for special chars */
  130.             quote = 1;        /* Set flag for next time. */
  131.             break;
  132.         default:
  133.             if (isupper(*cp))      /* Uppercase letter to lowercase */
  134.                     *pp++ = tolower(*cp);
  135.             else if (*cp == ' ')/* change blanks to underscore */
  136.                 *pp++ = '_';
  137.             else if (isprint(*cp)) /* Other printable, just keep */
  138.                 *pp++ = *cp;
  139.         }
  140.     }
  141.     *pp = '\0';            /* Done with name, terminate it */
  142.     if (strcmp(name,xp) == 0) {    /* If no renaming necessary, */
  143.         printf("(ok)\n");        /*  just give message. */
  144.         continue;
  145.         }
  146.     while (*name == '\0' || access(name,0) == 0) { /* Find unique name */
  147.         sprintf(name,"xxfile-%d",n++);
  148.     }
  149.     printf("=> %s ",name);        /* Tell what new name will be */
  150.     if (rename(xp,name) == 0)    /* Try to rename it */
  151.         printf("(ok)\n");        /* Say what happened */
  152.     else
  153.         perror("failed");
  154.     }
  155.     exit(0);                /* Done. */
  156. }
  157. /*****************************************************************
  158.  |  rename - for systems lacking the rename system call
  159.  |----------------------------------------------------------------
  160.  |  Arguments:
  161.  |   1) string - current filename
  162.  |   2) string - new filename
  163.  ****************************************************************/
  164.  
  165. #if    NO_RENAME
  166. rename(oldname, newname)
  167.     char *oldname, *newname;
  168. {
  169.     char cmdline[133];
  170.     
  171.     sprintf(cmdline, "mv \"%s\" %s", oldname, newname);
  172.     return system(cmdline);
  173. }
  174. #endif
  175. -- 
  176. Leila Burrell-Davis, Computing Service, University of Sussex, Brighton, UK
  177. Tel:   +44 273 678390              Fax:   +44 273 678470
  178. Email: leilabd@syma.sussex.ac.uk  (JANET: leilabd@uk.ac.sussex.syma)
  179.