Esempio n. 1
0
/*
 *	_rmt_write --- write a buffer to the remote tape
 */
static ssize_t
_rmt_write(int fildes, const void *buf, size_t nbyte)
{
	char buffer[BUFMAGIC];
	sig_t pstat;

	_DIAGASSERT(buf != NULL);

	(void)snprintf(buffer, sizeof buffer, "W%zu\n", nbyte);
	if (command(fildes, buffer) == -1)
		return -1;

	pstat = signal(SIGPIPE, SIG_IGN);
	if ((size_t)write(WRITE(fildes), buf, nbyte) == nbyte) {
		signal(SIGPIPE, pstat);
		return status(fildes);
	}

	signal(SIGPIPE, pstat);
	rmtabort(fildes);
	errno = EIO;
	return -1;
}
Esempio n. 2
0
/*
 * Expand the line buffer.  Return -1 on error.
#ifdef notdef
 * The `new size' does not account for a terminating '\0',
 * so we add 1 here.
#endif
 */
static int
__slbexpand(FILE *fp, size_t newsize)
{
	void *p;

#ifdef notdef
	++newsize;
#endif
	_DIAGASSERT(fp != NULL);

	/* fp->_lb._size is an int ..... */
	if (newsize > INT_MAX) {
		errno = EOVERFLOW;
		return (-1);
	}
	if ((size_t)fp->_lb._size >= newsize)
		return (0);
	if ((p = realloc(fp->_lb._base, newsize)) == NULL)
		return (-1);
	fp->_lb._base = p;
	fp->_lb._size = newsize;
	return (0);
}
Esempio n. 3
0
int
getdomainname(char *name, size_t namelen)
{
	int mib[2];
	size_t size;
	int olderrno;

	_DIAGASSERT(name != NULL);

	mib[0] = CTL_KERN;
	mib[1] = KERN_DOMAINNAME;
	size = namelen;
	olderrno = errno;
	if (sysctl(mib, 2, name, &size, NULL, 0) == -1) {
		if (errno == ENOMEM) {
			errno = olderrno;
			return (0);
		}
		return (-1);
	}

	return (0);
}
Esempio n. 4
0
/*
 * seek to an entry in a directory.
 * Only values returned by "telldir" should be passed to seekdir.
 */
