xref: /freebsd/lib/libnetbsd/util.c (revision 871afaefab95343d61a163b185824d9e9dd60088)
16bea8766SBrooks Davis /*-
26bea8766SBrooks Davis  * Copyright (c) 2012 SRI International
36bea8766SBrooks Davis  * All rights reserved.
46bea8766SBrooks Davis  *
56bea8766SBrooks Davis  * This software was developed by SRI International and the University of
66bea8766SBrooks Davis  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
76bea8766SBrooks Davis  * ("CTSRD"), as part of the DARPA CRASH research programme.
86bea8766SBrooks Davis  *
96bea8766SBrooks Davis  * Redistribution and use in source and binary forms, with or without
106bea8766SBrooks Davis  * modification, are permitted provided that the following conditions
116bea8766SBrooks Davis  * are met:
126bea8766SBrooks Davis  * 1. Redistributions of source code must retain the above copyright
136bea8766SBrooks Davis  *    notice, this list of conditions and the following disclaimer.
146bea8766SBrooks Davis  * 2. Redistributions in binary form must reproduce the above copyright
156bea8766SBrooks Davis  *    notice, this list of conditions and the following disclaimer in the
166bea8766SBrooks Davis  *    documentation and/or other materials provided with the distribution.
176bea8766SBrooks Davis  *
186bea8766SBrooks Davis  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
196bea8766SBrooks Davis  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
206bea8766SBrooks Davis  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
216bea8766SBrooks Davis  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
226bea8766SBrooks Davis  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
236bea8766SBrooks Davis  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
246bea8766SBrooks Davis  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
256bea8766SBrooks Davis  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
266bea8766SBrooks Davis  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
276bea8766SBrooks Davis  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286bea8766SBrooks Davis  * SUCH DAMAGE.
296bea8766SBrooks Davis  *
306bea8766SBrooks Davis  * $FreeBSD$
316bea8766SBrooks Davis  */
326bea8766SBrooks Davis 
336bea8766SBrooks Davis #include <sys/cdefs.h>
346bea8766SBrooks Davis #include <sys/types.h>
356bea8766SBrooks Davis 
366bea8766SBrooks Davis #include <stdlib.h>
376bea8766SBrooks Davis #include <string.h>
386bea8766SBrooks Davis #include <unistd.h>
39*871afaefSMarcel Moolenaar 
40*871afaefSMarcel Moolenaar #include "util.h"
416bea8766SBrooks Davis 
426bea8766SBrooks Davis char *
436bea8766SBrooks Davis flags_to_string(u_long flags, const char *def)
446bea8766SBrooks Davis {
456bea8766SBrooks Davis 	char *str;
466bea8766SBrooks Davis 
476bea8766SBrooks Davis 	str = fflagstostr(flags);
486bea8766SBrooks Davis 	if (*str == '\0') {
496bea8766SBrooks Davis 		free(str);
506bea8766SBrooks Davis 		str = strdup(def);
516bea8766SBrooks Davis 	}
526bea8766SBrooks Davis 	return (str);
536bea8766SBrooks Davis }
546bea8766SBrooks Davis 
556bea8766SBrooks Davis int
566bea8766SBrooks Davis string_to_flags(char **stringp, u_long *setp, u_long *clrp)
576bea8766SBrooks Davis {
586bea8766SBrooks Davis 
596bea8766SBrooks Davis 	return strtofflags(stringp, setp, clrp);
606bea8766SBrooks Davis }
61