home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / jikes-1.02 / src / amiga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-05  |  2.3 KB  |  122 lines

  1. #ifdef stat
  2. #undef stat
  3. #endif
  4. #ifdef fopen
  5. #undef fopen
  6. #endif
  7. #ifdef opendir
  8. #undef opendir
  9. #endif
  10.  
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <sys/param.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <dirent.h>
  18.  
  19. __BEGIN_DECLS
  20. /**
  21.  * Provide wrappers for the stat, fopen, and opendir functions that massage
  22.  * the file names given to them as arguments so that UNIX "." and ".."
  23.  * path names are translated to their AmigaOS equivalents. This is done so
  24.  * that no intervention is done in terms of file semantics to the jikes
  25.  * source.
  26.  */
  27. static char buf[MAXPATHLEN+1];
  28. static char cwd[MAXPATHLEN+1];
  29. static char pathComponent[MAXPATHLEN+1];
  30.  
  31. static void ix_out(char *s)
  32. {
  33.   int ptr;
  34.  
  35.   if (strcmp(s, ".") == '\0') {
  36.     if (buf[0] == '\0') {
  37.       strcat(buf, cwd);
  38.     }else{
  39.       ptr = strlen(buf)-1;
  40.       if (buf[ptr] == '/') {
  41.         buf[ptr] = '\0';
  42.       }
  43.     }
  44.   }else{
  45.     if (strcmp(s, "..") != 0) {
  46.       strcat(buf, s);
  47.     }
  48.   }
  49. }
  50.  
  51. static char *
  52. ix_path(const char *path)
  53. {
  54.   int len;
  55.   char sep[2];
  56.   int appendSep, skipNext = 0;
  57.   int i, j;
  58.  
  59.   buf[0] = '\0';
  60.   cwd[0] = '\0';
  61.   pathComponent[0] = '\0';
  62.   sep[1] = '\0';
  63.   getcwd(cwd, sizeof(cwd));
  64.   len = strlen(path);
  65.  
  66.   for (i=0, j=0; i<len; i++) {
  67.     if (path[i] == '/' || path[i] == ':') {
  68.       pathComponent[j] = '\0';
  69.       if (j != 0) {
  70.         if (buf[0] != '\0' && strcmp(pathComponent, ".") == 0 &&
  71.         buf[strlen(buf)-1] == ':') {
  72.       appendSep = 0;
  73.     }else{
  74.       appendSep = 1;
  75.     }
  76.         ix_out(pathComponent);
  77.         j = 0;
  78.         pathComponent[0] = '\0';
  79.       }
  80.       sep[0] = path[i];
  81.       if (appendSep && !skipNext) {
  82.         strcat(buf, sep);
  83.       }
  84.       /* Constructs of the type FOO:/bar are *probably* caused by appending
  85.        * UNIX-style a path to a directory, so we skip the bogus "/".
  86.        */
  87.       if (path[i] == ':' && path[i+1] == '/') {
  88.         skipNext = 1;
  89.       }else{
  90.         skipNext = 0;
  91.       }
  92.     }else{
  93.       pathComponent[j++] = path[i];
  94.     }
  95.   }
  96.   if (j > 0) {
  97.     pathComponent[j] = '\0';
  98.     ix_out(pathComponent);
  99.   }
  100.   return buf;
  101. }
  102.  
  103. int
  104. mystat(const char *path, struct stat *sb)
  105. {
  106.   int status = stat(ix_path(path), sb);
  107.   return status;
  108. }
  109.  
  110. FILE *
  111. myfopen(char *path, char *mode)
  112. {
  113.   FILE *f = fopen(ix_path(path), mode);
  114.   return f;
  115. }
  116.  
  117. DIR *myopendir(const char *path)
  118. {
  119.   return opendir(ix_path(path));
  120. }
  121. __END_DECLS
  122.