~ubuntu-branches/ubuntu/breezy/gettext/breezy

« back to all changes in this revision

Viewing changes to gettext-tools/lib/stpncpy.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2004-03-14 17:40:02 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040314174002-p1ad5ldve1hqzhye
Tags: 0.14.1-2
* Added libexpat1-dev to Build-Depends, for glade support.
* Added libc0.1-dev to Build-Depends, for GNU/kFreeBSD.
* Removed special-casing of knetbsd-gnu in debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 1993, 1995-1997, 2002 Free Software Foundation, Inc.
 
2
 
 
3
   NOTE: The canonical source of this file is maintained with the GNU C Library.
 
4
   Bugs can be reported to bug-glibc@gnu.org.
 
5
 
 
6
   This program is free software; you can redistribute it and/or modify it
 
7
   under the terms of the GNU General Public License as published by the
 
8
   Free Software Foundation; either version 2, or (at your option) any
 
9
   later version.
 
10
 
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with this program; if not, write to the Free Software
 
18
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 
19
   USA.  */
 
20
 
 
21
/* This is almost copied from strncpy.c, written by Torbjorn Granlund.  */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
# include <config.h>
 
25
#endif
 
26
 
 
27
/* Specification.  */
 
28
#include "stpncpy.h"
 
29
 
 
30
#ifndef __GNU_LIBRARY__
 
31
 
 
32
#ifdef _LIBC
 
33
# include <string.h>
 
34
#else
 
35
# include <sys/types.h>
 
36
/* We cannot generally use the name 'stpncpy' since AIX 4 defines an unusable
 
37
   variant of the function but we cannot use it.  */
 
38
# undef stpncpy
 
39
# define stpncpy gnu_stpncpy
 
40
#endif
 
41
 
 
42
#ifndef weak_alias
 
43
# define __stpncpy stpncpy
 
44
#endif
 
45
 
 
46
/* Copy no more than N characters of SRC to DEST, returning the address of
 
47
   the terminating '\0' in DEST, if any, or else DEST + N.  */
 
48
char *
 
49
__stpncpy (char *dest, const char *src, size_t n)
 
50
{
 
51
  char c;
 
52
  char *s = dest;
 
53
 
 
54
  if (n >= 4)
 
55
    {
 
56
      size_t n4 = n >> 2;
 
57
 
 
58
      for (;;)
 
59
        {
 
60
          c = *src++;
 
61
          *dest++ = c;
 
62
          if (c == '\0')
 
63
            break;
 
64
          c = *src++;
 
65
          *dest++ = c;
 
66
          if (c == '\0')
 
67
            break;
 
68
          c = *src++;
 
69
          *dest++ = c;
 
70
          if (c == '\0')
 
71
            break;
 
72
          c = *src++;
 
73
          *dest++ = c;
 
74
          if (c == '\0')
 
75
            break;
 
76
          if (--n4 == 0)
 
77
            goto last_chars;
 
78
        }
 
79
      n -= dest - s;
 
80
      goto zero_fill;
 
81
    }
 
82
 
 
83
 last_chars:
 
84
  n &= 3;
 
85
  if (n == 0)
 
86
    return dest;
 
87
 
 
88
  for (;;)
 
89
    {
 
90
      c = *src++;
 
91
      --n;
 
92
      *dest++ = c;
 
93
      if (c == '\0')
 
94
        break;
 
95
      if (n == 0)
 
96
        return dest;
 
97
    }
 
98
 
 
99
 zero_fill:
 
100
  while (n-- > 0)
 
101
    dest[n] = '\0';
 
102
 
 
103
  return dest - 1;
 
104
}
 
105
#ifdef weak_alias
 
106
weak_alias (__stpncpy, stpncpy)
 
107
#endif
 
108
 
 
109
#endif