1#ifndef MTOOLS_MTOOLS_H
2#define MTOOLS_MTOOLS_H
3/*  Copyright 1996-2005,2007-2009 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
21typedef 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 */
27#endif
28
29#define SCSI_FLAG 1
30#define PRIV_FLAG 2
31#define NOLOCK_FLAG 4
32#define USE_XDF_FLAG 8
33#define MFORMAT_ONLY_FLAG 16
34#define VOLD_FLAG 32
35#define FLOPPYD_FLAG 64
36#define FILTER_FLAG 128
37#define SWAP_FLAG 256
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
47typedef 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	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 */
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	unsigned int 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
83#ifndef OS_linux
84#define BOOTSIZE 512
85#else
86#define BOOTSIZE 256
87#endif
88
89typedef struct doscp_t doscp_t;
90
91#include "stream.h"
92
93
94extern const char *short_illegals, *long_illegals;
95
96#define maximize(target, max) do { \
97  if(max < 0) { \
98    if(target > 0) \
99      target = 0; \
100  } else if(target > max) { \
101    target = max; \
102  } \
103} while(0)
104
105#define minimize(target, min) do { \
106  if(target < min) \
107    target = min; \
108} while(0)
109
110int init_geom(int fd, struct device *dev, struct device *orig_dev,
111	      struct MT_STAT *statbuf);
112
113int readwrite_sectors(int fd, /* file descriptor */
114		      int *drive,
115		      int rate,
116		      int seektrack,
117		      int track, int head, int sector, int size, /* address */
118		      char *data,
119		      int bytes,
120		      int direction,
121		      int retries);
122
123int lock_dev(int fd, int mode, struct device *dev);
124
125char *unix_normalize (doscp_t *cp, char *ans, struct dos_name_t *dn);
126void dos_name(doscp_t *cp, const char *filename, int verbose, int *mangled,
127	      struct dos_name_t *);
128struct directory *mk_entry(const dos_name_t *filename, char attr,
129			   unsigned int fat, size_t size, time_t date,
130			   struct directory *ndir);
131
132struct directory *mk_entry_from_base(const char *base, char attr,
133				     unsigned int fat, size_t size, time_t date,
134				     struct directory *ndir);
135
136int copyfile(Stream_t *Source, Stream_t *Target);
137int getfreeMinClusters(Stream_t *Stream, size_t ref);
138
139FILE *opentty(int mode);
140
141int is_dir(Stream_t *Dir, char *path);
142void bufferize(Stream_t **Dir);
143
144int dir_grow(Stream_t *Dir, int size);
145int match(const wchar_t *, const wchar_t *, wchar_t *, int,  int);
146
147wchar_t *unix_name(doscp_t *fromDos,
148		   const char *base, const char *ext, char Case,
149		   wchar_t *answer);
150void *safe_malloc(size_t size);
151Stream_t *open_filter(Stream_t *Next,int convertCharset);
152
153extern int got_signal;
154/* int do_gotsignal(char *, int);
155#define got_signal do_gotsignal(__FILE__, __LINE__) */
156
157void setup_signal(void);
158
159
160#define SET_INT(target, source) \
161if(source)target=source
162
163
164UNUSED(static __inline__ int compare (long ref, long testee))
165{
166	return (ref && ref != testee);
167}
168
169Stream_t *GetFs(Stream_t *Fs);
170
171void label_name(doscp_t *cp, const char *filename, int verbose,
172		int *mangled, dos_name_t *ans);
173
174/* environmental variables */
175extern unsigned int mtools_skip_check;
176extern unsigned int mtools_fat_compatibility;
177extern unsigned int mtools_ignore_short_case;
178extern unsigned int mtools_no_vfat;
179extern unsigned int mtools_numeric_tail;
180extern unsigned int mtools_dotted_dir;
181extern unsigned int mtools_twenty_four_hour_clock;
182extern const char *mtools_date_string;
183extern unsigned int mtools_rate_0, mtools_rate_any;
184extern unsigned int mtools_default_codepage;
185extern int mtools_raw_tty;
186
187extern int batchmode;
188
189char get_default_drive(void);
190void set_cmd_line_image(char *img, int flags);
191void read_config(void);
192extern struct device *devices;
193extern struct device const_devices[];
194extern const int nr_const_devices;
195
196#define New(type) ((type*)(calloc(1,sizeof(type))))
197#define Grow(adr,n,type) ((type*)(realloc((char *)adr,n*sizeof(type))))
198#define Free(adr) (free((char *)adr));
199#define NewArray(size,type) ((type*)(calloc((size),sizeof(type))))
200
201void mattrib(int argc, char **argv, int type);
202void mbadblocks(int argc, char **argv, int type);
203void mcat(int argc, char **argv, int type);
204void mcd(int argc, char **argv, int type);
205void mclasserase(int argc, char **argv, int type);
206void mcopy(int argc, char **argv, int type);
207void mdel(int argc, char **argv, int type);
208void mdir(int argc, char **argv, int type);
209void mdoctorfat(int argc, char **argv, int type);
210void mdu(int argc, char **argv, int type);
211void mformat(int argc, char **argv, int type);
212void minfo(int argc, char **argv, int type);
213void mlabel(int argc, char **argv, int type);
214void mmd(int argc, char **argv, int type);
215void mmount(int argc, char **argv, int type);
216void mmove(int argc, char **argv, int type);
217void mpartition(int argc, char **argv, int type);
218void mshowfat(int argc, char **argv, int mtype);
219void mtoolstest(int argc, char **argv, int type);
220void mzip(int argc, char **argv, int type);
221
222extern int noPrivileges;
223void init_privs(void);
224void reclaim_privs(void);
225void drop_privs(void);
226void destroy_privs(void);
227uid_t get_real_uid(void);
228void closeExec(int fd);
229
230extern const char *progname;
231
232void precmd(struct device *dev);
233
234void print_sector(const char *message, unsigned char *data, int size);
235time_t getTimeNow(time_t *now);
236
237#ifdef USING_NEW_VOLD
238char *getVoldName(struct device *dev, char *name);
239#endif
240
241
242Stream_t *OpenDir(Stream_t *Parent, const char *filename);
243/* int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
244int unix_loop(MainParam_t *mp, char *arg); */
245
246struct dirCache_t **getDirCacheP(Stream_t *Stream);
247int isRootDir(Stream_t *Stream);
248unsigned int getStart(Stream_t *Dir, struct directory *dir);
249unsigned int countBlocks(Stream_t *Dir, unsigned int block);
250char getDrive(Stream_t *Stream);
251
252
253void printOom(void);
254int ask_confirmation(const char *, ...)  __attribute__ ((format (printf, 1, 2)));
255
256int helpFlag(int, char **);
257
258char *get_homedir(void);
259#define EXPAND_BUF 2048
260const char *expand(const char *, char *);
261FILE *open_mcwd(const char *mode);
262void unlink_mcwd(void);
263
264#ifndef OS_mingw32msvc
265int safePopenOut(const char **command, char *output, int len);
266#endif
267
268#define ROUND_DOWN(value, grain) ((value) - (value) % (grain))
269#define ROUND_UP(value, grain) ROUND_DOWN((value) + (grain)-1, (grain))
270
271#ifndef O_BINARY
272#define O_BINARY 0
273#endif
274
275#endif
276