mirror of
https://https.git.savannah.gnu.org/git/bash.git
synced 2026-08-08 11:12:36 +02:00
bash-20120322 remove leftover and stray files
This commit is contained in:
@@ -1,356 +0,0 @@
|
||||
/* getcwd.c -- get pathname of current directory */
|
||||
|
||||
/* Copyright (C) 1991 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash, the Bourne Again SHell.
|
||||
|
||||
Bash 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if !defined (HAVE_GETCWD)
|
||||
|
||||
#if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
|
||||
#pragma alloca
|
||||
#endif /* _AIX && RISC6000 && !__GNUC__ */
|
||||
|
||||
#if defined (__QNX__)
|
||||
# undef HAVE_LSTAT
|
||||
#endif
|
||||
|
||||
#include <bashtypes.h>
|
||||
#include <errno.h>
|
||||
|
||||
#if defined (HAVE_LIMITS_H)
|
||||
# include <limits.h>
|
||||
#endif
|
||||
|
||||
#if defined (HAVE_UNISTD_H)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <posixdir.h>
|
||||
#include <posixstat.h>
|
||||
#include <maxpath.h>
|
||||
#include <memalloc.h>
|
||||
|
||||
#include <bashansi.h>
|
||||
|
||||
#if defined (BROKEN_DIRENT_D_INO)
|
||||
# include "command.h"
|
||||
# include "general.h"
|
||||
# include "externs.h"
|
||||
#endif
|
||||
|
||||
#include <xmalloc.h>
|
||||
|
||||
#if !defined (errno)
|
||||
extern int errno;
|
||||
#endif /* !errno */
|
||||
|
||||
#if !defined (HAVE_LSTAT)
|
||||
# define lstat stat
|
||||
#endif
|
||||
|
||||
#if !defined (NULL)
|
||||
# define NULL 0
|
||||
#endif
|
||||
|
||||
/* If the d_fileno member of a struct dirent doesn't return anything useful,
|
||||
we need to check inode number equivalence the hard way. Return 1 if
|
||||
the inode corresponding to PATH/DIR is identical to THISINO. */
|
||||
#if defined (BROKEN_DIRENT_D_INO)
|
||||
static int
|
||||
_path_checkino (dotp, name, thisino)
|
||||
char *dotp;
|
||||
char *name;
|
||||
ino_t thisino;
|
||||
{
|
||||
char *fullpath;
|
||||
int r, e;
|
||||
struct stat st;
|
||||
|
||||
e = errno;
|
||||
fullpath = sh_makepath (dotp, name, MP_RMDOT);
|
||||
if (stat (fullpath, &st) < 0)
|
||||
{
|
||||
errno = e;
|
||||
return 0;
|
||||
}
|
||||
free (fullpath);
|
||||
errno = e;
|
||||
return (st.st_ino == thisino);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Get the pathname of the current working directory,
|
||||
and put it in SIZE bytes of BUF. Returns NULL if the
|
||||
directory couldn't be determined or SIZE was too small.
|
||||
If successful, returns BUF. In GNU, if BUF is NULL,
|
||||
an array is allocated with `malloc'; the array is SIZE
|
||||
bytes long, unless SIZE <= 0, in which case it is as
|
||||
big as necessary. */
|
||||
#if defined (__STDC__)
|
||||
char *
|
||||
getcwd (char *buf, size_t size)
|
||||
#else /* !__STDC__ */
|
||||
char *
|
||||
getcwd (buf, size)
|
||||
char *buf;
|
||||
size_t size;
|
||||
#endif /* !__STDC__ */
|
||||
{
|
||||
static const char dots[]
|
||||
= "../../../../../../../../../../../../../../../../../../../../../../../\
|
||||
../../../../../../../../../../../../../../../../../../../../../../../../../../\
|
||||
../../../../../../../../../../../../../../../../../../../../../../../../../..";
|
||||
const char *dotp, *dotlist;
|
||||
size_t dotsize;
|
||||
dev_t rootdev, thisdev;
|
||||
ino_t rootino, thisino;
|
||||
char path[PATH_MAX + 1];
|
||||
register char *pathp;
|
||||
char *pathbuf;
|
||||
size_t pathsize;
|
||||
struct stat st;
|
||||
int saved_errno;
|
||||
|
||||
if (buf != NULL && size == 0)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return ((char *)NULL);
|
||||
}
|
||||
|
||||
pathsize = sizeof (path);
|
||||
pathp = &path[pathsize];
|
||||
*--pathp = '\0';
|
||||
pathbuf = path;
|
||||
|
||||
if (stat (".", &st) < 0)
|
||||
return ((char *)NULL);
|
||||
thisdev = st.st_dev;
|
||||
thisino = st.st_ino;
|
||||
|
||||
if (stat ("/", &st) < 0)
|
||||
return ((char *)NULL);
|
||||
rootdev = st.st_dev;
|
||||
rootino = st.st_ino;
|
||||
|
||||
saved_errno = 0;
|
||||
|
||||
dotsize = sizeof (dots) - 1;
|
||||
dotp = &dots[sizeof (dots)];
|
||||
dotlist = dots;
|
||||
while (!(thisdev == rootdev && thisino == rootino))
|
||||
{
|
||||
register DIR *dirstream;
|
||||
register struct dirent *d;
|
||||
dev_t dotdev;
|
||||
ino_t dotino;
|
||||
char mount_point;
|
||||
int namlen;
|
||||
|
||||
/* Look at the parent directory. */
|
||||
if (dotp == dotlist)
|
||||
{
|
||||
/* My, what a deep directory tree you have, Grandma. */
|
||||
char *new;
|
||||
if (dotlist == dots)
|
||||
{
|
||||
new = (char *)malloc (dotsize * 2 + 1);
|
||||
if (new == NULL)
|
||||
goto lose;
|
||||
memcpy (new, dots, dotsize);
|
||||
}
|
||||
else
|
||||
{
|
||||
new = (char *)realloc ((PTR_T) dotlist, dotsize * 2 + 1);
|
||||
if (new == NULL)
|
||||
goto lose;
|
||||
}
|
||||
memcpy (&new[dotsize], new, dotsize);
|
||||
dotp = &new[dotsize];
|
||||
dotsize *= 2;
|
||||
new[dotsize] = '\0';
|
||||
dotlist = new;
|
||||
}
|
||||
|
||||
dotp -= 3;
|
||||
|
||||
/* Figure out if this directory is a mount point. */
|
||||
if (stat (dotp, &st) < 0)
|
||||
goto lose;
|
||||
dotdev = st.st_dev;
|
||||
dotino = st.st_ino;
|
||||
mount_point = dotdev != thisdev;
|
||||
|
||||
/* Search for the last directory. */
|
||||
dirstream = opendir (dotp);
|
||||
if (dirstream == NULL)
|
||||
goto lose;
|
||||
while ((d = readdir (dirstream)) != NULL)
|
||||
{
|
||||
if (d->d_name[0] == '.' &&
|
||||
(d->d_name[1] == '\0' ||
|
||||
(d->d_name[1] == '.' && d->d_name[2] == '\0')))
|
||||
continue;
|
||||
#if !defined (BROKEN_DIRENT_D_INO)
|
||||
if (mount_point || d->d_fileno == thisino)
|
||||
#else
|
||||
if (mount_point || _path_checkino (dotp, d->d_name, thisino))
|
||||
#endif
|
||||
{
|
||||
char *name;
|
||||
|
||||
namlen = D_NAMLEN(d);
|
||||
name = (char *)
|
||||
alloca (dotlist + dotsize - dotp + 1 + namlen + 1);
|
||||
memcpy (name, dotp, dotlist + dotsize - dotp);
|
||||
name[dotlist + dotsize - dotp] = '/';
|
||||
memcpy (&name[dotlist + dotsize - dotp + 1],
|
||||
d->d_name, namlen + 1);
|
||||
if (lstat (name, &st) < 0)
|
||||
{
|
||||
#if 0
|
||||
int save = errno;
|
||||
(void) closedir (dirstream);
|
||||
errno = save;
|
||||
goto lose;
|
||||
#else
|
||||
saved_errno = errno;
|
||||
#endif
|
||||
}
|
||||
if (st.st_dev == thisdev && st.st_ino == thisino)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (d == NULL)
|
||||
{
|
||||
#if 0
|
||||
int save = errno;
|
||||
#else
|
||||
int save = errno ? errno : saved_errno;
|
||||
#endif
|
||||
(void) closedir (dirstream);
|
||||
errno = save;
|
||||
goto lose;
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t space;
|
||||
|
||||
while ((space = pathp - pathbuf) <= namlen)
|
||||
{
|
||||
char *new;
|
||||
|
||||
if (pathbuf == path)
|
||||
{
|
||||
new = (char *)malloc (pathsize * 2);
|
||||
if (!new)
|
||||
goto lose;
|
||||
}
|
||||
else
|
||||
{
|
||||
new = (char *)realloc ((PTR_T) pathbuf, (pathsize * 2));
|
||||
if (!new)
|
||||
goto lose;
|
||||
pathp = new + space;
|
||||
}
|
||||
(void) memcpy (new + pathsize + space, pathp, pathsize - space);
|
||||
pathp = new + pathsize + space;
|
||||
pathbuf = new;
|
||||
pathsize *= 2;
|
||||
}
|
||||
|
||||
pathp -= namlen;
|
||||
(void) memcpy (pathp, d->d_name, namlen);
|
||||
*--pathp = '/';
|
||||
(void) closedir (dirstream);
|
||||
}
|
||||
|
||||
thisdev = dotdev;
|
||||
thisino = dotino;
|
||||
}
|
||||
|
||||
if (pathp == &path[sizeof(path) - 1])
|
||||
*--pathp = '/';
|
||||
|
||||
if (dotlist != dots)
|
||||
free ((PTR_T) dotlist);
|
||||
|
||||
{
|
||||
size_t len = pathbuf + pathsize - pathp;
|
||||
if (buf == NULL && size <= 0)
|
||||
size = len;
|
||||
|
||||
if ((size_t) size < len)
|
||||
{
|
||||
errno = ERANGE;
|
||||
goto lose2;
|
||||
}
|
||||
if (buf == NULL)
|
||||
{
|
||||
buf = (char *) malloc (size);
|
||||
if (buf == NULL)
|
||||
goto lose2;
|
||||
}
|
||||
|
||||
(void) memcpy((PTR_T) buf, (PTR_T) pathp, len);
|
||||
}
|
||||
|
||||
if (pathbuf != path)
|
||||
free (pathbuf);
|
||||
|
||||
return (buf);
|
||||
|
||||
lose:
|
||||
if ((dotlist != dots) && dotlist)
|
||||
{
|
||||
int e = errno;
|
||||
free ((PTR_T) dotlist);
|
||||
errno = e;
|
||||
}
|
||||
|
||||
lose2:
|
||||
if ((pathbuf != path) && pathbuf)
|
||||
{
|
||||
int e = errno;
|
||||
free ((PTR_T) pathbuf);
|
||||
errno = e;
|
||||
}
|
||||
return ((char *)NULL);
|
||||
}
|
||||
|
||||
#if defined (TEST)
|
||||
# include <stdio.h>
|
||||
main (argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
char b[PATH_MAX];
|
||||
|
||||
if (getcwd(b, sizeof(b)))
|
||||
{
|
||||
printf ("%s\n", b);
|
||||
exit (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
perror ("cwd: getcwd");
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
#endif /* TEST */
|
||||
#endif /* !HAVE_GETCWD */
|
||||
@@ -1,78 +0,0 @@
|
||||
/* mbschr.c - strchr(3) that handles multibyte characters. */
|
||||
|
||||
/* Copyright (C) 2002 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash, the Bourne Again SHell.
|
||||
|
||||
Bash 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#ifdef HAVE_STDLIB_H
|
||||
# include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#include "bashansi.h"
|
||||
#include "shmbutil.h"
|
||||
|
||||
#undef mbschr
|
||||
|
||||
/* In some locales, the non-first byte of some multibyte characters have
|
||||
the same value as some ascii character. Faced with these strings, a
|
||||
legacy strchr() might return the wrong value. */
|
||||
|
||||
char *
|
||||
#if defined (PROTOTYPES)
|
||||
mbschr (const char *s, int c)
|
||||
#else
|
||||
mbschr (s, c)
|
||||
const char *s;
|
||||
int c;
|
||||
#endif
|
||||
{
|
||||
#if HANDLE_MULTIBYTE
|
||||
char *pos;
|
||||
mbstate_t state;
|
||||
size_t strlength, mblength;
|
||||
|
||||
/* The locale encodings with said weird property are BIG5, BIG5-HKSCS,
|
||||
GBK, GB18030, SHIFT_JIS, and JOHAB. They exhibit the problem only
|
||||
when c >= 0x30. We can therefore use the faster bytewise search if
|
||||
c <= 0x30. */
|
||||
if ((unsigned char)c >= '0' && MB_CUR_MAX > 1)
|
||||
{
|
||||
pos = (char *)s;
|
||||
memset (&state, '\0', sizeof(mbstate_t));
|
||||
strlength = strlen (s);
|
||||
|
||||
while (strlength > 0)
|
||||
{
|
||||
mblength = mbrlen (pos, strlength, &state);
|
||||
if (mblength == (size_t)-2 || mblength == (size_t)-1 || mblength == (size_t)0)
|
||||
mblength = 1;
|
||||
|
||||
if (mblength == 1 && c == (unsigned char)*pos)
|
||||
return pos;
|
||||
|
||||
strlength -= mblength;
|
||||
pos += mblength;
|
||||
}
|
||||
|
||||
return ((char *)NULL);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
return (strchr (s, c));
|
||||
}
|
||||
@@ -1,340 +0,0 @@
|
||||
/* unicode.c - functions to convert unicode characters */
|
||||
|
||||
/* Copyright (C) 2010-2012 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash, the Bourne Again SHell.
|
||||
|
||||
Bash 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if defined (HANDLE_MULTIBYTE)
|
||||
|
||||
#include <stdc.h>
|
||||
#include <wchar.h>
|
||||
#include <bashansi.h>
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
|
||||
#if HAVE_ICONV
|
||||
# include <iconv.h>
|
||||
#endif
|
||||
|
||||
#include <xmalloc.h>
|
||||
|
||||
#ifndef USHORT_MAX
|
||||
# ifdef USHRT_MAX
|
||||
# define USHORT_MAX USHRT_MAX
|
||||
# else
|
||||
# define USHORT_MAX ((unsigned short) ~(unsigned short)0)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if !defined (STREQ)
|
||||
# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0)
|
||||
#endif /* !STREQ */
|
||||
|
||||
#if defined (HAVE_LOCALE_CHARSET)
|
||||
extern const char *locale_charset __P((void));
|
||||
#else
|
||||
extern char *get_locale_var __P((char *));
|
||||
#endif
|
||||
|
||||
static int u32init = 0;
|
||||
static int utf8locale = 0;
|
||||
#if defined (HAVE_ICONV)
|
||||
static iconv_t localconv;
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_LOCALE_CHARSET
|
||||
static char charsetbuf[40];
|
||||
|
||||
static char *
|
||||
stub_charset ()
|
||||
{
|
||||
char *locale, *s, *t;
|
||||
|
||||
locale = get_locale_var ("LC_CTYPE");
|
||||
if (locale == 0 || *locale == 0)
|
||||
{
|
||||
strcpy (charsetbuf, "ASCII");
|
||||
return charsetbuf;
|
||||
}
|
||||
s = strrchr (locale, '.');
|
||||
if (s)
|
||||
{
|
||||
strcpy (charsetbuf, s+1);
|
||||
t = strchr (charsetbuf, '@');
|
||||
if (t)
|
||||
*t = 0;
|
||||
return charsetbuf;
|
||||
}
|
||||
strcpy (charsetbuf, locale);
|
||||
return charsetbuf;
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
u32reset ()
|
||||
{
|
||||
#if defined (HAVE_ICONV)
|
||||
if (u32init && localconv != (iconv_t)-1)
|
||||
{
|
||||
iconv_close (localconv);
|
||||
localconv = (iconv_t)-1;
|
||||
}
|
||||
#endif
|
||||
u32init = 0;
|
||||
utf8locale = 0;
|
||||
}
|
||||
|
||||
/* u32toascii ? */
|
||||
int
|
||||
u32tochar (x, s)
|
||||
unsigned long x;
|
||||
char *s;
|
||||
{
|
||||
int l;
|
||||
|
||||
l = (x <= UCHAR_MAX) ? 1 : ((x <= USHORT_MAX) ? 2 : 4);
|
||||
|
||||
if (x <= UCHAR_MAX)
|
||||
s[0] = x & 0xFF;
|
||||
else if (x <= USHORT_MAX) /* assume unsigned short = 16 bits */
|
||||
{
|
||||
s[0] = (x >> 8) & 0xFF;
|
||||
s[1] = x & 0xFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
s[0] = (x >> 24) & 0xFF;
|
||||
s[1] = (x >> 16) & 0xFF;
|
||||
s[2] = (x >> 8) & 0xFF;
|
||||
s[3] = x & 0xFF;
|
||||
}
|
||||
s[l] = '\0';
|
||||
return l;
|
||||
}
|
||||
|
||||
int
|
||||
u32tocesc (wc, s)
|
||||
u_bits32_t wc;
|
||||
char *s;
|
||||
{
|
||||
int l;
|
||||
|
||||
if (wc < 0x10000)
|
||||
l = sprintf (s, "\\u%04X", wc);
|
||||
else
|
||||
l = sprintf (s, "\\u%08X", wc);
|
||||
return l;
|
||||
}
|
||||
|
||||
/* Convert unsigned 32-bit int to utf-8 character string */
|
||||
int
|
||||
u32toutf8 (wc, s)
|
||||
u_bits32_t wc;
|
||||
char *s;
|
||||
{
|
||||
int l;
|
||||
|
||||
if (wc < 0x0080)
|
||||
{
|
||||
s[0] = (char)wc;
|
||||
l = 1;
|
||||
}
|
||||
else if (wc < 0x0800)
|
||||
{
|
||||
s[0] = (wc >> 6) | 0xc0;
|
||||
s[1] = (wc & 0x3f) | 0x80;
|
||||
l = 2;
|
||||
}
|
||||
else if (wc < 0x10000)
|
||||
{
|
||||
/* Technically, we could return 0 here if 0xd800 <= wc <= 0x0dfff */
|
||||
s[0] = (wc >> 12) | 0xe0;
|
||||
s[1] = ((wc >> 6) & 0x3f) | 0x80;
|
||||
s[2] = (wc & 0x3f) | 0x80;
|
||||
l = 3;
|
||||
}
|
||||
else if (wc < 0x200000)
|
||||
{
|
||||
s[0] = (wc >> 18) | 0xf0;
|
||||
s[1] = ((wc >> 12) & 0x3f) | 0x80;
|
||||
s[2] = ((wc >> 6) & 0x3f) | 0x80;
|
||||
s[3] = (wc & 0x3f) | 0x80;
|
||||
l = 4;
|
||||
}
|
||||
/* Strictly speaking, UTF-8 doesn't have characters longer than 4 bytes */
|
||||
else if (wc < 0x04000000)
|
||||
{
|
||||
s[0] = (wc >> 24) | 0xf8;
|
||||
s[1] = ((wc >> 18) & 0x3f) | 0x80;
|
||||
s[2] = ((wc >> 12) & 0x3f) | 0x80;
|
||||
s[3] = ((wc >> 6) & 0x3f) | 0x80;
|
||||
s[4] = (wc & 0x3f) | 0x80;
|
||||
l = 5;
|
||||
}
|
||||
else if (wc < 0x080000000)
|
||||
{
|
||||
s[0] = (wc >> 30) | 0xf8;
|
||||
s[1] = ((wc >> 24) & 0x3f) | 0x80;
|
||||
s[2] = ((wc >> 18) & 0x3f) | 0x80;
|
||||
s[3] = ((wc >> 12) & 0x3f) | 0x80;
|
||||
s[4] = ((wc >> 6) & 0x3f) | 0x80;
|
||||
s[5] = (wc & 0x3f) | 0x80;
|
||||
l = 6;
|
||||
}
|
||||
else
|
||||
l = 0;
|
||||
|
||||
s[l] = '\0';
|
||||
return l;
|
||||
}
|
||||
|
||||
/* Convert a 32-bit unsigned int (unicode) to a UTF-16 string. Rarely used,
|
||||
only if sizeof(wchar_t) == 2. */
|
||||
int
|
||||
u32toutf16 (c, s)
|
||||
u_bits32_t c;
|
||||
unsigned short *s;
|
||||
{
|
||||
int l;
|
||||
|
||||
l = 0;
|
||||
if (c < 0x0d800)
|
||||
{
|
||||
s[0] = (unsigned short) (c & 0xFFFF);
|
||||
l = 1;
|
||||
}
|
||||
else if (c >= 0x0e000 && c <= 0x010ffff)
|
||||
{
|
||||
c -= 0x010000;
|
||||
s[0] = (unsigned short)((c >> 10) + 0xd800);
|
||||
s[1] = (unsigned short)((c & 0x3ff) + 0xdc00);
|
||||
l = 2;
|
||||
}
|
||||
s[l] = 0;
|
||||
return l;
|
||||
}
|
||||
|
||||
/* convert a single unicode-32 character into a multibyte string and put the
|
||||
result in S, which must be large enough (at least MB_LEN_MAX bytes) */
|
||||
int
|
||||
u32cconv (c, s)
|
||||
unsigned long c;
|
||||
char *s;
|
||||
{
|
||||
wchar_t wc;
|
||||
wchar_t ws[3];
|
||||
int n;
|
||||
#if HAVE_ICONV
|
||||
const char *charset;
|
||||
char obuf[25], *optr;
|
||||
size_t obytesleft;
|
||||
const char *iptr;
|
||||
size_t sn;
|
||||
#endif
|
||||
|
||||
#if __STDC_ISO_10646__
|
||||
wc = c;
|
||||
if (sizeof (wchar_t) == 4 && c <= 0x7fffffff)
|
||||
n = wctomb (s, wc);
|
||||
else if (sizeof (wchar_t) == 2 && c <= 0x10ffff && u32toutf16 (c, ws))
|
||||
n = wcstombs (s, ws, MB_LEN_MAX);
|
||||
else
|
||||
n = -1;
|
||||
if (n != -1)
|
||||
return n;
|
||||
#endif
|
||||
|
||||
#if HAVE_NL_LANGINFO
|
||||
codeset = nl_langinfo (CODESET);
|
||||
if (STREQ (codeset, "UTF-8"))
|
||||
{
|
||||
n = u32toutf8 (c, s);
|
||||
return n;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if HAVE_ICONV
|
||||
/* this is mostly from coreutils-8.5/lib/unicodeio.c */
|
||||
if (u32init == 0)
|
||||
{
|
||||
# if HAVE_LOCALE_CHARSET
|
||||
charset = locale_charset (); /* XXX - fix later */
|
||||
# else
|
||||
charset = stub_charset ();
|
||||
# endif
|
||||
if (STREQ (charset, "UTF-8"))
|
||||
utf8locale = 1;
|
||||
else
|
||||
{
|
||||
localconv = iconv_open (charset, "UTF-8");
|
||||
if (localconv == (iconv_t)-1)
|
||||
/* We assume ASCII when presented with an unknown encoding. */
|
||||
localconv = iconv_open ("ASCII", "UTF-8");
|
||||
}
|
||||
u32init = 1;
|
||||
}
|
||||
|
||||
/* If we have a UTF-8 locale, convert to UTF-8 and return converted value. */
|
||||
n = u32toutf8 (c, s);
|
||||
if (utf8locale)
|
||||
return n;
|
||||
|
||||
/* If the conversion is not supported, even the ASCII requested above, we
|
||||
bail now. Currently we return the UTF-8 conversion. We could return
|
||||
u32cesc(). */
|
||||
if (localconv == (iconv_t)-1)
|
||||
return n;
|
||||
|
||||
optr = obuf;
|
||||
obytesleft = sizeof (obuf);
|
||||
iptr = s;
|
||||
sn = n;
|
||||
|
||||
iconv (localconv, NULL, NULL, NULL, NULL);
|
||||
|
||||
if (iconv (localconv, (ICONV_CONST char **)&iptr, &sn, &optr, &obytesleft) == (size_t)-1)
|
||||
{
|
||||
#if 1
|
||||
/* You get ISO C99 escape sequences if iconv fails */
|
||||
n = u32tocesc (c, s);
|
||||
#else
|
||||
/* You get UTF-8 if iconv fails */
|
||||
#endif
|
||||
return n;
|
||||
}
|
||||
|
||||
*optr = '\0';
|
||||
|
||||
/* number of chars to be copied is optr - obuf if we want to do bounds
|
||||
checking */
|
||||
strcpy (s, obuf);
|
||||
return (optr - obuf);
|
||||
#endif /* HAVE_ICONV */
|
||||
|
||||
n = u32tocesc (c, s); /* fallback is ISO C99 escape sequences */
|
||||
return n;
|
||||
}
|
||||
#else
|
||||
void
|
||||
u32reset ()
|
||||
{
|
||||
}
|
||||
#endif /* HANDLE_MULTIBYTE */
|
||||
-174
@@ -1,174 +0,0 @@
|
||||
/* zread - read data from file descriptor into buffer with retries */
|
||||
|
||||
/* Copyright (C) 1999-2002 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash, the Bourne Again SHell.
|
||||
|
||||
Bash 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Bash 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 Bash. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#if defined (HAVE_UNISTD_H)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#if !defined (errno)
|
||||
extern int errno;
|
||||
#endif
|
||||
|
||||
#ifndef SEEK_CUR
|
||||
# define SEEK_CUR 1
|
||||
#endif
|
||||
|
||||
/* Read LEN bytes from FD into BUF. Retry the read on EINTR. Any other
|
||||
error causes the loop to break. */
|
||||
ssize_t
|
||||
zread (fd, buf, len)
|
||||
int fd;
|
||||
char *buf;
|
||||
size_t len;
|
||||
{
|
||||
ssize_t r;
|
||||
|
||||
while ((r = read (fd, buf, len)) < 0 && errno == EINTR)
|
||||
;
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Read LEN bytes from FD into BUF. Retry the read on EINTR, up to three
|
||||
interrupts. Any other error causes the loop to break. */
|
||||
|
||||
#ifdef NUM_INTR
|
||||
# undef NUM_INTR
|
||||
#endif
|
||||
#define NUM_INTR 3
|
||||
|
||||
ssize_t
|
||||
zreadretry (fd, buf, len)
|
||||
int fd;
|
||||
char *buf;
|
||||
size_t len;
|
||||
{
|
||||
ssize_t r;
|
||||
int nintr;
|
||||
|
||||
for (nintr = 0; ; )
|
||||
{
|
||||
r = read (fd, buf, len);
|
||||
if (r >= 0)
|
||||
return r;
|
||||
if (r == -1 && errno == EINTR)
|
||||
{
|
||||
if (++nintr >= NUM_INTR)
|
||||
return -1;
|
||||
continue;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
/* Call read(2) and allow it to be interrupted. Just a stub for now. */
|
||||
ssize_t
|
||||
zreadintr (fd, buf, len)
|
||||
int fd;
|
||||
char *buf;
|
||||
size_t len;
|
||||
{
|
||||
return (read (fd, buf, len));
|
||||
}
|
||||
|
||||
/* Read one character from FD and return it in CP. Return values are as
|
||||
in read(2). This does some local buffering to avoid many one-character
|
||||
calls to read(2), like those the `read' builtin performs. */
|
||||
|
||||
static char lbuf[128];
|
||||
static size_t lind, lused;
|
||||
|
||||
ssize_t
|
||||
zreadc (fd, cp)
|
||||
int fd;
|
||||
char *cp;
|
||||
{
|
||||
ssize_t nr;
|
||||
|
||||
if (lind == lused || lused == 0)
|
||||
{
|
||||
nr = zread (fd, lbuf, sizeof (lbuf));
|
||||
lind = 0;
|
||||
if (nr <= 0)
|
||||
{
|
||||
lused = 0;
|
||||
return nr;
|
||||
}
|
||||
lused = nr;
|
||||
}
|
||||
if (cp)
|
||||
*cp = lbuf[lind++];
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Don't mix calls to zreadc and zreadcintr in the same function, since they
|
||||
use the same local buffer. */
|
||||
ssize_t
|
||||
zreadcintr (fd, cp)
|
||||
int fd;
|
||||
char *cp;
|
||||
{
|
||||
ssize_t nr;
|
||||
|
||||
if (lind == lused || lused == 0)
|
||||
{
|
||||
nr = zreadintr (fd, lbuf, sizeof (lbuf));
|
||||
lind = 0;
|
||||
if (nr <= 0)
|
||||
{
|
||||
lused = 0;
|
||||
return nr;
|
||||
}
|
||||
lused = nr;
|
||||
}
|
||||
if (cp)
|
||||
*cp = lbuf[lind++];
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
zreset ()
|
||||
{
|
||||
lind = lused = 0;
|
||||
}
|
||||
|
||||
/* Sync the seek pointer for FD so that the kernel's idea of the last char
|
||||
read is the last char returned by zreadc. */
|
||||
void
|
||||
zsyncfd (fd)
|
||||
int fd;
|
||||
{
|
||||
off_t off, r;
|
||||
|
||||
off = lused - lind;
|
||||
r = 0;
|
||||
if (off > 0)
|
||||
r = lseek (fd, -off, SEEK_CUR);
|
||||
if (r < 0)
|
||||
itrace("zsyncfd: lseek (-%d) returns < 0: %s", off, strerror (errno));
|
||||
|
||||
if (r != -1)
|
||||
lused = lind = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user