765ca77d2ccfa92b76d5d5dadd32b9287bb812f1
[platform/upstream/mtools.git] / mtools.h
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 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
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         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
89 typedef struct doscp_t doscp_t;
90
91 #include "stream.h"
92
93
94 extern 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
110 int init_geom(int fd, struct device *dev, struct device *orig_dev,
111               struct MT_STAT *statbuf);
112
113 int 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
123 int lock_dev(int fd, int mode, struct device *dev);
124
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);
131
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);
135
136 int copyfile(Stream_t *Source, Stream_t *Target);
137 int getfreeMinClusters(Stream_t *Stream, size_t ref);
138
139 FILE *opentty(int mode);
140
141 int is_dir(Stream_t *Dir, char *path);
142 void bufferize(Stream_t **Dir);
143
144 int dir_grow(Stream_t *Dir, int size);
145 int match(const wchar_t *, const wchar_t *, wchar_t *, int,  int);
146
147 wchar_t *unix_name(doscp_t *fromDos,
148                    const char *base, const char *ext, char Case,
149                    wchar_t *answer);
150 void *safe_malloc(size_t size);
151 Stream_t *open_filter(Stream_t *Next,int convertCharset);
152
153 extern int got_signal;
154 /* int do_gotsignal(char *, int);
155 #define got_signal do_gotsignal(__FILE__, __LINE__) */
156
157 void setup_signal(void);
158
159
160 #define SET_INT(target, source) \
161 if(source)target=source
162
163
164 UNUSED(static __inline__ int compare (long ref, long testee))
165 {
166         return (ref && ref != testee);
167 }
168
169 Stream_t *GetFs(Stream_t *Fs);
170
171 void label_name(doscp_t *cp, const char *filename, int verbose, 
172                 int *mangled, dos_name_t *ans);
173
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;
186
187 extern int batchmode;
188
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;
196
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))))
201
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);
223
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);
231
232 extern const char *progname;
233
234 void precmd(struct device *dev);
235
236 void print_sector(const char *message, unsigned char *data, int size);
237 time_t getTimeNow(time_t *now);
238
239 #ifdef USING_NEW_VOLD
240 char *getVoldName(struct device *dev, char *name);
241 #endif
242
243
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); */
247
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);
253
254
255 void printOom(void);
256 int ask_confirmation(const char *, ...)  __attribute__ ((format (printf, 1, 2)));
257
258 int helpFlag(int, char **);
259
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);
265
266 #ifndef OS_mingw32msvc
267 int safePopenOut(const char **command, char *output, int len);
268 #endif
269
270 #define ROUND_DOWN(value, grain) ((value) - (value) % (grain))
271 #define ROUND_UP(value, grain) ROUND_DOWN((value) + (grain)-1, (grain))
272
273 #ifndef O_BINARY
274 #define O_BINARY 0
275 #endif
276
277 #endif