1 /* $Id: endfile.c,v 1.4 2008/05/04 10:38:33 ragge Exp $ */
2 /*
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
17 * International, Inc.
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35 #include <sys/wait.h>
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "fio.h"
42 static alist *ax;
43
44 int
f_end(alist * a)45 f_end(alist *a)
46 {
47 unit *b;
48 if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
49 b = &units[a->aunit];
50 if(b->ufd==NULL) return(0);
51 b->uend=1;
52 if( b->useek==0) return(0);
53 ax=a;
54 if(b->uwrt) nowreading(b);
55 return(t_runc(b));
56 }
57
58 int
t_runc(unit * b)59 t_runc(unit *b)
60 {
61 char buf[128],nm[16];
62 FILE *tmp;
63 int n,m, fd;
64 long loc,len;
65
66 if(b->url) return(0); /*don't truncate direct files*/
67 loc=ftell(b->ufd);
68 fseek(b->ufd,0L,2);
69 len=ftell(b->ufd);
70 if(loc==len || b->useek==0 || b->ufnm==NULL) return(0);
71 strcpy(nm,"tmp.FXXXXXX");
72 if(b->uwrt) nowreading(b);
73 fd = mkstemp(nm);
74 tmp=fdopen(fd,"w");
75 fseek(b->ufd,0L,0);
76 for(;loc>0;)
77 {
78 n=fread(buf,1,loc>128?128:(int)loc,b->ufd);
79 if(n>loc) n=loc;
80 loc -= n;
81 fwrite(buf,1,n,tmp);
82 }
83 fflush(tmp);
84 for(n=0;n<10;n++)
85 {
86 if((m=fork())==-1) continue;
87 else if(m==0)
88 {
89 execl("/bin/cp","cp",nm,b->ufnm,NULL);
90 execl("/usr/bin/cp","cp",nm,b->ufnm,NULL);
91 fprintf(stdout,"no cp\n");
92 exit(1);
93 }
94 wait(&m);
95 if(m!=0) err(ax->aerr,111,"endfile");
96 fclose(tmp);
97 unlink(nm);
98 return(0);
99 }
100 err(ax->aerr,111,"endfile");
101 }
102