/* test_libFLAC - Unit tester for libFLAC
- * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson
+ * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if HAVE_CONFIG_H
#if defined _MSC_VER || defined __MINGW32__
#include <sys/utime.h> /* for utime() */
#include <io.h> /* for chmod() */
-#if _MSC_VER <= 1600 /* @@@ [2G limit] */
-#define fseeko fseek
-#define ftello ftell
-#endif
#else
#include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
#include <utime.h> /* for utime() */
#include "FLAC/stream_decoder.h"
#include "FLAC/metadata.h"
#include "share/grabbag.h"
+#include "share/compat.h"
+#include "share/macros.h"
+#include "share/safe_str.h"
#include "test_libs_common/file_utils_flac.h"
#include "test_libs_common/metadata_utils.h"
#include "metadata.h"
static FLAC__bool open_tempfile_(const char *filename, FILE **tempfile, char **tempfilename)
{
static const char *tempfile_suffix = ".metadata_edit";
-
- if(0 == (*tempfilename = (char*)malloc(strlen(filename) + strlen(tempfile_suffix) + 1)))
+ size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1;
+ if(0 == (*tempfilename = malloc(dest_len)))
return false;
- strcpy(*tempfilename, filename);
- strcat(*tempfilename, tempfile_suffix);
+ safe_strncpy(*tempfilename, filename, dest_len);
+ safe_strncat(*tempfilename, tempfile_suffix, dest_len);
- if(0 == (*tempfile = fopen(*tempfilename, "wb")))
+ if(0 == (*tempfile = flac_fopen(*tempfilename, "wb")))
return false;
return true;
}
if(0 != *tempfilename) {
- (void)unlink(*tempfilename);
+ (void)flac_unlink(*tempfilename);
free(*tempfilename);
*tempfilename = 0;
}
}
#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
- /* on some flavors of windows, rename() will fail if the destination already exists */
- if(unlink(filename) < 0) {
+ /* on some flavors of windows, flac_rename() will fail if the destination already exists */
+ if(flac_unlink(filename) < 0) {
cleanup_tempfile_(tempfile, tempfilename);
return false;
}
#endif
- if(0 != rename(*tempfilename, filename)) {
+ if(0 != flac_rename(*tempfilename, filename)) {
cleanup_tempfile_(tempfile, tempfilename);
return false;
}
return true;
}
-static FLAC__bool get_file_stats_(const char *filename, struct stat *stats)
+static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != stats);
- return (0 == stat(filename, stats));
+ return (0 == flac_stat(filename, stats));
}
-static void set_file_stats_(const char *filename, struct stat *stats)
+static void set_file_stats_(const char *filename, struct flac_stat_s *stats)
{
struct utimbuf srctime;
srctime.actime = stats->st_atime;
srctime.modtime = stats->st_mtime;
- (void)chmod(filename, stats->st_mode);
- (void)utime(filename, &srctime);
-#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
- (void)chown(filename, stats->st_uid, -1);
- (void)chown(filename, -1, stats->st_gid);
+ (void)flac_chmod(filename, stats->st_mode);
+ (void)flac_utime(filename, &srctime);
+#if !defined _MSC_VER && !defined __MINGW32__
+ FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1));
+ FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid));
#endif
}
static int chain_seek_cb_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
{
- off_t o = (off_t)offset;
+ FLAC__off_t o = (FLAC__off_t)offset;
FLAC__ASSERT(offset == o);
return fseeko((FILE*)handle, o, whence);
}
callbacks.eof = chain_eof_cb_;
if(FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
- struct stat stats;
- FILE *file, *tempfile;
+ struct flac_stat_s stats;
+ FILE *file, *tempfile = 0;
char *tempfilename;
if(preserve_file_stats) {
if(!get_file_stats_(filename, &stats))
return false;
}
- if(0 == (file = fopen(filename, "rb")))
+ if(0 == (file = flac_fopen(filename, "rb")))
return false; /*@@@@ chain status still says OK though */
if(!open_tempfile_(filename, &tempfile, &tempfilename)) {
fclose(file);
set_file_stats_(filename, &stats);
}
else {
- FILE *file = fopen(filename, "r+b");
+ FILE *file = flac_fopen(filename, "r+b");
if(0 == file)
return false; /*@@@@ chain status still says OK though */
if(!FLAC__metadata_chain_write_with_callbacks(chain, use_padding, (FLAC__IOHandle)file, callbacks))
{
FLAC__bool ret;
- FILE *file = fopen(filename, "rb");
+ FILE *file = flac_fopen(filename, "rb");
if(0 == file)
return false; /*@@@@ chain status still says OK though */
ret = is_ogg?
if (0 == (cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET)))
return die_("priming our metadata");
cuesheet->is_last = false;
- strcpy(cuesheet->data.cue_sheet.media_catalog_number, "bogo-MCN");
+ safe_strncpy(cuesheet->data.cue_sheet.media_catalog_number, "bogo-MCN", sizeof(cuesheet->data.cue_sheet.media_catalog_number));
cuesheet->data.cue_sheet.lead_in = 123;
cuesheet->data.cue_sheet.is_cd = false;
if (!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, 0))
printf("read chain (callback-based)\n");
{
- FILE *file = fopen(flacfilename(is_ogg), "rb");
+ FILE *file = flac_fopen(flacfilename(is_ogg), "rb");
if(0 == file)
return die_("opening file");
if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) {
printf("read chain (callback-based)\n");
{
- FILE *file = fopen(flacfilename(is_ogg), "rb");
+ FILE *file = flac_fopen(flacfilename(is_ogg), "rb");
if(0 == file)
return die_("opening file");
if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) {
printf("read chain (callback-based)\n");
{
- FILE *file = fopen(flacfilename(is_ogg), "rb");
+ FILE *file = flac_fopen(flacfilename(is_ogg), "rb");
if(0 == file)
return die_("opening file");
if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) {