Subversion Repositories NedoOS

Rev

Rev 668 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdio.h>
  2. char *alloc(nbytes)
  3. unsigned nbytes;
  4. {
  5.         struct _header *p, *q, *cp;
  6.         int nunits;
  7.         nunits = 1 + (nbytes + (sizeof (_base) - 1)) / sizeof (_base);
  8.         if ((q = _allocp) == NULL) {
  9.                 _base._ptr = _allocp = q = &_base;
  10.                 _base._size = 0;
  11.          }
  12.         for (p = q -> _ptr; ; q = p, p = p -> _ptr) {
  13.                 if (p -> _size >= nunits) {
  14.                         _allocp = q;
  15.                         if (p -> _size == nunits)
  16.                                 _allocp->_ptr = p->_ptr;
  17.                         else {
  18.                                 q = _allocp->_ptr = p + nunits;
  19.                                 q->_ptr = p->_ptr;
  20.                                 q->_size = p->_size - nunits;
  21.                                 p -> _size = nunits;
  22.                          }
  23.                         return p + 1;
  24.                  }
  25.                 if (p == _allocp) {
  26.                         if ((cp = sbrk(nunits *  sizeof (_base))) == ERROR)
  27.                                 return NULL;
  28.                         cp -> _size = nunits;
  29.                         free(cp+1);     /* remember: pointer arithmetic! */
  30.                         p = _allocp;
  31.                 }
  32.          }
  33. }
  34.  
  35. free(ap)
  36. struct _header *ap;
  37. {
  38.         struct _header *p, *q;
  39.  
  40.         p = ap - 1;     /* No need for the cast when "ap" is a struct ptr */
  41.  
  42.         for (q = &_base; q->_ptr != &_base; q = q -> _ptr)
  43.                 if (p > q && p < q -> _ptr)
  44.                         break;
  45.  
  46.         if (p + p -> _size == q -> _ptr) {
  47.                 p -> _size += q -> _ptr -> _size;
  48.                 p -> _ptr = q -> _ptr -> _ptr;
  49.          }
  50.         else p -> _ptr = q -> _ptr;
  51.  
  52.         if (q + q -> _size == p) {
  53.                 q -> _size += p -> _size;
  54.                 q -> _ptr = p -> _ptr;
  55.          }
  56.         else q -> _ptr = p;
  57.  
  58.         _allocp = q;
  59. }
  60.  
  61. int fflush(fp)
  62. FILE *fp;
  63. {
  64.         int i; char *p;
  65.  
  66.         if (fp <= 4)
  67.                 return OK;
  68.         if (!(fp->_flags & _WRITE))
  69.                 return ERROR;
  70.  
  71.         if (fp->_nleft == (NSECTS * SECSIZ))
  72.                 return OK;
  73.  
  74.         i = NSECTS - (fp->_nleft / SECSIZ);
  75.         if (write(fp->_fd, fp->_buff, i) != i)
  76.         {
  77.                 fp->_flags |= _ERR;
  78.                 return ERROR;
  79.         }
  80.         i = (i-1) * SECSIZ;
  81.         if (fp->_nleft % SECSIZ) {
  82.                 movmem(fp->_buff + i, fp->_buff, SECSIZ);
  83.                 fp->_nleft += i;
  84.                 fp->_nextp -= i;
  85.                 return seek(fp->_fd, -1, 1); /* ??? */
  86.          }
  87.  
  88.         fp->_nleft = (NSECTS * SECSIZ);
  89.         fp->_nextp = fp->_buff;
  90.         return OK;
  91. }
  92.  
  93. int fread(buf, size, count, fp)
  94. char *buf;
  95. unsigned size, count;
  96. FILE *fp;
  97. {
  98.         int n_read, n_togo, cnt, i;
  99.  
  100.         n_togo = size * count;
  101.         n_read = 0;
  102.         if (fp->_flags & _EOF)
  103.                 return NULL;
  104.  
  105.         while (n_togo)
  106.         {
  107.                 cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft; /* how many bytes we can get from buffer at once */
  108.                 movmem(fp->_nextp, buf, cnt); /* get them */
  109.                 fp->_nextp += cnt; /* source pointer */
  110.                 buf += cnt; /* destination pointer */
  111.                 fp->_nleft -= cnt; /* how many bytes remained loaded in buffer */
  112.                 n_togo -= cnt; /* how many bytes we still need */
  113.                 n_read += cnt;
  114.                 if (n_togo) /* we need more bytes */
  115.                 {
  116.                         if ((cnt = read(fp->_fd, fp->_buff, NSECTS)) <=0)
  117.                         {
  118.                                 fp->_flags |= _EOF;
  119.                                 goto text_test;
  120.                         }
  121.                         fp->_nleft = cnt * SECSIZ;
  122.                         fp->_nextp = fp->_buff;
  123.                 }
  124.         }
  125.  text_test:
  126.         if (fp->_flags & _TEXT)
  127.         {
  128.                 i = min(n_read, SECSIZ);
  129.                 while (i--)
  130.                         if (*(buf-i) == CPMEOF)
  131.                         {
  132.                                 fp->_flags |= _EOF;
  133.                                 return (n_read - i);
  134.                         }
  135.         }
  136.         return (n_read/size);
  137. }
  138.  
  139. int fwrite(buf, size, count, fp)
  140. char *buf;
  141. unsigned size, count;
  142. FILE *fp;
  143. {
  144.         int n_done, n_togo, cnt;
  145.  
  146.         n_togo = size * count;
  147.         n_done = 0;
  148.  
  149.         if (fp->_flags & _ERR)
  150.                 return NULL;
  151.  
  152.         while (n_togo)
  153.         {
  154.                 cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft;
  155.                 movmem(buf, fp->_nextp, cnt);
  156.                 fp->_nextp += cnt;
  157.                 buf += cnt;
  158.                 fp->_nleft -= cnt;
  159.                 n_togo -= cnt;
  160.                 n_done += cnt;
  161.                 if (n_togo)
  162.                 {
  163.                         if ((cnt = write(fp->_fd, fp->_buff, NSECTS)) <= 0)
  164.                         {
  165.                                 fp->_flags |= _ERR;
  166.                                 return ERROR;
  167.                         }
  168.                         fp->_nleft = (NSECTS * SECSIZ);
  169.                         fp->_nextp = fp->_buff;
  170.                 }
  171.         }
  172.         return (n_done/size);
  173. }
  174.  
  175. #define LOADBUFSZ 4096
  176. char loadbuf[LOADBUFSZ];
  177. FILE g_fpout;
  178. int i;
  179.  
  180. main(argc,argv)
  181. char **argv;
  182. {
  183. FILE *fp;
  184. FILE *fpout;
  185. printf("Hello world!\n");
  186.  
  187. for (i = 1; i < argc; i++) printf("Arg #%d = %s\n",i,argv[i]);
  188.  
  189. /*fp = fopen("ex.c","rb");*/
  190. if ((fp = alloc(sizeof(*fp))) == NULL) return NULL;
  191. fp->_nextp = fp->_buff;
  192. fp->_nleft = 0;
  193. fp->_flags = _READ;
  194. fp->_fd = open("ex.c", 0);
  195.  
  196. /*fpout = fopen("myfile3.a","wb");*/
  197. if ((fpout = alloc(sizeof(*fp))) == NULL) return NULL;
  198. fpout->_nextp = fpout->_buff;
  199. fpout->_nleft = (NSECTS * SECSIZ);
  200. fpout->_flags = _WRITE;
  201. fpout->_fd = creat("myfile1.a");
  202.  
  203. while (1) {
  204.  i = fread(loadbuf, 1, LOADBUFSZ, fp);
  205.  /*read(fp->_fd, loadbuf, LOADBUFSZ/SECSIZ);*/
  206.  /*while (1);*/
  207.  if (!i) break;
  208.  fwrite(loadbuf, 1, i, fpout);
  209.  /*write(fpout->_fd, loadbuf, LOADBUFSZ/SECSIZ);*/
  210. }
  211.  
  212. /*fclose(fpout);*/
  213. if (fflush(fpout) == ERROR) return ERROR;
  214. close(fpout->_fd);
  215. free(fpout);
  216.  
  217. /*fclose(fp);*/
  218. close(fp->_fd);
  219. free(fp);
  220. /*getchar();
  221. setgfx();
  222. cls();
  223. putpixel(100,100,3);
  224. putpixel(101,102,3);
  225. getchar();*/
  226. /*while(1) {
  227.  i = 0;
  228.  i = 1;
  229. }*/
  230. }
  231.