Imported Upstream version 6.0.5
[platform/upstream/dos2unix.git] / common.h
1 /*
2  *   Copyright (C) 2009-2012 Erwin Waterlander
3  *   All rights reserved.
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
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
12  *      the distribution.
13  *
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.
25  */
26
27 /* define feature test macros for realpath() -- needed on    */
28 /* systems that have S_ISLNK, but chicken/egg means we must  */
29 /* define early, before including stdlib.h (or sys/stat.h)   */
30 #define _XOPEN_SOURCE 500
31
32 /* similarly, instead of realpath we like to use, if         */
33 /* available, the canonicalize_file_name() function, which   */
34 /* is a GNU extension. We only ACTUALLY use the function if  */
35 /* USE_CANONICALIZE_FILE_NAME is defined, but we don't define*/
36 /* that until later. So...define the feature test macro now. */
37 #define _GNU_SOURCE
38
39 #ifdef ENABLE_NLS
40
41 #include <libintl.h>
42 #define _(String) gettext (String)
43 #define gettext_noop(String) String
44 #define N_(String) gettext_noop (String)
45
46 #else
47
48 #define _(String) (String)
49 #define N_(String) String
50 #define textdomain(Domain)
51 #define bindtextdomain(Package, Directory)
52
53 #endif
54
55 #if defined(__DJGPP__) || defined(__TURBOC__) /* DJGPP */
56 #  include <dir.h>
57 #else
58 #  if !defined(__MSYS__) && !defined(_MSC_VER)
59 #    include <libgen.h>
60 #  endif
61 #endif
62 #if !defined(__TURBOC__) && !defined(_MSC_VER)
63 #include <unistd.h>
64 #endif
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #ifdef __GNUC__
69 #ifndef strcmpi
70 #  include <strings.h>
71 #  define strcmpi(s1, s2) strcasecmp(s1, s2)
72 #endif
73 #endif
74 #ifdef _MSC_VER
75 #  include <sys/utime.h>
76 #else
77 #  include <utime.h>
78 #endif
79 #include <limits.h>
80 #ifdef __TURBOC__
81 #define __FLAT__
82 #endif
83 #include <sys/stat.h>
84 #include <errno.h>
85 #ifdef D2U_UNICODE
86 #include <wchar.h>
87 #endif
88
89 #if (defined(__WATCOMC__) && defined(__NT__))  /* Watcom */
90 #  define _WIN32 1
91 #endif
92
93 #if defined(__WATCOMC__) && defined(__I86__) /* Watcom C, 16 bit Intel */
94 #define __MSDOS__ 1
95 #endif
96
97 #if defined(__WATCOMC__) && defined(__DOS__) /* Watcom C, 32 bit DOS */
98 #define __MSDOS__ 1
99 #endif
100
101 #if defined(ENABLE_NLS) || (defined(D2U_UNICODE) && !defined(__MSDOS__) && !defined(_WIN32) && !defined(__OS2__))
102 /* setlocale() is also needed for nl_langinfo() */
103 #include <locale.h>
104 #endif
105
106 #if  defined(__TURBOC__) || defined(__DJGPP__) || defined(__MINGW32__) || defined(_MSC_VER)
107 /* Some compilers have no mkstemp().
108  * Use mktemp() instead.
109  * BORLANDC, DJGPP, MINGW32 */
110 #define NO_MKSTEMP 1
111 #endif
112
113 #if  defined(__TURBOC__) || defined(__DJGPP__) || defined(__MINGW32__) || defined(__WATCOMC__) || defined(_MSC_VER)
114 /* Some compilers have no chown(). */
115 #define NO_CHOWN 1
116 #endif
117
118 /* Watcom C defines S_ISLNK */
119 #ifdef __WATCOMC__
120 #undef S_ISLNK
121 #endif
122
123 /* Microsoft Visual C++ */
124 #ifdef _MSC_VER
125 #define S_ISCHR( m )    (((m) & _S_IFMT) == _S_IFCHR)
126 #define S_ISDIR( m )    (((m) & _S_IFMT) == _S_IFDIR)
127 #define S_ISFIFO( m )   (((m) & _S_IFMT) == _S_IFIFO)
128 #define S_ISREG( m )    (((m) & _S_IFMT) == _S_IFREG)
129 #define NO_CHMOD 1  /* no chmod() available */
130 #endif
131
132 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__)
133 /* Systems without soft links use 'stat' instead of 'lstat'. */
134 #define STAT stat
135 #else
136 #define STAT lstat
137 #endif
138
139 #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__)
140 /* On some systems rename() will always fail if target file already exists. */
141 #define NEED_REMOVE 1
142 #endif
143
144 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__) /* DJGPP, MINGW32 and OS/2 */
145 /* required for setmode() and O_BINARY */
146 #include <fcntl.h>
147 #include <io.h>
148 #endif
149
150 #if defined(__MSDOS__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__)
151   #define R_CNTRL   "rb"
152   #define W_CNTRL   "wb"
153 #else
154   #define R_CNTRL   "r"
155   #define W_CNTRL   "w"
156 #endif
157
158 #define BINARY_FILE 0x1
159 #define NO_REGFILE  0x2
160 #define WRONG_CODEPAGE  0x4
161 #define OUTPUTFILE_SYMLINK 0x8
162 #define INPUT_TARGET_NO_REGFILE 0x10
163 #define OUTPUT_TARGET_NO_REGFILE 0x20
164 #define LOCALE_NOT_UTF8 0x40
165 #define WCHAR_T_TOO_SMALL 0x80
166 #define UNICODE_CONVERSION_ERROR 0x100
167
168 #define CONVMODE_ASCII   0
169 #define CONVMODE_UTF16LE 1
170 #define CONVMODE_UTF16BE 2
171 #define CONVMODE_7BIT    3
172 #define CONVMODE_437     437
173 #define CONVMODE_850     850
174 #define CONVMODE_860     860
175 #define CONVMODE_863     863
176 #define CONVMODE_865     865
177 #define CONVMODE_1252    1252
178
179 #define FROMTO_DOS2UNIX 0
180 #define FROMTO_MAC2UNIX 1
181 #define FROMTO_UNIX2DOS 0
182 #define FROMTO_UNIX2MAC 1
183
184 #define SYMLINK_SKIP 0
185 #define SYMLINK_FOLLOW 1
186 #define SYMLINK_REPLACE 2
187
188 #define FILE_MBS     0  /* Multi-byte string or 8-bit char */
189 #define FILE_UTF16LE 1  /* UTF-16 Little Endian */
190 #define FILE_UTF16BE 2  /* UTF-16 Big Endian */
191 #define FILE_UTF8    3  /* UTF-8 */
192
193 typedef struct
194 {
195   int NewFile;                          /* is in new file mode? */
196   int Quiet;                            /* is in quiet mode? */
197   int KeepDate;                         /* should keep date stamp? */
198   int ConvMode;                         /* 0: ascii, 1: 7bit, 2: iso */
199   int FromToMode;                       /* 0: dos2unix/unix2dos, 1: mac2unix/unix2mac */
200   int NewLine;                          /* if TRUE, then additional newline */
201   int Force;                            /* if TRUE, force conversion of all files. */
202   int Follow;                           /* 0: skip symlink, 1: follow symbolic link, 2: replace symlink. */
203   int status;
204   int stdio_mode;                       /* if TRUE, stdio mode */
205   int error;                            /* an error occurred */
206   int bomtype;                          /* byte order mark */
207   int add_bom;                          /* 1: write BOM */
208 } CFlag;
209
210
211 int symbolic_link(const char *path);
212 int regfile(char *path, int allowSymlinks, CFlag *ipFlag, char *progname);
213 int regfile_target(char *path, CFlag *ipFlag, char *progname);
214 void PrintUsage(char *progname);
215 void PrintBSDLicense(void);
216 void PrintVersion(char *progname);
217 #ifdef ENABLE_NLS
218 void PrintLocaledir(char *localedir);
219 #endif
220 FILE* OpenInFile(char *ipFN);
221 FILE* OpenOutFile(int fd);
222 #if defined(__TURBOC__) || defined(__MSYS__)
223 char *dirname(char *path);
224 #endif
225 #ifdef NO_MKSTEMP
226 FILE* MakeTempFileFrom(const char *OutFN, char **fname_ret);
227 #else
228 int MakeTempFileFrom(const char *OutFN, char **fname_ret);
229 #endif
230 int ResolveSymbolicLink(char *lFN, char **rFN, CFlag *ipFlag, char *progname);
231 FILE *read_bom (FILE *f, int *bomtype);
232 #ifdef D2U_UNICODE
233 wint_t d2u_getwc(FILE *f, int bomtype);
234 wint_t d2u_ungetwc(wint_t wc, FILE *f, int bomtype);
235 wint_t d2u_putwc(wint_t wc, FILE *f, CFlag *ipFlag);
236 #endif
237