home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / mkmanifest.c < prev    next >
C/C++ Source or Header  |  1997-11-12  |  2KB  |  104 lines

  1. /*
  2.  * A program to create a manifest (shipping list) that is a shell script
  3.  * to return a Unix file name to it's original state after it has been
  4.  * clobbered by MSDOS's file name restrictions.
  5.  *
  6.  *    This code also used in arc, mtools, and pcomm
  7.  */
  8.  
  9. #include "sysincludes.h"
  10. #include "msdos.h"
  11. #include "patchlevel.h"
  12.  
  13. static char *dos_name2();
  14.  
  15. void
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     int i;
  21.     char *name, *new_name;
  22.  
  23.     /* print the version */
  24.     if(argc >= 2 && strcmp(argv[1], "-V") == 0) {
  25.         printf("Mtools version %s, dated %s\n", VERSION, DATE);
  26.         exit(0);
  27.     }
  28.  
  29.     if (argc == 1) {
  30.         fprintf(stderr, "Usage: mkmanifest [-V] <list-of-files>\n");
  31.         exit(1);
  32.     }
  33.  
  34.     for (i=1; i<argc; i++) {
  35.                     /* zap the leading path */
  36.         if ((name = strrchr(argv[i], '/')))
  37.             name++;
  38.         else
  39.             name = argv[i];
  40.                     /* create new name */
  41.         new_name = dos_name2(name);
  42.  
  43.         if (strcasecmp(new_name, name))
  44.             printf("mv %s %s\n", new_name, name);
  45.     }
  46.     exit(0);
  47. }
  48.  
  49. static char *
  50. dos_name2(name)
  51. char *name;
  52. {
  53.     static const char *dev[9] = {"con", "aux", "com1", "com2", "lpt1", 
  54.                      "prn", "lpt2", "lpt3", "nul"};
  55.     char *s;
  56.     char *ext,*temp;
  57.     char buf[MAX_PATH];
  58.     int i, dot;
  59.     static char ans[13];
  60.  
  61.     strcpy(buf, name);
  62.     temp = buf;
  63.                     /* separate the name from extension */
  64.     ext = 0;
  65.     dot = 0;
  66.     for (i=strlen(buf)-1; i>=0; i--) {
  67.         if (buf[i] == '.' && !dot) {
  68.             dot = 1;
  69.             buf[i] = '\0';
  70.             ext = &buf[i+1];
  71.         }
  72.         if (isupper(buf[i]))
  73.             buf[i] = tolower(buf[i]);
  74.     }
  75.                     /* if no name */
  76.     if (*temp == '\0')
  77.         strcpy(ans, "x");
  78.     else {
  79.         /* if name is a device */
  80.         for (i=0; i<9; i++) {
  81.             if (!strcasecmp(temp, dev[i])) 
  82.                 *temp = 'x';
  83.         }
  84.         /* name too long? */
  85.         if (strlen(temp) > 8)
  86.             *(temp+8) = '\0';
  87.         /* extension too long? */
  88.         if (ext && strlen(ext) > 3)
  89.             *(ext+3) = '\0';
  90.         /* illegal characters? */
  91.         while ((s = strpbrk(temp, "^+=/[]:',?*\\<>|\". ")))
  92.             *s = 'x';
  93.  
  94.         while (ext && (s = strpbrk(ext, "^+=/[]:',?*\\<>|\". ")))
  95.             *s = 'x';          
  96.         strcpy(ans, temp);
  97.     }
  98.     if (ext && *ext) {
  99.         strcat(ans, ".");
  100.         strcat(ans, ext);
  101.     }
  102.     return(ans);
  103. }
  104.