aboutsummaryrefslogtreecommitdiff
blob: 4ebb61c50d70dded549f081535f7fbb94dd77a28 (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
/*
 * string.c
 *
 * Miscellaneous macro's and functions.
 *
 * Copyright 1999-2008 Gentoo Foundation
 * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
 * Licensed under the GPL-2
 */

#include "headers.h"
#include "rcscripts/rcutil.h"

char *
rc_memrepchr (char **str, char old, char new, size_t size)
{
  char *str_p;

  if (!check_arg_strv (str))
    return NULL;

  str_p = memchr (*str, old, size);

  while (NULL != str_p)
    {
      str_p[0] = new;
      str_p = memchr (&str_p[1], old, size - (str_p - *str) - 1);
    }

  return *str;
}

char *
rc_strcatpaths (const char *pathname1, const char *pathname2)
{
  char *new_path = NULL;
  int lenght;

  if ((!check_arg_str (pathname1)) || (!check_arg_str (pathname2)))
    return 0;

  /* Lenght of pathname1 + lenght of pathname2 + '/' if needed */
  lenght = strlen (pathname1) + strlen (pathname2) + 2;
  /* lenght + '\0' */
  new_path = xmalloc (lenght);
  if (NULL == new_path)
    return NULL;

  snprintf (new_path, lenght, "%s%s%s", pathname1,
	    (pathname1[strlen (pathname1) - 1] != '/') ? "/" : "",
	    pathname2);

  return new_path;
}

char *
rc_strndup (const char *str, size_t size)
{
  char *new_str = NULL;
  size_t len;

  /* We cannot check if its a valid string here, as it might
   * not be '\0' terminated ... */
  if (!check_arg_ptr (str))
    return NULL;

  /* Check lenght of str without breaching the size limit */
  for (len = 0; (len < size) && ('\0' != str[len]); len++);

  new_str = xmalloc (len + 1);
  if (NULL == new_str)
    return NULL;

  /* Make sure our string is NULL terminated */
  new_str[len] = '\0';

  return (char *) memcpy (new_str, str, len);
}

char *
rc_basename (const char *path)
{
  char *new_path = NULL;

  if (!check_arg_str (path))
    return NULL;

  /* Copied from glibc */
  new_path = strrchr (path, '/');
  return new_path ? new_path + 1 : (char *) path;
}