summaryrefslogtreecommitdiffhomepage
path: root/src/text.c
blob: 1536eac812fe91c2dc5512b59a2c4908b66f8d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* tinyproxy - A fast light-weight HTTP proxy
 * Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* The functions included here are useful for text manipulation.  They
 * replace or augment the standard C string library.  These functions
 * are either safer replacements, or they provide services not included
 * with the standard C string library.
 */

#include "main.h"

#include "text.h"

#ifndef HAVE_STRLCPY
/*
 * Function API taken from OpenBSD. Like strncpy(), but does not 0 fill the
 * buffer, and always NULL terminates the buffer. size is the size of the
 * destination buffer.
 */
size_t
strlcpy (char *dst, const char *src, size_t size)
{
  size_t len = strlen (src);
  size_t ret = len;

  if (len >= size)
    len = size - 1;

  memcpy (dst, src, len);
  dst[len] = '\0';

  return ret;
}
#endif

#ifndef HAVE_STRLCAT
/*
 * Function API taken from OpenBSD. Like strncat(), but does not 0 fill the
 * buffer, and always NULL terminates the buffer. size is the length of the
 * buffer, which should be one more than the maximum resulting string
 * length.
 */
size_t
strlcat (char *dst, const char *src, size_t size)
{
  size_t len1 = strlen (dst);
  size_t len2 = strlen (src);
  size_t ret = len1 + len2;

  if (len1 + len2 >= size)
    len2 = size - len1 - 1;
  if (len2 > 0)
    {
      memcpy (dst + len1, src, len2);
      dst[len1 + len2] = '\0';
    }

  return ret;
}
#endif

/*
 * Removes any new-line or carriage-return characters from the end of the
 * string. This function is named after the same function in Perl.
 * "length" should be the number of characters in the buffer, not including
 * the trailing NULL.
 *
 * Returns the number of characters removed from the end of the string.  A
 * negative return value indicates an error.
 */
ssize_t
chomp (char *buffer, size_t length)
{
  size_t chars;

  assert (buffer != NULL);
  assert (length > 0);

  /* Make sure the arguments are valid */
  if (buffer == NULL)
    return -EFAULT;
  if (length < 1)
    return -ERANGE;

  chars = 0;

  --length;
  while (buffer[length] == '\r' || buffer[length] == '\n')
    {
      buffer[length] = '\0';
      chars++;

      /* Stop once we get to zero to prevent wrap-around */
      if (length-- == 0)
        break;
    }

  return chars;
}