void
_seekdir_unlocked(DIR *dirp, long loc)
{
	struct dirpos *lp;

	_DIAGASSERT(dirp != NULL);

	for (lp = dirp->dd_internal; lp; lp = lp->dp_next)
		if ((intptr_t)lp == loc)
			break;

	if (lp == NULL)
		return;

	if (lp->dp_loc == dirp->dd_loc && lp->dp_seek == dirp->dd_seek)
		return;

	dirp->dd_seek = lseek(dirp->dd_fd, lp->dp_seek, SEEK_SET);
	dirp->dd_loc = 0;
	while (dirp->dd_loc < lp->dp_loc)
		if (_readdir_unlocked(dirp, 0) == NULL)
			break;
}
Esempio n. 5
0
char *
RMD160File(char *filename, char *buf)
{
    uint8_t buffer[BUFSIZ];
    RMD160_CTX ctx;
    int fd, num, oerrno;

    _DIAGASSERT(filename != NULL);
    /* XXX: buf may be NULL ? */

    RMD160Init(&ctx);

    if ((fd = open(filename, O_RDONLY)) < 0)
	return(0);

    while ((num = read(fd, buffer, sizeof(buffer))) > 0)
	RMD160Update(&ctx, buffer, (size_t)num);

    oerrno = errno;
    close(fd);
    errno = oerrno;
    return(num < 0 ? 0 : RMD160End(&ctx, buf));
}
Esempio n. 6
0
/*ARGSUSED*/
static int
_compat_getgrgid(void *nsrv, void *nscb, va_list ap)
{
	struct group	**retval = va_arg(ap, struct group **);
	gid_t		 gid	= va_arg(ap, gid_t);

	int	rv, rerror;

	_DIAGASSERT(retval != NULL);

	*retval = NULL;
	rv = __grstart_compat(&_compat_state);
	if (rv != NS_SUCCESS)
		return rv;
	rv = __grscan_compat(&rerror, &_compat_group,
	    _compat_groupbuf, sizeof(_compat_groupbuf),
	    &_compat_state, 1, NULL, gid, NULL, NULL);
	if (!_compat_state.stayopen)
		__grend_compat(&_compat_state);
	if (rv == NS_SUCCESS)
		*retval = &_compat_group;
	return rv;
}
Esempio n. 7
0
/*ARGSUSED*/
static int
_files_getgrnam(void *nsrv, void *nscb, va_list ap)
{
	struct group	**retval = va_arg(ap, struct group **);
	const char	*name	= va_arg(ap, const char *);

	int	rv, rerror;

	_DIAGASSERT(retval != NULL);

	*retval = NULL;
	rv = __grstart_files(&_files_state);
	if (rv != NS_SUCCESS)
		return rv;
	rv = __grscan_files(&rerror, &_files_group,
	    _files_groupbuf, sizeof(_files_groupbuf),
	    &_files_state, 1, name, 0);
	if (!_files_state.stayopen)
		__grend_files(&_files_state);
	if (rv == NS_SUCCESS)
		*retval = &_files_group;
	return rv;
}
Esempio n. 8
0
int
__grstart_nis(struct __grstate_nis *state)
{

	_DIAGASSERT(state != NULL);

	state->done = 0;
	if (state->current) {
		free(state->current);
		state->current = NULL;
	}
	if (state->domain == NULL) {			/* setup NIS */
		switch (yp_get_default_domain(&state->domain)) {
		case 0:
			break;
		case YPERR_RESRC:
			return NS_TRYAGAIN;
		default:
			return NS_UNAVAIL;
		}
	}
	return NS_SUCCESS;
}
Esempio n. 9
0
char           *
SHA256_File(char *filename, char *buf)
{
	unsigned char          buffer[BUFSIZ * 20];
	SHA256_CTX      ctx;
	int             fd, num, oerrno;

	_DIAGASSERT(filename != NULL);
	/* XXX: buf may be NULL ? */

	SHA256_Init(&ctx);

	if ((fd = open(filename, O_RDONLY)) < 0)
		return (0);

	while ((num = read(fd, buffer, sizeof(buffer))) > 0)
		SHA256_Update(&ctx, buffer, (size_t) num);

	oerrno = errno;
	close(fd);
	errno = oerrno;
	return (num < 0 ? 0 : SHA256_End(&ctx, buf));
}
static bool_t
xdrrec_putbytes(XDR *xdrs, const char *addr, u_int len)
{
	RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private);
	size_t current;

	while (len > 0) {
		current = (size_t)((u_long)rstrm->out_boundry -
		    (u_long)rstrm->out_finger);
		current = (len < current) ? len : current;
		memmove(rstrm->out_finger, addr, current);
		rstrm->out_finger += current;
		addr += current;
		_DIAGASSERT(__type_fit(u_int, current));
		len -= (u_int)current;
		if (rstrm->out_finger == rstrm->out_boundry) {
			rstrm->frag_sent = TRUE;
			if (! flush_out(rstrm, FALSE))
				return (FALSE);
		}
	}
	return (TRUE);
}
Esempio n. 11
0
/*
 - dupl - emit a duplicate of a bunch of sops
 == static sopno dupl(struct parse *p, sopno start, sopno finish);
 */
static sopno			/* start of duplicate */
dupl(
    struct parse *p,
    sopno start,			/* from here */
    sopno finish)			/* to this less one */
{
	sopno ret;
	sopno len = finish - start;

	_DIAGASSERT(p != NULL);

	ret = HERE();

	assert(finish >= start);
	if (len == 0)
		return(ret);
	enlarge(p, p->ssize + len);	/* this many unexpected additions */
	assert(p->ssize >= p->slen + len);
	(void)memcpy(p->strip + p->slen, p->strip + start,
	    (size_t)len * sizeof(sop));
	p->slen += len;
	return(ret);
}
Esempio n. 12
0
static void
svc_dg_destroy(SVCXPRT *xprt)
{
	struct svc_dg_data *su;

	_DIAGASSERT(xprt != NULL);

	su = su_data(xprt);

	xprt_unregister(xprt);
	if (xprt->xp_fd != -1)
		(void)close(xprt->xp_fd);
	XDR_DESTROY(&(su->su_xdrs));
	(void) mem_free(rpc_buffer(xprt), su->su_iosz);
	(void) mem_free(su, sizeof (*su));
	if (xprt->xp_rtaddr.buf)
		(void) mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
	if (xprt->xp_ltaddr.buf)
		(void) mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
	if (xprt->xp_tp)
		(void) free(xprt->xp_tp);
	(void) mem_free(xprt, sizeof (SVCXPRT));
}
/*
 * The feraiseexcept() function shall attempt to raise the supported
 * floating-point exceptions represented by the argument excepts. The order
 * in which these floating-point exceptions are raised is unspecified. 
 */
