| 1 | // |
|---|
| 2 | // TxWin, Textmode Windowing Library |
|---|
| 3 | // |
|---|
| 4 | // Original code Copyright (c) 1995-2005 Fsys Software and Jan van Wijk |
|---|
| 5 | // |
|---|
| 6 | // ========================================================================== |
|---|
| 7 | // |
|---|
| 8 | // This file contains Original Code and/or Modifications of Original Code as |
|---|
| 9 | // defined in and that are subject to the GNU Lesser General Public License. |
|---|
| 10 | // You may not use this file except in compliance with the License. |
|---|
| 11 | // BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE LICENSE. |
|---|
| 12 | // A copy of the License is provided with the Original Code and Modifications, |
|---|
| 13 | // and is also available at http://www.dfsee.com/txwin/lgpl.htm |
|---|
| 14 | // |
|---|
| 15 | // This library is free software; you can redistribute it and/or modify |
|---|
| 16 | // it under the terms of the GNU Lesser General Public License as published |
|---|
| 17 | // by the Free Software Foundation; either version 2.1 of the License, |
|---|
| 18 | // or (at your option) any later version. |
|---|
| 19 | // |
|---|
| 20 | // This library is distributed in the hope that it will be useful, |
|---|
| 21 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 22 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 23 | // See the GNU Lesser General Public License for more details. |
|---|
| 24 | // |
|---|
| 25 | // You should have received a copy of the GNU Lesser General Public License |
|---|
| 26 | // along with this library; (lgpl.htm) if not, write to the Free Software |
|---|
| 27 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 28 | // |
|---|
| 29 | // Questions on TxWin licensing can be directed to: txwin@fsys.nl |
|---|
| 30 | // |
|---|
| 31 | // ========================================================================== |
|---|
| 32 | // |
|---|
| 33 | // TxLib file functions, generic Seek and determine filesize |
|---|
| 34 | // |
|---|
| 35 | |
|---|
| 36 | #include <txlib.h> // TxLib interface |
|---|
| 37 | |
|---|
| 38 | #include <sys/stat.h> // for low level stuff |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /*****************************************************************************/ |
|---|
| 42 | // Test if exact (path+) filename is accessible; determine size > 2GiB files |
|---|
| 43 | /*****************************************************************************/ |
|---|
| 44 | BOOL TxFileSize // RET file exists |
|---|
| 45 | ( |
|---|
| 46 | char *fname, // IN filename string |
|---|
| 47 | LLONG *size // OUT filesize or NULL |
|---|
| 48 | ) |
|---|
| 49 | { |
|---|
| 50 | BOOL rc = FALSE; // function return |
|---|
| 51 | TXHFILE fh = 0; |
|---|
| 52 | #if defined (DEV32) |
|---|
| 53 | ULONG action; // action taken on open |
|---|
| 54 | TXF_OS2LFAPI api; // large file API |
|---|
| 55 | #elif defined (WIN32) |
|---|
| 56 | ULONG hi = 0; // hi part of size |
|---|
| 57 | ULONG lo = 0; // lo part of size |
|---|
| 58 | #elif defined (LINUX) |
|---|
| 59 | #else |
|---|
| 60 | #endif |
|---|
| 61 | |
|---|
| 62 | ENTER(); |
|---|
| 63 | |
|---|
| 64 | #if defined (DEV32) // test large file support APIs |
|---|
| 65 | if (TxLargeFileApiOS2( &api)) |
|---|
| 66 | { |
|---|
| 67 | (api.DosOpenLarge)( fname, &fh, &action, 0, FILE_NORMAL, FILE_OPEN, |
|---|
| 68 | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL); |
|---|
| 69 | } |
|---|
| 70 | else |
|---|
| 71 | { |
|---|
| 72 | DosOpen( fname, &fh, &action, 0, FILE_NORMAL, FILE_OPEN, |
|---|
| 73 | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL); |
|---|
| 74 | } |
|---|
| 75 | #elif defined (WIN32) |
|---|
| 76 | fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, NULL, |
|---|
| 77 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
|---|
| 78 | #elif defined (LINUX) |
|---|
| 79 | fh = open( fname, O_RDONLY | O_LARGEFILE); |
|---|
| 80 | #else |
|---|
| 81 | fh = fopen( fname, "r"); |
|---|
| 82 | #endif |
|---|
| 83 | |
|---|
| 84 | if ((fh != 0) && (fh != ((TXHFILE) -1))) |
|---|
| 85 | { |
|---|
| 86 | rc = TRUE; |
|---|
| 87 | |
|---|
| 88 | if (size != NULL) |
|---|
| 89 | { |
|---|
| 90 | *size = 0; // initialize to zero size |
|---|
| 91 | |
|---|
| 92 | #if defined (DEV32) |
|---|
| 93 | if (TxLargeFileApiOS2( &api)) |
|---|
| 94 | { |
|---|
| 95 | (api.DosSeekLarge)( fh, 0, FILE_END, size); |
|---|
| 96 | } |
|---|
| 97 | else |
|---|
| 98 | { |
|---|
| 99 | DosSetFilePtr( fh, 0, FILE_END, (ULONG *) size); |
|---|
| 100 | } |
|---|
| 101 | #elif defined (WIN32) |
|---|
| 102 | lo = GetFileSize( fh, &hi); |
|---|
| 103 | *size = (((LLONG) hi) << 32) + lo; |
|---|
| 104 | |
|---|
| 105 | TxPrint( "TxFileSize: hi = %8.8lx lo = %8.8lx\n", hi, lo); |
|---|
| 106 | TxPrint( "Size: %16.16llX = %lld bytes\n", *size, *size); |
|---|
| 107 | #elif defined (LINUX) |
|---|
| 108 | _llseek( fh, 0, 0, size, SEEK_END); |
|---|
| 109 | #else |
|---|
| 110 | fseek( fh, 0, SEEK_END); |
|---|
| 111 | *size = (LLONG) ftell( fh); |
|---|
| 112 | #endif |
|---|
| 113 | TRACES(( "Size: %16.16llX = %lld bytes\n", *size, *size)); |
|---|
| 114 | } |
|---|
| 115 | (void) TxClose( fh); |
|---|
| 116 | } |
|---|
| 117 | BRETURN( rc); |
|---|
| 118 | } // end 'TxFileSize' |
|---|
| 119 | /*---------------------------------------------------------------------------*/ |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /*****************************************************************************/ |
|---|
| 123 | // Seek to specified position in open file (platform specific fseek) |
|---|
| 124 | /*****************************************************************************/ |
|---|
| 125 | ULONG TxFileSeek |
|---|
| 126 | ( |
|---|
| 127 | TXHFILE fh, // IN file handle |
|---|
| 128 | LLONG offset, // IN seek offset |
|---|
| 129 | int whence // IN seek reference |
|---|
| 130 | ) |
|---|
| 131 | { |
|---|
| 132 | ULONG rc = NO_ERROR; // function return |
|---|
| 133 | #if defined (DEV32) |
|---|
| 134 | TXF_OS2LFAPI api; // large file API |
|---|
| 135 | LLONG current; |
|---|
| 136 | #elif defined (WIN32) |
|---|
| 137 | ULONG hi = 0; // High part of seek offset |
|---|
| 138 | ULONG lo = 0; // Low part of seek offset |
|---|
| 139 | #elif defined (LINUX) |
|---|
| 140 | LLONG ll; |
|---|
| 141 | #else |
|---|
| 142 | #endif |
|---|
| 143 | |
|---|
| 144 | ENTER(); |
|---|
| 145 | |
|---|
| 146 | #if defined (DEV32) |
|---|
| 147 | if (TxLargeFileApiOS2( &api)) |
|---|
| 148 | { |
|---|
| 149 | (api.DosSeekLarge)( fh, offset, (ULONG) whence, ¤t); |
|---|
| 150 | } |
|---|
| 151 | else |
|---|
| 152 | { |
|---|
| 153 | DosSetFilePtr( fh, (ULONG) offset, (ULONG) whence, (ULONG *) ¤t); |
|---|
| 154 | } |
|---|
| 155 | #elif defined (WIN32) |
|---|
| 156 | lo = offset & 0xffffffff; // Note: no ...PointerEx() use |
|---|
| 157 | hi = offset >> 32; // to allow running on NT4! |
|---|
| 158 | |
|---|
| 159 | if (SetFilePointer( fh, lo, (PLONG) &hi, (DWORD) whence) == 0xffffffff) |
|---|
| 160 | { |
|---|
| 161 | rc = GetLastError(); |
|---|
| 162 | } |
|---|
| 163 | #elif defined (LINUX) |
|---|
| 164 | if ( _llseek( fh, (offset >> 32), (offset & 0xffffffff), &ll, whence) == -1) |
|---|
| 165 | { |
|---|
| 166 | rc = TxRcFromErrno( errno); |
|---|
| 167 | } |
|---|
| 168 | #else |
|---|
| 169 | fseek( fh, (LONG) offset, whence); |
|---|
| 170 | #endif |
|---|
| 171 | RETURN (rc); |
|---|
| 172 | } // end 'TxFileSeek' |
|---|
| 173 | /*---------------------------------------------------------------------------*/ |
|---|