home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / rlelib / rle_open_f.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-15  |  3.3 KB  |  148 lines

  1. /* 
  2.  * rle_open_f.c - Open a file with defaults.
  3.  * 
  4.  * Author :     Jerry Winters 
  5.  *         EECS Dept.
  6.  *         University of Michigan
  7.  * Date:    11/14/89
  8.  * Copyright (c) 1990, University of Michigan
  9.  */
  10.  
  11. #include "rle_config.h"
  12. #include <stdio.h>
  13. #ifdef USE_STRING_H
  14. #include <string.h>
  15. #else
  16. #include <strings.h>
  17. #endif
  18.  
  19. /* 
  20.  *  Purpose : Open a file for input or ouput as controled by the mode
  21.  *  parameter.  If no file name is specified (ie. file_name is null) then
  22.  *  a pointer to stdin or stdout will be returned.  The calling routine may
  23.  *  call this routine with a file name of "-".  For this case rle_open_f
  24.  *  will return a pointer to stdin or stdout depending on the mode.
  25.  *    If the user specifies a non-null file name and an I/O error occurs
  26.  *  when trying to open the file, rle_open_f will terminate execution with
  27.  *  an appropiate error message.
  28.  *
  29.  *  parameters
  30.  *   input:
  31.  *     prog_name:     name of the calling program.
  32.  *     file_name :     name of the file to open
  33.  *     mode :         either "r" for read or input file or "w" for write or
  34.  *                    output file
  35.  *
  36.  *   output:
  37.  *     a file pointer
  38.  * 
  39.  */
  40. FILE *
  41. rle_open_f_noexit( prog_name, file_name, mode ) 
  42. char *prog_name, *file_name, *mode;
  43. {
  44.     FILE *fp;
  45.     void perror();
  46.     char *err_str;
  47.     register char *cp;
  48.     char *combuf;
  49.  
  50. #ifdef STDIO_NEEDS_BINARY
  51.     char mode_string[32];    /* Should be enough. */
  52.  
  53.     /* Concatenate a 'b' onto the mode. */
  54.     mode_string[0] = mode[0];
  55.     mode_string[1] = 'b';
  56.     strcpy( mode_string + 2, mode + 1 );
  57.     mode = mode_string;
  58. #endif
  59.  
  60.     if ( *mode == 'w' || *mode == 'a' )
  61.     fp = stdout;     /* Set the default value */
  62.     else
  63.     fp = stdin;
  64.     
  65.     if ( file_name != NULL && strcmp( file_name, "-" ) != 0 )
  66.     {
  67. #ifndef    NO_OPEN_PIPES
  68.     /*  Real file, not stdin or stdout.  If name ends in ".Z",
  69.      *  pipe from/to un/compress (depending on r/w mode).
  70.      *  
  71.      *  If it starts with "|", popen that command.
  72.      */
  73.  
  74.     cp = file_name + strlen( file_name ) - 2;
  75.     /* Pipe case. */
  76.     if ( *file_name == '|' )
  77.     {
  78.         if ( (fp = popen( file_name + 1, mode )) == NULL )
  79.         {
  80.         err_str = "%s: can't invoke <<%s>> for %s: ";
  81.         goto err;
  82.         }
  83.     }
  84.  
  85.     /* Compress case. */
  86.     else if ( cp > file_name && *cp == '.' && *(cp + 1) == 'Z' )
  87.     {
  88.         combuf = (char *)malloc( 20 + strlen( file_name ) );
  89.         if ( combuf == NULL )
  90.         {
  91.         err_str = "%s: out of memory opening (compressed) %s for %s";
  92.         goto err;
  93.         }
  94.  
  95.         if ( *mode == 'w' )
  96.         sprintf( combuf, "compress > %s", file_name );
  97.         else if ( *mode == 'a' )
  98.         sprintf( combuf, "compress >> %s", file_name );
  99.         else
  100.         sprintf( combuf, "compress -d < %s", file_name );
  101.  
  102.         fp = popen( combuf, mode );
  103.         free( combuf );
  104.  
  105.         if ( fp == NULL )
  106.         {
  107.         err_str =
  108.     "%s: can't invoke 'compress' program, trying to open %s for %s";
  109.         goto err;
  110.         }
  111.     }
  112.  
  113.     /* Ordinary, boring file case. */
  114.     else
  115. #endif /* !NO_OPEN_PIPES */
  116.         if ( (fp = fopen(file_name, mode)) == NULL )
  117.         {
  118.         err_str = "%s: can't open %s for %s: ";
  119.         goto err;
  120.         }
  121.     }
  122.  
  123.     return fp;
  124.  
  125. err:
  126.     fprintf( stderr, err_str,
  127.          prog_name, file_name,
  128.          (*mode == 'w') ? "output" :
  129.          (*mode == 'a') ? "append" :
  130.          "input" );
  131.     perror( "" );
  132.     return NULL;
  133.  
  134. }
  135.  
  136. FILE *
  137. rle_open_f( prog_name, file_name, mode )
  138. char *prog_name, *file_name, *mode;
  139. {
  140.     FILE *fp;
  141.  
  142.     if ( (fp = rle_open_f_noexit( prog_name, file_name, mode )) == NULL )
  143.     exit( -1 );
  144.  
  145.     return fp;
  146. }
  147.  
  148.