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_remove_file(const char *filename)
113 return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
116 FILE *grabbag__file_get_binary_stdin()
118 /* if something breaks here it is probably due to the presence or
119 * absence of an underscore before the identifiers 'setmode',
120 * 'fileno', and/or 'O_BINARY'; check your system header files.
122 #if defined _MSC_VER || defined __MINGW32__
123 _setmode(_fileno(stdin), _O_BINARY);
124 #elif defined __CYGWIN__
125 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
126 setmode(_fileno(stdin), _O_BINARY);
127 #elif defined __EMX__
128 setmode(fileno(stdin), O_BINARY);
134 FILE *grabbag__file_get_binary_stdout()
136 /* if something breaks here it is probably due to the presence or
137 * absence of an underscore before the identifiers 'setmode',
138 * 'fileno', and/or 'O_BINARY'; check your system header files.
140 #if defined _MSC_VER || defined __MINGW32__
141 _setmode(_fileno(stdout), _O_BINARY);
142 #elif defined __CYGWIN__
143 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
144 setmode(_fileno(stdout), _O_BINARY);
145 #elif defined __EMX__
146 setmode(fileno(stdout), O_BINARY);