int
feraiseexcept(int excepts)
{
#ifndef lint
	_DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
#endif
#ifdef __SOFTFP__
	excepts &= fpgetsticky();

	if (excepts) {
		siginfo_t info;
		memset(&info, 0, sizeof info);
		info.si_signo = SIGFPE;
		info.si_pid = getpid();
		info.si_uid = geteuid();
		if (excepts & FE_UNDERFLOW)
			info.si_code = FPE_FLTUND;
		else if (excepts & FE_OVERFLOW)
			info.si_code = FPE_FLTOVF;
		else if (excepts & FE_DIVBYZERO)
			info.si_code = FPE_FLTDIV;
		else if (excepts & FE_INVALID)
			info.si_code = FPE_FLTINV;
		else if (excepts & FE_INEXACT)
			info.si_code = FPE_FLTRES;
		sigqueueinfo(getpid(), &info);
	}
#else
	unsigned int fpsr = reg_fpsr_read();
	fpsr = (fpsr & ~FPSR_CSUM) | __SHIFTIN(excepts, FPSR_CSUM);
	reg_fpsr_write(fpsr);
	unsigned int fpcr = reg_fpcr_read();
	fpcr = (fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM);
	reg_fpcr_write(fpcr);
#endif
	return 0;
}
Esempio n. 14
0
int
asprintf(char **str, char const *fmt, ...)
{
	int ret;
	va_list ap;
	FILE f;
	struct __sfileext fext;
	unsigned char *_base;

	_DIAGASSERT(str != NULL);

	_FILEEXT_SETUP(&f, &fext);
	f._file = -1;
	f._flags = __SWR | __SSTR | __SALC;
	f._bf._base = f._p = malloc((size_t)128);
	if (f._bf._base == NULL)
		goto err;
	f._bf._size = f._w = 127;		/* Leave room for the NUL */
	va_start(ap, fmt);
	ret = __vfprintf_unlocked(&f, fmt, ap);
	va_end(ap);
	if (ret < 0)
		goto err;
	*f._p = '\0';
	_base = realloc(f._bf._base, (size_t)ret + 1);
	if (_base == NULL)
		goto err;
	*str = (char *)_base;
	return (ret);

err:
	if (f._bf._base)
		free(f._bf._base);
	*str = NULL;
	errno = ENOMEM;
	return (-1);
}
/*
 * putenv(name) --
 *	This version implicitly copies the string for compatibility.
 */
int
putenv(char *name)
{
	size_t l_name;
	char *copy;
	int rv;

	_DIAGASSERT(name != NULL);

        l_name = __envvarnamelen(name, true); 
        if (l_name == 0) { 
                errno = EINVAL;
                return -1;
        }

	if ((copy = strdup(name)) == NULL)
		return -1;
	copy[l_name++] = '\0';

	rv = setenv(copy, copy + l_name, 1);

	free(copy);
	return rv;
}
Esempio n. 16
0
static bool_t
clnt_vc_freeres(
	CLIENT *cl,
	xdrproc_t xdr_res,
	caddr_t res_ptr
)
{
	struct ct_data *ct;
	XDR *xdrs;
	bool_t dummy;
#ifdef _REENTRANT
	sigset_t mask;
#endif
	sigset_t newmask;

	_DIAGASSERT(cl != NULL);

	ct = (struct ct_data *)cl->cl_private;
	xdrs = &(ct->ct_xdrs);

	__clnt_sigfillset(&newmask);
	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
	mutex_lock(&clnt_fd_lock);
#ifdef _REENTRANT
	while (vc_fd_locks[ct->ct_fd])
		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
#endif

	xdrs->x_op = XDR_FREE;
	dummy = (*xdr_res)(xdrs, res_ptr);
	mutex_unlock(&clnt_fd_lock);
	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
	cond_signal(&vc_cv[ct->ct_fd]);

	return dummy;
}
Esempio n. 17
0
static const char *
__get_locale_env(int category)
{
	const char *env;

	_DIAGASSERT(category != LC_ALL);

	/* 1. check LC_ALL. */
	env = getenv(categories[0].name);

	/* 2. check LC_* */
	if (env == NULL || *env == '\0')
		env = getenv(categories[category].name);

	/* 3. check LANG */
	if (env == NULL || *env == '\0')
		env = getenv("LANG");

	/* 4. if none is set, fall to "C" */
	if (env == NULL || *env == '\0' || strchr(env, '/'))
		env = "C";

	return(env);
}
Esempio n. 18
0
wint_t
ungetwc(wint_t wc, FILE *fp)
{
  struct wchar_io_data *wcio;

  _DIAGASSERT(fp);

  if (wc == WEOF)
    return WEOF;

  FLOCKFILE(fp);
  _SET_ORIENTATION(fp, 1);
  /*
   * XXX since we have no way to transform a wchar string to
   * a char string in reverse order, we can't use ungetc.
   */
  /* XXX should we flush ungetc buffer? */

  wcio = WCIO_GET(fp);
  if (wcio == 0) {
    FUNLOCKFILE(fp);
    errno = ENOMEM; /* XXX */
    return WEOF;
  }

  if (wcio->wcio_ungetwc_inbuf >= WCIO_UNGETWC_BUFSIZE) {
    FUNLOCKFILE(fp);
    return WEOF;
  }

  wcio->wcio_ungetwc_buf[wcio->wcio_ungetwc_inbuf++] = (wchar_t)wc;
  __sclearerr(fp);
  FUNLOCKFILE(fp);

  return wc;
}
Esempio n. 19
0
/*
 - doinsert - insert a sop into the strip
 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
 */
