home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap10 / scrsave.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-06  |  1.6 KB  |  58 lines

  1. /* scrsave.c  --  demonstrates write() by saving the */
  2. /*                text screen to a file.             */
  3.  
  4. #include <stdio.h>        /* for stderr              */
  5. #include <fcntl.h>        /* for O_CREAT | O_BINARY  */
  6. #include <sys\types.h>    /* for stat.h              */
  7. #include <sys\stat.h>     /* for S_IREAD | S_IWRITE  */
  8.  
  9. #define SCRCHARS  (25 * 80)
  10. int Buf[SCRCHARS];
  11.  
  12. main(argc, argv)
  13. int argc;
  14. char *argv[];
  15. {
  16.     int *cp, *ep, fname[16];
  17.     int far *sp;
  18.     int fd_out, bytes;
  19.  
  20.     if (argc != 2)
  21.         {
  22.         fprintf(stderr, "usage: scrsave file\n");
  23.         exit(0);
  24.         }
  25.     if (strlen(argv[1]) > 8)
  26.         {
  27.         fprintf(stderr, "\"%s\": File name too long.\n", argv[1]);
  28.         exit(1);
  29.         }
  30.     strcpy(fname, argv[1]);
  31.     strcat(fname, ".SCR");
  32.     if (access(fname, 0) == 0)
  33.         {
  34.         fprintf(stderr, "\"%s\": Won't overwrite.\n", fname);
  35.         exit(1);
  36.         }
  37.     if ((fd_out = open(fname, O_WRONLY|O_CREAT|O_BINARY,
  38.                        S_IREAD|S_IWRITE)) < 0)
  39.         {
  40.         fprintf(stderr, "\"%s\": Can't create.\n", fname);
  41.         exit(1);
  42.         }
  43.     /* Copy the screen into a near buffer. */
  44.     ep = &Buf[SCRCHARS - 1];
  45.     cp = Buf;
  46.     /* use 0xB800000 for EGA or VGA */
  47.     sp = (int far *)(0xB0000000);
  48.     for (; cp < ep; ++cp, ++sp)
  49.         *cp = *sp;
  50.     /* Write it. */
  51.     bytes = write(fd_out, Buf, SCRCHARS * 2);
  52.     if (bytes != SCRCHARS * 2)
  53.         {
  54.         fprintf(stderr, "\"%s\": Error writing.\n", fname);
  55.         exit(1);
  56.         }
  57. }
  58.