home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GROFFSRC / SRC / SPOOLER / PRINTDVI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-03  |  1.6 KB  |  83 lines

  1. #include <stdlib.h>
  2. #include <io.h>
  3. #include <fcntl.h>
  4.  
  5. #define VIEW "v" /* default */
  6.  
  7. void printdvi(int input, char *cmd)
  8. {
  9.   char buf[1024], out[256], *tmp;
  10.   int len, output, bytes;
  11.  
  12.   tmp = getenv("TMP");
  13.  
  14.   strcpy(out, tmp ? tmp : "\\");
  15.   len = strlen(out);
  16.   if ( out[len - 1] == '/' || out[len - 1] == '\\' )
  17.     out[len - 1] = 0;
  18.   strcat(out, "\\d_XXXXXX");
  19.   mktemp(out);
  20.   strcat(out, "."); /* fix emTeX dvi driver bug: name without extension */
  21.  
  22.   if ( (output = open(out, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) == -1 )
  23.     return;
  24.  
  25.   while ( (bytes = read(input, buf, sizeof(buf))) > 0 )
  26.     if ( write(output, buf, bytes) == -1 )
  27.       break;
  28.     
  29.   close(output);
  30.  
  31.   if ( strstr(cmd, "%s") )
  32.     sprintf(buf, cmd, out);
  33.   else
  34.   {
  35.     strcpy(buf, cmd);
  36.     strcat(buf, " ");
  37.     strcat(buf, out);
  38.   }
  39.  
  40.   system(buf);
  41.  
  42.   unlink(out);
  43. }
  44.  
  45. void main(int argc, char **argv)
  46. {
  47.   char *cmd = getenv("DVI_PRINT_COMMAND");
  48.   char buf[1024];
  49.   int arg, file;
  50.  
  51.   cmd = cmd ? cmd : VIEW;
  52.  
  53.   if ( argc == 1 )
  54.     if ( isatty(0) )
  55.     {
  56.       printf("\nUsage: %s [file ...]"
  57.              "\n   or: command | %s\n", argv[0], argv[0]);
  58.       exit(1);
  59.     }
  60.     else
  61.     {
  62.       setmode(0, O_BINARY);
  63.       printdvi(0, cmd);
  64.     }
  65.   else
  66.     for ( arg = 1; arg < argc; arg++ )
  67.       if ( access(argv[arg], 0) == 0 )
  68.       {
  69.     if ( strstr(cmd, "%s") )
  70.       sprintf(buf, cmd, argv[arg]);
  71.     else
  72.     {
  73.       strcpy(buf, cmd);
  74.       strcat(buf, " ");
  75.       strcat(buf, argv[arg]);
  76.     }
  77.  
  78.     system(buf);
  79.       }
  80.   
  81.   exit(0);
  82. }
  83.