home *** CD-ROM | disk | FTP | other *** search
/ CICA 1996 August / CICA2_0896.bin / utils / src / rockridg.c < prev    next >
C/C++ Source or Header  |  1995-03-01  |  2KB  |  92 lines

  1. /*
  2.  * Rockridg.c      handles filename conversion.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <assert.h>
  9. #include "rockridg.h"
  10.  
  11. static const char trans_tbl[] = "\\00_trans.tbl";
  12. #if 0
  13. static const char trans_tbl[] = "\\trans.tbl";
  14. #endif
  15.  
  16. int
  17. is_a_slash(char c) {
  18.   return (c == '\\' || c == '/');
  19. }
  20.  
  21. /*  If the disc has Rock Ridge extensions, the names in the 00_index.txt
  22.  *  files are not the real names on the disc.  Look up the
  23.  *  actual names in the 00_trans.tbl files.
  24.  */
  25. char *
  26. get_real_filename(char *base_dir, char *rockridge_name) {
  27.   static char rtn_str[80];
  28.   char in_line[128];
  29.   FILE *fp;
  30.   char *s, *t, c, *eof;
  31.   int i, flen, len;
  32.  
  33.   assert(base_dir[0] != '\0');
  34.   assert(base_dir[1] == ':');
  35.  
  36.   strcpy(rtn_str, base_dir);
  37.   s = rockridge_name;
  38.   t = rtn_str + strlen(rtn_str) - 1;
  39.   if (is_a_slash(*t))
  40.     *t = '\0';
  41.   else
  42.     t++;
  43.   if (is_a_slash(*s))
  44.     s++;
  45.  
  46.  /*
  47.   * Step through the rockridge path one directory at a time and build
  48.   * the "real" path by reading the 00_trans.tbl at each level.
  49.   */
  50.  
  51.   while (1) {
  52.     flen = strlen(rtn_str);
  53.     strcpy(rtn_str + flen, trans_tbl);
  54.  
  55.     for (t = s; *t != '\0' && *t != '/' && *t != '\\'; ++t);
  56.     c = *t;
  57.     *t = '\0';
  58.  
  59.     if ((fp = fopen(rtn_str, "rt")) == NULL) {
  60.       *t = c;                           /* Can't open 00_trans.tbl. */
  61.       return rockridge_name;            /* Return the original filename. */
  62.     }
  63.  
  64.     while ((eof = fgets(in_line, sizeof(in_line) - 1, fp)) != NULL) {
  65.       if (*in_line == 'L')
  66.         continue;
  67.  
  68.       assert(*in_line == 'F' || *in_line == 'D');
  69.       assert(in_line[1] == ' ');
  70.       assert(in_line[36] == '\t');
  71.       len = strlen(in_line);
  72.       assert(in_line[len - 1] == '\n');
  73.       in_line[--len] = '\0';
  74.       if (stricmp(s, in_line + 37) == 0)
  75.         break;
  76.     }
  77.     fclose(fp);
  78.     if (eof == NULL)        /* Entry not found.  Return original filename. */
  79.       return rockridge_name;
  80.  
  81.     for (i = 36; in_line[i] <= ' '; --i)
  82.       in_line[i] = '\0';
  83.  
  84.     strcpy(rtn_str + flen + 1, in_line + 2);
  85.     if (c == '\0')
  86.       return rtn_str;
  87.  
  88.     *t = c;
  89.     s = t + 1;
  90.   }
  91. }
  92.