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