2 * Copyright (C) 2009-2014 Erwin Waterlander
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice in the documentation and/or other materials provided with
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef __D2U_COMMON_H
28 #define __D2U_COMMON_H
30 /* define feature test macros for realpath() -- needed on */
31 /* systems that have S_ISLNK, but chicken/egg means we must */
32 /* define early, before including stdlib.h (or sys/stat.h) */
33 #define _XOPEN_SOURCE 500
35 /* similarly, instead of realpath we like to use, if */
36 /* available, the canonicalize_file_name() function, which */
37 /* is a GNU extension. We only ACTUALLY use the function if */
38 /* USE_CANONICALIZE_FILE_NAME is defined, but we don't define*/
39 /* that until later. So...define the feature test macro now. */
45 #define _(String) gettext (String)
46 #define gettext_noop(String) String
47 #define N_(String) gettext_noop (String)
51 #define _(String) (String)
52 #define N_(String) String
53 #define textdomain(Domain)
54 #define bindtextdomain(Package, Directory)
58 #if defined(__DJGPP__) || defined(__TURBOC__) /* DJGPP */
61 # if !defined(__MSYS__) && !defined(_MSC_VER)
65 #if !defined(__TURBOC__) && !defined(_MSC_VER)
74 # define strcmpi(s1, s2) strcasecmp(s1, s2)
78 # include <sys/utime.h>
92 #if (defined(__WATCOMC__) && defined(__NT__)) /* Watcom */
96 #if defined(__WATCOMC__) && defined(__I86__) /* Watcom C, 16 bit Intel */
100 #if defined(__WATCOMC__) && defined(__DOS__) /* Watcom C, 32 bit DOS */
104 #if defined(ENABLE_NLS) || (defined(D2U_UNICODE) && !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__))
105 /* setlocale() is also needed for nl_langinfo() */
109 #if defined(__TURBOC__) || defined(__DJGPP__) || defined(__MINGW32__) || defined(_MSC_VER)
110 /* Some compilers have no mkstemp().
111 * Use mktemp() instead.
112 * BORLANDC, DJGPP, MINGW32 */
116 #if defined(__TURBOC__) || defined(__DJGPP__) || defined(__MINGW32__) || defined(__WATCOMC__) || defined(_MSC_VER)
117 /* Some compilers have no chown(). */
121 /* Watcom C defines S_ISLNK */
126 /* Microsoft Visual C++ */
128 #define S_ISCHR( m ) (((m) & _S_IFMT) == _S_IFCHR)
129 #define S_ISDIR( m ) (((m) & _S_IFMT) == _S_IFDIR)
130 #define S_ISFIFO( m ) (((m) & _S_IFMT) == _S_IFIFO)
131 #define S_ISREG( m ) (((m) & _S_IFMT) == _S_IFREG)
132 #define NO_CHMOD 1 /* no chmod() available */
135 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__)
136 /* Systems without soft links use 'stat' instead of 'lstat'. */
142 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__)
143 /* On some systems rename() will always fail if target file already exists. */
144 #define NEED_REMOVE 1
147 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__) /* DJGPP, MINGW32 and OS/2 */
148 /* required for setmode() and O_BINARY */
153 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__)
161 #define BINARY_FILE 0x1
162 #define NO_REGFILE 0x2
163 #define WRONG_CODEPAGE 0x4
164 #define OUTPUTFILE_SYMLINK 0x8
165 #define INPUT_TARGET_NO_REGFILE 0x10
166 #define OUTPUT_TARGET_NO_REGFILE 0x20
167 #define LOCALE_NOT_UTF8 0x40
168 #define WCHAR_T_TOO_SMALL 0x80
169 #define UNICODE_CONVERSION_ERROR 0x100
171 #define CONVMODE_ASCII 0
172 #define CONVMODE_UTF16LE 1
173 #define CONVMODE_UTF16BE 2
174 #define CONVMODE_7BIT 3
175 #define CONVMODE_437 437
176 #define CONVMODE_850 850
177 #define CONVMODE_860 860
178 #define CONVMODE_863 863
179 #define CONVMODE_865 865
180 #define CONVMODE_1252 1252
182 #define FROMTO_DOS2UNIX 0
183 #define FROMTO_MAC2UNIX 1
184 #define FROMTO_UNIX2DOS 0
185 #define FROMTO_UNIX2MAC 1
187 #define SYMLINK_SKIP 0
188 #define SYMLINK_FOLLOW 1
189 #define SYMLINK_REPLACE 2
191 #define FILE_MBS 0 /* Multi-byte string or 8-bit char */
192 #define FILE_UTF16LE 1 /* UTF-16 Little Endian */
193 #define FILE_UTF16BE 2 /* UTF-16 Big Endian */
194 #define FILE_UTF8 3 /* UTF-8 */
198 int NewFile; /* is in new file mode? */
199 int verbose; /* 0 = quiet, 1 = normal, 2 = verbose */
200 int KeepDate; /* should keep date stamp? */
201 int ConvMode; /* 0: ascii, 1: 7bit, 2: iso */
202 int FromToMode; /* 0: dos2unix/unix2dos, 1: mac2unix/unix2mac */
203 int NewLine; /* if TRUE, then additional newline */
204 int Force; /* if TRUE, force conversion of all files. */
205 int Follow; /* 0: skip symlink, 1: follow symbolic link, 2: replace symlink. */
207 int stdio_mode; /* if TRUE, stdio mode */
208 int error; /* an error occurred */
209 int bomtype; /* byte order mark */
210 int add_bom; /* 1: write BOM */
211 int keep_bom; /* 1: write BOM if input file has BOM. 0: Do not write BOM */
212 int keep_utf16; /* 1: write UTF-16 format when input file is UTF-16 format */
216 int symbolic_link(const char *path);
217 int regfile(char *path, int allowSymlinks, CFlag *ipFlag, const char *progname);
218 int regfile_target(char *path, CFlag *ipFlag, const char *progname);
219 void PrintUsage(const char *progname);
220 void PrintBSDLicense(void);
221 void PrintVersion(const char *progname, const char *localedir);
223 void PrintLocaledir(const char *localedir);
225 FILE* OpenInFile(char *ipFN);
226 FILE* OpenOutFile(int fd);
227 #if defined(__TURBOC__) || defined(__MSYS__)
228 char *dirname(char *path);
231 FILE* MakeTempFileFrom(const char *OutFN, char **fname_ret);
233 int MakeTempFileFrom(const char *OutFN, char **fname_ret);
235 int ResolveSymbolicLink(char *lFN, char **rFN, CFlag *ipFlag, const char *progname);
236 FILE *read_bom (FILE *f, int *bomtype);
237 FILE *write_bom (FILE *f, CFlag *ipFlag, const char *progname);
238 void print_bom (const int bomtype, const char *filename, const char *progname);
239 int check_unicode(FILE *InF, FILE *TempF, CFlag *ipFlag, const char *ipInFN, const char *progname);
240 void print_messages_stdio(const CFlag *pFlag, const char *progname);
241 void print_messages_newfile(const CFlag *pFlag, const char *infile, const char *outfile, const char *progname, const int RetVal);
242 void print_messages_oldfile(const CFlag *pFlag, const char *infile, const char *progname, const int RetVal);
243 int ConvertNewFile(char *ipInFN, char *ipOutFN, CFlag *ipFlag, const char *progname,
244 int (*Convert)(FILE*, FILE*, CFlag *, const char *)
246 , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
249 int ConvertStdio(CFlag *ipFlag, const char *progname,
250 int (*Convert)(FILE*, FILE*, CFlag *, const char *)
252 , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
255 int parse_options(int argc, char *argv[], CFlag *pFlag, const char *localedir, const char *progname,
256 void (*PrintLicense)(void),
257 int (*Convert)(FILE*, FILE*, CFlag *, const char *)
259 , int (*ConvertW)(FILE*, FILE*, CFlag *, const char *)
263 wint_t d2u_getwc(FILE *f, int bomtype);
264 wint_t d2u_ungetwc(wint_t wc, FILE *f, int bomtype);
265 wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag);