#ifdef DBG
#include <stdio.h>
#else
#define NDEBUG
#endif
#include <assert.h>
#include "sh.h"
#ifdef MBCHAR
#include <widec.h>
#endif
#include <sys/param.h>
#include <fcntl.h>
#include <unistd.h>
bool cflg;
tchar *
strtots(tchar *to, char *from)
{
int i;
if (to == NOSTR) {
int i;
i = mbstotcs(NOSTR, from, 0);
if (i < 0) {
return (NOSTR);
}
to = (tchar *)xalloc(i * sizeof (tchar));
}
i = mbstotcs(to, from, INT_MAX);
if (i < 0) {
return (NOSTR);
}
return (to);
}
char *
tstostr(char *to, tchar *from)
{
tchar *ptc;
wchar_t wc;
char *pmb;
int len;
if (to == (char *)NULL) {
int i;
int i1;
char junk[MB_LEN_MAX];
i = 0;
ptc = from;
while (wc = (wchar_t)((*ptc++)&TRIM)) {
if ((i1 = wctomb(junk, wc)) <= 0) {
i1 = 1;
}
i += i1;
}
to = (char *)xalloc(i + 1);
}
ptc = from;
pmb = to;
while (wc = (wchar_t)((*ptc++)&TRIM)) {
if ((len = wctomb(pmb, wc)) <= 0) {
*pmb = (unsigned char)wc;
len = 1;
}
pmb += len;
}
*pmb = (char)0;
return (to);
}
int
mbstotcs(tchar *to, char *from, int tosize)
{
tchar *ptc = to;
char *pmb = from;
wchar_t wc;
int chcnt = 0;
int j;
if (to == (tchar *)NULL) {
while (*pmb) {
if ((j = mbtowc(&wc, pmb, MB_CUR_MAX)) <= 0) {
j = 1;
}
pmb += j;
chcnt++;
}
chcnt++;
return (chcnt);
} else {
while (*pmb) {
if ((j = mbtowc(&wc, pmb, MB_CUR_MAX)) <= 0) {
wc = (unsigned char)*pmb;
j = 1;
}
pmb += j;
*(ptc++) = (tchar)wc;
if (++chcnt >= tosize) {
break;
}
}
if (chcnt < tosize) {
*ptc = (tchar)0;
++chcnt;
}
return (chcnt);
}
}
int
strlen_(tchar *s)
{
int n;
n = 0;
while (*s++) {
n++;
}
return (n);
}
tchar *
strcat_(tchar *s1, tchar *s2)
{
tchar *os1;
os1 = s1;
while (*s1++)
;
--s1;
while (*s1++ = *s2++)
;
return (os1);
}
int
strcmp_(tchar *s1, tchar *s2)
{
while (*s1 == *s2++) {
if (*s1++ == (tchar)0) {
return (0);
}
}
return (((unsigned long)*s1) - ((unsigned long)*(--s2)));
}
int
strcoll_(tchar *s1, tchar *s2)
{
char buf1[BUFSIZ];
char buf2[BUFSIZ];
tstostr(buf1, s1);
tstostr(buf2, s2);
return (strcoll(buf1, buf2));
}
tchar *
strcpy_(tchar *s1, tchar *s2)
{
tchar *os1;
os1 = s1;
while (*s1++ = *s2++)
;
return (os1);
}
tchar *
index_(tchar *sp, tchar c)
{
do {
if (*sp == c) {
return (sp);
}
} while (*sp++);
return (NULL);
}
tchar *
rindex_(tchar *sp, tchar c)
{
tchar *r;
r = NOSTR;
do {
if (*sp == c) {
r = sp;
}
} while (*sp++);
return (r);
}
int
tswidth(tchar *ts)
{
#ifdef MBCHAR
wchar_t tc;
int w = 0;
int p_col;
while (tc = *ts++) {
if ((p_col = wcwidth((wchar_t)tc)) > 0)
w += p_col;
}
return (w);
#else
return (strlen_(ts));
#endif
}
#define LONGEST_ENVVARNAME 256
tchar *
getenv_(tchar *name_)
{
char name[LONGEST_ENVVARNAME * MB_LEN_MAX];
assert(strlen_(name_) < LONGEST_ENVVARNAME);
return (getenvs_(tstostr(name, name_)));
}
tchar *
getenvs_(char *name)
{
static tchar *pbuf = (tchar *)NULL;
char *val;
if (pbuf) {
xfree(pbuf);
pbuf = NOSTR;
}
val = getenv(name);
if (val == (char *)NULL) {
return (NOSTR);
}
return (pbuf = strtots(NOSTR, val));
}
int
creat_(tchar *name_, int mode)
{
int fd;
char chbuf[MAXPATHLEN * MB_LEN_MAX];
tstostr(chbuf, name_);
fd = creat((char *)chbuf, mode);
if (fd != -1) {
setfd(fd);
}
return (fd);
}
int
open_(path_, flags, mode)
tchar *path_;
int flags;
int mode;
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
int fd;
tstostr(chbuf, path_);
fd = open((char *)chbuf, flags, mode);
if (fd != -1) {
setfd(fd);
}
return (fd);
}
int
mkstemp_(tchar *name_)
{
int fd;
char chbuf[MAXPATHLEN * MB_LEN_MAX];
tstostr(chbuf, name_);
fd = mkstemp((char *)chbuf);
if (fd != -1) {
setfd(fd);
strtots(name_, chbuf);
}
return (fd);
}
int
read_(int d, tchar *buf, int nchreq)
{
unsigned char chbuf[BUFSIZ * MB_LEN_MAX];
#ifdef MBCHAR
int i, j, fflags;
unsigned char *s;
unsigned char *p;
tchar *t;
wchar_t wc;
int b_len;
int nchread = 0;
int nbytread = 0;
int delta;
unsigned char *q;
int mb_cur_max = MB_CUR_MAX;
#ifdef DBG
tprintf("Entering read_(d=%d, buf=0x%x, nchreq=%d);\n",
d, buf, nchreq);
#endif
assert(nchreq <= BUFSIZ);
delta = 0;
p = s = chbuf;
t = buf;
while (nchread < nchreq) {
int m;
int k;
retry:
m = nchreq - nchread;
assert(p + m < chbuf + sizeof (chbuf));
k = read(d, p, m);
if (k == 0) {
if ((intty && !onelflg && !cflg) &&
((fflags = fcntl(d, F_GETFL, 0)) & O_NDELAY)) {
fflags &= ~O_NDELAY;
fcntl(d, F_SETFL, fflags);
goto retry;
}
} else if (k < 0) {
if (errno == EAGAIN) {
fflags = fcntl(d, F_GETFL, 0);
fflags &= ~O_NONBLOCK;
fcntl(d, F_SETFL, fflags);
goto retry;
}
return (-1);
}
nbytread += k;
q = p + k;
delta = 0;
while (s < q) {
if (*s == 0) {
*t++ = 0;
s++;
nchread++;
continue;
}
if ((b_len = q - s) > mb_cur_max) {
b_len = mb_cur_max;
}
if ((j = mbtowc(&wc, (char *)s, b_len)) <= 0) {
if (mb_cur_max > 1 && b_len < mb_cur_max) {
break;
}
wc = (unsigned char)*s;
j = 1;
}
*t++ = wc;
nchread++;
s += j;
}
if (k < m) {
while (s < q) {
if ((b_len = q - s) > mb_cur_max) {
b_len = mb_cur_max;
}
if ((j = mbtowc(&wc, (char *)s, b_len)) <= 0) {
wc = (unsigned char)*s;
j = 1;
}
*t++ = wc;
nchread++;
s += j;
}
return (nchread);
}
p = q;
}
if (mb_cur_max == 1 || (delta = q - s) == 0) {
return (nchread);
}
do {
if ((j = mbtowc(&wc, (char *)s, delta)) <= 0)
break;
*t++ = wc;
nchread++;
s += j;
delta -= j;
} while (delta > 0);
if (delta == 0)
return (nchread);
while (delta < mb_cur_max) {
assert((q + 1) < (chbuf + sizeof (chbuf)));
if (read(d, q, 1) != 1)
break;
delta++;
q++;
if (mbtowc(&wc, (char *)s, delta) > 0) {
*t = wc;
return (nchread + 1);
}
}
while (s < q) {
b_len = q - s;
if ((j = mbtowc(&wc, (char *)s, b_len)) <= 0) {
wc = (unsigned char)*s;
j = 1;
}
*t++ = wc;
nchread++;
s += j;
}
return (nchread);
#else
int i;
unsigned char *s;
tchar *t;
int nchread;
#ifdef DBG
tprintf("Entering read_(d=%d, buf=0x%x, nchreq=%d);\n",
d, buf, nchreq);
#endif
assert(nchreq <= BUFSIZ);
retry:
nchread = read(d, (char *)chbuf, nchreq);
if (nchread == 0) {
if ((intty && !onelflg && !cflg) &&
((fflags = fcntl(d, F_GETFL, 0)) & O_NDELAY)) {
fflags &= ~O_NDELAY;
fcntl(d, F_SETFL, fflags);
goto retry;
}
} else if (nchread < 0) {
if (errno == EAGAIN) {
fflags = fcntl(d, F_GETFL, 0);
fflags &= ~O_NONBLOCK;
fcntl(d, F_SETFL, fflags);
goto retry;
}
len = 0;
} else {
for (i = 0, t = buf, s = chbuf; i < nchread; ++i) {
*t++ = ((tchar)*s++);
}
}
return (nchread);
#endif
}
int
write_(int d, tchar *buf, int nch)
{
unsigned char chbuf[BUFSIZ*MB_LEN_MAX];
#ifdef MBCHAR
tchar *pt;
unsigned char *pc;
wchar_t wc;
int i, j;
#ifdef DBG
tprintf("Entering write_(d=%d, buf=0x%x, nch=%d);\n",
d, buf, nch);
#endif
assert(nch * MB_CUR_MAX < sizeof (chbuf));
i = nch;
pt = buf;
pc = chbuf;
while (i--) {
wc = (wchar_t)((*pt++)&TRIM);
if (wc == (wchar_t)0) {
*pc++ = 0;
} else {
if ((j = wctomb((char *)pc, wc)) <= 0) {
*pc = (unsigned char)wc;
j = 1;
}
pc += j;
}
}
return (write(d, chbuf, pc - chbuf));
#else
int i;
unsigned char *s;
tchar *t;
#ifdef DBG
tprintf("Entering write_(d=%d, buf=0x%x, nch=%d);\n",
d, buf, nch);
#endif
assert(nch <= sizeof (chbuf));
for (i = 0, t = buf, s = chbuf; i < nch; ++i) {
*s++ = (char)((*t++)&0xff);
}
return (write(d, (char *)chbuf, nch));
#endif
}
#undef chbuf
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
extern DIR *Dirp;
int
stat_(tchar *path, struct stat *buf)
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
tstostr(chbuf, path);
return (stat((char *)chbuf, buf));
}
int
lstat_(tchar *path, struct stat *buf)
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
tstostr(chbuf, path);
return (lstat((char *)chbuf, buf));
}
int
chdir_(tchar *path)
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
tstostr(chbuf, path);
return (chdir((char *)chbuf));
}
tchar *
getwd_(tchar *path)
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
int rc;
rc = (int)getwd((char *)chbuf);
if (rc == 0) {
return (0);
} else {
return (strtots(path, chbuf));
}
}
int
unlink_(tchar *path)
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
tstostr(chbuf, path);
return (unlink((char *)chbuf));
}
DIR *
opendir_(tchar *dirname)
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
extern DIR *opendir();
DIR *dir;
dir = opendir(tstostr(chbuf, dirname));
if (dir != NULL) {
setfd(dir->dd_fd);
}
return (Dirp = dir);
}
int
closedir_(DIR *dirp)
{
int ret;
extern int closedir();
ret = closedir(dirp);
Dirp = NULL;
return (ret);
}
int
gethostname_(tchar *name, int namelen)
{
char chbuf[BUFSIZ * MB_LEN_MAX];
assert(namelen < BUFSIZ);
if (gethostname((char *)chbuf, sizeof (chbuf)) != 0) {
return (-1);
}
if (mbstotcs(name, chbuf, namelen) < 0) {
return (-1);
}
return (0);
}
int
readlink_(tchar *path, tchar *buf, int bufsiz)
{
char chbuf[MAXPATHLEN * MB_LEN_MAX];
char chpath[MAXPATHLEN + 1];
int i;
tstostr(chpath, path);
i = readlink(chpath, (char *)chbuf, sizeof (chbuf));
if (i < 0) {
return (-1);
}
chbuf[i] = (char)0;
i = mbstotcs(buf, chbuf, bufsiz);
if (i < 0) {
return (-1);
}
return (i - 1);
}
int
chkalldigit_(tchar *str)
{
char chbuf[BUFSIZ * MB_LEN_MAX];
char *c = chbuf;
(void) tstostr(chbuf, str);
while (*c)
if (!isdigit(*(c++)))
return (-1);
return (0);
}
int
atoi_(tchar *str)
{
char chbuf[BUFSIZ * MB_LEN_MAX];
tstostr(chbuf, str);
return (atoi((char *)chbuf));
}
tchar *
simple(tchar *s)
{
tchar *sname = s;
while (1) {
if (any('/', sname)) {
while (*sname++ != '/')
;
} else {
return (sname);
}
}
}