add support for building on os/2 with emx
[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  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 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 */
23 #else
24 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
25 #include <utime.h> /* for utime() */
26 #endif
27 #if defined __CYGWIN__ || defined __EMX__
28 #include <io.h> /* for setmode(), O_BINARY */
29 #include <fcntl.h> /* for _O_BINARY */
30 #endif
31 #include <sys/stat.h> /* for stat(), maybe chmod() */
32 #if defined _WIN32 && !defined __CYGWIN__
33 #else
34 #include <unistd.h> /* for unlink() */
35 #endif
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h> /* for strrchr() */
39 #include "share/grabbag.h"
40
41
42 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
43 {
44         struct stat srcstat;
45         struct utimbuf srctime;
46
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);
52         }
53 }
54
55 off_t grabbag__file_get_filesize(const char *srcpath)
56 {
57         struct stat srcstat;
58
59         if(0 == stat(srcpath, &srcstat))
60                 return srcstat.st_size;
61         else
62                 return -1;
63 }
64
65 const char *grabbag__file_get_basename(const char *srcpath)
66 {
67         const char *p;
68
69         p = strrchr(srcpath, '/');
70         if(0 == p) {
71                 p = strrchr(srcpath, '\\');
72                 if(0 == p)
73                         return srcpath;
74         }
75         return ++p;
76 }
77
78 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
79 {
80         struct stat stats;
81
82         if(0 == stat(filename, &stats)) {
83 #if !defined _MSC_VER && !defined __MINGW32__
84                 if(read_only) {
85                         stats.st_mode &= ~S_IWUSR;
86                         stats.st_mode &= ~S_IWGRP;
87                         stats.st_mode &= ~S_IWOTH;
88                 }
89                 else {
90                         stats.st_mode |= S_IWUSR;
91                 }
92 #else
93                 if(read_only)
94                         stats.st_mode &= ~S_IWRITE;
95                 else
96                         stats.st_mode |= S_IWRITE;
97 #endif
98                 if(0 != chmod(filename, stats.st_mode))
99                         return false;
100         }
101         else
102                 return false;
103
104         return true;
105 }
106
107 FLAC__bool grabbag__file_remove_file(const char *filename)
108 {
109         return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
110 }
111
112 FILE *grabbag__file_get_binary_stdin()
113 {
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.
117          */
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);
123 #elif defined __EMX__
124         setmode(fileno(stdin), O_BINARY);
125 #endif
126
127         return stdin;
128 }
129
130 FILE *grabbag__file_get_binary_stdout()
131 {
132         /* if something breaks here it is probably due to the presence or
133          * absence of an underscore before the identifiers 'setmode',
134          * 'fileno', and/or 'O_BINARY'; check your system header files.
135          */
136 #if defined _MSC_VER || defined __MINGW32__
137         _setmode(_fileno(stdout), _O_BINARY);
138 #elif defined __CYGWIN__
139         /* almost certainly not needed for any modern Cygwin, but let's be safe... */
140         setmode(_fileno(stdout), _O_BINARY);
141 #elif defined __EMX__
142         setmode(fileno(stdout), O_BINARY);
143 #endif
144
145         return stdout;
146 }