Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / unx / readwrite_helper.c
blob347f632bbbb1246449dabf82d08e4b03497351a6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "readwrite_helper.h"
12 #include <osl/diagnose.h>
13 #include <system.h>
15 sal_Bool safeWrite(int fd, void* data, sal_uInt32 dataSize)
17 sal_Int32 nToWrite = dataSize;
18 unsigned char* dataToWrite = data;
20 // Check for overflow as we convert a signed to an unsigned.
21 OSL_ASSERT(dataSize == (sal_uInt32)nToWrite);
22 while ( nToWrite ) {
23 sal_Int32 nWritten = write(fd, dataToWrite, nToWrite);
24 if ( nWritten < 0 ) {
25 if ( errno == EINTR )
26 continue;
28 return sal_False;
32 OSL_ASSERT(nWritten > 0);
33 nToWrite -= nWritten;
34 dataToWrite += nWritten;
37 return sal_True;
40 sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count )
42 sal_Int32 nToRead = count;
43 unsigned char* bufferForReading = buffer;
45 // Check for overflow as we convert a signed to an unsigned.
46 OSL_ASSERT(count == (sal_uInt32)nToRead);
47 while ( nToRead ) {
48 sal_Int32 nRead = read(fd, bufferForReading, nToRead);
49 if ( nRead < 0 ) {
50 // We were interrupted before reading, retry.
51 if (errno == EINTR)
52 continue;
54 return sal_False;
57 // If we reach the EOF, we consider this a partial transfer and thus
58 // an error.
59 if ( nRead == 0 )
60 return sal_False;
62 nToRead -= nRead;
63 bufferForReading += nRead;
66 return sal_True;
69 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */