home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 24b / memsave.zip / MEMSAVE.C next >
Text File  |  1987-12-02  |  3KB  |  81 lines

  1. /* This program writes a designated range of memory to
  2.     a disk file.  It requires several parameters, specifying
  3.     the output file, the start address in memory and the byte count.
  4.         It was compiled with the small model of the MSC 5.0 compiler,
  5.     taking advantage of the far pointer capability.
  6.         The program will set the errorlevel on output to 22 (EINVAL) if
  7.     it gets a bad parameter, or to the value of errno otherwise.  The
  8.     errors that would typically be encountered are:
  9.         ENOSPC    28        out of disk space
  10.         ENOENT     2        some component of pathlist invalid
  11.         EACCES    13        probably trying to overwrite a r/o file
  12.  
  13. */
  14. /* Edition History:
  15.     1.0    First version            HB
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21.  
  22. main(argc,argv)
  23. int argc;
  24. char **argv;
  25. {
  26.     FILE *outfile;
  27.     int c;
  28.     long memsize;
  29.     char far *memptr; /* we use the long pointer type here    */
  30.     if (argc != 4)
  31.         help(argv);
  32.     sscanf(argv[2],"%9p",&memptr); /* get the start addr in seg:off form */
  33.     sscanf(argv[3],"%9I",&memsize);   /* get the size to write in hex */
  34.     
  35.     if ((outfile = fopen(argv[1],"wb")) == NULL) /* open the file in binary mode! */
  36.         {
  37.         perror("Error opening outfile");
  38.         exit(errno);
  39.         }
  40.  
  41.     for ( ; memsize > 0; memsize--, memptr++)
  42.         {
  43.         c = *memptr;
  44.         putc(c,outfile);
  45.         if (ferror(outfile))
  46.             break;
  47.         }
  48.      if ((ferror(outfile)) | (fflush(outfile)))    /*flush the file to see if any errors are generated! */
  49.          perror("Error writing output file");
  50.      exit(errno);
  51. }
  52.  
  53. /*************************************************
  54. help routine
  55. gets passed argv to find the name of the program
  56. *************************************************/
  57. help(filenam)
  58. char **filenam;
  59. {
  60.     char *prognam;
  61.     prognam = strrchr(*filenam,'\\');
  62.     prognam++;
  63.     fprintf(stderr,"%s\n",*filenam);
  64.     fprintf(stderr,"Purpose: Copies a selected portion of memory to a file\n");
  65.     fprintf(stderr,"Usage:%s outfile startaddr size\n",prognam);
  66.     fprintf(stderr,"where:\n");
  67.     fprintf(stderr,"       outfile = output file name\n");
  68.     fprintf(stderr,"       startaddr = memory starting address in segment:offset form\n");
  69.     fprintf(stderr,"       size = number of bytes to be written\n\n");
  70.     fprintf(stderr,"Note that the memory start address must be specified as:\n");
  71.     fprintf(stderr,"    XXXX:YYYY where XXXX is the segment and YYYY is the\n");
  72.     fprintf(stderr,"    offset value, in upper case hexadecimal characters.\n");
  73.     fprintf(stderr,"    The value of size is expected in decimal, but may be\n");
  74.     fprintf(stderr,"    in hex by prefixing the value with 0x.\n");
  75.     fprintf(stderr,"\nAuthor: H.Burman\nVersion: 1.0  Dec 2, 1987\n");
  76.     exit(EINVAL);    /* Invalid argument error */
  77. }
  78.  
  79.  
  80.  
  81.