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.
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.
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.
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/>.
21 typedef struct dos_name_t dos_name_t;
24 #define MAXPATHLEN 1024
26 extern int lockf(int, int, off_t); /* SCO has no proper include file for lockf */
32 #define USE_XDF_FLAG 8
33 #define MFORMAT_ONLY_FLAG 16
35 #define FLOPPYD_FLAG 64
36 #define FILTER_FLAG 128
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))
47 typedef struct device {
48 const char *name; /* full path to device */
50 char drive; /* the drive letter */
51 int fat_bits; /* FAT encoding scheme */
53 unsigned int mode; /* any special open() flags */
54 unsigned int tracks; /* tracks */
55 unsigned int heads; /* heads */
56 unsigned int sectors; /* sectors */
57 unsigned int hidden; /* number of hidden sectors. Used for
58 * mformatting partitioned devices */
60 off_t offset; /* skip this many bytes */
62 unsigned int partition;
64 unsigned int misc_flags;
66 /* Linux only stuff */
70 char *precmd; /* command to be executed before opening
73 /* internal variables */
74 int file_nr; /* used during parsing */
75 unsigned int blocksize; /* size of disk block in bytes */
77 int codepage; /* codepage for shortname encoding */
79 const char *cfg_filename; /* used for debugging purposes */
89 typedef struct doscp_t doscp_t;
94 extern const char *short_illegals, *long_illegals;
96 #define maximize(target, max) do { \
100 } else if(target > max) { \
105 #define minimize(target, min) do { \
110 int init_geom(int fd, struct device *dev, struct device *orig_dev,
111 struct MT_STAT *statbuf);
113 int readwrite_sectors(int fd, /* file descriptor */
117 int track, int head, int sector, int size, /* address */
123 int lock_dev(int fd, int mode, struct device *dev);
125 char *unix_normalize (doscp_t *cp, char *ans, struct dos_name_t *dn);
126 void dos_name(doscp_t *cp, const char *filename, int verbose, int *mangled,
127 struct dos_name_t *);
128 struct directory *mk_entry(const dos_name_t *filename, char attr,
129 unsigned int fat, size_t size, time_t date,
130 struct directory *ndir);
132 struct directory *mk_entry_from_base(const char *base, char attr,
133 unsigned int fat, size_t size, time_t date,
134 struct directory *ndir);
136 int copyfile(Stream_t *Source, Stream_t *Target);
137 int getfreeMinClusters(Stream_t *Stream, size_t ref);
139 FILE *opentty(int mode);
141 int is_dir(Stream_t *Dir, char *path);
142 void bufferize(Stream_t **Dir);
144 int dir_grow(Stream_t *Dir, int size);
145 int match(const wchar_t *, const wchar_t *, wchar_t *, int, int);
147 wchar_t *unix_name(doscp_t *fromDos,
148 const char *base, const char *ext, char Case,
150 void *safe_malloc(size_t size);
151 Stream_t *open_filter(Stream_t *Next,int convertCharset);
153 extern int got_signal;
154 /* int do_gotsignal(char *, int);
155 #define got_signal do_gotsignal(__FILE__, __LINE__) */
157 void setup_signal(void);
160 #define SET_INT(target, source) \
161 if(source)target=source
164 UNUSED(static __inline__ int compare (long ref, long testee))
166 return (ref && ref != testee);
169 Stream_t *GetFs(Stream_t *Fs);
171 void label_name(doscp_t *cp, const char *filename, int verbose,
172 int *mangled, dos_name_t *ans);
174 /* environmental variables */
175 extern unsigned int mtools_skip_check;
176 extern unsigned int mtools_fat_compatibility;
177 extern unsigned int mtools_ignore_short_case;
178 extern unsigned int mtools_no_vfat;
179 extern unsigned int mtools_numeric_tail;
180 extern unsigned int mtools_dotted_dir;
181 extern unsigned int mtools_twenty_four_hour_clock;
182 extern const char *mtools_date_string;
183 extern unsigned int mtools_rate_0, mtools_rate_any;
184 extern unsigned int mtools_default_codepage;
185 extern int mtools_raw_tty;
187 extern int batchmode;
189 char get_default_drive(void);
190 void set_cmd_line_image(char *img, int flags);
191 void read_config(void);
192 off_t str_to_offset(char *str);
193 extern struct device *devices;
194 extern struct device const_devices[];
195 extern const int nr_const_devices;
197 #define New(type) ((type*)(calloc(1,sizeof(type))))
198 #define Grow(adr,n,type) ((type*)(realloc((char *)adr,n*sizeof(type))))
199 #define Free(adr) (free((char *)adr));
200 #define NewArray(size,type) ((type*)(calloc((size),sizeof(type))))
202 void mattrib(int argc, char **argv, int type);
203 void mbadblocks(int argc, char **argv, int type);
204 void mcat(int argc, char **argv, int type);
205 void mcd(int argc, char **argv, int type);
206 void mclasserase(int argc, char **argv, int type);
207 void mcopy(int argc, char **argv, int type);
208 void mdel(int argc, char **argv, int type);
209 void mdir(int argc, char **argv, int type);
210 void mdoctorfat(int argc, char **argv, int type);
211 void mdu(int argc, char **argv, int type);
212 void mformat(int argc, char **argv, int type);
213 void minfo(int argc, char **argv, int type);
214 void mlabel(int argc, char **argv, int type);
215 void mmd(int argc, char **argv, int type);
216 void mmount(int argc, char **argv, int type);
217 void mmove(int argc, char **argv, int type);
218 void mpartition(int argc, char **argv, int type);
219 void mshortname(int argc, char **argv, int mtype);
220 void mshowfat(int argc, char **argv, int mtype);
221 void mtoolstest(int argc, char **argv, int type);
222 void mzip(int argc, char **argv, int type);
224 extern int noPrivileges;
225 void init_privs(void);
226 void reclaim_privs(void);
227 void drop_privs(void);
228 void destroy_privs(void);
229 uid_t get_real_uid(void);
230 void closeExec(int fd);
232 extern const char *progname;
234 void precmd(struct device *dev);
236 void print_sector(const char *message, unsigned char *data, int size);
237 time_t getTimeNow(time_t *now);
239 #ifdef USING_NEW_VOLD
240 char *getVoldName(struct device *dev, char *name);
244 Stream_t *OpenDir(Stream_t *Parent, const char *filename);
245 /* int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
246 int unix_loop(MainParam_t *mp, char *arg); */
248 struct dirCache_t **getDirCacheP(Stream_t *Stream);
249 int isRootDir(Stream_t *Stream);
250 unsigned int getStart(Stream_t *Dir, struct directory *dir);
251 unsigned int countBlocks(Stream_t *Dir, unsigned int block);
252 char getDrive(Stream_t *Stream);
256 int ask_confirmation(const char *, ...) __attribute__ ((format (printf, 1, 2)));
258 int helpFlag(int, char **);
260 char *get_homedir(void);
261 #define EXPAND_BUF 2048
262 const char *expand(const char *, char *);
263 FILE *open_mcwd(const char *mode);
264 void unlink_mcwd(void);
266 #ifndef OS_mingw32msvc
267 int safePopenOut(const char **command, char *output, int len);
270 #define ROUND_DOWN(value, grain) ((value) - (value) % (grain))
271 #define ROUND_UP(value, grain) ROUND_DOWN((value) + (grain)-1, (grain))