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