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