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
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 #define SCSI_FLAG 0x001u
30 #define PRIV_FLAG 0x002u
31 #define NOLOCK_FLAG 0x004u
32 #define USE_XDF_FLAG 0x008u
33 #define MFORMAT_ONLY_FLAG 0x010u
34 #define VOLD_FLAG 0x020u
35 #define FLOPPYD_FLAG 0x040u
36 #define FILTER_FLAG 0x080u
37 #define SWAP_FLAG 0x100u
38
39 #define IS_SCSI(x) ((x) && ((x)->misc_flags & SCSI_FLAG))
40 #define IS_PRIVILEGED(x) ((x) && ((x)->misc_flags & PRIV_FLAG))
41 #define IS_NOLOCK(x) ((x) && ((x)->misc_flags & NOLOCK_FLAG))
42 #define IS_MFORMAT_ONLY(x) ((x) && ((x)->misc_flags & MFORMAT_ONLY_FLAG))
43 #define SHOULD_USE_VOLD(x) ((x)&& ((x)->misc_flags & VOLD_FLAG))
44 #define SHOULD_USE_XDF(x) ((x)&& ((x)->misc_flags & USE_XDF_FLAG))
45 #define DO_SWAP(x) ((x) && ((x)->misc_flags & SWAP_FLAG))
46
47 typedef struct device {
48 const char *name; /* full path to device */
49
50 char drive; /* the drive letter */
51 int fat_bits; /* FAT encoding scheme */
52
53 int mode; /* any special open() flags */
54 unsigned int tracks; /* tracks */
55 uint16_t heads; /* heads */
56 uint16_t sectors; /* sectors */
57 unsigned int hidden; /* number of hidden sectors. Used for
58 * mformatting partitioned devices */
59
60 off_t offset; /* skip this many bytes */
61
62 unsigned int partition;
63
64 unsigned int misc_flags;
65
66 /* Linux only stuff */
67 uint8_t ssize;
68 unsigned int use_2m;
69
70 char *precmd; /* command to be executed before opening
71 * the drive */
72
73 /* internal variables */
74 int file_nr; /* used during parsing */
75 unsigned int blocksize; /* size of disk block in bytes */
76
77 int codepage; /* codepage for shortname encoding */
78
79 const char *cfg_filename; /* used for debugging purposes */
80 } device_t;
81
82 struct OldDos_t {
83 unsigned int tracks;
84 uint16_t sectors;
85 uint16_t heads;
86
87 unsigned int dir_len;
88 unsigned int cluster_size;
89 unsigned int fat_len;
90
91 uint8_t media;
92 };
93
94 extern struct OldDos_t *getOldDosBySize(size_t size);
95 extern struct OldDos_t *getOldDosByMedia(int media);
96 extern struct OldDos_t *getOldDosByParams(unsigned int tracks,
97 unsigned int heads,
98 unsigned int sectors,
99 unsigned int dir_len,
100 unsigned int cluster_size);
101 int setDeviceFromOldDos(int media, struct device *dev);
102
103
104 #ifndef OS_linux
105 #define BOOTSIZE 512
106 #else
107 #define BOOTSIZE 256
108 #endif
109
110 typedef struct doscp_t doscp_t;
111
112 #include "stream.h"
113
114
115 extern const char *short_illegals, *long_illegals;
116
117 #define maximize(target, max) do { \
118 if(target > max) { \
119 target = max; \
120 } \
121 } while(0)
122
123 #define smaximize(target, max) do { \
124 if(max < 0) { \
125 if(target > 0) \
126 target = 0; \
127 } else if(target > max) { \
128 target = max; \
129 } \
130 } while(0)
131
132 #define sizemaximize(target, max) do { \
133 if(max < 0) { \
134 if(target > 0) \
135 target = 0; \
136 } else if(target > (size_t) max) { \
137 target = max; \
138 } \
139 } while(0)
140
141 #define minimize(target, min) do { \
142 if(target < min) \
143 target = min; \
144 } while(0)
145
146 int init_geom(int fd, struct device *dev, struct device *orig_dev,
147 struct MT_STAT *statbuf);
148
149 int readwrite_sectors(int fd, /* file descriptor */
150 int *drive,
151 int rate,
152 int seektrack,
153 int track, int head, int sector, int size, /* address */
154 char *data,
155 int bytes,
156 int direction,
157 int retries);
158
159 int lock_dev(int fd, int mode, struct device *dev);
160
161 char *unix_normalize (doscp_t *cp, char *ans, struct dos_name_t *dn,
162 size_t ans_size);
163 void dos_name(doscp_t *cp, const char *filename, int verbose, int *mangled,
164 struct dos_name_t *);
165 struct directory *mk_entry(const dos_name_t *filename, unsigned char attr,
166 unsigned int fat, size_t size, time_t date,
167 struct directory *ndir);
168
169 struct directory *mk_entry_from_base(const char *base, unsigned char attr,
170 unsigned int fat, size_t size, time_t date,
171 struct directory *ndir);
172
173 int copyfile(Stream_t *Source, Stream_t *Target);
174 int getfreeMinClusters(Stream_t *Stream, size_t ref);
175
176 FILE *opentty(int mode);
177
178 int is_dir(Stream_t *Dir, char *path);
179 void bufferize(Stream_t **Dir);
180
181 int dir_grow(Stream_t *Dir, int size);
182 int match(const wchar_t *, const wchar_t *, wchar_t *, int, int);
183
184 wchar_t *unix_name(doscp_t *fromDos,
185 const char *base, const char *ext, char Case,
186 wchar_t *answer);
187 void *safe_malloc(size_t size);
188 Stream_t *open_filter(Stream_t *Next,int convertCharset);
189
190 extern int got_signal;
191 /* int do_gotsignal(char *, int);
192 #define got_signal do_gotsignal(__FILE__, __LINE__) */
193
194 void setup_signal(void);
195 #ifdef HAVE_SIGACTION
196 typedef struct { struct sigaction sa[4]; } saved_sig_state;
197 #else
198 typedef int saved_sig_state;
199 #endif
200
201 void allow_interrupts(saved_sig_state *ss);
202 void restore_interrupts(saved_sig_state *ss);
203
204 #define SET_INT(target, source) \
205 if(source)target=source
206
207
UNUSED(static __inline__ int compare (long ref,long testee))208 UNUSED(static __inline__ int compare (long ref, long testee))
209 {
210 return (ref && ref != testee);
211 }
212
UNUSED(static __inline__ char ch_toupper (char ch))213 UNUSED(static __inline__ char ch_toupper(char ch))
214 {
215 return (char) toupper( (unsigned char) ch);
216 }
217
UNUSED(static __inline__ char ch_tolower (char ch))218 UNUSED(static __inline__ char ch_tolower(char ch))
219 {
220 return (char) tolower( (unsigned char) ch);
221 }
222
UNUSED(static __inline__ wchar_t ch_towupper (wchar_t ch))223 UNUSED(static __inline__ wchar_t ch_towupper(wchar_t ch))
224 {
225 return (wchar_t) towupper( (wint_t) ch);
226 }
227
UNUSED(static __inline__ wchar_t ch_towlower (wchar_t ch))228 UNUSED(static __inline__ wchar_t ch_towlower(wchar_t ch))
229 {
230 return (wchar_t) towlower( (wint_t) ch);
231 }
232
UNUSED(static __inline__ void init_random (void))233 UNUSED(static __inline__ void init_random(void))
234 {
235 srandom((unsigned int)time (0));
236 }
237
238
239 Stream_t *GetFs(Stream_t *Fs);
240
241 void label_name_uc(doscp_t *cp, const char *filename, int verbose,
242 int *mangled, dos_name_t *ans);
243
244 void label_name_pc(doscp_t *cp, const char *filename, int verbose,
245 int *mangled, dos_name_t *ans);
246
247 /* environmental variables */
248 extern unsigned int mtools_skip_check;
249 extern unsigned int mtools_fat_compatibility;
250 extern unsigned int mtools_ignore_short_case;
251 extern unsigned int mtools_no_vfat;
252 extern unsigned int mtools_numeric_tail;
253 extern unsigned int mtools_dotted_dir;
254 extern unsigned int mtools_lock_timeout;
255 extern unsigned int mtools_twenty_four_hour_clock;
256 extern const char *mtools_date_string;
257 extern uint8_t mtools_rate_0, mtools_rate_any;
258 extern unsigned int mtools_default_codepage;
259 extern int mtools_raw_tty;
260
261 extern int batchmode;
262
263 char get_default_drive(void);
264 void set_cmd_line_image(char *img);
265 void check_number_parse_errno(char c, const char *optarg, char *endptr);
266 void read_config(void);
267 off_t str_to_offset(char *str);
268 unsigned int strtoui(const char *nptr, char **endptr, int base);
269 unsigned int atoui(const char *nptr);
270 #ifndef HAVE_STRTOI
271 int strtoi(const char *nptr, char **endptr, int base);
272 #endif
273 unsigned long atoul(const char *nptr);
274 uint8_t strtou8(const char *nptr, char **endptr, int base);
275 uint8_t atou8(const char *str);
276 uint16_t strtou16(const char *nptr, char **endptr, int base);
277 uint16_t atou16(const char *str);
278 uint32_t strtou32(const char *nptr, char **endptr, int base);
279 uint32_t atou32(const char *str);
280
281 extern struct device *devices;
282 extern struct device const_devices[];
283 extern const unsigned int nr_const_devices;
284
285 #define New(type) ((type*)(calloc(1,sizeof(type))))
286 #define Grow(adr,n,type) ((type*)(realloc((char *)adr,n*sizeof(type))))
287 #define Free(adr) (free((char *)adr));
288 #define NewArray(size,type) ((type*)(calloc((size),sizeof(type))))
289
290 void mattrib(int argc, char **argv, int type);
291 void mbadblocks(int argc, char **argv, int type);
292 void mcat(int argc, char **argv, int type);
293 void mcd(int argc, char **argv, int type);
294 void mclasserase(int argc, char **argv, int type);
295 void mcopy(int argc, char **argv, int type);
296 void mdel(int argc, char **argv, int type);
297 void mdir(int argc, char **argv, int type);
298 void mdoctorfat(int argc, char **argv, int type);
299 void mdu(int argc, char **argv, int type);
300 void mformat(int argc, char **argv, int type);
301 void minfo(int argc, char **argv, int type);
302 void mlabel(int argc, char **argv, int type);
303 void mmd(int argc, char **argv, int type);
304 void mmount(int argc, char **argv, int type);
305 void mmove(int argc, char **argv, int type);
306 void mpartition(int argc, char **argv, int type);
307 void mshortname(int argc, char **argv, int mtype);
308 void mshowfat(int argc, char **argv, int mtype);
309 void mtoolstest(int argc, char **argv, int type);
310 void mzip(int argc, char **argv, int type);
311
312 extern int noPrivileges;
313 void init_privs(void);
314 void reclaim_privs(void);
315 void drop_privs(void);
316 void destroy_privs(void);
317 uid_t get_real_uid(void);
318 void closeExec(int fd);
319
320 extern const char *progname;
321
322 void precmd(struct device *dev);
323
324 void print_sector(const char *message, unsigned char *data, int size);
325 time_t getTimeNow(time_t *now);
326
327 #ifdef USING_NEW_VOLD
328 char *getVoldName(struct device *dev, char *name);
329 #endif
330
331
332 Stream_t *OpenDir(const char *filename);
333 /* int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
334 int unix_loop(MainParam_t *mp, char *arg); */
335
336 struct dirCache_t **getDirCacheP(Stream_t *Stream);
337 int isRootDir(Stream_t *Stream);
338 unsigned int getStart(Stream_t *Dir, struct directory *dir);
339 unsigned int countBlocks(Stream_t *Dir, unsigned int block);
340 char getDrive(Stream_t *Stream);
341
342
343 void printOom(void);
344 int ask_confirmation(const char *, ...) __attribute__ ((format (printf, 1, 2)));
345
346 int helpFlag(int, char **);
347
348 char *get_homedir(void);
349 #define EXPAND_BUF 2048
350 const char *expand(const char *, char *);
351 FILE *open_mcwd(const char *mode);
352 void unlink_mcwd(void);
353
354 #ifndef OS_mingw32msvc
355 ssize_t safePopenOut(const char **command, char *output, size_t len);
356 #endif
357
358 #define ROUND_DOWN(value, grain) ((value) - (value) % (grain))
359 #define ROUND_UP(value, grain) ROUND_DOWN((value) + (grain)-1, (grain))
360
361 #ifndef O_BINARY
362 #define O_BINARY 0
363 #endif
364
365 #endif
366