1 #ifndef MTOOLS_MTOOLS_H
2 #define MTOOLS_MTOOLS_H
3 /* Copyright 1996-2005,2007-2011 Alain Knaff.
4 * This file is part of mtools.
5 *
6 * Mtools is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Mtools is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "msdos.h"
20 #include "llong.h"
21 typedef struct dos_name_t dos_name_t;
22
23 #if defined(OS_sco3)
24 #define MAXPATHLEN 1024
25 #include <signal.h>
26 extern int lockf(int, int, off_t); /* SCO has no proper include file for lockf */
27 #endif
28
29
30
31
32 #ifndef OS_linux
33 #define BOOTSIZE 512
34 #else
35 #define BOOTSIZE 256
36 #endif
37
38 typedef struct doscp_t doscp_t;
39
40 #include "stream.h"
41
42
43 extern const char *short_illegals, *long_illegals;
44
45 #define maximize(target, max) do { \
46 if(target > max) { \
47 target = max; \
48 } \
49 } while(0)
50
51 #define sizemaximize(target, max) do { \
52 if(max < 0) { \
53 if(target > 0) \
54 target = 0; \
55 } else if(target > (size_t) max) { \
56 target = (size_t) max; \
57 } \
58 } while(0)
59
60 #define minimize(target, min) do { \
61 if(target < min) \
62 target = min; \
63 } while(0)
64
65 #ifdef OS_linux
66 int get_sector_size(int fd);
67 #endif
68
69 int readwrite_sectors(int fd, /* file descriptor */
70 int *drive,
71 int rate,
72 int seektrack,
73 int track, int head, int sector, int size, /* address */
74 char *data,
75 int bytes,
76 int direction,
77 int retries);
78
79 char *unix_normalize (doscp_t *cp, char *ans, struct dos_name_t *dn,
80 size_t ans_size);
81 void dos_name(doscp_t *cp, const char *filename, int verbose, int *mangled,
82 struct dos_name_t *);
83 struct directory *mk_entry(const dos_name_t *filename, unsigned char attr,
84 unsigned int fat, uint32_t size, time_t date,
85 struct directory *ndir);
86
87 struct directory *mk_entry_from_base(const char *base, unsigned char attr,
88 unsigned int fat, uint32_t size, time_t date,
89 struct directory *ndir);
90
91 mt_off_t copyfile(Stream_t *Source, Stream_t *Target);
92 int getfreeMinClusters(Stream_t *Stream, uint32_t ref);
93
94 FILE *opentty(int mode);
95
96 int is_dir(Stream_t *Dir, char *path);
97
98 int dir_grow(Stream_t *Dir, int size);
99 int match(const wchar_t *, const wchar_t *, wchar_t *, int, int);
100
101 wchar_t *unix_name(doscp_t *fromDos,
102 const char *base, const char *ext, uint8_t Case,
103 wchar_t *answer);
104 void *safe_malloc(size_t size);
105 Stream_t *open_dos2unix(Stream_t *Next,int convertCharset);
106 Stream_t *open_unix2dos(Stream_t *Next,int convertCharset);
107
108 extern int got_signal;
109 /* int do_gotsignal(char *, int);
110 #define got_signal do_gotsignal(__FILE__, __LINE__) */
111
112 void setup_signal(void);
113 #ifdef HAVE_SIGACTION
114 typedef struct { struct sigaction sa[4]; } saved_sig_state;
115 #else
116 typedef int saved_sig_state;
117 #endif
118
119 void allow_interrupts(saved_sig_state *ss);
120 void restore_interrupts(saved_sig_state *ss);
121
122 #define SET_INT(target, source) \
123 if(source)target=source
124
125 #define compare(ref,testee) ((ref) && (ref) != (testee))
126
UNUSED(static __inline__ char ch_toupper (char ch))127 UNUSED(static __inline__ char ch_toupper(char ch))
128 {
129 return (char) toupper( (unsigned char) ch);
130 }
131
UNUSED(static __inline__ char ch_tolower (char ch))132 UNUSED(static __inline__ char ch_tolower(char ch))
133 {
134 return (char) tolower( (unsigned char) ch);
135 }
136
UNUSED(static __inline__ wchar_t ch_towupper (wchar_t ch))137 UNUSED(static __inline__ wchar_t ch_towupper(wchar_t ch))
138 {
139 return (wchar_t) towupper( (wint_t) ch);
140 }
141
UNUSED(static __inline__ wchar_t ch_towlower (wchar_t ch))142 UNUSED(static __inline__ wchar_t ch_towlower(wchar_t ch))
143 {
144 return (wchar_t) towlower( (wint_t) ch);
145 }
146
UNUSED(static __inline__ void init_random (void))147 UNUSED(static __inline__ void init_random(void))
148 {
149 srandom((unsigned int)time (0));
150 }
151
UNUSED(static __inline__ size_t ptrdiff (const char * end,const char * begin))152 UNUSED(static __inline__ size_t ptrdiff (const char *end, const char *begin))
153 {
154 return (size_t) (end-begin);
155 }
156
157 Stream_t *GetFs(Stream_t *Fs);
158
159 void label_name_uc(doscp_t *cp, const char *filename, int verbose,
160 int *mangled, dos_name_t *ans);
161
162 void label_name_pc(doscp_t *cp, const char *filename, int verbose,
163 int *mangled, dos_name_t *ans);
164
165 /* environmental variables */
166 extern unsigned int mtools_skip_check;
167 extern unsigned int mtools_fat_compatibility;
168 extern unsigned int mtools_ignore_short_case;
169 extern unsigned int mtools_no_vfat;
170 extern unsigned int mtools_numeric_tail;
171 extern unsigned int mtools_dotted_dir;
172 extern unsigned int mtools_lock_timeout;
173 extern unsigned int mtools_twenty_four_hour_clock;
174 extern const char *mtools_date_string;
175 extern uint8_t mtools_rate_0, mtools_rate_any;
176 extern unsigned int mtools_default_codepage;
177 extern int mtools_raw_tty;
178
179 extern int batchmode;
180
181 char get_default_drive(void);
182 void set_cmd_line_image(char *img);
183 void check_number_parse_errno(char c, const char *optarg, char *endptr);
184 void read_config(void);
185 off_t str_to_offset_with_end(const char *str, char **endp);
186 mt_off_t str_to_off_with_end(const char *str, char **endp);
187 off_t str_to_offset(char *str);
188 uint32_t parseSize(char *sizeStr);
189 unsigned int strtoui(const char *nptr, char **endptr, int base);
190 unsigned int atoui(const char *nptr);
191 #ifndef HAVE_STRTOI
192 int strtoi(const char *nptr, char **endptr, int base);
193 #endif
194 unsigned long atoul(const char *nptr);
195 uint8_t strtou8(const char *nptr, char **endptr, int base);
196 uint8_t atou8(const char *str);
197 uint16_t strtou16(const char *nptr, char **endptr, int base);
198 uint16_t atou16(const char *str);
199 uint32_t strtou32(const char *nptr, char **endptr, int base);
200 uint32_t atou32(const char *str);
201
202 #define New(type) ((type*)(calloc(1,sizeof(type))))
203 #define Grow(adr,n,type) ((type*)(realloc((char *)adr,n*sizeof(type))))
204 #define Free(adr) (free((char *)adr))
205 #define NewArray(size,type) ((type*)(calloc((size),sizeof(type))))
206
207 void mattrib(int argc, char **argv, int type);
208 void mbadblocks(int argc, char **argv, int type);
209 void mcat(int argc, char **argv, int type);
210 void mcd(int argc, char **argv, int type);
211 void mcopy(int argc, char **argv, int type);
212 void mdel(int argc, char **argv, int type);
213 void mdir(int argc, char **argv, int type);
214 void mdoctorfat(int argc, char **argv, int type);
215 void mdu(int argc, char **argv, int type);
216 void mformat(int argc, char **argv, int type);
217 void minfo(int argc, char **argv, int type);
218 void mlabel(int argc, char **argv, int type);
219 void mmd(int argc, char **argv, int type);
220 void mmount(int argc, char **argv, int type);
221 void mmove(int argc, char **argv, int type);
222 void mpartition(int argc, char **argv, int type);
223 void mshortname(int argc, char **argv, int mtype);
224 void mshowfat(int argc, char **argv, int mtype);
225 void mtoolstest(int argc, char **argv, int type);
226 void mzip(int argc, char **argv, int type);
227
228 extern int noPrivileges;
229 void init_privs(void);
230 void reclaim_privs(void);
231 void drop_privs(void);
232 void destroy_privs(void);
233 uid_t get_real_uid(void);
234 void closeExec(int fd);
235
236 extern const char *progname;
237
238 void print_sector(const char *message, unsigned char *data, int size);
239 time_t getTimeNow(time_t *now);
240
241 #ifdef USING_NEW_VOLD
242 char *getVoldName(struct device *dev, char *name);
243 #endif
244
245
246 Stream_t *OpenDir(const char *filename);
247 /* int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
248 int unix_loop(MainParam_t *mp, char *arg); */
249
250 struct dirCache_t **getDirCacheP(Stream_t *Stream);
251 int isRootDir(Stream_t *Stream);
252 unsigned int getStart(Stream_t *Dir, struct directory *dir);
253 unsigned int countBlocks(Stream_t *Dir, unsigned int block);
254 char getDrive(Stream_t *Stream);
255
256
257 void printOom(void);
258 int ask_confirmation(const char *, ...) __attribute__ ((format (printf, 1, 2)));
259
260 int helpFlag(int, char **);
261
262 char *get_homedir(void);
263 #define EXPAND_BUF 2048
264 const char *expand(const char *, char *);
265 FILE *open_mcwd(const char *mode);
266 void unlink_mcwd(void);
267
268 #ifndef OS_mingw32msvc
269 ssize_t safePopenOut(const char **command, char *output, size_t len);
270 #endif
271
272 #define ROUND_DOWN(value, grain) ((value) - (value) % (grain))
273 #define ROUND_UP(value, grain) ROUND_DOWN((value) + (grain)-1, (grain))
274
275 #ifndef O_BINARY
276 #define O_BINARY 0
277 #endif
278
279 #endif
280