1 /* Copyright 1996-2002,2005,2007,2009,2011 Alain Knaff.
2 * This file is part of mtools.
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.
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.
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/>.
17 * Miscellaneous routines.
20 #include "sysincludes.h"
29 fprintf(stderr, "Out of memory error");
32 char *get_homedir(void)
34 #ifndef OS_mingw32msvc
40 homedir = getenv ("HOME");
42 * first we call getlogin.
43 * There might be several accounts sharing one uid
50 username = getenv("LOGNAME");
52 username = getlogin();
54 pw = getpwnam( username);
57 /* if we can't getlogin, look up the pwent by uid */
62 /* we might still get no entry */
67 return getenv("HOME");
72 static void get_mcwd_file_name(char *file)
77 mcwd_path = getenv("MCWD");
78 if (mcwd_path == NULL || *mcwd_path == '\0'){
79 homedir= get_homedir();
82 strncpy(file, homedir, MAXPATHLEN-6);
83 file[MAXPATHLEN-6]='\0';
84 strcat( file, "/.mcwd");
86 strncpy(file, mcwd_path, MAXPATHLEN);
87 file[MAXPATHLEN]='\0';
91 void unlink_mcwd(void)
93 char file[MAXPATHLEN+1];
94 get_mcwd_file_name(file);
98 FILE *open_mcwd(const char *mode)
101 char file[MAXPATHLEN+1];
104 get_mcwd_file_name(file);
106 if (MT_STAT(file, &sbuf) < 0)
109 * Ignore the info, if the file is more than 6 hours old
112 if (now - sbuf.st_mtime > 6 * 60 * 60) {
114 "Warning: \"%s\" is out of date, removing it\n",
121 return fopen(file, mode);
126 void *safe_malloc(size_t size)
138 void print_sector(const char *message, unsigned char *data, int size)
143 printf("%s:\n", message);
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]);
159 #if (SIZEOF_TIME_T > SIZEOF_LONG) && defined (HAVE_STRTOLL)
160 # define STRTOTIME strtoll
162 # define STRTOTIME strtol
165 time_t getTimeNow(time_t *now)
167 static int haveTime = 0;
168 static time_t sharedNow;
171 const char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
172 if (source_date_epoch) {
175 STRTOTIME(source_date_epoch, &endptr, 10);
178 if (endptr == source_date_epoch)
180 "SOURCE_DATE_EPOCH \"%s\" invalid\n",
184 "SOURCE_DATE_EPOCH: strtoll: %s: %s\n",
185 strerror(errno), source_date_epoch);
186 else if (*endptr != '\0')
188 "SOURCE_DATE_EPOCH has trailing garbage \"%s\"\n",
206 /* Convert a string to an offset. The string should be a number,
207 optionally followed by S (sectors), K (K-Bytes), M (Megabytes), G
209 off_t str_to_offset_with_end(const char *str, char **endp) {
214 ofs = strtol(str, endp, 0);
216 /* trailing char, see if it is a size specifier */
217 if (s == 's' || s == 'S') /* sector */
219 else if (s == 'k' || s == 'K') /* kb */
221 else if (s == 'm' || s == 'M') /* Mb */
223 else if (s == 'g' || s == 'G') /* Gb */
226 return ofs; /* invalid character */
231 /* Convert a string to a size. The string should be a number,
232 optionally followed by S (sectors), K (K-Bytes), M (Megabytes), G
234 size_t str_to_size_with_end(const char *str, char **endp) {
239 siz = strtoul(str, endp, 0);
241 /* trailing char, see if it is a size specifier */
242 if (s == 's' || s == 'S') /* sector */
244 else if (s == 'k' || s == 'K') /* kb */
246 else if (s == 'm' || s == 'M') /* Mb */
248 else if (s == 'g' || s == 'G') /* Gb */
251 return siz; /* invalid character */
256 off_t str_to_offset(char *str) {
258 off_t ofs = str_to_offset_with_end(str, &end);
260 return 0; /* invalid or missing offset */
262 return 0; /* extra char, invalid */
275 void myfree(void *ptr)
277 int *size = ((int *) ptr)-1;
279 fprintf(stderr, "freeing %d bytes at %p total allocated=%d\n",
284 void *mymalloc(size_t size)
287 ptr = (int *)malloc(size+sizeof(int));
293 fprintf(stderr, "allocating %d bytes at %p total allocated=%d\n",
298 void *mycalloc(size_t nmemb, size_t size)
300 void *ptr = mymalloc(nmemb * size);
303 memset(ptr, 0, size);
307 void *myrealloc(void *ptr, size_t size)
309 int oldsize = ((int *)ptr) [-1];
310 void *new = mymalloc(size);
313 memcpy(new, ptr, oldsize);
318 char *mystrdup(char *src)
321 dest = mymalloc(strlen(src)+1);