Initial import package mtools: Programs for accessing MS-DOS disks without mounting...
[profile/ivi/mtools.git] / llong.c
1 /*  Copyright 1999-2003,2006,2008,2009 Alain Knaff.
2  *  This file is part of mtools.
3  *
4  *  Mtools is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Mtools is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "sysincludes.h"
19 #include "stream.h"
20 #include "fsP.h"
21 #include "llong.h"
22 #include "mtools.h"
23
24 #if 1
25 const mt_off_t max_off_t_31 = MAX_OFF_T_B(31); /* Floppyd */
26 const mt_off_t max_off_t_32 = MAX_OFF_T_B(32); /* Directory */
27 const mt_off_t max_off_t_41 = MAX_OFF_T_B(41); /* SCSI */
28 const mt_off_t max_off_t_seek = MAX_OFF_T_B(SEEK_BITS); /* SCSI */
29 #else
30 const mt_off_t max_off_t_31 = MAX_OFF_T_B(10); /* Floppyd */
31 const mt_off_t max_off_t_41 = MAX_OFF_T_B(10); /* SCSI */
32 const mt_off_t max_off_t_seek = MAX_OFF_T_B(10); /* SCSI */
33 #endif
34
35 int fileTooBig(mt_off_t off) {
36         return (off & ~max_off_t_32) != 0;
37 }
38
39 off_t truncBytes32(mt_off_t off)
40 {
41         if (fileTooBig(off)) {
42                 fprintf(stderr, "Internal error, offset too big\n");
43                 exit(1);
44         }
45         return (off_t) off;
46 }
47
48 mt_off_t sectorsToBytes(Stream_t *Stream, off_t off)
49 {
50         DeclareThis(Fs_t);
51         return (mt_off_t) off << This->sectorShift;
52 }
53
54 #if defined HAVE_LLSEEK
55 # ifndef HAVE_LLSEEK_PROTOTYPE
56 extern long long llseek (int fd, long long offset, int origin);
57 # endif
58 #endif
59
60 #if defined HAVE_LSEEK64
61 # ifndef HAVE_LSEEK64_PROTOTYPE
62 extern long long lseek64 (int fd, long long offset, int origin);
63 # endif
64 #endif
65
66
67 int mt_lseek(int fd, mt_off_t where, int whence)
68 {
69 #if defined HAVE_LSEEK64
70         if(lseek64(fd, where, whence) >= 0)
71                 return 0;
72         else
73                 return -1;
74 #elif defined HAVE_LLSEEK
75         if(llseek(fd, where, whence) >= 0)
76                 return 0;
77         else
78                 return -1;              
79 #else
80         if (lseek(fd, (off_t) where, whence) >= 0)
81                 return 0;
82         else
83                 return 1;
84 #endif
85 }
86
87 unsigned int log_2(int size)
88 {
89         unsigned int i;
90
91         for(i=0; i<24; i++) {
92                 if(1 << i == size)
93                         return i;
94         }
95         return 24;
96 }