testbuffer.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of the License "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description:
  15. * This program reads from stdin into a "buffer" structure. It is designed to be
  16. * run from within valgrind to detect memory corruption errors.
  17. * The buffer is then written to /tmp/outfile where it can be compared
  18. * with the input to determine if they are the same
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "buffer.h"
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #define OSIZE 40
  28. #define ISIZEMAX 2049
  29. int main(int argc, char *argv[])
  30. {
  31. int nbytes = 0;
  32. byteblock *bb=NULL;
  33. buffer *b;
  34. char *pointertospace = NULL;
  35. int iterator = 0;
  36. int ofile;
  37. unsigned int space=51;
  38. b = buffer_new();
  39. do {
  40. // space %= 5;
  41. // space++;
  42. pointertospace = buffer_makespace(b, space);
  43. if (!pointertospace)
  44. exit(1);
  45. nbytes = read(STDIN_FILENO, pointertospace, space);
  46. if (nbytes == -1)
  47. break;
  48. buffer_usespace(b, nbytes);
  49. }
  50. while (nbytes == space);
  51. iterator = 0;
  52. ofile = open("/tmp/outfile", O_CREAT | O_WRONLY, 00777);
  53. if (ofile <= 0)
  54. {
  55. perror("error");
  56. return 1;
  57. }
  58. while ((bb = buffer_getbytes(b, &iterator)))
  59. {
  60. write(ofile, &bb->byte0, bb->fill);
  61. }
  62. close(ofile);
  63. buffer_free(&b);
  64. return 0;
  65. }