2 * (c) Copyright 1992 by Panagiotis Tsirigotis
3 * (c) Sections Copyright 1998-2001 by Rob Braun
4 * All rights reserved. The file named COPYRIGHT specifies the terms
5 * and conditions for redistribution.
10 #include <sys/types.h>
14 #if defined (HAVE_SYS_SOCKET_H)
15 #include <sys/socket.h>
18 * The following ifdef is for TIOCNOTTY
21 #ifdef HAVE_SYS_TERMIOS_H
22 #include <sys/termios.h>
28 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_FILE_H
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
48 void out_of_memory( const char *func )
50 msg( LOG_CRIT, func, ES_NOMEM ) ;
54 const struct name_value *nv_find_value( const struct name_value nv_array[], const char *name )
56 const struct name_value *nvp ;
58 for ( nvp = nv_array ; nvp->name ; nvp++ )
60 if ( EQ( name, nvp->name ) )
67 const struct name_value *nv_find_name( const struct name_value nv_array[], int value )
69 const struct name_value *nvp ;
71 for ( nvp = nv_array ; nvp->name ; nvp++ )
73 if ( value == nvp->value )
81 * A name-value list is exactly what its name says.
82 * The functions nv_get_name() and nv_get_value() return a pointer to
83 * the entry with the specified value or name respectively.
85 * The list ends when an antry with a NULL name is encountered.
86 * The value field of that entry is treated in a special manner: if it
87 * is non-zero, it is assumed that there exists one more entry whose
88 * name field will be returned by the nv_get_name function if it can't
89 * find an entry whose value field is equal to its 2nd parameter.
90 * If the value field of the NULL entry is 0, then nv_get_name() will
93 const char *nv_get_name( const struct name_value nv_array[], int value )
95 const struct name_value *nvp ;
97 for ( nvp = nv_array ; nvp->name ; nvp++ )
99 if ( value == nvp->value )
100 return( nvp->name ) ;
102 return( nvp->value ? (nvp+1)->name : NULL ) ;
107 char **argv_alloc( unsigned count )
109 unsigned argv_size = (count + 1) * sizeof( char *) ;
111 const char *func = "new_argv" ;
113 argv = (char **) malloc( argv_size ) ;
116 out_of_memory( func ) ;
119 (void) memset( (char *)argv, 0, argv_size ) ;
125 * If size is 0, the pset holds strings
127 status_e copy_pset( const pset_h from, pset_h *to, unsigned size )
130 const char *func = "copy_pset" ;
134 *to = pset_create( pset_count( from ), 0 ) ;
137 out_of_memory( func ) ;
142 for ( u = 0 ; u < pset_count( from ) ; u++ )
144 char *p = (char *) pset_pointer( from, u ) ;
148 new_s = new_string( p ) ;
150 new_s = (char *)malloc( size ) ;
154 out_of_memory( func ) ;
159 (void) memcpy( new_s, p, size ) ;
161 if ( pset_add( *to, new_s ) == NULL )
164 out_of_memory( func ) ;
173 * Disassociate from controlling terminal
175 void no_control_tty(void)
177 #if !defined(HAVE_SETSID)
179 const char *func = "no_control_tty" ;
181 if ( ( fd = open( "/dev/tty", O_RDWR ) ) == -1 )
182 msg( LOG_WARNING, func, "open of /dev/tty failed: %m" ) ;
185 if ( ioctl( fd, TIOCNOTTY, (caddr_t)0 ) == -1 )
186 msg( LOG_WARNING, func, "ioctl on /dev/tty failed: %m" ) ;
187 (void) Sclose( fd ) ;
189 (void) setpgrp( getpid(), 0 ) ;
197 * Write the whole buffer to the given file descriptor ignoring interrupts
199 status_e write_buf( int fd, const char *buf, int len )
203 for ( i = 0 ; len > 0 ; i += cc, len -= cc )
205 cc = write( fd, buf+i, len ) ;
208 if ( errno != EINTR )
217 void tabprint( int fd, int tab_level, const char *fmt, ...)
222 for ( i = 0 ; i < tab_level ; i++ )
223 Sputchar( fd, '\t' ) ;
225 va_start( ap, fmt ) ;
226 Sprintv( fd, fmt, ap ) ;
232 * Empty the socket receive buffers of all data.
236 char buf[ 256 ] ; /* This size is arbitrarily chosen */
240 /* Put in non-blocking mode so we don't hang. */
241 old_val = fcntl( sd, F_GETFL, FNDELAY );
242 if ( fcntl( sd, F_SETFL, FNDELAY ) < 0 )
245 msg( LOG_DEBUG, "drain",
246 "UDP socket could not be made non-blocking: %m" ) ;
251 ret = recv( sd, buf, sizeof( buf ), 0 ) ;
254 /* Restore the value since the connection will be freed, not closed. */
256 fcntl( sd, F_SETFL, old_val );
259 msg( LOG_DEBUG, "drain", "UDP socket should be empty" ) ;
263 * Convert string to an int detecting errors.
265 int parse_int(const char *str, int base, int term, int *res)
271 * "Because 0, LONG_MIN and LONG_MAX are returned on error and are also
272 * valid returns on success, an application wishing to check for error
273 * situations should set errno to 0, then call strtol(), then check errno." */
275 strtol_res = strtol(str, (char **)&endptr, base);
277 if (errno == 0 && *str != NUL) {
278 /* Special case: -1 means allow trailing whitespace */
280 while (*endptr != NUL && isspace(*endptr))
285 if (*endptr == term) {
295 int parse_uint(const char *str, int base, int term, unsigned int *res)
297 unsigned long long tmp;
299 ret = parse_ull(str, base, term, &tmp);
300 *res = (unsigned int)tmp;
304 int parse_ull(const char *str, int base, int term, unsigned long long *res)
307 unsigned long long strtol_res;
310 * "Because 0, LONG_MIN and LONG_MAX are returned on error and are also
311 * valid returns on success, an application wishing to check for error
312 * situations should set errno to 0, then call strtol(), then check errno." */
314 strtol_res = strtoull(str, (char **)&endptr, base);
316 if (errno == 0 && *str != NUL) {
317 /* Special case: -1 means allow trailing whitespace */
319 while (*endptr != NUL && isspace(*endptr))
324 if (*endptr == term) {
334 int parse_ubase10(const char *str, unsigned int *res)
336 return parse_uint(str, 10, -1, res);
339 int parse_base10(const char *str, int *res)
341 return parse_int(str, 10, -1, res);
344 bool_int parse_all_digits(const char *ptr)
346 size_t num=0, len = strlen(ptr);
348 while (isdigit(*ptr++))