crlf.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 1993 W. Richard Stevens. All rights reserved.
  3. * Permission to use or modify this software and its documentation only for
  4. * educational purposes and without fee is hereby granted, provided that
  5. * the above copyright notice appear in all copies. The author makes no
  6. * representations about the suitability of this software for any purpose.
  7. * It is provided "as is" without express or implied warranty.
  8. */
  9. #include "sock.h"
  10. /* Convert newline to return/newline. */
  11. int
  12. crlf_add(char *dst, int dstsize, const char *src, int lenin)
  13. {
  14. int lenout;
  15. char c;
  16. if ( (lenout = lenin) > dstsize)
  17. err_quit("crlf_add: destination not big enough");
  18. for ( ; lenin > 0; lenin--) {
  19. if ( (c = *src++) == '\n') {
  20. if (++lenout >= dstsize)
  21. err_quit("crlf_add: destination not big enough");
  22. *dst++ = '\r';
  23. }
  24. *dst++ = c;
  25. }
  26. return(lenout);
  27. }
  28. int
  29. crlf_strip(char *dst, int dstsize, const char *src, int lenin)
  30. {
  31. int lenout;
  32. char c;
  33. for (lenout = 0; lenin > 0; lenin--) {
  34. if ( (c = *src++) != '\r') {
  35. if (++lenout >= dstsize)
  36. err_quit("crlf_strip: destination not big enough");
  37. *dst++ = c;
  38. }
  39. }
  40. return(lenout);
  41. }