static void
doinsert(
    struct parse *p,
    sop op,
    sopno opnd,
    sopno pos)
{
	sopno sn;
	sop s;
	int i;

	_DIAGASSERT(p != NULL);

	/* avoid making error situations worse */
	if (p->error != 0)
		return;

	sn = HERE();
	EMIT(op, opnd);		/* do checks, ensure space */
	assert(HERE() == sn+1);
	s = p->strip[sn];

	/* adjust paren pointers */
	assert(pos > 0);
	for (i = 1; i < NPAREN; i++) {
		if (p->pbegin[i] >= pos) {
			p->pbegin[i]++;
		}
		if (p->pend[i] >= pos) {
			p->pend[i]++;
		}
	}

	memmove(&p->strip[pos+1], &p->strip[pos], (HERE()-pos-1)*sizeof(sop));
	p->strip[pos] = s;
}
Esempio n. 20
0
/*
 - doemit - emit a strip operator
 == static void doemit(struct parse *p, sop op, size_t opnd);
 *
 * It might seem better to implement this as a macro with a function as
 * hard-case backup, but it's just too big and messy unless there are
 * some changes to the data structures.  Maybe later.
 */
static void
doemit(
    struct parse *p,
    sop op,
    sopno opnd)
{

	_DIAGASSERT(p != NULL);

	/* avoid making error situations worse */
	if (p->error != 0)
		return;

	/* deal with oversize operands ("can't happen", more or less) */
	assert(opnd < 1<<OPSHIFT);

	/* deal with undersized strip */
	if (p->slen >= p->ssize)
		enlarge(p, (p->ssize+1) / 2 * 3);	/* +50% */
	assert(p->slen < p->ssize);

	/* finally, it's all reduced to the easy case */
	p->strip[p->slen++] = SOP(op, opnd);
}
Esempio n. 21
0
/*
 - nonnewline - emit REG_NEWLINE version of OANY
 == static void nonnewline(struct parse *p);
 *
 * Boy, is this implementation ever a kludge...
 */
static void
nonnewline(
    struct parse *p)
{
	const char *oldnext;
	const char *oldend;
	char bracket[4];

	_DIAGASSERT(p != NULL);

	oldnext = p->next;
	oldend = p->end;

