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"
27 fprintf(stderr, "Out of memory error");
30 char *get_homedir(void)
32 #ifndef OS_mingw32msvc
38 homedir = getenv ("HOME");
40 * first we call getlogin.
41 * There might be several accounts sharing one uid
48 username = getenv("LOGNAME");
50 username = getlogin();
52 pw = getpwnam( username);
55 /* if we can't getlogin, look up the pwent by uid */
60 /* we might still get no entry */
65 return getenv("HOME");
70 static void get_mcwd_file_name(char *file)
75 mcwd_path = getenv("MCWD");
76 if (mcwd_path == NULL || *mcwd_path == '\0'){
77 homedir= get_homedir();
80 strncpy(file, homedir, MAXPATHLEN-6);
81 file[MAXPATHLEN-6]='\0';
82 strcat( file, "/.mcwd");
84 strncpy(file, mcwd_path, MAXPATHLEN);
85 file[MAXPATHLEN]='\0';
89 void unlink_mcwd(void)
91 char file[MAXPATHLEN+1];
92 get_mcwd_file_name(file);
96 FILE *open_mcwd(const char *mode)
99 char file[MAXPATHLEN+1];
102 get_mcwd_file_name(file);
104 if (MT_STAT(file, &sbuf) < 0)
107 * Ignore the info, if the file is more than 6 hours old
110 if (now - sbuf.st_mtime > 6 * 60 * 60) {
112 "Warning: \"%s\" is out of date, removing it\n",
119 return fopen(file, mode);
124 void *safe_malloc(size_t size)
136 void print_sector(const char *message, unsigned char *data, int size)
141 printf("%s:\n", message);
143 for(row = 0; row * 16 < size; row++){
144 printf("%03x ", row * 16);
145 for(col = 0; col < 16; col++)
146 printf("%02x ", data [row*16+col]);
147 for(col = 0; col < 16; col++) {
148 if(isprint(data [row*16+col]))
149 printf("%c", data [row*16+col]);
157 #if (SIZEOF_TIME_T > SIZEOF_LONG) && defined (HAVE_STRTOLL)
158 # define STRTOTIME strtoll
160 # define STRTOTIME strtol
163 time_t getTimeNow(time_t *now)
165 static int haveTime = 0;
166 static time_t sharedNow;
169 const char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
170 if (source_date_epoch) {
173 STRTOTIME(source_date_epoch, &endptr, 10);
176 if (endptr == source_date_epoch)
178 "SOURCE_DATE_EPOCH \"%s\" invalid\n",
182 "SOURCE_DATE_EPOCH: strtoll: %s: %s\n",
183 strerror(errno), source_date_epoch);
184 else if (*endptr != '\0')
186 "SOURCE_DATE_EPOCH has trailing garbage \"%s\"\n",
204 /* Convert a string to an offset. The string should be a number,
205 optionally followed by S (sectors), K (K-Bytes), M (Megabytes), G
207 off_t str_to_offset_with_end(const char *str, char **endp) {
212 ofs = strtol(str, endp, 0);
214 /* trailing char, see if it is a size specifier */
215 if (s == 's' || s == 'S') /* sector */
217 else if (s == 'k' || s == 'K') /* kb */
219 else if (s == 'm' || s == 'M') /* Mb */
221 else if (s == 'g' || s == 'G') /* Gb */
224 return ofs; /* invalid character */
229 /* Convert a string to a size. The string should be a number,
230 optionally followed by S (sectors), K (K-Bytes), M (Megabytes), G
232 mt_off_t str_to_off_with_end(const char *str, char **endp) {
237 siz = strtol(str, endp, 0);
239 /* trailing char, see if it is a size specifier */
240 if (s == 's' || s == 'S') /* sector */
242 else if (s == 'k' || s == 'K') /* kb */
244 else if (s == 'm' || s == 'M') /* Mb */
246 else if (s == 'g' || s == 'G') /* Gb */
249 return siz; /* invalid character */
254 off_t str_to_offset(char *str) {
256 off_t ofs = str_to_offset_with_end(str, &end);
258 return 0; /* invalid or missing offset */
260 return 0; /* extra char, invalid */
273 void myfree(void *ptr)
275 int *size = ((int *) ptr)-1;
277 fprintf(stderr, "freeing %d bytes at %p total allocated=%d\n",
282 void *mymalloc(size_t size)
285 ptr = (int *)malloc(size+sizeof(int));
291 fprintf(stderr, "allocating %d bytes at %p total allocated=%d\n",
296 void *mycalloc(size_t nmemb, size_t size)
298 void *ptr = mymalloc(nmemb * size);
301 memset(ptr, 0, size);
305 void *myrealloc(void *ptr, size_t size)
307 int oldsize = ((int *)ptr) [-1];
308 void *new = mymalloc(size);
311 memcpy(new, ptr, oldsize);
316 char *mystrdup(char *src)
319 dest = mymalloc(strlen(src)+1);