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