	p->next = bracket;
	p->end = bracket+3;
	bracket[0] = '^';
	bracket[1] = '\n';
	bracket[2] = ']';
	bracket[3] = '\0';
	p_bracket(p);
	assert(p->next == bracket+3);
	p->next = oldnext;
	p->end = oldend;
}
Esempio n. 22
0
int
getenv_r(const char *name, char *buf, size_t len)
{
    int offset;
    char *result;
    int rv = -1;

    _DIAGASSERT(name != NULL);

    rwlock_rdlock(&__environ_lock);
    result = __findenv(name, &offset);
    if (result == NULL) {
        errno = ENOENT;
        goto out;
    }
    if (strlcpy(buf, result, len) >= len) {
        errno = ERANGE;
        goto out;
    }
    rv = 0;
out:
    rwlock_unlock(&__environ_lock);
    return rv;
}
Esempio n. 23
0
NAN_TYPE
NAN_FUNCTION(const char *tagp)
{
    const char *nstr;
    char *buf;
    NAN_TYPE res;

    _DIAGASSERT(tagp != NULL);

    nstr = "NAN()";
    buf = NULL;

    if (tagp[0] != '\0') {
        size_t l;

        l = strlen(tagp);
        buf = malloc(5 + l + 1);

        if (buf != NULL) {
            /* Avoiding stdio in libm. */
            memcpy(buf,		"NAN(",	4);
            memcpy(buf + 4,		tagp,	l);
            memcpy(buf + 4 + l,	")",	2);
            nstr = buf;
        } else {
            /* Best effort: Fall back to "NAN()". */
        }
    }

    res = NAN_STRTOD(nstr, NULL);

    if (buf != NULL)
        free(buf);

    return res;
}
Esempio n. 24
0
/* fparseln():
 *	Read a line from a file parsing continuations ending in \
 *	and eliminating trailing newlines, or comments starting with
 *	the comment char.
 */
char *
fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
{
	static const char dstr[3] = { '\\', '\\', '#' };

	size_t	s, len;
	char   *buf;
	char   *ptr, *cp;
	int	cnt;
	char	esc, con, nl, com;

#if 0
	_DIAGASSERT(fp != NULL);
#endif

	len = 0;
	buf = NULL;
	cnt = 1;

	if (str == NULL)
		str = dstr;

	esc = str[0];
	con = str[1];
	com = str[2];
	/*
	 * XXX: it would be cool to be able to specify the newline character,
	 * but unfortunately, fgetln does not let us
	 */
	nl  = '\n';

	while (cnt) {
		cnt = 0;

		if (lineno)
			(*lineno)++;

		if ((ptr = fgetln(fp, &s)) == NULL)
			break;

		if (s && com) {		/* Check and eliminate comments */
			for (cp = ptr; cp < ptr + s; cp++)
				if (*cp == com && !isescaped(ptr, cp, esc)) {
					s = cp - ptr;
					cnt = s == 0 && buf == NULL;
					break;
				}
		}

		if (s && nl) { 		/* Check and eliminate newlines */
			cp = &ptr[s - 1];

			if (*cp == nl)
				s--;	/* forget newline */
		}

		if (s && con) {		/* Check and eliminate continuations */
			cp = &ptr[s - 1];

			if (*cp == con && !isescaped(ptr, cp, esc)) {
				s--;	/* forget escape */
				cnt = 1;
			}
		}

		if (s == 0 && buf != NULL)
			continue;

		if ((cp = realloc(buf, len + s + 1)) == NULL) {
			free(buf);
			return NULL;
		}
		buf = cp;

		(void) memcpy(buf + len, ptr, s);
		len += s;
		buf[len] = '\0';
	}

	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
	    strchr(buf, esc) != NULL) {
		ptr = cp = buf;
		while (cp[0] != '\0') {
			int skipesc;

			while (cp[0] != '\0' && cp[0] != esc)
				*ptr++ = *cp++;
			if (cp[0] == '\0' || cp[1] == '\0')
				break;

			skipesc = 0;
			if (cp[1] == com)
				skipesc += (flags & FPARSELN_UNESCCOMM);
			if (cp[1] == con)
				skipesc += (flags & FPARSELN_UNESCCONT);
			if (cp[1] == esc)
				skipesc += (flags & FPARSELN_UNESCESC);
			if (cp[1] != com && cp[1] != con && cp[1] != esc)
				skipesc = (flags & FPARSELN_UNESCREST);

			if (skipesc)
				cp++;
			else
				*ptr++ = *cp++;
			*ptr++ = *cp++;
		}
		*ptr = '\0';
		len = strlen(buf);
	}

	if (size)
		*size = len;
	return buf;
}
Esempio n. 25
0
/*
 * A common clnt create routine
 */
