1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1996-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                     Phong Vo <phongvo@gmail.com>                     *
18 *               Glenn Fowler <glenn.s.fowler@gmail.com>                *
19 *                                                                      *
20 ***********************************************************************/
21 #include "rshdr.h"
22 
23 /*
24  * create temp stream ready for write
25  */
26 
27 #if __STD_C
rstempwrite(Rs_t * rs,Sfio_t * sp)28 Sfio_t* rstempwrite(Rs_t* rs, Sfio_t* sp)
29 #else
30 Sfio_t* rstempwrite(rs, sp)
31 Rs_t*	rs;
32 Sfio_t*	sp;
33 #endif
34 {
35 	Sfio_t*		op = sp;
36 
37 	if (!sp)
38 	{
39 #if _PACKAGE_ast
40 		char	path[PATH_MAX];
41 		int	fd;
42 
43 		if (!pathtemp(path, sizeof(path), NiL, "sf", &fd))
44 			return 0;
45 		remove(path);
46 		if (!(sp = sfnew(NiL, NiL, SF_UNBOUND, fd, SF_READ|SF_WRITE)))
47 			return 0;
48 #else
49 		if (!(sp = sftmp(0)))
50 			return 0;
51 #endif
52 	}
53 	else
54 		sfresize(sp, 0);
55 	if ((rs->events & RS_TEMP_WRITE) && rsnotify(rs, RS_TEMP_WRITE, sp, (Void_t*)0, rs->disc) < 0)
56 	{
57 		if (!op)
58 			sfclose(sp);
59 		sp = 0;
60 	}
61 	return sp;
62 }
63 
64 /*
65  * rewind temp stream and prepare for read
66  */
67 
68 #if __STD_C
rstempread(Rs_t * rs,Sfio_t * sp)69 int rstempread(Rs_t* rs, Sfio_t* sp)
70 #else
71 int rstempread(rs, sp)
72 Rs_t*	rs;
73 Sfio_t*	sp;
74 #endif
75 {
76 	int	n;
77 
78 	if (sfsync(sp))
79 		return -1;
80 	if (rs->events & RS_TEMP_READ)
81 	{
82 		if ((n = rsnotify(rs, RS_TEMP_READ, sp, (Void_t*)0, rs->disc)) < 0)
83 			return -1;
84 		if (n)
85 			return 0;
86 	}
87 	return sfseek(sp, (Sfoff_t)0, SEEK_SET) ? -1 : 0;
88 }
89 
90 /*
91  * close temp stream
92  */
93 
94 #if __STD_C
rstempclose(Rs_t * rs,Sfio_t * sp)95 int rstempclose(Rs_t* rs, Sfio_t* sp)
96 #else
97 int rstempclose(rs, sp)
98 Rs_t*	rs;
99 Sfio_t*	sp;
100 #endif
101 {
102 	int	n;
103 
104 	if (rs->events & RS_TEMP_CLOSE)
105 	{
106 		if ((n = rsnotify(rs, RS_TEMP_CLOSE, sp, (Void_t*)0, rs->disc)) < 0)
107 			return -1;
108 		if (n)
109 			return 0;
110 	}
111 	return sfclose(sp);
112 }
113