13 Open Files

      General

<..header functions..>+
 static FILE* f_open( ARG_II(const char*,const char*) );
 -_-_-

<..functions..>+
 
 static FILE* f_open( name, flags )
                           const char*  name ;
                           const char*  flags
 ;{                        FILE* file;
   if( (file = fopen(name,flags) ) != NULL ) {
      (IGNORED)  printf("(%s)\n",name);
   }
   return file;
 }
 -_-_-

<..header functions..>+
 static FILE* f_home_open( ARG_II(const char*,const char*) );
 -_-_-

<..functions..>+
 
 static FILE* f_home_open( name, flags )
                           const char*  name ;
                           const char*  flags
 ;{                        FILE* file;
                           U_CHAR *str;
   if( *name == ’~’ ){
      if( HOME_DIR ){
          str = m_alloc(char, strlen((char *) HOME_DIR)+strlen(name));
          (IGNORED) sprintf(str,"%s%s", HOME_DIR, name+1);
          file = f_open(str,flags);
          free((void *)  str);
          return file;
      } else { return NULL; }
   } else {     return f_open( name, flags );   }
 }
 -_-_-

General

<..header functions..>+
 static FILE* open_file( ARG_II(const C_CHAR *, const C_CHAR *) );
 -_-_-

<..functions..>+
 
 static FILE* open_file(name,ext)
                          const C_CHAR *name; const C_CHAR *ext
 ;{                       FILE* file;
                          C_CHAR filename[255], *p;
    if( eq_str( ext,LG_EXT ) ) {
       (IGNORED) strcpy((char *) filename, (char *) job_name);
       (IGNORED) strct(filename, ext);
    } else {
       (IGNORED) strcpy((char *)  filename, name );
       p = filename;
       while( TRUE ){
         if( *p == ’.’ ){  break; }
         if( *p == ’\0’ ){ (IGNORED) strcpy((char *) p, ext); break; }
         p++;
       }
    }
    file  = fopen(filename, READ_TEXT_FLAGS);
    if( !file ) {
       (IGNORED) warn_i_str(5,filename);
    } else { (IGNORED) printf ("Entering %s\n", filename); }
 
    return file;
 }
 -_-_-

We have here a little inconsistency with tex4ht.c. There the input file name is appended with the extension .dvi, allowing double extensions. Here, if an extension is present, we remove it before putting .lg on top of the name. Should we fix that?

<..defines..>+
 #define LG_EXT ".lg"
 -_-_-

<..mark start lg file..>
 begin_lg_file = ftell(lg_file);
 -_-_-

<..rewind lg file..>
 (IGNORED)  fseek(lg_file, begin_lg_file, <.abs file addr.>);
 -_-_-

<..abs file addr..>
 0-_-_-

<..vars..>+
 static FILE* lg_file;
 static long  begin_lg_file;
 -_-_-

open(file, ..._FLAGS): All text files should be opened with ”r” or ”w”; all binary files with ”rb” or ”wb”. This is the portable way and will work both under Unix and DOS; as Unix doesn’t distinguish between text and binary files, its compilers will simply ignore the ”b” part.

On the subject of the .lg file: you open the tex4ht.env file in binary mode (”rb”), which results in strange line endings in the .lg file (CR/CR/LF). Changing the mode to ”r” fixes this.

<..t4ht.h..>+
 #if defined(DOS_WIN32) || defined(__MSDOS__)
 #define READ_BIN_FLAGS "rb"
 #define READ_TEXT_FLAGS "r"
 #define WRITE_BIN_FLAGS "wb"
 #ifdef WIN32
 #define WRITE_TEXT_FLAGS "wb"
 #else
 #define WRITE_TEXT_FLAGS "w"
 #endif
 #else
 #define READ_BIN_FLAGS "r"
 #define READ_TEXT_FLAGS "r"
 #define WRITE_BIN_FLAGS "w"
 #define WRITE_TEXT_FLAGS "w"
 #endif
 -_-_-

I’ve been held up a bit by a 
bug in tex4ht.c that caused emTeX to complain that the created .idv file 
was corrupt: 
 
*** Fatal error 2106: corrupt DVI file (postamble not found) 
 
  I found the cause, though: you open it with mode "w", which is text 
mode. Changing this to "wb" solved the problem.