home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2259 < prev    next >
Text File  |  1990-12-28  |  5KB  |  136 lines

  1. Newsgroups: alt.sources
  2. From: dupuy@cs.columbia.edu (Alexander Dupuy)
  3. Subject: Re: Neat utility to convert uppercase filenames
  4. Message-ID: <DUPUY.90Dec11143402@hudson.cs.columbia.edu>
  5. Date: 11 Dec 90 14:34:02
  6.  
  7. Well, I didn't write this, and if I did it now, I'd do it in perl, but it's
  8. pretty cute, since it not only downcases files, it will strip directory names
  9. and Twenex/VMS extensions and attributes.
  10.  
  11. : This is a shar archive.  Extract with sh, not csh.
  12. : The rest of this file will extract: 
  13. :
  14. :    xxu.c
  15. :
  16. echo x - xxu.c
  17. sed 's/^X//' > xxu.c << '//go.sysin dd *'
  18. X/*  X X U  --  20-to-Unix filename converter  */
  19. X
  20. X/*
  21. X Change DEC-20 or VAX/VMS style filenames into normal Unix names.
  22. X Handy for use after ftp MGETs, when you find your directory full of
  23. X files with names like LIB:<KERMIT>CKUFIO.C.2 or FRED::[ETHEL]A.B;37
  24. X when all you really wanted was ckufio.c and a.b.
  25. X
  26. X Usage: xxu file(s)
  27. X
  28. X Action: Renames argument files as follows:
  29. X   strips Unix path name from front (up to rightmost '/') if present
  30. X   strips DEC device:, node:: names from front (up to rightmost ':') if present
  31. X   strips DEC-20 <directory> or VMS [directory] name if present
  32. X   strips DEC-20 version number from end (everything after 2nd dot) if present
  33. X   strips VMS generation number from end (everything after ';') if present
  34. X   lowercases any uppercase letters
  35. X   honors DEC-20 CTRL-V quote for special characters
  36. X   discards unquoted unprintable characters
  37. X   if result is null, file is renamed to xxfile-n, where n is a number.
  38. X   if result would write over an existing file, file also renamed to xxfile-n.
  39. X
  40. X Recommended procedure: make a new directory, cd to it, then FTP files
  41. X from DEC-20 or VMS system, then do "xxu *".
  42. X
  43. X Author:  F. da Cruz, CUCCA, July 85
  44. X*/
  45. X#include <stdio.h>
  46. X#include <ctype.h>
  47. X#include <sys/file.h>            /* For access() */
  48. X
  49. Xchar name[500];                /* File name buffer */
  50. Xchar *pp, *cp, *xp;            /* Character pointers */
  51. Xchar delim;                /* Directory Delimiter */
  52. Xint dc = 0, n = 0;            /* Counters */
  53. Xint quote = 0, indir = 0; done = 0;    /* Flags */
  54. X
  55. Xmain(argc,argv) int argc; char **argv; {
  56. X
  57. X    if (argc < 2) {            /* Give message if no args */
  58. X    fprintf(stderr,"Usage: xxu file(s)\n");
  59. X    exit(1);
  60. X    }
  61. X    n = 0;                /* Unfixable filename counter */
  62. X    while (--argc > 0) {        /* For all files on command line... */
  63. X    argv++;
  64. X    xp = *argv;            /* Copy pointer for simplicity */
  65. X    printf("%s ",*argv);        /* Echo name of this file */
  66. X
  67. X    pp = name;            /* Point to translation buffer */
  68. X    *name = '\0';            /* Initialize buffer */
  69. X    dc = 0;                /* Filename dot counter */
  70. X    done = 0;            /* Flag for early completion */
  71. X
  72. X    for (cp = xp; (*cp != '\0') && !done; cp++) { /* Loop thru chars... */
  73. X
  74. X        if (quote) {        /* If this char quoted, */
  75. X        *pp++ = *cp;        /*  include it literally. */
  76. X        quote = 0;
  77. X        }
  78. X        else if (indir) {        /* If in directory name, */
  79. X        if (*cp == delim) indir = 0; /* look for end delimiter. */
  80. X        }
  81. X        else switch (*cp) {
  82. X        case '<':        /* Discard DEC-20 directory name */
  83. X            indir = 1;
  84. X            delim = '>';
  85. X            break;
  86. X        case '[':        /* Discard VMS directory name */
  87. X            indir = 1;
  88. X            delim = ']';
  89. X            break;
  90. X        case '/':        /* Discard Unix path name */
  91. X        case '\\':        /*  or messy-dos path name */
  92. X        case ':':               /*  or DEC dev: or node:: name */
  93. X            pp = name; 
  94. X            break;
  95. X        case '.':        /* DEC -20 generation number */
  96. X                if (++dc == 1 && cp[1] != '.' && cp[1] != '\0')
  97. X                *pp++ = *cp;    /* Keep first dot if nonempty .ext */
  98. X            else        /* Discard everything starting */
  99. X                done = 1;    /* with second dot. */
  100. X            break;
  101. X        case ';':        /* VMS generation or DEC-20 attrib */
  102. X            done = 1;        /* Discard everything starting with */
  103. X            break;        /* semicolon */
  104. X            case '\026':        /* Control-V quote for special chars */
  105. X            quote = 1;        /* Set flag for next time. */
  106. X            break;
  107. X        default:
  108. X            if (isupper(*cp))      /* Uppercase letter to lowercase */
  109. X                    *pp++ = tolower(*cp);
  110. X            else if (isprint(*cp)) /* Other printable, just keep */
  111. X                *pp++ = *cp;
  112. X        }
  113. X    }
  114. X    *pp = '\0';            /* Done with name, terminate it */
  115. X    if (strcmp(name,xp) == 0) {    /* If no renaming necessary, */
  116. X        printf("(ok)\n");        /*  just give message. */
  117. X        continue;
  118. X        }
  119. X    while (*name == '\0' || access(name,0) == 0) { /* Find unique name */
  120. X        sprintf(name,"xxfile-%d",n++);
  121. X    }
  122. X    printf("=> %s ",name);        /* Tell what new name will be */
  123. X    if (rename(xp,name) == 0)    /* Try to rename it */
  124. X        printf("(ok)\n");        /* Say what happened */
  125. X    else
  126. X        perror("failed");
  127. X    }
  128. X    exit(0);                /* Done. */
  129. X}
  130. //go.sysin dd *
  131. exit
  132. --
  133. -- 
  134. inet: dupuy@cs.columbia.edu
  135. uucp: ...!rutgers!cs.columbia.edu!dupuy
  136.