00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <stdio.h>
00040 #include "general.h"
00041 #include "strings.h"
00042 char RRi_is_Z[100];
00043
00044 FILE *rr_iopen(char *path)
00045 {
00046 static char rname[]="rr_iopen";
00047 FILE *fp;
00048 char pipe[256], is_Z;
00049 int lpath;
00050
00051 if (strcmp(path,"-")==0) return(stdin);
00052
00053 lpath = strlen(path);
00054 if (lpath > sizeof(pipe) - strlen("cat | gunzip ") - 4)
00055 quit(-1,"%s: pathname '%s' is too long\n",rname,path);
00056
00057 if (strcmp(&path[lpath-2],".Z")==0) {
00058
00059 if (!rr_fexists(path)) quit(-1,"%s: file '%s' not found\n",rname,path);
00060 sprintf(pipe,"zcat %s",path);
00061 goto Z;
00062 }
00063
00064 else if (strcmp(&path[lpath-3],".gz")==0) {
00065
00066 if (!rr_fexists(path)) quit(-1,"%s: file '%s' not found\n",rname,path);
00067 sprintf(pipe,"cat %s | gunzip",path);
00068 goto Z;
00069 }
00070
00071 else if (!rr_fexists(path)) {
00072 sprintf(pipe,"%s.Z",path);
00073
00074 if (!rr_fexists(pipe)) {
00075 sprintf(pipe,"%s.gz",path);
00076 if (!rr_fexists(pipe)) {
00077 quit(-1,"%s: None of '%s' '%s.Z' or '%s.gz' exist.\n",rname,path,path,path);
00078 }
00079 sprintf(pipe,"cat %s.gz | gunzip",path);
00080 goto Z;
00081 }
00082 sprintf(pipe,"zcat %s.Z",path);
00083 goto Z;
00084 }
00085 else {
00086 fp = rr_fopen(path,"r");
00087 is_Z = 0;
00088 goto record;
00089 }
00090
00091 Z:
00092 fp = popen(pipe,"r");
00093 if (!fp) quit(-1,"%s: problems opening the pipe '%s' for input.\n", rname,pipe);
00094 is_Z = 1;
00095
00096 record:
00097 if (fileno(fp) > sizeof(RRi_is_Z)-1) quit(-1,"%s: fileno = %d is too large\n",rname,fileno(fp));
00098 RRi_is_Z[fileno(fp)] = is_Z;
00099
00100 return(fp);
00101 }
00102
00103 void *rr_iclose(FILE *fp)
00104 {
00105 if (fp==stdin) return(0);
00106 else if (RRi_is_Z[fileno(fp)]) pclose(fp);
00107 else fclose(fp);
00108
00109 return(0);
00110
00111 }