version 0.9.6
[rofl0r-htun.git] / include / util.h
bloba6a85546d72723e19f5be3cab4a5acd9175b6e7a
1 /* -------------------------------------------------------------------------
2 * util.h - htun miscellaneous utility functions
3 * Copyright (C) 2002 Moshe Jacobson <moshe@runslinux.net>,
4 * Ola Nordström <ola@triblock.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * -------------------------------------------------------------------------
21 /* $Id: util.h,v 2.13 2002/08/01 04:51:05 jehsom Exp $ */
23 #ifndef __UTIL_H
24 #define __UTIL_H
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <stdarg.h>
29 #include <ctype.h>
31 #undef __EI
32 #define __EI extern __inline__
35 * Provides a strncasecmp for non-BSD C libraries
37 inline int xstrncasecmp( const char *s1, const char *s2, int n );
39 __EI
40 int xstrcasecmp( const char *s1, const char *s2 ) {
41 return xstrncasecmp( s1, s2, 0 );
44 /*
45 * fprintf()-like function but uses a file descriptor or socket descriptor
46 * instead of a FILE*.
48 int fdprintf(int fd, char *fmt, ...);
51 * receives a line of data from fd, and puts up to len bytes into buf.
52 * Returns NULL if there was no data, or buf on success.
54 char *recvline( char *buf, int len, int fd );
57 * Ensures there is no extra data waiting to be received on fd. If there is,
58 * it is discarded, and the number of discarded bytes is returned.
60 int recvflush( int fd );
62 /*
63 * Reads exactly len bytes from fd and returns the data in a dynamically
64 * allocated buffer
66 char *readloop( int fd, size_t len );
69 * Takes in a char * and returns a dynamically allocated array of pointers to
70 * the start of each line. The pointer after the last is set to NULL to
71 * indicate that there are no more pointers after it. Every occurance of '\n'
72 * in the input string is CHANGED to '\0' to ease the reading of the strings.
74 char **splitlines( char *buf );
77 * Removes any trailing whitespace from str by moving up the null pointer.
79 __EI
80 char *chomp( char *str ){
81 int i=strlen(str)-1;
82 while( i >= 0 ){
83 if( isspace((int)str[i]) ) str[i--]='\0'; else break;
85 return str;
89 * Pass in a hostname, a pointer to a user supplied hostent struct, a
90 * sufficiently-sized buffer for user by gethostbyname_r(), and the length of
91 * the buffer, and resolve() fills in the hostent struct for you, returning a
92 * pointer to it on success, or NULL on failure.
94 struct hostent *
95 resolve( const char *name, struct hostent *hostbuf, char *buf, size_t len );
98 * Pass in a string you wish to base64-encode in "from", and the length of
99 * that string in "len". "to" should be a buffer at least 3*len long.
100 * The null-terminated base64-encoded result will be placed into "to".
101 * Returns the number of characters placed into "to".
103 long base64_encode( char *to, const char *from, unsigned int len );
105 #endif