|
|
1.1 root 1: /* @(#)endfile.c 1.3 */
2:
3: #include "fio.h"
4: static alist *ax;
5: extern char *mktemp(), *strcpy();
6: f_end(a) alist *a;
7: {
8: unit *b;
9: if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
10: b = &units[a->aunit];
11: if(b->ufd==NULL) return(0);
12: b->uend=1;
13: if( b->useek==0) return(0);
14: ax=a;
15: if(b->uwrt) (void) nowreading(b);
16: return(t_runc(b));
17: }
18: t_runc(b) unit *b;
19: {
20: char buf[128],nm[16];
21: FILE *tmp;
22: int n,m;
23: long loc,len;
24: if(b->url) return(0); /*don't truncate direct files*/
25: loc=ftell(b->ufd);
26: (void) fseek(b->ufd,0L,2);
27: len=ftell(b->ufd);
28: if(loc==len || b->useek==0 || b->ufnm==NULL) return(0);
29: (void) strcpy(nm,"tmp.FXXXXXX");
30: if(b->uwrt) (void) nowreading(b);
31: (void) mktemp(nm);
32: tmp=fopen(nm,"w");
33: if(!tmp)
34: printf("hey %s\n", nm);
35: (void) fseek(b->ufd,0L,0);
36: for(;loc>0;)
37: {
38: n=fread(buf,1,loc>128?128:(int)loc,b->ufd);
39: if(n>loc) n=loc; else if (n<=0) break;
40: loc -= n;
41: (void) fwrite(buf,1,n,tmp);
42: }
43: (void) fflush(tmp);
44: for(n=0;n<10;n++)
45: {
46: if((m=fork())==-1) continue;
47: else if(m==0)
48: {
49: (void) execl("/bin/cp","cp",nm,b->ufnm,0);
50: (void) execl("/usr/bin/cp","cp",nm,b->ufnm,0);
51: fprintf(stdout,"no cp\n");
52: exit(1);
53: }
54: (void) wait(&m);
55: if(m!=0) err(ax->aerr,111,"endfile");
56: (void) fclose(tmp);
57: (void) unlink(nm);
58: return(0);
59: }
60: err(ax->aerr,111,"endfile");
61: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.