| 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 |
#include <stdio.h> |
| •26 |
#include <stdlib.h> |
| 27 |
#include <ctype.h> |
| 28 |
#include <string.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <sys/types.h> |
| •31 |
#include <sys/stat.h> |
| 32 |
#include <sys/param.h> |
| •33 |
#include <time.h> |
| 34 |
#include <netdb.h> |
| 35 |
#include <fcntl.h> |
| •36 |
#include <unistd.h> |
| 37 |
#include "xmalloc.h" |
| 38 |
#include "common.h" |
| 39 |
#include "part.h" |
| 40 |
|
| •41 |
extern void warn(char *s); |
| 42 |
|
| 43 |
#ifndef MAXHOSTNAMELEN |
| 44 |
#define MAXHOSTNAMELEN 64 |
| 45 |
#endif |
| 46 |
|
| •47 |
#ifndef errno |
| 48 |
extern int errno; |
| •49 |
#endif |
| 50 |
|
| 51 |
int overwrite_files = 0; |
| 52 |
int didchat; |
| •53 |
char replacement_char = 'X'; |
| 54 |
|
| 55 |
|
| 56 |
static char *output_fname = 0; |
| 57 |
|
| 58 |
|
| 59 |
#define BADCHARS "!$&*()|\'\";<>[]{}?/`\\ \t" |
| 60 |
|
| 61 |
|
| 62 |
char *os_genid(void) |
| 63 |
{ |
| 64 |
static int pid = 0; |
| 65 |
static time_t curtime; |
| 66 |
static char hostname[MAXHOSTNAMELEN+1]; |
| 67 |
char *result; |
| 68 |
struct hostent *hp; |
| 69 |
|
| 70 |
|
| 71 |
if (pid == 0) { |
| 72 |
pid = getpid(); |
| 73 |
time(&curtime); |
| 74 |
gethostname(hostname, sizeof(hostname)); |
| 75 |
|
| 76 |
|
| 77 |
if (!strchr(hostname, '.')) { |
| 78 |
hp = gethostbyname(hostname); |
| 79 |
if (hp) { |
| 80 |
strcpy(hostname, hp->h_name); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
result = malloc(25+strlen(hostname)); |
| •86 |
sprintf(result, "%d.%lu@%s", pid, (unsigned long) curtime++, hostname); |
| 87 |
return result; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
char *os_idtodir(char *id) |
| 92 |
{ |
| 93 |
static char buf[4096]; |
| 94 |
char *p; |
| 95 |
|
| 96 |
if (getenv("TMPDIR")) { |
| 97 |
strcpy(buf, getenv("TMPDIR")); |
| 98 |
} |
| 99 |
else { |
| •100 |
strcpy(buf, "/tmp"); |
| 101 |
} |
| 102 |
strcat(buf, "/m-prts-"); |
| 103 |
p = getenv("USER"); |
| 104 |
if (!p) p = getenv("LOGNAME"); |
| 105 |
if (!p) p = "x"; |
| 106 |
strcat(buf, p); |
| 107 |
|
| 108 |
(void)mkdir(buf, 0700); |
| 109 |
|
| 110 |
p = buf + strlen(buf); |
| 111 |
*p++ = '/'; |
| 112 |
while (*id && p < buf+sizeof(buf)-10 ) { |
| 113 |
if (isprint(*id) && !strchr(BADCHARS, *id)) *p++ = *id; |
| 114 |
id++; |
| 115 |
} |
| 116 |
*p = '\0'; |
| 117 |
if (mkdir(buf, 0700) == -1 && errno != EEXIST) { |
| 118 |
perror(buf); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
*p++ = '/'; |
| 122 |
*p = '\0'; |
| 123 |
return buf; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
void os_donewithdir(char *dir) |
| 131 |
{ |
| 132 |
char *p; |
| 133 |
|
| 134 |
|
| 135 |
p = dir + strlen(dir) - 1; |
| 136 |
*p = '\0'; |
| 137 |
|
| 138 |
rmdir(dir); |
| 139 |
} |
| 140 |
|
| 141 |
FILE *os_createnewfile(char *fname) |
| 142 |
{ |
| 143 |
int fd; |
| 144 |
FILE *ret; |
| 145 |
|
| 146 |
#ifdef O_EXCL |
| •147 |
struct stat statbuf; |
| 148 |
|
| 149 |
if ((stat(fname, &statbuf) == 0) && (S_ISCHR(statbuf.st_mode))) { |
| 150 |
fd=open(fname, O_RDWR); |
| 151 |
} |
| 152 |
else { |
| •153 |
fd=open(fname, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 154 |
} |
| 155 |
#else |
| •156 |
fd=open(fname, O_RDWR|O_CREAT|O_TRUNC, 0600); |
| 157 |
#endif |
| 158 |
|
| 159 |
if (fd == -1) |
| 160 |
return NULL; |
| 161 |
|
| 162 |
ret=fdopen(fd, "w"); |
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
FILE *os_newtypedfile(char *fname, char *contentType, int flags, params contentParams) |
| 175 |
{ |
| 176 |
char *p; |
| 177 |
static int filesuffix=0; |
| 178 |
char buf[128], *descfname=0; |
| 179 |
FILE *outfile = 0; |
| 180 |
|
| 181 |
if (!fname) fname = ""; |
| 182 |
|
| 183 |
|
| 184 |
if (*fname == '/') { |
| 185 |
p = strrchr(fname, '/'); |
| 186 |
fname = p+1; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
while (*fname == '~' || *fname == '/') fname++; |
| 191 |
|
| 192 |
while (!strncmp(fname, "../", 3)) fname += 3; |
| 193 |
|
| 194 |
|
| 195 |
for (p=fname; *p; p++) { |
| 196 |
if (*p == '/') { |
| 197 |
if (!strncmp(p, "/../", 4)) { |
| •198 |
p[1] = p[2] = replacement_char; |
| 199 |
} |
| 200 |
*p = '\0'; |
| 201 |
(void) mkdir(fname, 0777); |
| 202 |
*p = '/'; |
| 203 |
} |
| •204 |
else if (!isprint(*p) || strchr(BADCHARS, *p)) *p = replacement_char; |
| 205 |
} |
| 206 |
|
| 207 |
if (!fname[0]) { |
| 208 |
do { |
| 209 |
if (outfile) fclose(outfile); |
| 210 |
sprintf(buf, "part%d", ++filesuffix); |
| •211 |
} while ((outfile = fopen(buf, "r"))); |
| 212 |
fname = buf; |
| 213 |
} |
| 214 |
else if (!overwrite_files && (outfile = fopen(fname, "r"))) { |
| 215 |
do { |
| 216 |
fclose(outfile); |
| 217 |
sprintf(buf, "%s.%d", fname, ++filesuffix); |
| 218 |
|
| •219 |
} while ((outfile = fopen(buf, "r"))); |
| 220 |
fname = buf; |
| 221 |
} |
| 222 |
|
| 223 |
if (overwrite_files) |
| 224 |
outfile = fopen(fname, "w"); |
| 225 |
else |
| 226 |
outfile = os_createnewfile(fname); |
| 227 |
|
| 228 |
if (!outfile) { |
| 229 |
perror(fname); |
| 230 |
} |
| 231 |
|
| 232 |
if (output_fname) free(output_fname); |
| 233 |
output_fname = strsave(fname); |
| 234 |
|
| 235 |
if (strlen(fname) > sizeof(buf)-6) { |
| 236 |
descfname = xmalloc(strlen(fname)+6); |
| 237 |
} |
| 238 |
else { |
| 239 |
descfname = buf; |
| 240 |
} |
| 241 |
strcpy(descfname, fname); |
| 242 |
|
| 243 |
p = strchr(descfname, '/'); |
| 244 |
if (!p) p = descfname; |
| •245 |
if ((p = strrchr(p, '.'))) *p = '\0'; |
| 246 |
|
| 247 |
strcat(descfname, ".desc"); |
| 248 |
(void) rename(TEMPFILENAME, descfname); |
| 249 |
if (descfname != buf) free(descfname); |
| 250 |
|
| 251 |
fprintf(stdout, "%s (%s)\n", output_fname, contentType); |
| 252 |
didchat = 1; |
| 253 |
|
| 254 |
return outfile; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
void os_closetypedfile(FILE *outfile) |
| 261 |
{ |
| 262 |
fclose(outfile); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
int |
| 269 |
os_binhex(struct part *inpart, int part, int nparts) |
| 270 |
{ |
| 271 |
return 1; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
void os_warnMD5mismatch(void) |
| 279 |
{ |
| 280 |
char *warning; |
| 281 |
|
| 282 |
warning = xmalloc(strlen(output_fname) + 100); |
| 283 |
sprintf(warning, "%s was corrupted in transit", |
| 284 |
output_fname); |
| 285 |
warn(warning); |
| 286 |
free(warning); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
void os_perror(char *file) |
| 293 |
{ |
| 294 |
perror(file); |
| 295 |
} |