| 1 |
/* |
| 2 |
* (c) 2002 Thomas Osterried DL9SAU <[email protected]> |
| 3 |
* License: GPL. See http://www.fsf.org/ |
| 4 |
* Sources: http://x-berg.in-berlin.de/cgi-bin/viewcvs.cgi/ampr/axgetput/ |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "includes.h" |
| 8 |
|
| 9 |
#include "axgetput.h" |
| 10 |
#include "util.h" |
| 11 |
|
| 12 |
/* Use private function because some platforms are broken, eg 386BSD */ |
| 13 |
|
| 14 |
static int Xtolower(int c) |
| 15 |
{ |
| 16 |
return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c; |
| 17 |
} |
| 18 |
|
| 19 |
/*---------------------------------------------------------------------------*/ |
| 20 |
|
| 21 |
char *strlwc(char *s) |
| 22 |
{ |
| 23 |
char *p; |
| 24 |
|
| 25 |
for (p = s; (*p = Xtolower(*p)); p++) ; |
| 26 |
return s; |
| 27 |
} |
| 28 |
|
| 29 |
/*---------------------------------------------------------------------------*/ |
| 30 |
|
| 31 |
char *strtrim(char *s) |
| 32 |
{ |
| 33 |
char *p; |
| 34 |
|
| 35 |
for (p = s; *p; p++) ; |
| 36 |
while (--p >= s && isspace(*p & 0xff)) ; |
| 37 |
p[1] = 0; |
| 38 |
return s; |
| 39 |
} |
| 40 |
|
| 41 |
/*---------------------------------------------------------------------------*/ |
| 42 |
|
| 43 |
int my_read(int fd, char *s, int len_max, int *eof, char *p_break) |
| 44 |
{ |
| 45 |
|
| 46 |
struct timeval timeout; |
| 47 |
fd_set errfds; |
| 48 |
int len_got; |
| 49 |
|
| 50 |
*eof = 0; |
| 51 |
|
| 52 |
for (len_got = 0; len_got < len_max; ) { |
| 53 |
int len; |
| 54 |
char *s_curr = s + len_got; |
| 55 |
|
| 56 |
len = read(fd, s_curr, (p_break ? 1 : len_max)); |
| 57 |
if (len < 1) { |
| 58 |
if (len == -1 && errno == EAGAIN) { |
| 59 |
sleep(10); |
| 60 |
continue; |
| 61 |
} |
| 62 |
*eof = 1; |
| 63 |
/* |
| 64 |
* len = 0: normal eof. if we're looking for a string, return -1 since |
| 65 |
* we have'nt found |
| 66 |
*/ |
| 67 |
return len == 0 && p_break ? -1 : (len_got ? len_got : len); |
| 68 |
} |
| 69 |
len_got += len; |
| 70 |
|
| 71 |
/* read once? or char in p_break found? */ |
| 72 |
if (!p_break || strchr(p_break, *s_curr)) |
| 73 |
break; |
| 74 |
} |
| 75 |
timeout.tv_sec = 0; |
| 76 |
timeout.tv_usec = 0; |
| 77 |
FD_ZERO(&errfds); |
| 78 |
FD_SET(fdout, &errfds); |
| 79 |
if (select(fd+1, NULL, NULL, &errfds, &timeout) && FD_ISSET(fd, &errfds)) |
| 80 |
*eof = 1; |
| 81 |
|
| 82 |
return len_got; |
| 83 |
} |
| 84 |
|
| 85 |
/*---------------------------------------------------------------------------*/ |
| 86 |
|
| 87 |
int secure_write(int fd, char *s, int len_write) { |
| 88 |
int len_write_left_curr = len_write; |
| 89 |
|
| 90 |
while (len_write_left_curr) { |
| 91 |
int len; |
| 92 |
len = write(fd, s, len_write_left_curr); |
| 93 |
if (len < 0) { |
| 94 |
if (len == -1 && errno == EAGAIN) { |
| 95 |
sleep(10); |
| 96 |
continue; |
| 97 |
} |
| 98 |
return -1; |
| 99 |
} |
| 100 |
if (len != len_write_left_curr) { |
| 101 |
/* queue busy.. */ |
| 102 |
sleep(10); |
| 103 |
} |
| 104 |
s += len; |
| 105 |
len_write_left_curr -= len; |
| 106 |
} |
| 107 |
return len_write; |
| 108 |
} |
| 109 |
|
| 110 |
/*---------------------------------------------------------------------------*/ |
| 111 |
|
| 112 |
char *get_fixed_filename(char *line, long size, unsigned int msg_crc, int generate_filename) { |
| 113 |
|
| 114 |
static char filename[1024]; |
| 115 |
char *p, *q, *r; |
| 116 |
|
| 117 |
*filename = 0; |
| 118 |
|
| 119 |
p = strchr(line, '\"'); |
| 120 |
if (p) { |
| 121 |
p++; |
| 122 |
} |
| 123 |
|
| 124 |
/* security.. */ |
| 125 |
|
| 126 |
q = strrchr((p ? p : line), '/'); |
| 127 |
if (q) { |
| 128 |
p = ++q; |
| 129 |
} |
| 130 |
q = strrchr((p ? p : line), '\\'); |
| 131 |
if (q) { |
| 132 |
p = ++q; |
| 133 |
} |
| 134 |
q = strrchr((p ? p : line), ':'); |
| 135 |
if (q) { |
| 136 |
p = ++q; |
| 137 |
} |
| 138 |
if (!p) { |
| 139 |
p = line; |
| 140 |
} |
| 141 |
while (*p && isspace(*p & 0xff)) |
| 142 |
p++; |
| 143 |
for (r = filename, q = p; *q; q++) { |
| 144 |
if (*q == '"' || *q == ';') { |
| 145 |
break; |
| 146 |
} |
| 147 |
*r++ = *q; |
| 148 |
} |
| 149 |
*r = 0; |
| 150 |
strtrim(filename); |
| 151 |
q = strrchr(filename, '.'); |
| 152 |
if (q) { |
| 153 |
if (!*(q+1)) { |
| 154 |
/* remove trailing dots */ |
| 155 |
*q = 0; |
| 156 |
} else { |
| 157 |
/* make suffix lowercase */ |
| 158 |
strlwc(q); |
| 159 |
} |
| 160 |
} |
| 161 |
if (!*filename && generate_filename) { |
| 162 |
sprintf(filename, "unknown-%ld%d%ld.bin", size, msg_crc, time(NULL)); |
| 163 |
} |
| 164 |
return filename; |
| 165 |
} |
| 166 |
|
| 167 |
/*---------------------------------------------------------------------------*/ |
| 168 |
|
| 169 |
/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */ |
| 170 |
|
| 171 |
/* Linear day numbers of the respective 1sts in non-leap years. */ |
| 172 |
static int day_n[] = { 0,31,59,90,120,151,181,212,243,273,304,334,0,0,0,0 }; |
| 173 |
/* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */ |
| 174 |
|
| 175 |
/*---------------------------------------------------------------------------*/ |
| 176 |
|
| 177 |
long date_dos2unix(unsigned short time,unsigned short date) |
| 178 |
{ |
| 179 |
int month,year; |
| 180 |
long secs; |
| 181 |
|
| 182 |
month = ((date >> 5) & 15)-1; |
| 183 |
year = date >> 9; |
| 184 |
secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400* |
| 185 |
((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 && |
| 186 |
month < 2 ? 1 : 0)+3653); |
| 187 |
/* days since 1.1.70 plus 80's leap day */ |
| 188 |
return secs; |
| 189 |
} |
| 190 |
|
| 191 |
/*---------------------------------------------------------------------------*/ |
| 192 |
|
| 193 |
/* Convert linear UNIX date to a MS-DOS time/date pair. */ |
| 194 |
|
| 195 |
void date_unix2dos(int unix_date,unsigned short *time, unsigned short *date) |
| 196 |
{ |
| 197 |
int day,year,nl_day,month; |
| 198 |
|
| 199 |
*time = (unix_date % 60)/2+(((unix_date/60) % 60) << 5)+ |
| 200 |
(((unix_date/3600) % 24) << 11); |
| 201 |
day = unix_date/86400-3652; |
| 202 |
year = day/365; |
| 203 |
if ((year+3)/4+365*year > day) year--; |
| 204 |
day -= (year+3)/4+365*year; |
| 205 |
if (day == 59 && !(year & 3)) { |
| 206 |
nl_day = day; |
| 207 |
month = 2; |
| 208 |
} |
| 209 |
else { |
| 210 |
nl_day = (year & 3) || day <= 59 ? day : day-1; |
| 211 |
for (month = 0; month < 12; month++) |
| 212 |
if (day_n[month] > nl_day) break; |
| 213 |
} |
| 214 |
*date = nl_day-day_n[month-1]+1+(month << 5)+(year << 9); |
| 215 |
} |