home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / macintosh / macos_filesys.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  4KB  |  195 lines

  1. /*
  2.  * macos_filesys.c
  3.  * Filesystem handling stuff for macos
  4.  *
  5.  * Michael Ladwig <mike@twinpeaks.prc.com> --- November 1995 (Initial version)
  6.  *                                                         --- May 1996 (Handled relative paths better)
  7.  *                                                         --- June 1996 (Have open look absolute and relative)
  8.  */
  9.  
  10. #include "mac_config.h"
  11. #include <config.h>
  12. #include <system.h>
  13.  
  14. #ifdef MSL_LIBRARY
  15. #include <errno.h>
  16. #else
  17. #include <sys/errno.h>
  18. #endif
  19.  
  20. #include <fcntl.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. char *macos_fixpath (const char *path);
  26. char scratchPath[1024];
  27.  
  28. int
  29. macos_mkdir( const char *path, int oflag )
  30. {
  31.     return mkdir( macos_fixpath(path) );
  32. }
  33.  
  34. int
  35. macos_open( const char *path, int oflag, ... )
  36. {
  37.     int    r;
  38.     char    *macPath;
  39.     
  40.     macPath = macos_fixpath(path);
  41.  
  42.     r = open( macPath, oflag );
  43.     if( r < 0 ) return open( macPath+1, oflag );
  44.  
  45.     return r;
  46. }
  47.  
  48. int
  49. macos_chmod( const char *path, mode_t mode )
  50. {
  51.     return chmod(macos_fixpath(path), mode);
  52. }
  53.  
  54. int
  55. macos_creat( const char *path, mode_t mode )
  56. {
  57.     return creat( macos_fixpath(path) );
  58. }
  59.  
  60. FILE *
  61. macos_fopen( const char *path, const char *mode )
  62. {
  63.     FILE    *fp;
  64.         
  65.     fp = fopen(macos_fixpath(path), mode);
  66.     
  67.     /* I'm getting ENOTDIR, CVS expects ENOENT */
  68.     
  69.     if( (fp == NULL) && (errno == ENOTDIR) ) errno = ENOENT;
  70.     
  71.     return fp;
  72. }
  73.  
  74. int
  75. macos_chdir( const char *path )
  76. {
  77.     int    r;
  78.     char    *macPath;
  79.     
  80.     macPath = macos_fixpath(path);
  81.  
  82.     r = chdir(macPath+1);
  83.     if( r < 0 ) return chdir(macPath);
  84.  
  85.     return r;
  86. }
  87.  
  88. int
  89. macos_access(const char *path, int amode)
  90. {    
  91.     return access( macos_fixpath(path), amode );
  92. }
  93.  
  94. DIR *
  95. macos_opendir(const char *path)
  96. {
  97.     return opendir( macos_fixpath(path) );
  98. }
  99.  
  100. int
  101. macos_stat (const char *path, struct stat *ststr)
  102. {
  103.     return stat( macos_fixpath(path), ststr );
  104. }
  105.  
  106. int
  107. macos_rename (const char *path, const char *newpath)
  108. {
  109.     char    macPath_from[1024], macPath_to[1024];
  110.  
  111.     strcpy( macPath_from, macos_fixpath(path) );
  112.     strcpy( macPath_to, macos_fixpath(newpath) );
  113.  
  114.     return rename( macPath_from, macPath_to );
  115. }
  116.  
  117. int
  118. macos_rmdir (const char *path)
  119. {
  120.     return rmdir( macos_fixpath(path) );
  121. }
  122.  
  123. int
  124. macos_unlink (const char *path)
  125. {
  126.     return unlink( macos_fixpath(path) );
  127. }
  128.  
  129. char *
  130. macos_fixpath (const char *path)
  131. {
  132.     char        *sepCh;
  133.     
  134.     strcpy( scratchPath, ":" );
  135.     
  136.     if( (*path == '.') && (*(path+1) == '/') )
  137.         strcat( scratchPath, path+2 );
  138.     else
  139.     {
  140.         if( strcmp(path, ".") != 0 )
  141.             strcat( scratchPath, path );
  142.     }
  143.     while( (sepCh = strchr(scratchPath, '/')) != NULL )
  144.         *sepCh = ':';
  145.         
  146.     //fprintf(stderr,"MacOS fixpath <%s>", path);
  147.     //fprintf(stderr," -> <%s>\n", scratchPath);
  148.         
  149.     return scratchPath;
  150. }
  151.  
  152. /*
  153.  * I intended to shamelessly steal from the OS2 port.  Oddly, only the
  154.  * fopen calls seem to respect the binary-text distinction, so I have
  155.  * rewritten the code to use fopen, fread, fwrite, and fclose instead of
  156.  * open, read, write, and close
  157.  */
  158.     
  159. void
  160. convert_file (char *infile,  int inflags,
  161.           char *outfile, int outflags)
  162. {
  163.     FILE *infd, *outfd;
  164.     char buf[8192];
  165.     int len;
  166.     char iflags[10], oflags[10];
  167.  
  168.     if( inflags & OPEN_BINARY )
  169.         strcpy( iflags, "rb" );
  170.     else
  171.         strcpy( iflags, "r" );
  172.         
  173.     if( outflags & OPEN_BINARY )
  174.         strcpy( oflags, "wb" );
  175.     else
  176.         strcpy( oflags, "w" );
  177.  
  178.     if ((infd = CVS_FOPEN (infile, iflags)) == NULL)
  179.         error (1, errno, "couldn't read %s", infile);
  180.     if ((outfd = CVS_FOPEN (outfile, oflags)) == NULL)
  181.         error (1, errno, "couldn't write %s", outfile);
  182.  
  183.     while ((len = fread (buf, sizeof (char), sizeof (buf), infd)) > 0)
  184.         if (fwrite (buf, sizeof (char), len, outfd) < 0)
  185.         error (1, errno, "error writing %s", outfile);
  186.     if (len < 0)
  187.         error (1, errno, "error reading %s", infile);
  188.  
  189.     if (fclose (outfd) < 0)
  190.         error (0, errno, "warning: couldn't close %s", outfile);
  191.     if (fclose (infd) < 0)
  192.         error (0, errno, "warning: couldn't close %s", infile);
  193. }
  194.  
  195.