for grabbag__file_are_same() on windows use strcmp() on filenames temporarily since...
[platform/upstream/flac.git] / src / share / grabbag / file.c
1 /* grabbag - Convenience lib for various routines common to several tools
2  * Copyright (C) 2002,2003,2004,2005,2006  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
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 */
27 #else
28 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
29 #include <utime.h> /* for utime() */
30 #endif
31 #if defined __CYGWIN__ || defined __EMX__
32 #include <io.h> /* for setmode(), O_BINARY */
33 #include <fcntl.h> /* for _O_BINARY */
34 #endif
35 #include <sys/stat.h> /* for stat(), maybe chmod() */
36 #if defined _WIN32 && !defined __CYGWIN__
37 #else
38 #include <unistd.h> /* for unlink() */
39 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h> /* for strrchr() */
43 #include "share/grabbag.h"
44
45
46 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
47 {
48         struct stat srcstat;
49         struct utimbuf srctime;
50
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);
56         }
57 }
58
59 off_t grabbag__file_get_filesize(const char *srcpath)
60 {
61         struct stat srcstat;
62
63         if(0 == stat(srcpath, &srcstat))
64                 return srcstat.st_size;
65         else
66                 return -1;
67 }
68
69 const char *grabbag__file_get_basename(const char *srcpath)
70 {
71         const char *p;
72
73         p = strrchr(srcpath, '/');
74         if(0 == p) {
75                 p = strrchr(srcpath, '\\');
76                 if(0 == p)
77                         return srcpath;
78         }
79         return ++p;
80 }
81
82 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
83 {
84         struct stat stats;
85
86         if(0 == stat(filename, &stats)) {
87 #if !defined _MSC_VER && !defined __MINGW32__
88                 if(read_only) {
89                         stats.st_mode &= ~S_IWUSR;
90                         stats.st_mode &= ~S_IWGRP;
91                         stats.st_mode &= ~S_IWOTH;
92                 }
93                 else {
94                         stats.st_mode |= S_IWUSR;
95                 }
96 #else
97                 if(read_only)
98                         stats.st_mode &= ~S_IWRITE;
99                 else
100                         stats.st_mode |= S_IWRITE;
101 #endif
102                 if(0 != chmod(filename, stats.st_mode))
103                         return false;
104         }
105         else
106                 return false;
107
108         return true;
109 }
110
111 FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
112 {
113         struct stat s1, s2;
114 #if defined _MSC_VER || defined __MINGW32__
115         return f1 && f2 && 0 == strcmp(s1, s2); /*@@@@@@ need better method than strcmp */
116 #else
117         return f1 && f2 && stat(f1, &s1) == 0 && stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino;
118 #endif
119 }
120
121 FLAC__bool grabbag__file_remove_file(const char *filename)
122 {
123         return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
124 }
125
126 FILE *grabbag__file_get_binary_stdin()
127 {
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.
131          */
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);
139 #endif
140
141         return stdin;
142 }
143
144 FILE *grabbag__file_get_binary_stdout()
145 {
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.
149          */
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);
157 #endif
158
159         return stdout;
160 }