static CLIENT *
clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers,
	int *sockp, u_int sendsz, u_int recvsz, const char *tp)
{
	CLIENT *cl;
	int madefd = FALSE;
	int fd;
	struct netconfig *nconf;
	struct netbuf bindaddr;

	_DIAGASSERT(raddr != NULL);
	_DIAGASSERT(sockp != NULL);
	_DIAGASSERT(tp != NULL);

	fd = *sockp;

	mutex_lock(&rpcsoc_lock);
	if ((nconf = __rpc_getconfip(tp)) == NULL) {
		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
		mutex_unlock(&rpcsoc_lock);
		return (NULL);
	}
	if (fd == RPC_ANYSOCK) {
		fd = __rpc_nconf2fd(nconf);
		if (fd == -1)
			goto syserror;
		madefd = TRUE;
	}

	if (raddr->sin_port == 0) {
		u_int proto;
		u_short sport;

		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
		    proto);
		if (sport == 0) {
			goto err;
		}
		raddr->sin_port = htons(sport);
		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
	}

	/* Transform sockaddr_in to netbuf */
	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
	bindaddr.buf = raddr;

	(void)bindresvport(fd, NULL);
	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
				sendsz, recvsz);
	if (cl) {
		if (madefd == TRUE) {
			/*
			 * The fd should be closed while destroying the handle.
			 */
			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
			*sockp = fd;
		}
		(void) freenetconfigent(nconf);
		mutex_unlock(&rpcsoc_lock);
		return (cl);
	}
	goto err;

syserror:
	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
	rpc_createerr.cf_error.re_errno = errno;

err:	if (madefd == TRUE)
		(void) close(fd);
	(void) freenetconfigent(nconf);
	mutex_unlock(&rpcsoc_lock);
	return (NULL);
}
Esempio n. 26
0
/*
 * __grscan_dns
 *	Search Hesiod for the next desired entry.
 *	If search is zero, return the next entry.
 *	If search is non-zero, look for a specific name (if name != NULL),
 *	or a specific gid (if name == NULL).
 */
int
__grscan_dns(int *retval, struct group *grp, char *buffer, size_t buflen,
	struct __grstate_dns *state, int search, const char *name, gid_t gid)
{
	const char	**curzone;
	char		**hp, *ep;
	int		rv;

	static const char *zones_gid_group[] = {
		"gid",
		"group",
		NULL
	};

	static const char *zones_group[] = {
		"group",
		NULL
	};

	_DIAGASSERT(retval != NULL);
	_DIAGASSERT(grp != NULL);
	_DIAGASSERT(buffer != NULL);
	_DIAGASSERT(state != NULL);
	/* name is NULL to indicate searching for gid */

	*retval = 0;

	if (state->context == NULL) {	/* only start if Hesiod not setup */
		rv = __grstart_dns(state);
		if (rv != NS_SUCCESS)
			return rv;
	}

 next_dns_entry:
	hp = NULL;
	rv = NS_NOTFOUND;

	if (! search) {			/* find next entry */
		if (state->num == -1)		/* exhausted search */
			return NS_NOTFOUND;
						/* find group-NNN */
		snprintf(buffer, buflen, "group-%u", state->num);
		state->num++;
		curzone = zones_group;
	} else if (name) {		/* find group name */
		snprintf(buffer, buflen, "%s", name);
		curzone = zones_group;
	} else {			/* find gid */
		snprintf(buffer, buflen, "%u", (unsigned int)gid);
		curzone = zones_gid_group;
	}

	for (; *curzone; curzone++) {		/* search zones */
		hp = hesiod_resolve(state->context, buffer, *curzone);
		if (hp != NULL)
			break;
		if (errno != ENOENT) {
			rv = NS_UNAVAIL;
			goto dnsgrscan_out;
		}
	}
	if (*curzone == NULL) {
		if (! search)
			state->num = -1;
		goto dnsgrscan_out;
	}

	if ((ep = strchr(hp[0], '\n')) != NULL)
		*ep = '\0';				/* clear trailing \n */
	if (_gr_parse(hp[0], grp, buffer, buflen)) {	/* validate line */
		if (! search) {				/* just want this one */
			rv = NS_SUCCESS;
		} else if ((name && strcmp(name, grp->gr_name) == 0) ||
		    (!name && gid == grp->gr_gid)) {	/* want specific */
			rv = NS_SUCCESS;
		}
	} else {					/* dodgy entry */
		if (!search) {			/* try again if ! searching */
			hesiod_free_list(state->context, hp);
			goto next_dns_entry;
		}
	}

