1
0

diff.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Loco program runner core
  3. * Copyright (C) 2011 Lodevil(Du Jiong)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "diff.h"
  19. #include <sys/mman.h>
  20. int equalStr(const char *s, const char *s2) {
  21. while (*s && *s2) {
  22. if (*s++ != *s2++) {
  23. return 1;
  24. }
  25. }
  26. return 0;
  27. }
  28. #define RETURN(rst) {*result = rst;return 0;}
  29. int checkDiff(int rightout_fd, int userout_fd, int *result) {
  30. char *userout, *rightout;
  31. const char *cuser, *cright, *end_user, *end_right;
  32. off_t userout_len, rightout_len;
  33. userout_len = lseek(userout_fd, 0, SEEK_END);
  34. rightout_len = lseek(rightout_fd, 0, SEEK_END);
  35. if (userout_len == -1 || rightout_len == -1)
  36. RAISE1("lseek failure");
  37. if (userout_len >= MAX_OUTPUT)
  38. RETURN(OLE);
  39. lseek(userout_fd, 0, SEEK_SET);
  40. lseek(rightout_fd, 0, SEEK_SET);
  41. if ((userout_len && rightout_len) == 0) {
  42. if (userout_len || rightout_len)
  43. RETURN(WA)
  44. else
  45. RETURN(AC)
  46. }
  47. if ((userout = (char*) mmap(NULL, userout_len, PROT_READ | PROT_WRITE,
  48. MAP_PRIVATE, userout_fd, 0)) == MAP_FAILED) {
  49. RAISE1("mmap userout filure");
  50. }
  51. if ((rightout = (char*) mmap(NULL, rightout_len, PROT_READ | PROT_WRITE,
  52. MAP_PRIVATE, rightout_fd, 0)) == MAP_FAILED) {
  53. munmap(userout, userout_len);
  54. RAISE1("mmap right filure");
  55. }
  56. if ((userout_len == rightout_len) && equalStr(userout, rightout) == 0) {
  57. munmap(userout, userout_len);
  58. munmap(rightout, rightout_len);
  59. RETURN(AC);
  60. }
  61. cuser = userout;
  62. cright = rightout;
  63. end_user = userout + userout_len;
  64. end_right = rightout + rightout_len;
  65. while ((cuser < end_user) && (cright < end_right)) {
  66. while ((cuser < end_user)
  67. && (*cuser == ' ' || *cuser == '\n' || *cuser == '\r'
  68. || *cuser == '\t'))
  69. cuser++;
  70. while ((cright < end_right)
  71. && (*cright == ' ' || *cright == '\n' || *cright == '\r'
  72. || *cright == '\t'))
  73. cright++;
  74. if (cuser == end_user || cright == end_right)
  75. break;
  76. if (*cuser != *cright)
  77. break;
  78. cuser++;
  79. cright++;
  80. }
  81. while ((cuser < end_user)
  82. && (*cuser == ' ' || *cuser == '\n' || *cuser == '\r'
  83. || *cuser == '\t'))
  84. cuser++;
  85. while ((cright < end_right)
  86. && (*cright == ' ' || *cright == '\n' || *cright == '\r'
  87. || *cright == '\t'))
  88. cright++;
  89. if (cuser == end_user && cright == end_right) {
  90. munmap(userout, userout_len);
  91. munmap(rightout, rightout_len);
  92. RETURN(PE);
  93. }
  94. munmap(userout, userout_len);
  95. munmap(rightout, rightout_len);
  96. RETURN(WA);
  97. }