getline.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* $NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp $ */
  2. /*-
  3. * Copyright (c) 2011 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Christos Zoulas.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "file.h"
  31. #if !HAVE_GETLINE
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. public ssize_t
  38. getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
  39. {
  40. char *ptr, *eptr;
  41. if (*buf == NULL || *bufsiz == 0) {
  42. *bufsiz = BUFSIZ;
  43. if ((*buf = malloc(*bufsiz)) == NULL)
  44. return -1;
  45. }
  46. for (ptr = *buf, eptr = *buf + *bufsiz;;) {
  47. int c = fgetc(fp);
  48. if (c == -1) {
  49. if (feof(fp)) {
  50. ssize_t diff = (ssize_t)(ptr - *buf);
  51. if (diff != 0) {
  52. *ptr = '\0';
  53. return diff;
  54. }
  55. }
  56. return -1;
  57. }
  58. *ptr++ = c;
  59. if (c == delimiter) {
  60. *ptr = '\0';
  61. return ptr - *buf;
  62. }
  63. if (ptr + 2 >= eptr) {
  64. char *nbuf;
  65. size_t nbufsiz = *bufsiz * 2;
  66. ssize_t d = ptr - *buf;
  67. if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
  68. return -1;
  69. *buf = nbuf;
  70. *bufsiz = nbufsiz;
  71. eptr = nbuf + nbufsiz;
  72. ptr = nbuf + d;
  73. }
  74. }
  75. }
  76. public ssize_t
  77. getline(char **buf, size_t *bufsiz, FILE *fp)
  78. {
  79. return getdelim(buf, bufsiz, '\n', fp);
  80. }
  81. #endif
  82. #ifdef TEST
  83. int
  84. main(int argc, char *argv[])
  85. {
  86. char *p = NULL;
  87. ssize_t len;
  88. size_t n = 0;
  89. while ((len = getline(&p, &n, stdin)) != -1)
  90. (void)printf("%" SIZE_T_FORMAT "d %s", len, p);
  91. free(p);
  92. return 0;
  93. }
  94. #endif