 dnsgrscan_out:
	if (rv != NS_SUCCESS && rv != NS_NOTFOUND)
		*retval = errno;
	if (hp)
		hesiod_free_list(state->context, hp);
	return rv;
}
Esempio n. 27
0
static int
_initctypemodule(_citrus_ctype_t cc, char const *modname,
		 _citrus_module_t handle, void *variable, size_t lenvar,
		 size_t szpriv)
{
	int ret;
	_citrus_ctype_getops_t getops;

	_DIAGASSERT(cc != NULL);

	cc->cc_module = handle;

	getops = (_citrus_ctype_getops_t)_citrus_find_getops(cc->cc_module,
							     modname,
							     "ctype");
	if (getops == NULL)
		return (EINVAL);

	cc->cc_ops = (_citrus_ctype_ops_rec_t *)malloc(sizeof(*cc->cc_ops));
	if (cc->cc_ops == NULL)
		return (ENOMEM);

	ret = (*getops)(cc->cc_ops, sizeof(*cc->cc_ops),
			_CITRUS_CTYPE_ABI_VERSION);
	if (ret)
		goto bad;

	/* If return ABI version is not expected, fixup it here*/
	switch (cc->cc_ops->co_abi_version) {
	case 0x00000001:
		cc->cc_ops->co_btowc = &_citrus_ctype_btowc_fallback;
		cc->cc_ops->co_wctob = &_citrus_ctype_wctob_fallback;
		/* FALLTHROUGH */
	case 0x00000002:
		/* FALLTHROUGH */
	default:
		break;
	}

	/* validation check */
	if (cc->cc_ops->co_init == NULL ||
	    cc->cc_ops->co_uninit == NULL ||
	    cc->cc_ops->co_get_mb_cur_max == NULL ||
	    cc->cc_ops->co_mblen == NULL ||
	    cc->cc_ops->co_mbrlen == NULL ||
	    cc->cc_ops->co_mbrtowc == NULL ||
	    cc->cc_ops->co_mbsinit == NULL ||
	    cc->cc_ops->co_mbsrtowcs == NULL ||
	    cc->cc_ops->co_mbstowcs == NULL ||
	    cc->cc_ops->co_mbtowc == NULL ||
	    cc->cc_ops->co_wcrtomb == NULL ||
	    cc->cc_ops->co_wcsrtombs == NULL ||
	    cc->cc_ops->co_wcstombs == NULL ||
	    cc->cc_ops->co_wctomb == NULL ||
	    cc->cc_ops->co_btowc == NULL ||
	    cc->cc_ops->co_wctob == NULL) {
		ret = EINVAL;
		goto bad;
	}

	/* init and get closure */
	ret = (*cc->cc_ops->co_init)(
		&cc->cc_closure, variable, lenvar, szpriv);
	if (ret)
		goto bad;

	return (0);

bad:
	if (cc->cc_ops)
		free(cc->cc_ops);
	cc->cc_ops = NULL;

	return (ret);
}
Esempio n. 28
0
char *
captoinfo(char *cap)
{
	char *info, *ip, *token, *val, *p, tok[3];
	const char *name;
	size_t len, lp, nl, vl, rl;
	int defs[__arraycount(def_infos)], fv;

	_DIAGASSERT(cap != NULL);

	len = strlen(cap) * 2;
	len += __arraycount(def_infos) * (5 + 4 + 3); /* reserve for defs */
	info = ip = malloc(len);
	if (info == NULL)
		return NULL;

	memset(defs, 0, sizeof(defs));
	lp = 0;
	tok[2] = '\0';
	for (token = _ti_get_token(&cap, ':');
	     token != NULL;
	     token = _ti_get_token(&cap, ':'))
	{
		if (token[0] == '\0')
			continue;
		name = token;
		val = p = NULL;
		fv = nl = 0;
		if (token[1] != '\0') {
			tok[0] = token[0];
			tok[1] = token[1];
			nl = 1;
			if (token[2] == '\0') {
				name = flagname(tok);
				val = NULL;
			} else if (token[2] == '#') {
				name = numname(tok);
				val = token + 2;
			} else if (token[2] == '=') {
				name = strname(tok);
				val = strval(token + 2);
				fv = 1;
			} else
				nl = 0;
		}
		/* If not matched we may need to convert padding still. */
		if (nl == 0) {
			p = strchr(name, '=');
			if (p != NULL) {
				val = strval(p);
				*p = '\0';
				fv = 1;
			}
		}

		/* See if this sets a default. */
		for (nl = 0; nl < __arraycount(def_infos); nl++) {
			if (strcmp(name, def_infos[nl].name) == 0) {
				defs[nl] = 1;
				break;
			}
		}

		nl = strlen(name);
		if (val == NULL)
			vl = 0;
		else
			vl = strlen(val);
		rl = nl + vl + 3; /* , \0 */

		if (lp + rl > len) {
			if (rl < 256)
				len += 256;
			else
				len += rl;
			p = realloc(info, len);
			if (p == NULL) {
				if (fv == 1)
					free(val);
				return NULL;
			}
			info = p;
		}

		if (ip != info) {
			*ip++ = ',';
			*ip++ = ' ';
		}

		strcpy(ip, name);
		ip += nl;
		if (val != NULL) {
			strcpy(ip, val);
			ip += vl;
			if (fv == 1)
				free(val);
		}
	}

	/* Add any defaults not set above. */
	for (nl = 0; nl < __arraycount(def_infos); nl++) {
		if (defs[nl] == 0) {
			*ip++ = ',';
			*ip++ = ' ';
			strcpy(ip, def_infos[nl].name);
			ip += strlen(def_infos[nl].name);
			*ip++ = '=';
			strcpy(ip, def_infos[nl].cap);
			ip += strlen(def_infos[nl].cap);
		}
	}

	*ip = '\0';
	return info;
}
Esempio n. 29
0
char *
tgoto(const char *cm, int destcol, int destline)
{
	_DIAGASSERT(cm != NULL);
	return tiparm(cm, destline, destcol);
}
Esempio n. 30
0
/*
 * __grscan_nis
 *	Search NIS for the next desired entry.
 *	If search is zero, return the next entry.
 *	If search is non-zero, look for a specific name (if name != NULL),
 *	or a specific gid (if name == NULL).
 */
