1: /* 2: char id_endfile[] = "@(#)endfile.c 1.7"; 3: * 4: * endfile 5: */ 6: 7: #include "fio.h" 8: 9: static char endf[] = "endfile"; 10: extern char *tmplate; 11: 12: f_end (a) 13: alist *a; 14: { 15: unit *b; 16: 17: lfname = NULL; 18: elist = NO; 19: errflag = a->aerr; 20: lunit = a->aunit; 21: if (not_legal(lunit)) 22: err (errflag, F_ERUNIT, endf) 23: b = &units[lunit]; 24: if (!b->ufd) 25: err (errflag, F_ERNOPEN, endf) 26: if (b->uend) 27: return(0); 28: lfname = b->ufnm; 29: b->uend = YES; 30: return ( t_runc (b, errflag, endf) ); 31: } 32: 33: t_runc (b, flag, str) 34: unit *b; 35: ioflag flag; 36: char *str; 37: { 38: char buf[128],nm[16]; 39: FILE *tmp; 40: int n,m; 41: long loc,len; 42: fflush (b->ufd); 43: if(b->url || !b->useek || !b->ufnm) 44: return (OK); /* don't truncate direct access files, etc. */ 45: if(b->uwrt && ! nowreading(b)) 46: err(errflag, errno, str); 47: loc = ftell (b->ufd); 48: fseek(b->ufd,0L,2); 49: len = ftell (b->ufd); 50: if (loc>=len) 51: return(OK); 52: strcpy(nm,tmplate); 53: mktemp(nm); 54: if(!(tmp=fopen(nm,"w"))) err(flag,errno,str); 55: fseek(b->ufd,0L,0); 56: while (loc) 57: { 58: n=fread(buf, 1, loc > sizeof(buf)?sizeof(buf):(int)loc, b->ufd); 59: loc -= n; 60: fwrite(buf,1,n,tmp); 61: } 62: fflush(tmp); 63: for(n=0;n<10;n++) 64: { 65: if((m=fork())==-1) continue; 66: else if(m==0) 67: { 68: execl("/bin/cp","cp",nm,b->ufnm,0); 69: execl("/usr/bin/cp","cp",nm,b->ufnm,0); 70: fatal(F_ERSYS,"no cp for trunc"); 71: } 72: wait(&m); 73: if(m) err(flag,F_ERTRUNC,str); 74: fclose(tmp); 75: unlink(nm); 76: return(OK); 77: } 78: err(flag,F_ERTRUNC,str); 79: }