?login_element?

Subversion Repositories NedoOS

Rev

Rev 1154 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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) /*stdin, stdout, stderr?*/
  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 * SECSIZ - fp->_nleft;
  75.         if (write(fp->_fd, fp->_buff, i) != i)
  76.         {
  77.                 fp->_flags |= _ERR;
  78.                 return ERROR;
  79.         }
  80.  
  81.         fp->_nleft = (NSECTS * SECSIZ);
  82.         fp->_nextp = fp->_buff;
  83.         return OK;
  84. }
  85.  
  86. int fread(buf, size, count, fp)
  87. char *buf;
  88. unsigned size, count;
  89. FILE *fp;
  90. {
  91.         int n_read, n_togo, cnt, i;
  92.  
  93.         n_togo = size * count;
  94.         n_read = 0;
  95.         if (fp->_flags & _EOF)
  96.                 return NULL;
  97.  
  98.         while (n_togo)
  99.         {
  100.                 cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft; /* how many bytes we can get from buffer at once */
  101.                 movmem(fp->_nextp, buf, cnt); /* get them */
  102.                 fp->_nextp += cnt; /* source pointer */
  103.                 buf += cnt; /* destination pointer */
  104.                 fp->_nleft -= cnt; /* how many bytes remained loaded in buffer */
  105.                 n_togo -= cnt; /* how many bytes we still need */
  106.                 n_read += cnt;
  107.                 if (n_togo) /* we need more bytes */
  108.                 {
  109.                         if ((cnt = read(fp->_fd, fp->_buff, NSECTS*SECSIZ)) <=0)
  110.                         {
  111.                                 fp->_flags |= _EOF;
  112.                                 goto text_test;
  113.                         }
  114.                         fp->_nleft = cnt;
  115.                         fp->_nextp = fp->_buff;
  116.                 }
  117.         }
  118.  text_test:
  119.         if (fp->_flags & _TEXT)
  120.         {
  121.                 i = min(n_read, SECSIZ);
  122.                 while (i--)
  123.                         if (*(buf-i) == CPMEOF)
  124.                         {
  125.                                 fp->_flags |= _EOF;
  126.                                 return (n_read - i);
  127.                         }
  128.         }
  129.         return (n_read/size);
  130. }
  131.  
  132. int fwrite(buf, size, count, fp)
  133. char *buf;
  134. unsigned size, count;
  135. FILE *fp;
  136. {
  137.         int n_done, n_togo, cnt;
  138.  
  139.         n_togo = size * count;
  140.         n_done = 0;
  141.  
  142.         if (fp->_flags & _ERR)
  143.                 return NULL;
  144.  
  145.         while (n_togo)
  146.         {
  147.                 cnt = (n_togo <= fp->_nleft) ? n_togo : fp->_nleft;
  148.                 movmem(buf, fp->_nextp, cnt);
  149.                 fp->_nextp += cnt;
  150.                 buf += cnt;
  151.                 fp->_nleft -= cnt;
  152.                 n_togo -= cnt;
  153.                 n_done += cnt;
  154.                 if (n_togo)
  155.                 {
  156.                         if ((cnt = write(fp->_fd, fp->_buff, NSECTS*SECSIZ)) <= 0)
  157.                         {
  158.                                 fp->_flags |= _ERR;
  159.                                 return ERROR;
  160.                         }
  161.                         fp->_nleft = (NSECTS * SECSIZ);
  162.                         fp->_nextp = fp->_buff;
  163.                 }
  164.         }
  165.         return (n_done/size);
  166. }
  167.  
  168. unsigned strtouint(s)
  169. char *s;
  170. {
  171. unsigned n;
  172. char c;
  173. n = 0;
  174. while(c = *s++) {
  175.         n = n*10;
  176.         n += c-'0';
  177. }
  178. return n;
  179. }
  180.  
  181. #define LOADBUFSZ 4096
  182. char loadbuf[LOADBUFSZ];
  183. FILE g_fpout;
  184. int i;
  185. int fd;
  186.  
  187. main(argc,argv)
  188. char **argv;
  189. {
  190. FILE *fpin;
  191. FILE *fpout;
  192. unsigned size;
  193. unsigned sizeremain;
  194. unsigned sizetoread;
  195. int parindex;
  196. parindex = 3;
  197. printf("Cut file. Usage: cut SIZE infile outfile1 outfile2...\n");
  198.  
  199. for (i = 0; i < argc; i++) printf("Arg #%d = %s\n",i,argv[i]);
  200. if (argc < 4) return;
  201.  
  202. size = strtouint(argv[1]);
  203. /*while(1);*/
  204.  
  205. /*fpout = fopen("myfile3.a","wb");*/
  206. if ((fpin = alloc(sizeof(*fpin))) == NULL) return NULL;
  207. fpin->_nextp = fpin->_buff;
  208. /*fpout->_nleft = (NSECTS * SECSIZ);*/
  209. /*fpout->_flags = _WRITE;*/
  210. /*fpout->_fd = creat(argv[1]);*/
  211. fpin->_nleft = 0;
  212. fpin->_flags = _READ;
  213. fpin->_fd = open(argv[2], 0);
  214.  
  215. while (1) {
  216.  
  217.         /*fp = fopen("ex.c","rb");*/
  218.         if ((fpout = alloc(sizeof(*fpout))) == NULL) return NULL;
  219.         fpout->_nextp = fpout->_buff;
  220.         /*fp->_nleft = 0;*/
  221.         /*fp->_fd = open(argv[2], 0);*/
  222.         fpout->_nleft = (NSECTS * SECSIZ);
  223.         fpout->_flags = _WRITE;
  224.         fpout->_fd = creat(argv[parindex]);
  225.  
  226.         sizeremain = size;
  227.         while (sizeremain) {
  228.          sizetoread = sizeremain;
  229.          if (sizetoread > LOADBUFSZ) sizetoread = LOADBUFSZ;
  230.          i = fread(loadbuf, 1, sizetoread, fpin);
  231.          sizeremain -= i;
  232.          /*while (1);*/
  233.          if (!i) break;
  234.          fwrite(loadbuf, 1, i, fpout);
  235.         }
  236.  
  237.         /*fclose(fpout);*/
  238.         if (fflush(fpout) == ERROR) return ERROR;
  239.         close(fpout->_fd);
  240.         free(fpout);
  241.        
  242.         parindex++;
  243.         if (parindex == argc) break;
  244.  
  245. }
  246.  
  247. /*fclose(fpin);*/
  248. close(fpin->_fd);
  249. free(fpin);
  250. }
  251.