int
__grscan_nis(int *retval, struct group *grp, char *buffer, size_t buflen,
	struct __grstate_nis *state, int search, const char *name, gid_t gid)
{
	const char *map;
	char	*key, *data;
	int	nisr, rv, keylen, datalen;

	_DIAGASSERT(retval != NULL);
	_DIAGASSERT(grp != NULL);
	_DIAGASSERT(buffer != NULL);
	_DIAGASSERT(state != NULL);
	/* name is NULL to indicate searching for gid */

	*retval = 0;

	if (state->domain == NULL) {	/* only start if NIS not setup */
		rv = __grstart_nis(state);
		if (rv != NS_SUCCESS)
			return rv;
	}

 next_nis_entry:
	key = NULL;
	data = NULL;
	rv = NS_SUCCESS;

	if (! search) 	{			/* find next entry */
		if (state->done)			/* exhausted search */
			return NS_NOTFOUND;
		map = "group.byname";
		if (state->current) {			/* already searching */
			nisr = yp_next(state->domain, map,
			    state->current, state->currentlen,
			    &key, &keylen, &data, &datalen);
			free(state->current);
			state->current = NULL;
			switch (nisr) {
			case 0:
				state->current = key;
				state->currentlen = keylen;
				key = NULL;
				break;
			case YPERR_NOMORE:
				rv = NS_NOTFOUND;
				state->done = 1;
				break;
			default:
				rv = NS_UNAVAIL;
				break;
			}
		} else {				/* new search */
			if (yp_first(state->domain, map,
			    &state->current, &state->currentlen,
			    &data, &datalen)) {
				rv = NS_UNAVAIL;
			}
		}
	} else {				/* search for specific item */
		if (name) {			/* find group name */
			snprintf(buffer, buflen, "%s", name);
			map = "group.byname";
		} else {			/* find gid */
			snprintf(buffer, buflen, "%u", (unsigned int)gid);
			map = "group.bygid";
		}
		nisr = yp_match(state->domain, map, buffer, (int)strlen(buffer),
		    &data, &datalen);
		switch (nisr) {
		case 0:
			break;
		case YPERR_KEY:
			rv = NS_NOTFOUND;
			break;
		default:
			rv = NS_UNAVAIL;
			break;
		}
	}
	if (rv == NS_SUCCESS) {				/* validate data */
		data[datalen] = '\0';			/* clear trailing \n */
		if (_gr_parse(data, grp, buffer, buflen)) {
			if (! search) {			/* just want this one */
				rv = NS_SUCCESS;
			} else if ((name && strcmp(name, grp->gr_name) == 0) ||
			    (!name && gid == grp->gr_gid)) {
							/* want specific */
				rv = NS_SUCCESS;
			}
		} else {				/* dodgy entry */
			if (!search) {		/* try again if ! searching */
				free(data);
				goto next_nis_entry;
			}
		}
	}

	if (rv != NS_SUCCESS && rv != NS_NOTFOUND)
		*retval = errno;
	if (key)
		free(key);
	if (data)
		free(data);
	return rv;
}