d181ff250fadb79c4e12ef6feab0071f699a908e
[platform/upstream/mtools.git] / misc.c
1 /*  Copyright 1996-2002,2005,2007,2009,2011 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  * Miscellaneous routines.
18  */
19
20 #include "sysincludes.h"
21 #include "msdos.h"
22 #include "stream.h"
23 #include "vfat.h"
24 #include "mtools.h"
25
26
27 void printOom(void)
28 {
29         fprintf(stderr, "Out of memory error");
30 }
31
32 char *get_homedir(void)
33 {
34 #ifndef OS_mingw32msvc
35         struct passwd *pw;
36         uid_t uid;
37         char *homedir;
38         char *username;
39         
40         homedir = getenv ("HOME");    
41         /* 
42          * first we call getlogin. 
43          * There might be several accounts sharing one uid 
44          */
45         if ( homedir )
46                 return homedir;
47         
48         pw = 0;
49         
50         username = getenv("LOGNAME");
51         if ( !username )
52                 username = getlogin();
53         if ( username )
54                 pw = getpwnam( username);
55   
56         if ( pw == 0 ){
57                 /* if we can't getlogin, look up the pwent by uid */
58                 uid = geteuid();
59                 pw = getpwuid(uid);
60         }
61         
62         /* we might still get no entry */
63         if ( pw )
64                 return pw->pw_dir;
65         return 0;
66 #else
67         return getenv("HOME");
68 #endif
69 }
70
71
72 static void get_mcwd_file_name(char *file)
73 {
74         char *mcwd_path;
75         const char *homedir;
76
77         mcwd_path = getenv("MCWD");
78         if (mcwd_path == NULL || *mcwd_path == '\0'){
79                 homedir= get_homedir();
80                 if(!homedir)
81                         homedir="/tmp";
82                 strncpy(file, homedir, MAXPATHLEN-6);
83                 file[MAXPATHLEN-6]='\0';
84                 strcat( file, "/.mcwd");
85         } else {
86                 strncpy(file, mcwd_path, MAXPATHLEN);
87                 file[MAXPATHLEN]='\0';
88         }
89 }
90
91 void unlink_mcwd(void)
92 {
93         char file[MAXPATHLEN+1];
94         get_mcwd_file_name(file);
95         unlink(file);
96 }
97
98 FILE *open_mcwd(const char *mode)
99 {
100         struct MT_STAT sbuf;
101         char file[MAXPATHLEN+1];
102         time_t now;
103         
104         get_mcwd_file_name(file);
105         if (*mode == 'r'){
106                 if (MT_STAT(file, &sbuf) < 0)
107                         return NULL;
108                 /*
109                  * Ignore the info, if the file is more than 6 hours old
110                  */
111                 getTimeNow(&now);
112                 if (now - sbuf.st_mtime > 6 * 60 * 60) {
113                         fprintf(stderr,
114                                 "Warning: \"%s\" is out of date, removing it\n",
115                                 file);
116                         unlink(file);
117                         return NULL;
118                 }
119         }
120         
121         return  fopen(file, mode);
122 }
123         
124
125
126 void *safe_malloc(size_t size)
127 {
128         void *p;
129
130         p = malloc(size);
131         if(!p){
132                 printOom();
133                 exit(1);
134         }
135         return p;
136 }
137
138 void print_sector(const char *message, unsigned char *data, int size)
139 {
140         int col;
141         int row;
142
143         printf("%s:\n", message);
144         
145         for(row = 0; row * 16 < size; row++){
146                 printf("%03x  ", row * 16);
147                 for(col = 0; col < 16; col++)                   
148                         printf("%02x ", data [row*16+col]);
149                 for(col = 0; col < 16; col++) {
150                         if(isprint(data [row*16+col]))
151                                 printf("%c", data [row*16+col]);
152                         else
153                                 printf(".");
154                 }
155                 printf("\n");
156         }
157 }
158
159
160 time_t getTimeNow(time_t *now)
161 {
162         static int haveTime = 0;
163         static time_t sharedNow;
164
165         if(!haveTime) {
166                 const char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
167                 if (source_date_epoch) {
168                         char *endptr;
169                         unsigned long long epoch =
170                                 strtoll(source_date_epoch, &endptr, 10);
171
172                         if (endptr == source_date_epoch)
173                                 fprintf(stderr, "SOURCE_DATE_EPOCH invalid\n");
174                         else if ((errno == ERANGE &&
175                                   (epoch == ULLONG_MAX || epoch == 0))
176                                  || (errno != 0 && epoch == 0))
177                                 fprintf(stderr,
178                                         "SOURCE_DATE_EPOCH: strtoll: %s: %llu\n",
179                                         strerror(errno), epoch);
180                         else if (*endptr != '\0')
181                                 fprintf(stderr,
182                                         "SOURCE_DATE_EPOCH has trailing garbage\n");
183                         else if (epoch > ULONG_MAX)
184                                 fprintf(stderr,
185                                         "SOURCE_DATE_EPOCH must be <= %lu but saw: %llu\n",
186                                         ULONG_MAX, epoch);
187                         else {
188                                 sharedNow = epoch;
189                                 haveTime = 1;
190                         }
191                 }
192         }
193         
194         if(!haveTime) {
195                 time(&sharedNow);
196                 haveTime = 1;
197         }
198         if(now)
199                 *now = sharedNow;
200         return sharedNow;
201 }
202
203 /* Convert a string to an offset. The string should be a number,
204    optionally followed by S (sectors), K (K-Bytes), M (Megabytes), G
205    (Gigabytes) */
206 off_t str_to_offset(char *str) {
207         char s, *endp = NULL;
208         off_t ofs;
209
210         ofs = strtol(str, &endp, 0);
211         if (ofs <= 0)
212                 return 0; /* invalid or missing offset */
213         s = *endp++;
214         if (s) {   /* trailing char, see if it is a size specifier */
215                 if (s == 's' || s == 'S')       /* sector */
216                         ofs <<= 9;
217                 else if (s == 'k' || s == 'K')  /* kb */
218                         ofs <<= 10;
219                 else if (s == 'm' || s == 'M')  /* Mb */
220                         ofs <<= 20;
221                 else if (s == 'g' || s == 'G')  /* Gb */
222                         ofs <<= 30;
223                 else
224                         return 0;      /* invalid character */
225                 if (*endp)
226                         return 0;      /* extra char, invalid */
227         }
228         return ofs;
229 }
230
231 #if 0
232
233 #undef free
234 #undef malloc
235
236 static int total=0;
237
238 void myfree(void *ptr)
239 {
240         int *size = ((int *) ptr)-1;
241         total -= *size;
242         fprintf(stderr, "freeing %d bytes at %p total alloced=%d\n",
243                 *size, ptr, total);
244         free(size);
245 }
246
247 void *mymalloc(size_t size)
248 {
249         int *ptr;
250         ptr = (int *)malloc(size+sizeof(int));
251         if(!ptr)
252                 return 0;
253         *ptr = size;
254         ptr++;
255         total += size;
256         fprintf(stderr, "allocating %d bytes at %p total allocated=%d\n",
257                 size, ptr, total);
258         return (void *) ptr;
259 }
260
261 void *mycalloc(size_t nmemb, size_t size)
262 {
263         void *ptr = mymalloc(nmemb * size);
264         if(!ptr)
265                 return 0;
266         memset(ptr, 0, size);
267         return ptr;
268 }
269
270 void *myrealloc(void *ptr, size_t size)
271 {
272         int oldsize = ((int *)ptr) [-1];
273         void *new = mymalloc(size);
274         if(!new)
275                 return 0;
276         memcpy(new, ptr, oldsize);
277         myfree(ptr);
278         return new;
279 }
280
281 char *mystrdup(char *src)
282 {
283         char *dest;
284         dest = mymalloc(strlen(src)+1);
285         if(!dest)
286                 return 0;
287         strcpy(dest, src);
288         return dest;
289 }
290
291 #endif