blob: b3fd3c86bf402264fa3189f4fc1f50e6951b67d3 [file] [log] [blame]
Alistair Delvabeaee832021-02-24 11:27:23 -08001#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"
Yi Kong39bbd962022-01-09 19:41:38 +080020#include "llong.h"
Alistair Delvabeaee832021-02-24 11:27:23 -080021typedef struct dos_name_t dos_name_t;
22
23#if defined(OS_sco3)
24#define MAXPATHLEN 1024
25#include <signal.h>
26extern int lockf(int, int, off_t); /* SCO has no proper include file for lockf */
Yi Kong39bbd962022-01-09 19:41:38 +080027#endif
Alistair Delvabeaee832021-02-24 11:27:23 -080028
Alistair Delvabeaee832021-02-24 11:27:23 -080029
Alistair Delvabeaee832021-02-24 11:27:23 -080030
31
32#ifndef OS_linux
33#define BOOTSIZE 512
34#else
35#define BOOTSIZE 256
36#endif
37
38typedef struct doscp_t doscp_t;
39
40#include "stream.h"
41
42
43extern 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
Alistair Delvabeaee832021-02-24 11:27:23 -080051#define sizemaximize(target, max) do { \
52 if(max < 0) { \
53 if(target > 0) \
54 target = 0; \
55 } else if(target > (size_t) max) { \
Yi Kong39bbd962022-01-09 19:41:38 +080056 target = (size_t) max; \
Alistair Delvabeaee832021-02-24 11:27:23 -080057 } \
58} while(0)
59
60#define minimize(target, min) do { \
61 if(target < min) \
62 target = min; \
Yi Kong39bbd962022-01-09 19:41:38 +080063} while(0)
Alistair Delvabeaee832021-02-24 11:27:23 -080064
Yi Kong39bbd962022-01-09 19:41:38 +080065#ifdef OS_linux
66int get_sector_size(int fd);
67#endif
Alistair Delvabeaee832021-02-24 11:27:23 -080068
69int 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 */
Yi Kong39bbd962022-01-09 19:41:38 +080074 char *data,
Alistair Delvabeaee832021-02-24 11:27:23 -080075 int bytes,
76 int direction,
77 int retries);
78
Alistair Delvabeaee832021-02-24 11:27:23 -080079char *unix_normalize (doscp_t *cp, char *ans, struct dos_name_t *dn,
80 size_t ans_size);
81void dos_name(doscp_t *cp, const char *filename, int verbose, int *mangled,
82 struct dos_name_t *);
83struct directory *mk_entry(const dos_name_t *filename, unsigned char attr,
Yi Kong39bbd962022-01-09 19:41:38 +080084 unsigned int fat, uint32_t size, time_t date,
Alistair Delvabeaee832021-02-24 11:27:23 -080085 struct directory *ndir);
86
87struct directory *mk_entry_from_base(const char *base, unsigned char attr,
Yi Kong39bbd962022-01-09 19:41:38 +080088 unsigned int fat, uint32_t size, time_t date,
Alistair Delvabeaee832021-02-24 11:27:23 -080089 struct directory *ndir);
90
Yi Kong39bbd962022-01-09 19:41:38 +080091mt_off_t copyfile(Stream_t *Source, Stream_t *Target);
92int getfreeMinClusters(Stream_t *Stream, uint32_t ref);
Alistair Delvabeaee832021-02-24 11:27:23 -080093
94FILE *opentty(int mode);
95
96int is_dir(Stream_t *Dir, char *path);
Alistair Delvabeaee832021-02-24 11:27:23 -080097
98int dir_grow(Stream_t *Dir, int size);
99int match(const wchar_t *, const wchar_t *, wchar_t *, int, int);
100
101wchar_t *unix_name(doscp_t *fromDos,
Yi Kong39bbd962022-01-09 19:41:38 +0800102 const char *base, const char *ext, uint8_t Case,
Alistair Delvabeaee832021-02-24 11:27:23 -0800103 wchar_t *answer);
104void *safe_malloc(size_t size);
Yi Kong39bbd962022-01-09 19:41:38 +0800105Stream_t *open_dos2unix(Stream_t *Next,int convertCharset);
106Stream_t *open_unix2dos(Stream_t *Next,int convertCharset);
Alistair Delvabeaee832021-02-24 11:27:23 -0800107
108extern int got_signal;
109/* int do_gotsignal(char *, int);
110#define got_signal do_gotsignal(__FILE__, __LINE__) */
111
112void setup_signal(void);
113#ifdef HAVE_SIGACTION
114typedef struct { struct sigaction sa[4]; } saved_sig_state;
115#else
116typedef int saved_sig_state;
117#endif
118
119void allow_interrupts(saved_sig_state *ss);
120void restore_interrupts(saved_sig_state *ss);
121
122#define SET_INT(target, source) \
123if(source)target=source
124
Yi Kong39bbd962022-01-09 19:41:38 +0800125#define compare(ref,testee) ((ref) && (ref) != (testee))
Alistair Delvabeaee832021-02-24 11:27:23 -0800126
127UNUSED(static __inline__ char ch_toupper(char ch))
128{
129 return (char) toupper( (unsigned char) ch);
130}
131
132UNUSED(static __inline__ char ch_tolower(char ch))
133{
134 return (char) tolower( (unsigned char) ch);
135}
136
137UNUSED(static __inline__ wchar_t ch_towupper(wchar_t ch))
138{
139 return (wchar_t) towupper( (wint_t) ch);
140}
141
142UNUSED(static __inline__ wchar_t ch_towlower(wchar_t ch))
143{
144 return (wchar_t) towlower( (wint_t) ch);
145}
146
147UNUSED(static __inline__ void init_random(void))
148{
149 srandom((unsigned int)time (0));
150}
151
Yi Kong39bbd962022-01-09 19:41:38 +0800152UNUSED(static __inline__ size_t ptrdiff (const char *end, const char *begin))
153{
154 return (size_t) (end-begin);
155}
Alistair Delvabeaee832021-02-24 11:27:23 -0800156
157Stream_t *GetFs(Stream_t *Fs);
158
Yi Kong39bbd962022-01-09 19:41:38 +0800159void label_name_uc(doscp_t *cp, const char *filename, int verbose,
Alistair Delvabeaee832021-02-24 11:27:23 -0800160 int *mangled, dos_name_t *ans);
161
Yi Kong39bbd962022-01-09 19:41:38 +0800162void label_name_pc(doscp_t *cp, const char *filename, int verbose,
Alistair Delvabeaee832021-02-24 11:27:23 -0800163 int *mangled, dos_name_t *ans);
164
165/* environmental variables */
166extern unsigned int mtools_skip_check;
167extern unsigned int mtools_fat_compatibility;
168extern unsigned int mtools_ignore_short_case;
169extern unsigned int mtools_no_vfat;
170extern unsigned int mtools_numeric_tail;
171extern unsigned int mtools_dotted_dir;
172extern unsigned int mtools_lock_timeout;
173extern unsigned int mtools_twenty_four_hour_clock;
174extern const char *mtools_date_string;
175extern uint8_t mtools_rate_0, mtools_rate_any;
176extern unsigned int mtools_default_codepage;
177extern int mtools_raw_tty;
178
179extern int batchmode;
180
181char get_default_drive(void);
182void set_cmd_line_image(char *img);
183void check_number_parse_errno(char c, const char *optarg, char *endptr);
184void read_config(void);
Yi Kong39bbd962022-01-09 19:41:38 +0800185off_t str_to_offset_with_end(const char *str, char **endp);
186mt_off_t str_to_off_with_end(const char *str, char **endp);
Alistair Delvabeaee832021-02-24 11:27:23 -0800187off_t str_to_offset(char *str);
Yi Kong39bbd962022-01-09 19:41:38 +0800188uint32_t parseSize(char *sizeStr);
Alistair Delvabeaee832021-02-24 11:27:23 -0800189unsigned int strtoui(const char *nptr, char **endptr, int base);
190unsigned int atoui(const char *nptr);
191#ifndef HAVE_STRTOI
192int strtoi(const char *nptr, char **endptr, int base);
193#endif
194unsigned long atoul(const char *nptr);
195uint8_t strtou8(const char *nptr, char **endptr, int base);
196uint8_t atou8(const char *str);
197uint16_t strtou16(const char *nptr, char **endptr, int base);
198uint16_t atou16(const char *str);
199uint32_t strtou32(const char *nptr, char **endptr, int base);
200uint32_t atou32(const char *str);
201
Alistair Delvabeaee832021-02-24 11:27:23 -0800202#define New(type) ((type*)(calloc(1,sizeof(type))))
203#define Grow(adr,n,type) ((type*)(realloc((char *)adr,n*sizeof(type))))
Yi Kong39bbd962022-01-09 19:41:38 +0800204#define Free(adr) (free((char *)adr))
Alistair Delvabeaee832021-02-24 11:27:23 -0800205#define NewArray(size,type) ((type*)(calloc((size),sizeof(type))))
206
207void mattrib(int argc, char **argv, int type);
208void mbadblocks(int argc, char **argv, int type);
209void mcat(int argc, char **argv, int type);
210void mcd(int argc, char **argv, int type);
Alistair Delvabeaee832021-02-24 11:27:23 -0800211void mcopy(int argc, char **argv, int type);
212void mdel(int argc, char **argv, int type);
213void mdir(int argc, char **argv, int type);
214void mdoctorfat(int argc, char **argv, int type);
215void mdu(int argc, char **argv, int type);
216void mformat(int argc, char **argv, int type);
217void minfo(int argc, char **argv, int type);
218void mlabel(int argc, char **argv, int type);
219void mmd(int argc, char **argv, int type);
220void mmount(int argc, char **argv, int type);
221void mmove(int argc, char **argv, int type);
222void mpartition(int argc, char **argv, int type);
223void mshortname(int argc, char **argv, int mtype);
224void mshowfat(int argc, char **argv, int mtype);
225void mtoolstest(int argc, char **argv, int type);
226void mzip(int argc, char **argv, int type);
227
228extern int noPrivileges;
229void init_privs(void);
230void reclaim_privs(void);
231void drop_privs(void);
232void destroy_privs(void);
233uid_t get_real_uid(void);
234void closeExec(int fd);
235
236extern const char *progname;
237
Alistair Delvabeaee832021-02-24 11:27:23 -0800238void print_sector(const char *message, unsigned char *data, int size);
239time_t getTimeNow(time_t *now);
240
241#ifdef USING_NEW_VOLD
242char *getVoldName(struct device *dev, char *name);
243#endif
244
245
246Stream_t *OpenDir(const char *filename);
Yi Kong39bbd962022-01-09 19:41:38 +0800247/* int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
Alistair Delvabeaee832021-02-24 11:27:23 -0800248int unix_loop(MainParam_t *mp, char *arg); */
249
250struct dirCache_t **getDirCacheP(Stream_t *Stream);
251int isRootDir(Stream_t *Stream);
252unsigned int getStart(Stream_t *Dir, struct directory *dir);
253unsigned int countBlocks(Stream_t *Dir, unsigned int block);
254char getDrive(Stream_t *Stream);
255
256
257void printOom(void);
258int ask_confirmation(const char *, ...) __attribute__ ((format (printf, 1, 2)));
259
260int helpFlag(int, char **);
261
262char *get_homedir(void);
263#define EXPAND_BUF 2048
264const char *expand(const char *, char *);
265FILE *open_mcwd(const char *mode);
266void unlink_mcwd(void);
267
268#ifndef OS_mingw32msvc
269ssize_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