| 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 filesystem functions, file-exist section |
|---|
| 34 | // |
|---|
| 35 | |
|---|
| 36 | #include <txlib.h> // TxLib interface |
|---|
| 37 | |
|---|
| 38 | #include <sys/stat.h> // for low level stuff |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /*****************************************************************************/ |
|---|
| 42 | // Create empty file with specified path/name, prompt to replace existing |
|---|
| 43 | /*****************************************************************************/ |
|---|
| 44 | ULONG TxCreateEmptyFile |
|---|
| 45 | ( |
|---|
| 46 | char *fname, // IN path and filename |
|---|
| 47 | BOOL prompt // IN prompt on replace |
|---|
| 48 | ) |
|---|
| 49 | { |
|---|
| 50 | ULONG rc = NO_ERROR; // function return |
|---|
| 51 | FILE *fp; |
|---|
| 52 | |
|---|
| 53 | ENTER(); |
|---|
| 54 | TRACES(( "Fname:'%s' prompt:%s\n", fname, (prompt) ? "YES" : "NO")); |
|---|
| 55 | |
|---|
| 56 | if (prompt && (TxFileExists( fname))) |
|---|
| 57 | { |
|---|
| 58 | if (!TxConfirm( 0, "File '%s' exists, replace ? [Y/N] : ", fname)) |
|---|
| 59 | { |
|---|
| 60 | rc = TX_ABORTED; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | if (rc == NO_ERROR) |
|---|
| 64 | { |
|---|
| 65 | if ((fp = fopen( fname, "w")) != NULL) |
|---|
| 66 | { |
|---|
| 67 | fclose( fp); |
|---|
| 68 | } |
|---|
| 69 | else |
|---|
| 70 | { |
|---|
| 71 | rc = TX_ACCESS_DENIED; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | RETURN (rc); |
|---|
| 75 | } // end 'TxCreateEmptyFile' |
|---|
| 76 | /*---------------------------------------------------------------------------*/ |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /*****************************************************************************/ |
|---|
| 80 | // Test if exact (path+) filename is accessible; supports > 2GiB files |
|---|
| 81 | /*****************************************************************************/ |
|---|
| 82 | BOOL TxFileExists // RET file is accessible |
|---|
| 83 | ( |
|---|
| 84 | char *fname // IN filename string |
|---|
| 85 | ) |
|---|
| 86 | { |
|---|
| 87 | BOOL rc = FALSE; // function return |
|---|
| 88 | TXHFILE fh = 0; |
|---|
| 89 | |
|---|
| 90 | #if defined (DEV32) // test large file support APIs |
|---|
| 91 | ULONG action; // action taken on open |
|---|
| 92 | TXF_OS2LFAPI api; // large file API |
|---|
| 93 | |
|---|
| 94 | if (TxLargeFileApiOS2( &api)) |
|---|
| 95 | { |
|---|
| 96 | (api.DosOpenLarge)( fname, &fh, &action, 0, FILE_NORMAL, FILE_OPEN, |
|---|
| 97 | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL); |
|---|
| 98 | } |
|---|
| 99 | else |
|---|
| 100 | { |
|---|
| 101 | DosOpen( fname, &fh, &action, 0, FILE_NORMAL, FILE_OPEN, |
|---|
| 102 | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, NULL); |
|---|
| 103 | } |
|---|
| 104 | #elif defined (WIN32) |
|---|
| 105 | fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ, NULL, |
|---|
| 106 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
|---|
| 107 | #elif defined (LINUX) |
|---|
| 108 | fh = open( fname, O_RDONLY); |
|---|
| 109 | #else |
|---|
| 110 | fh = fopen( fname, "r"); |
|---|
| 111 | #endif |
|---|
| 112 | |
|---|
| 113 | if ((fh != 0) && (fh != ((TXHFILE) -1))) |
|---|
| 114 | { |
|---|
| 115 | (void) TxClose( fh); |
|---|
| 116 | rc = TRUE; |
|---|
| 117 | } |
|---|
| 118 | return( rc); |
|---|
| 119 | } // end 'TxFileExists' |
|---|
| 120 | /*---------------------------------------------------------------------------*/ |
|---|