| 416 | } |
| 417 | |
| 418 | static int compareFileMem(const char *filename, const char *mem, int size) { |
| 419 | int res; |
| 420 | int fd; |
| 421 | char bytes[4096]; |
| 422 | int idx = 0; |
| 423 | struct stat info; |
| 424 | |
| 425 | if (update_results) { |
| 426 | if (size == 0) { |
| 427 | unlink(filename); |
| 428 | return(0); |
| 429 | } |
| 430 | fd = open(filename, WR_FLAGS, 0644); |
| 431 | if (fd < 0) { |
| 432 | fprintf(stderr, "failed to open %s for writing", filename); |
| 433 | return(-1); |
| 434 | } |
| 435 | res = write(fd, mem, size); |
| 436 | close(fd); |
| 437 | return(res != size); |
| 438 | } |
| 439 | |
| 440 | if (stat(filename, &info) < 0) { |
| 441 | if (size == 0) |
| 442 | return(0); |
| 443 | fprintf(stderr, "failed to stat %s\n", filename); |
| 444 | return(-1); |
| 445 | } |
| 446 | if (info.st_size != size) { |
| 447 | fprintf(stderr, "file %s is %ld bytes, result is %d bytes\n", |
| 448 | filename, (long) info.st_size, size); |
| 449 | return(-1); |
| 450 | } |
| 451 | fd = open(filename, RD_FLAGS); |
| 452 | if (fd < 0) { |
| 453 | fprintf(stderr, "failed to open %s for reading", filename); |
| 454 | return(-1); |
| 455 | } |
| 456 | while (idx < size) { |
| 457 | res = read(fd, bytes, 4096); |
| 458 | if (res <= 0) |
| 459 | break; |
| 460 | if (res + idx > size) |
| 461 | break; |
| 462 | if (memcmp(bytes, &mem[idx], res) != 0) { |
| 463 | int ix; |
| 464 | for (ix=0; ix<res; ix++) |
| 465 | if (bytes[ix] != mem[idx+ix]) |
| 466 | break; |
| 467 | fprintf(stderr,"Compare error at position %d\n", idx+ix); |
| 468 | close(fd); |
| 469 | return(1); |
| 470 | } |
| 471 | idx += res; |
| 472 | } |
| 473 | close(fd); |
| 474 | if (idx != size) { |
| 475 | fprintf(stderr,"Compare error index %d, size %d\n", idx, size); |
no test coverage detected