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