1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002,2003,2004,2005 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.
19 #if defined _MSC_VER || defined __MINGW32__
20 #include <sys/utime.h> /* for utime() */
21 #include <io.h> /* for chmod(), _setmode(), unlink() */
22 #include <fcntl.h> /* for _O_BINARY */
24 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
25 #include <utime.h> /* for utime() */
28 #include <io.h> /* for setmode(), O_BINARY */
29 #include <fcntl.h> /* for _O_BINARY */
31 #include <sys/stat.h> /* for stat(), maybe chmod() */
32 #if defined _WIN32 && !defined __CYGWIN__
34 #include <unistd.h> /* for unlink() */
38 #include <string.h> /* for strrchr() */
39 #include "share/grabbag.h"
42 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
45 struct utimbuf srctime;
47 if(0 == stat(srcpath, &srcstat)) {
48 srctime.actime = srcstat.st_atime;
49 srctime.modtime = srcstat.st_mtime;
50 (void)chmod(destpath, srcstat.st_mode);
51 (void)utime(destpath, &srctime);
55 off_t grabbag__file_get_filesize(const char *srcpath)
59 if(0 == stat(srcpath, &srcstat))
60 return srcstat.st_size;
65 const char *grabbag__file_get_basename(const char *srcpath)
69 p = strrchr(srcpath, '/');
71 p = strrchr(srcpath, '\\');
78 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
82 if(0 == stat(filename, &stats)) {
83 #if !defined _MSC_VER && !defined __MINGW32__
85 stats.st_mode &= ~S_IWUSR;
86 stats.st_mode &= ~S_IWGRP;
87 stats.st_mode &= ~S_IWOTH;
90 stats.st_mode |= S_IWUSR;
94 stats.st_mode &= ~S_IWRITE;
96 stats.st_mode |= S_IWRITE;
98 if(0 != chmod(filename, stats.st_mode))
107 FLAC__bool grabbag__file_remove_file(const char *filename)
109 return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
112 FILE *grabbag__file_get_binary_stdin()
114 /* if something breaks here it is probably due to the presence or
115 * absence of an underscore before the identifiers 'setmode',
116 * 'fileno', and/or 'O_BINARY'; check your system header files.
118 #if defined _MSC_VER || defined __MINGW32__
119 _setmode(_fileno(stdin), _O_BINARY);
120 #elif defined __CYGWIN__
121 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
122 setmode(_fileno(stdin), _O_BINARY);
128 FILE *grabbag__file_get_binary_stdout()
130 /* if something breaks here it is probably due to the presence or
131 * absence of an underscore before the identifiers 'setmode',
132 * 'fileno', and/or 'O_BINARY'; check your system header files.
134 #if defined _MSC_VER || defined __MINGW32__
135 _setmode(_fileno(stdout), _O_BINARY);
136 #elif defined __CYGWIN__
137 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
138 setmode(_fileno(stdout), _O_BINARY);