Chris@0: /* Chris@0: ** Copyright (C) 2001-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** This program is free software; you can redistribute it and/or modify Chris@0: ** it under the terms of the GNU General Public License as published by Chris@0: ** the Free Software Foundation; either version 2 of the License, or Chris@0: ** (at your option) any later version. Chris@0: ** Chris@0: ** This program is distributed in the hope that it will be useful, Chris@0: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: ** GNU General Public License for more details. Chris@0: ** Chris@0: ** You should have received a copy of the GNU General Public License Chris@0: ** along with this program; if not, write to the Free Software Chris@0: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Chris@0: */ Chris@0: Chris@0: #include "sfconfig.h" Chris@0: #include "sndfile.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #if HAVE_UNISTD_H Chris@0: #include Chris@0: #endif Chris@0: Chris@0: #if (HAVE_DECL_S_IRGRP == 0) Chris@0: #include Chris@0: #endif Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #define SIGNED_SIZEOF(x) ((int) sizeof (x)) Chris@0: Chris@0: /* EMX is OS/2. */ Chris@0: #if defined (__CYGWIN__) || defined (__EMX__) Chris@0: Chris@0: #define LSEEK lseek Chris@0: #define FSTAT fstat Chris@0: Chris@0: typedef struct stat STATBUF ; Chris@0: typedef off_t INT64 ; Chris@0: Chris@0: static char dir_cmd [] = "ls -l" ; Chris@0: Chris@0: #elif (defined (WIN32) || defined (_WIN32)) Chris@0: Chris@0: #define LSEEK _lseeki64 Chris@0: #define FSTAT _fstati64 Chris@0: Chris@0: typedef struct _stati64 STATBUF ; Chris@0: typedef __int64 INT64 ; Chris@0: Chris@0: static char dir_cmd [] = "dir" ; Chris@0: Chris@0: #else Chris@0: Chris@0: #define LSEEK lseek Chris@0: #define FSTAT fstat Chris@0: Chris@0: typedef struct stat STATBUF ; Chris@0: typedef sf_count_t INT64 ; Chris@0: Chris@0: #define O_BINARY 0 Chris@0: static char dir_cmd [] = "ls -l" ; Chris@0: Chris@0: #endif Chris@0: Chris@0: static void show_fstat_error (void) ; Chris@0: static void show_lseek_error (void) ; Chris@0: static void show_stat_fstat_error (void) ; Chris@0: static void write_to_closed_file (void) ; Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { Chris@0: puts ("\n\n\n\n" Chris@0: "This program shows up errors in the Win32 implementation of\n" Chris@0: "a couple of POSIX API functions on some versions of windoze.\n" Chris@0: "It can also be compiled on Linux (which works correctly) and\n" Chris@0: "other OSes just to provide a sanity check.\n" Chris@0: ) ; Chris@0: Chris@0: show_fstat_error () ; Chris@0: show_lseek_error () ; Chris@0: show_stat_fstat_error () ; Chris@0: write_to_closed_file () ; Chris@0: Chris@0: puts ("\n\n") ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: static void Chris@0: show_fstat_error (void) Chris@0: { static const char *filename = "fstat.dat" ; Chris@0: static char data [256] ; Chris@0: Chris@0: STATBUF statbuf ; Chris@0: int fd, mode, flags ; Chris@0: Chris@0: if (sizeof (statbuf.st_size) != sizeof (INT64)) Chris@0: { printf ("\n\nLine %d: Error, sizeof (statbuf.st_size) != 8.\n\n", __LINE__) ; Chris@0: return ; Chris@0: } ; Chris@0: Chris@0: puts ("\n64 bit fstat() test.\n--------------------") ; Chris@0: Chris@0: printf ("0) Create a file, write %d bytes and close it.\n", SIGNED_SIZEOF (data)) ; Chris@0: mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ; Chris@0: flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ; Chris@0: if ((fd = open (filename, mode, flags)) < 0) Chris@0: { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: return ; Chris@0: } ; Chris@0: assert (write (fd, data, sizeof (data)) > 0) ; Chris@0: close (fd) ; Chris@0: Chris@0: printf ("1) Re-open file in read/write mode and write another %d bytes at the end.\n", SIGNED_SIZEOF (data)) ; Chris@0: mode = O_RDWR | O_BINARY ; Chris@0: flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ; Chris@0: if ((fd = open (filename, mode, flags)) < 0) Chris@0: { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: return ; Chris@0: } ; Chris@0: LSEEK (fd, 0, SEEK_END) ; Chris@0: assert (write (fd, data, sizeof (data)) > 0) ; Chris@0: Chris@0: printf ("2) Now use system (\"%s %s\") to show the file length.\n\n", dir_cmd, filename) ; Chris@0: Chris@0: /* Would use snprintf, but thats not really available on windows. */ Chris@0: memset (data, 0, sizeof (data)) ; Chris@0: strncpy (data, dir_cmd, sizeof (data) - 1) ; Chris@0: strncat (data, " ", sizeof (data) - 1 - strlen (data)) ; Chris@0: strncat (data, filename, sizeof (data) - 1 - strlen (data)) ; Chris@0: Chris@0: assert (system (data) >= 0) ; Chris@0: puts ("") ; Chris@0: Chris@0: printf ("3) Now use fstat() to get the file length.\n") ; Chris@0: if (FSTAT (fd, &statbuf) != 0) Chris@0: { printf ("\n\nLine %d: fstat() returned error : %s\n", __LINE__, strerror (errno)) ; Chris@0: return ; Chris@0: } ; Chris@0: Chris@0: printf ("4) According to fstat(), the file length is %ld, ", (long) statbuf.st_size) ; Chris@0: Chris@0: close (fd) ; Chris@0: Chris@0: if (statbuf.st_size != 2 * sizeof (data)) Chris@0: printf ("but thats just plain ***WRONG***.\n\n") ; Chris@0: else Chris@0: { printf ("which is correct.\n\n") ; Chris@0: unlink (filename) ; Chris@0: } ; Chris@0: Chris@0: } /* show_fstat_error */ Chris@0: Chris@0: static void Chris@0: show_lseek_error (void) Chris@0: { static const char *filename = "fstat.dat" ; Chris@0: static char data [256] ; Chris@0: Chris@0: INT64 retval ; Chris@0: int fd, mode, flags ; Chris@0: Chris@0: puts ("\n64 bit lseek() test.\n--------------------") ; Chris@0: Chris@0: printf ("0) Create a file, write %d bytes and close it.\n", SIGNED_SIZEOF (data)) ; Chris@0: mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ; Chris@0: flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ; Chris@0: if ((fd = open (filename, mode, flags)) < 0) Chris@0: { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: return ; Chris@0: } ; Chris@0: assert (write (fd, data, sizeof (data)) > 0) ; Chris@0: close (fd) ; Chris@0: Chris@0: printf ("1) Re-open file in read/write mode and write another %d bytes at the end.\n", SIGNED_SIZEOF (data)) ; Chris@0: mode = O_RDWR | O_BINARY ; Chris@0: flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ; Chris@0: if ((fd = open (filename, mode, flags)) < 0) Chris@0: { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: return ; Chris@0: } ; Chris@0: Chris@0: LSEEK (fd, 0, SEEK_END) ; Chris@0: assert (write (fd, data, sizeof (data)) > 0) ; Chris@0: Chris@0: printf ("2) Now use system (\"%s %s\") to show the file length.\n\n", dir_cmd, filename) ; Chris@0: Chris@0: /* Would use snprintf, but thats not really available on windows. */ Chris@0: memset (data, 0, sizeof (data)) ; Chris@0: strncpy (data, dir_cmd, sizeof (data) - 1) ; Chris@0: strncat (data, " ", sizeof (data) - 1 - strlen (data)) ; Chris@0: strncat (data, filename, sizeof (data) - 1 - strlen (data)) ; Chris@0: Chris@0: assert (system (data) >= 0) ; Chris@0: puts ("") ; Chris@0: Chris@0: printf ("3) Now use lseek() to go to the end of the file.\n") ; Chris@0: retval = LSEEK (fd, 0, SEEK_END) ; Chris@0: Chris@0: printf ("4) We are now at position %ld, ", (long) retval) ; Chris@0: Chris@0: close (fd) ; Chris@0: Chris@0: if (retval != 2 * sizeof (data)) Chris@0: printf ("but thats just plain ***WRONG***.\n\n") ; Chris@0: else Chris@0: { printf ("which is correct.\n\n") ; Chris@0: unlink (filename) ; Chris@0: } ; Chris@0: Chris@0: } /* show_lseek_error */ Chris@0: Chris@0: static void Chris@0: show_stat_fstat_error (void) Chris@0: { static const char *filename = "stat_fstat.dat" ; Chris@0: static char data [256] ; Chris@0: Chris@0: int fd, mode, flags ; Chris@0: int stat_size, fstat_size ; Chris@0: struct stat buf ; Chris@0: Chris@0: /* Known to fail on WinXP. */ Chris@0: puts ("\nstat/fstat test.\n----------------") ; Chris@0: Chris@0: printf ("0) Create a file and write %d bytes.\n", SIGNED_SIZEOF (data)) ; Chris@0: Chris@0: mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ; Chris@0: flags = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ; Chris@0: if ((fd = open (filename, mode, flags)) < 0) Chris@0: { printf ("\n\nLine %d: open() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: return ; Chris@0: } ; Chris@0: Chris@0: assert (write (fd, data, sizeof (data)) > 0) ; Chris@0: Chris@0: printf ("1) Now call stat and fstat on the file and retreive the file lengths.\n") ; Chris@0: Chris@0: if (stat (filename, &buf) != 0) Chris@0: { printf ("\n\nLine %d: stat() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: goto error_exit ; Chris@0: } ; Chris@0: stat_size = buf.st_size ; Chris@0: Chris@0: if (fstat (fd, &buf) != 0) Chris@0: { printf ("\n\nLine %d: fstat() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: goto error_exit ; Chris@0: } ; Chris@0: fstat_size = buf.st_size ; Chris@0: Chris@0: printf ("2) Size returned by stat and fstat is %d and %d, ", stat_size, fstat_size) ; Chris@0: Chris@0: Chris@0: if (stat_size == 0 || stat_size != fstat_size) Chris@0: printf ("but thats just plain ***WRONG***.\n\n") ; Chris@0: else Chris@0: printf ("which is correct.\n\n") ; Chris@0: Chris@0: error_exit : Chris@0: Chris@0: close (fd) ; Chris@0: unlink (filename) ; Chris@0: Chris@0: return ; Chris@0: } /* show_stat_fstat_error */ Chris@0: Chris@0: Chris@0: static void Chris@0: write_to_closed_file (void) Chris@0: { const char * filename = "closed_write_test.txt" ; Chris@0: struct stat buf ; Chris@0: FILE * file ; Chris@0: int fd ; Chris@0: Chris@0: puts ("\nWrite to closed file test.\n--------------------------") ; Chris@0: Chris@0: printf ("0) First we open file for write using fopen().\n") ; Chris@0: if ((file = fopen (filename, "w")) == NULL) Chris@0: { printf ("\n\nLine %d: fopen() failed : %s\n\n", __LINE__, strerror (errno)) ; Chris@0: return ; Chris@0: } ; Chris@0: Chris@0: printf ("1) Now we grab the file descriptor fileno().\n") ; Chris@0: fd = fileno (file) ; Chris@0: Chris@0: printf ("2) Write some text via the file descriptor.\n") ; Chris@0: assert (write (fd, "a\n", 2) > 0) ; Chris@0: Chris@0: printf ("3) Now we close the file using fclose().\n") ; Chris@0: fclose (file) ; Chris@0: Chris@0: stat (filename, &buf) ; Chris@0: printf (" File size is %d bytes.\n", (int) buf.st_size) ; Chris@0: Chris@0: printf ("4) Now write more data to the file descriptor which should fail.\n") ; Chris@0: if (write (fd, "b\n", 2) < 0) Chris@0: printf ("5) Good, write returned an error code as it should have.\n") ; Chris@0: else Chris@0: { printf ("5) Attempting to write to a closed file should have failed but didn't! *** WRONG ***\n") ; Chris@0: Chris@0: stat (filename, &buf) ; Chris@0: printf (" File size is %d bytes.\n", (int) buf.st_size) ; Chris@0: } ; Chris@0: Chris@0: unlink (filename) ; Chris@0: Chris@0: return ; Chris@0: } /* write_to_closed_file */