home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume2 / advsys / part03 / advjunk.c < prev    next >
C/C++ Source or Header  |  1987-10-23  |  2KB  |  125 lines

  1. #define UNIX
  2. #include <stdio.h>
  3.  
  4. long _seed = 1L;
  5.  
  6. #ifndef UNIX
  7. int rand()
  8. {
  9.     _seed *= 397204094L;
  10.     return (_seed & 0x7FFF);
  11. }
  12.  
  13. srand(n)
  14.   long n;
  15. {
  16.    _seed = n;
  17. }
  18. #endif
  19.  
  20. int getch()
  21. {
  22. #ifdef UNIX
  23.     return getchar();
  24. #else
  25.     int ch;
  26.     if ((ch = bdos(1) & 0xFF) == '\r') { bdos(6,'\n'); ch = '\n'; }
  27.     return (ch);
  28. #endif
  29. }
  30.  
  31. waitch()
  32. {
  33. #ifndef UNIX
  34.     bdos(7);
  35. #endif
  36. }
  37.  
  38. putch(ch,fp)
  39.   int ch; FILE *fp;
  40. {
  41. #ifdef UNIX
  42.     putc(ch,fp);
  43. #else
  44.     aputc(ch,fp);
  45. #endif
  46. }
  47.  
  48. int advsave(hdr,hlen,save,slen)
  49.   char *hdr; int hlen; char *save; int slen;
  50. {
  51.     char fname[50];
  52.     int fd;
  53.  
  54.     trm_str("File name? ");
  55.     trm_get(fname);
  56.  
  57.     /* add the extension */
  58.     strcat(fname,".sav");
  59.  
  60.     /* create the data file */
  61.     if ((fd = creat(fname,0666)) == -1)
  62.     return (0);
  63.  
  64.     /* write the header */
  65.     if (write(fd,hdr,hlen) != hlen) {
  66.     close(fd);
  67.     return (0);
  68.     }
  69.  
  70.     /* write the data */
  71.     if (write(fd,save,slen) != slen) {
  72.     close(fd);
  73.     return (0);
  74.     }
  75.  
  76.     /* close the file and return successfully */
  77.     close(fd);
  78.     return (1);
  79. }
  80.  
  81. int advrestore(hdr,hlen,save,slen)
  82.   char *hdr; int hlen; char *save; int slen;
  83. {
  84.     char fname[50],hbuf[50],*p;
  85.     int fd;
  86.  
  87.     if (hlen > 50)
  88.     error("save file header buffer too small");
  89.  
  90.     trm_str("File name? ");
  91.     trm_get(fname);
  92.  
  93.     /* add the extension */
  94.     strcat(fname,".sav");
  95.  
  96.     /* create the data file */
  97.     if ((fd = open(fname,0)) == -1)
  98.     return (0);
  99.  
  100.     /* read the header */
  101.     if (read(fd,hbuf,hlen) != hlen) {
  102.     close(fd);
  103.     return (0);
  104.     }
  105.  
  106.     /* compare the headers */
  107.     for (p = hbuf; hlen--; )
  108.     if (*hdr++ != *p++) {
  109.         trm_str("This save file does not match the adventure!\n");
  110.         return (0);
  111.     }
  112.  
  113.     /* read the data */
  114.     if (read(fd,save,slen) != slen) {
  115.     close(fd);
  116.     return (0);
  117.     }
  118.  
  119.     /* close the file and return successfully */
  120.     close(fd);
  121.     return (1);
  122. }
  123.  
  124.  
  125.