home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / gigop806.zip / MUNGE163.CPP < prev    next >
Text File  |  1994-08-04  |  4KB  |  166 lines

  1.  
  2. /**/
  3. /*                                                        */
  4. /* This function will "munge" a uucp filename into a DOS  */
  5. /* compatible filename in the scheme of WAFFLE 1.63       */
  6. /*                                                        */
  7. /* See the WAFFLE DOCS for information on the scheme used */
  8. /*                                                        */
  9. /**/
  10.  
  11. /* Modified 3/8/91 to fix a bug with "eastern" "D.eastee48eca7"
  12.    returning VE48ECA7.D instead of EE48ECA7.D  by calculating the
  13.    "residue" on only the last 6 chars of the stripped name */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19.  
  20.  
  21. /* Function Prototypes */
  22. int             pow2(int n);
  23. /* Table of prefix characters */
  24. char            codes[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
  25.     'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
  26.     'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  27. 'X', 'Y', 'Z'};
  28.  
  29. char           *munge163(char *hostname, char *filename)
  30. {
  31.  
  32.     static char     munged[256];
  33.     char            temp[256],
  34.                    *cptr;
  35.     int             i,
  36.                     j,
  37.                     cd,
  38.                     cmax;
  39.     /* Check that filename begins with 'D.' or 'X.' */
  40.     if (strnicmp(filename, "D.", 2) && strnicmp(filename, "X.", 2) && strnicmp(filename, "C.", 2))
  41.         return "BOGUS";
  42.  
  43.     /* Check hostname for validity */
  44.     if (strlen(hostname) > 7) {
  45.         /* Truncate it if > 7 */
  46.         *(hostname + 7) = '\0';
  47.     }
  48.     /* strip off first n characters that match the hostname */
  49.     for (i = 0; i < strlen(hostname); i++) {
  50.         if (*(hostname + i) != *(filename + i + 2))
  51.             break;
  52.     }
  53.     /*
  54.      * unknown what this does if(i==0){ if(mode == 0){ strcpy(munged,
  55.      * "#BOGUS#"); return(munged); } else{ i = mode - 1; } }
  56.      */
  57.  
  58.     /* Copy just the unique part to temp */
  59.     strcpy(temp, (filename + i + 2));
  60.  
  61.     /* see if temp is longer than 6 chars */
  62.     /* if so, only calculate residue on last 6 */
  63.     if (strlen(temp) >= 7) {
  64.         cptr = temp + (strlen(temp) - 6);
  65.     } else {
  66.         cptr = temp;
  67.     }
  68.  
  69.     /* Calculate 'code'  */
  70.     cd = 0;
  71.     cmax = strlen(cptr) - 1;
  72.  
  73. #ifdef AK
  74.     for (i = cmax; i >= 0; i--)
  75.         if (islower(*(cptr + i)))
  76.             cd |= 1 << (cmax - i);
  77. #else
  78.     for (i = cmax; i >= 0; i--) {
  79.         /*
  80.          * AK: Is this program written in C or in BASIC? In C it is the most
  81.          * complicated way possible.
  82.          */
  83.         if (islower(*(cptr + i))) {
  84.             j = 1;
  85.         } else {
  86.             j = 0;
  87.         }
  88.         cd += j * (pow2(cmax - i));
  89.     }
  90. #endif
  91.  
  92.     /* Build final filename */
  93.     munged[0] = codes[cd & 0x1f];
  94.     munged[1] = '\0';
  95.     strcat(munged, temp);
  96.  
  97.     strcat(munged, ".");
  98.     strncpy(temp, filename, 1);
  99.     temp[1] = '\0';
  100.     strcat(munged, temp);
  101.  
  102.     return strupr(munged);
  103. }
  104. #ifndef AK
  105.  
  106. /**/
  107. /* raise 2 to the nth power */
  108. /**/
  109.  
  110. int             pow2(int n)
  111. {
  112.     switch (n) {
  113.  case 0:
  114.         return 1;
  115.       case 1:
  116.         return 2;
  117.       case 2:
  118.         return 4;
  119.       case 3:
  120.         return 8;
  121.       case 4:
  122.         return 16;
  123.       case 5:
  124.         return 32;
  125.       case 6:
  126.         return 64;
  127.       case 7:
  128.         return 128;
  129.       default:
  130.         fprintf(stderr, "Out of range in function'munge163/pow2()'\n");
  131.         fprintf(stderr, "        --THIS SHOULD NEVER HAPPEN--\n");
  132.         exit(1);
  133.     }
  134.     return 0;
  135. }
  136. #endif
  137.  
  138. char           *waffle_munger(char *str, char *system, char *ourname)
  139. {
  140.     char           *p;
  141.     char            temp[128];
  142.  
  143.     int             picky = 8;
  144. loop:
  145.     strcpy(temp, system);strcat(temp,"-----------------------");
  146.     temp[picky] = 0;
  147.     if (strncmp(temp, str + 2, strlen(temp)) == NULL) {
  148.         strcpy(temp, system);
  149.         p = munge163(temp, str);
  150.         return p;
  151.     }
  152.     strcpy(temp, ourname);strcat(temp,"-----------------------");
  153.     temp[picky] = 0;
  154.     if (strncmp(temp, str + 2, strlen(temp)) == NULL) {
  155.         strcpy(temp, ourname);
  156.         p = munge163(temp, str);
  157.         return p;
  158.     }
  159.     picky--;
  160.     if (!picky) {
  161.         fprintf(stderr,"**WARNING**  WAFFLE_MUNGER(%s,%s,%s) - NO MATCH FOUND TO MUNGE!", str, system, ourname);
  162.         return ("MUNGE_ER");
  163.     }
  164.     goto loop;
  165. }
  166.