1 /* $Id$
2  *  Provide compiler-independent functions to file buffers flush.
3  *
4  * HUSKYLIB: common defines, types and functions for HUSKY
5  *
6  * This is part of The HUSKY Fidonet Software project:
7  * see http://husky.sourceforge.net for details
8  *
9  *
10  * HUSKYLIB is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * HUSKYLIB is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; see file COPYING. If not, write to the
22  * Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * See also http://www.gnu.org, license may be found here.
25  */
26 
27 /* standard headers */
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 /* huskylib: compiler.h */
32 #include <compiler.h>
33 
34 /* standard headers */
35 #if defined(HAS_IO_H)
36 #include <io.h>
37 #endif
38 
39 #if defined(HAS_UNISTD_H)
40 #include <unistd.h>
41 #endif
42 
43 #if defined(HAS_DOS_H)
44 #include <dos.h>
45 #endif
46 
47 
48 /* huskylib headers */
49 #define DLLEXPORT
50 #include <huskyext.h>
51 
52 
53 #ifdef __OS2__
54 
55 #  define INCL_NOPM
56 #  include <os2.h>
57 
58 #  if defined(__WATCOMC__)
59 #    ifndef DosBufReset
60 #      define DosBufReset DosResetBuffer
61 #    endif
62 #  elif defined(__EMX__) || defined(__FLAT__)
63 #    undef DosBufReset
64 #    define DosBufReset DosResetBuffer
65 #  endif
66 #endif
67 
68 #if defined(__NT__) || defined(__MINGW32__)
69 #  define WIN32_LEAN_AND_MEAN
70 #  define NOGDI
71 #  define NOUSER
72 #  define NOMSG
73 #  include <windows.h>
74 #endif
75 
76 #if defined(__DJGPP__)
77 /*
78 void pascal far flush_handle2(int fh)
79 {
80     fsync(fh);
81 }
82 */
83 #define flush_handle2(f)  fsync(f)
84 
85 #elif defined(__UNIX__) || defined(SASC)
86 /*
87 void pascal far flush_handle2(int fh)
88 {
89     unused(fh);
90 }
91 */
92 #define flush_handle2(f)  unused(f)
93 
94 
95 #elif defined(__OS2__)
96 /*
97 void pascal far flush_handle2(int fh)
98 {
99     DosBufReset((HFILE) fh);
100 }
101 */
102 #define flush_handle2(f)  DosBufReset((HFILE) f)
103 
104 #elif defined(__WIN32__)
105 
106 #ifdef __RSXNT__
107 #  include <emx/syscalls.h>
108 
109 #  ifndef F_GETOSFD
110 #    define F_GETOSFD 6
111 #  endif
112 
113 #  define flush_handle2(fh)  FlushFileBuffers((HANDLE) __fcntl((fh), F_GETOSFD, 0))
114 
115 #else
116 
117 #  define flush_handle2(fh)  FlushFileBuffers((HANDLE) (fh))
118 
119 #endif
120 
121 /*
122 void pascal far flush_handle2(int fh)
123 {
124 #ifdef __RSXNT__
125     int nt_handle = __fcntl(fh, F_GETOSFD, 0);
126 #else
127     int nt_handle = fh;
128 #endif
129     FlushFileBuffers((HANDLE) nt_handle);
130 }
131 */
132 
133 #elif defined(__WATCOMC__DOS__) || defined(__MSC__)
134 
135 #include <dos.h>
136 /*
137 void pascal far flush_handle2(int fh)
138 {
139     _dos_commit(fh);
140 }
141 */
142 #  define flush_handle2(fh)  _dos_commit(fh)
143 
144 #elif defined (__DOS__)
145 
flush_handle2(int fd)146 void pascal far flush_handle2(int fd)
147 {
148  union REGS in, out;
149 
150  in.h.ah=0x45;
151 #if defined(__DPMI__) && !defined(__DJGPP__)
152  in.x.ebx=fd;
153  int386(0x21, &in, &out);
154 #else  /* #elif defined(__DOS16__) || defined(__DJGPP__) */
155  in.x.bx=fd;
156  int86(0x21,&in,&out);
157 #endif
158 
159  if(out.h.cflag) return;
160 
161  in.h.ah=0x3e;
162 #if defined(__DPMI__) && !defined(__DJGPP__)
163  in.x.ebx=out.x.eax;
164  int386(0x21, &in, &out);
165 #else  /* #elif defined(__DOS16__) || defined(__DJGPP__) */
166  in.x.bx=out.x.ax;
167  int86(0x21,&in,&out);
168 #endif
169 }
170 
171 #else
172 
173 #error unknown compiler
174 
175 #endif
176 
177 /*
178  *  This makes sure a file gets flushed to disk.  Thanks to Ray Duncan
179  *  for this tip in his _Advanced MS-DOS_ book.
180  */
181 
flush_handle(FILE * fp)182 void _fast flush_handle(FILE * fp)
183 {
184     fflush(fp);
185 
186 #if defined(__OS2__) || defined(__DOS__) || defined(__NT__) || defined(__TURBOC__) || defined(SASC) || defined(__DJGPP__)
187     flush_handle2(fileno(fp));
188 #else
189     {
190         int nfd;
191 
192         nfd = dup(fileno(fp));
193         if (nfd != -1)
194         {
195             close(nfd);
196         }
197     }
198 #endif
199 }
200