Blame string/test-stpncpy.c

Packit 6c4009
/* Test and measure stpncpy functions.
Packit 6c4009
   Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Written by Jakub Jelinek <jakub@redhat.com>, 1999.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#define STRNCPY_RESULT(dst, len, n) ((dst) + ((len) > (n) ? (n) : (len)))
Packit 6c4009
#define TEST_MAIN
Packit 6c4009
#ifndef WIDE
Packit 6c4009
# define TEST_NAME "stpncpy"
Packit 6c4009
#else
Packit 6c4009
# define TEST_NAME "wcpncpy"
Packit 6c4009
#endif /* WIDE */
Packit 6c4009
#include "test-string.h"
Packit 6c4009
#ifndef WIDE
Packit 6c4009
# define CHAR char
Packit 6c4009
# define SIMPLE_STPNCPY simple_stpncpy
Packit 6c4009
# define STUPID_STPNCPY stupid_stpncpy
Packit 6c4009
# define STPNCPY stpncpy
Packit 6c4009
# define STRNLEN strnlen
Packit 6c4009
#else
Packit 6c4009
# include <wchar.h>
Packit 6c4009
# define CHAR wchar_t
Packit 6c4009
# define SIMPLE_STPNCPY simple_wcpncpy
Packit 6c4009
# define STUPID_STPNCPY stupid_wcpncpy
Packit 6c4009
# define STPNCPY wcpncpy
Packit 6c4009
# define STRNLEN wcsnlen
Packit 6c4009
#endif /* WIDE */
Packit 6c4009
Packit 6c4009
CHAR *SIMPLE_STPNCPY (CHAR *, const CHAR *, size_t);
Packit 6c4009
CHAR *STUPID_STPNCPY (CHAR *, const CHAR *, size_t);
Packit 6c4009
Packit 6c4009
IMPL (STUPID_STPNCPY, 0)
Packit 6c4009
IMPL (SIMPLE_STPNCPY, 0)
Packit 6c4009
IMPL (STPNCPY, 1)
Packit 6c4009
Packit 6c4009
CHAR *
Packit 6c4009
SIMPLE_STPNCPY (CHAR *dst, const CHAR *src, size_t n)
Packit 6c4009
{
Packit 6c4009
  while (n--)
Packit 6c4009
    if ((*dst++ = *src++) == '\0')
Packit 6c4009
      {
Packit 6c4009
	size_t i;
Packit 6c4009
Packit 6c4009
	for (i = 0; i < n; ++i)
Packit 6c4009
	  dst[i] = '\0';
Packit 6c4009
	return dst - 1;
Packit 6c4009
      }
Packit 6c4009
  return dst;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
CHAR *
Packit 6c4009
STUPID_STPNCPY (CHAR *dst, const CHAR *src, size_t n)
Packit 6c4009
{
Packit 6c4009
  size_t nc = STRNLEN (src, n);
Packit 6c4009
  size_t i;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < nc; ++i)
Packit 6c4009
    dst[i] = src[i];
Packit 6c4009
  for (; i < n; ++i)
Packit 6c4009
    dst[i] = '\0';
Packit 6c4009
  return dst + nc;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#undef CHAR
Packit 6c4009
#include "test-strncpy.c"