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]);
160 time_t getTimeNow(time_t *now)
162 static int haveTime = 0;
163 static time_t sharedNow;
166 const char *source_date_epoch = getenv("SOURCE_DATE_EPOCH");
167 if (source_date_epoch) {
169 unsigned long long epoch =
170 strtoll(source_date_epoch, &endptr, 10);
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))
178 "SOURCE_DATE_EPOCH: strtoll: %s: %llu\n",
179 strerror(errno), epoch);
180 else if (*endptr != '\0')
182 "SOURCE_DATE_EPOCH has trailing garbage\n");
183 else if (epoch > ULONG_MAX)
185 "SOURCE_DATE_EPOCH must be <= %lu but saw: %llu\n",
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
206 off_t str_to_offset(char *str) {
207 char s, *endp = NULL;
210 ofs = strtol(str, &endp, 0);
212 return 0; /* invalid or missing offset */
214 if (s) { /* 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 0; /* invalid character */
226 return 0; /* extra char, invalid */
238 void myfree(void *ptr)
240 int *size = ((int *) ptr)-1;
242 fprintf(stderr, "freeing %d bytes at %p total alloced=%d\n",
247 void *mymalloc(size_t size)
250 ptr = (int *)malloc(size+sizeof(int));
256 fprintf(stderr, "allocating %d bytes at %p total allocated=%d\n",
261 void *mycalloc(size_t nmemb, size_t size)
263 void *ptr = mymalloc(nmemb * size);
266 memset(ptr, 0, size);
270 void *myrealloc(void *ptr, size_t size)
272 int oldsize = ((int *)ptr) [-1];
273 void *new = mymalloc(size);
276 memcpy(new, ptr, oldsize);
281 char *mystrdup(char *src)
284 dest = mymalloc(strlen(src)+1);