home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n15.zip / WRITEVAR.C < prev   
C/C++ Source or Header  |  1989-08-10  |  1KB  |  53 lines

  1.  
  2.  
  3. /* WRITEVAR.C - Writes new variable contents back to the executable file */
  4. /* Compile with -DTC -O -w -I$(INCLUDE -L$(LIB) -Z -lt -mt */
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<errno.h>
  9.  
  10. #define STORAGE_SIZE 80
  11. char storage[STORAGE_SIZE] = "Hello, out there!";
  12. char storage2[STORAGE_SIZE];
  13.  
  14. int writevar(char *filename, void *var, unsigned varlen);
  15. void main(int argc, char **argv);
  16.  
  17. void main(int argc, char **argv)
  18.     {
  19.     printf("Storage=%s\n",storage);
  20.     printf("Enter new value and press <RETURN>\n");
  21.     if(gets(storage2) && strlen(storage2))        /* get new values    */
  22.         {
  23.         storage2[sizeof(storage)-1] = '\0';        /* force NULL terminate    */
  24.         strcpy(storage,storage2);             /* update the executable */
  25.         if(writevar(argv[0], storage, sizeof(storage)))
  26.             printf("Storage successfully updated\n");
  27.         else
  28.             printf("Unable to update Storage\n");
  29.         }
  30.     }
  31.  
  32. int writevar(char *filename, void *var, unsigned varlen)
  33.     {
  34.     FILE *fp;
  35.     long off = 0L;
  36.     unsigned x;
  37.     int retval = 0;
  38.  
  39.     x = (unsigned)var;        /* get address of variable    */
  40.     x -= 0x100;                /* adjust for PSP    */
  41.     off += x;                /* off now has disk offset    */
  42.  
  43.     if(fp = fopen(filename,"r+b"))        /* open the executable    */
  44.         {
  45.         if(!fseek(fp,off,SEEK_SET))        /* seek to disk offset    */
  46.             if(fwrite(var,1,varlen,fp) == varlen)        /* write the variable */
  47.                 retval = 1;
  48.                 fclose(fp);        /* close the file    */
  49.         }
  50.     return retval;
  51.     }
  52.  
  53.