1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002,2003,2004,2005,2006 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #if defined _MSC_VER || defined __MINGW32__
24 #include <sys/utime.h> /* for utime() */
25 #include <io.h> /* for chmod(), _setmode(), unlink() */
26 #include <fcntl.h> /* for _O_BINARY */
28 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
29 #include <utime.h> /* for utime() */
31 #if defined __CYGWIN__ || defined __EMX__
32 #include <io.h> /* for setmode(), O_BINARY */
33 #include <fcntl.h> /* for _O_BINARY */
35 #include <sys/stat.h> /* for stat(), maybe chmod() */
36 #if defined _WIN32 && !defined __CYGWIN__
38 #include <unistd.h> /* for unlink() */
42 #include <string.h> /* for strrchr() */
43 #include "share/grabbag.h"
46 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
49 struct utimbuf srctime;
51 if(0 == stat(srcpath, &srcstat)) {
52 srctime.actime = srcstat.st_atime;
53 srctime.modtime = srcstat.st_mtime;
54 (void)chmod(destpath, srcstat.st_mode);
55 (void)utime(destpath, &srctime);
59 off_t grabbag__file_get_filesize(const char *srcpath)
63 if(0 == stat(srcpath, &srcstat))
64 return srcstat.st_size;
69 const char *grabbag__file_get_basename(const char *srcpath)
73 p = strrchr(srcpath, '/');
75 p = strrchr(srcpath, '\\');
82 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
86 if(0 == stat(filename, &stats)) {
87 #if !defined _MSC_VER && !defined __MINGW32__
89 stats.st_mode &= ~S_IWUSR;
90 stats.st_mode &= ~S_IWGRP;
91 stats.st_mode &= ~S_IWOTH;
94 stats.st_mode |= S_IWUSR;
98 stats.st_mode &= ~S_IWRITE;
100 stats.st_mode |= S_IWRITE;
102 if(0 != chmod(filename, stats.st_mode))
111 FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
113 #if defined _MSC_VER || defined __MINGW32__
114 return f1 && f2 && 0 == strcmp(f1, f2); /*@@@@@@ need better method than strcmp */
117 return f1 && f2 && stat(f1, &s1) == 0 && stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino;
121 FLAC__bool grabbag__file_remove_file(const char *filename)
123 return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
126 FILE *grabbag__file_get_binary_stdin()
128 /* if something breaks here it is probably due to the presence or
129 * absence of an underscore before the identifiers 'setmode',
130 * 'fileno', and/or 'O_BINARY'; check your system header files.
132 #if defined _MSC_VER || defined __MINGW32__
133 _setmode(_fileno(stdin), _O_BINARY);
134 #elif defined __CYGWIN__
135 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
136 setmode(_fileno(stdin), _O_BINARY);
137 #elif defined __EMX__
138 setmode(fileno(stdin), O_BINARY);
144 FILE *grabbag__file_get_binary_stdout()
146 /* if something breaks here it is probably due to the presence or
147 * absence of an underscore before the identifiers 'setmode',
148 * 'fileno', and/or 'O_BINARY'; check your system header files.
150 #if defined _MSC_VER || defined __MINGW32__
151 _setmode(_fileno(stdout), _O_BINARY);
152 #elif defined __CYGWIN__
153 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
154 setmode(_fileno(stdout), _O_BINARY);
155 #elif defined __EMX__
156 setmode(fileno(stdout), O_BINARY);