test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) Christos Zoulas 2003.
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice immediately at the beginning of the file, without modification,
  10. * this list of conditions, and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "magic.h"
  31. static void *
  32. xrealloc(void *p, size_t n)
  33. {
  34. p = realloc(p, n);
  35. if (p == NULL) {
  36. (void)fprintf(stderr, "ERROR slurping file: out of memory\n");
  37. exit(10);
  38. }
  39. return p;
  40. }
  41. static char *
  42. slurp(FILE *fp, size_t *final_len)
  43. {
  44. size_t len = 256;
  45. int c;
  46. char *l = (char *)xrealloc(NULL, len), *s = l;
  47. for (c = getc(fp); c != EOF; c = getc(fp)) {
  48. if (s == l + len) {
  49. l = (char *)xrealloc(l, len * 2);
  50. len *= 2;
  51. }
  52. *s++ = c;
  53. }
  54. if (s == l + len)
  55. l = (char *)xrealloc(l, len + 1);
  56. *s++ = '\0';
  57. *final_len = s - l;
  58. l = (char *)xrealloc(l, s - l);
  59. return l;
  60. }
  61. int
  62. main(int argc, char **argv)
  63. {
  64. struct magic_set *ms;
  65. const char *result;
  66. char *desired;
  67. size_t desired_len;
  68. int i;
  69. FILE *fp;
  70. ms = magic_open(MAGIC_NONE);
  71. if (ms == NULL) {
  72. (void)fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory\n");
  73. return 10;
  74. }
  75. if (magic_load(ms, NULL) == -1) {
  76. (void)fprintf(stderr, "ERROR loading with NULL file: %s\n", magic_error(ms));
  77. return 11;
  78. }
  79. if (argc > 1) {
  80. if (argc != 3) {
  81. (void)fprintf(stderr, "Usage: test TEST-FILE RESULT\n");
  82. } else {
  83. if ((result = magic_file(ms, argv[1])) == NULL) {
  84. (void)fprintf(stderr, "ERROR loading file %s: %s\n", argv[1], magic_error(ms));
  85. return 12;
  86. } else {
  87. fp = fopen(argv[2], "r");
  88. if (fp == NULL) {
  89. (void)fprintf(stderr, "ERROR opening `%s': ", argv[2]);
  90. perror(NULL);
  91. return 13;
  92. }
  93. desired = slurp(fp, &desired_len);
  94. fclose(fp);
  95. (void)printf("%s: %s\n", argv[1], result);
  96. if (strcmp(result, desired) != 0) {
  97. (void)fprintf(stderr, "Error: result was\n%s\nexpected:\n%s\n", result, desired);
  98. return 1;
  99. }
  100. }
  101. }
  102. }
  103. magic_close(ms);
  104. return 0;
  105. }