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