The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
misc.h
Go to the documentation of this file.
1 /* This file is part of Netsukuku
2  * (c) Copyright 2004 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
3  *
4  * This source code is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This source code is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * Please refer to the GNU Public License for more details.
13  *
14  * You should have received a copy of the GNU Public License along with
15  * this source code; if not, write to:
16  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #ifndef MISC_H
20 #define MISC_H
21 
22 /*
23  * NMEMB: returns the number of members of the `x' array
24  */
25 #define NMEMB(x) (sizeof((x))/sizeof(typeof((x)[0])))
26 
27 /*
28  * MILLISEC: converts a timeval struct to a int. The time will be returned in
29  * milliseconds.
30  */
31 #define MILLISEC(x) (((x).tv_sec*1000)+((x).tv_usec/1000))
32 
33 /*
34  * MILLISEC_TO_TV: Converts `x', which is an int into `t', a timeval struct
35  */
36 #define MILLISEC_TO_TV(x,t) \
37 do{ \
38  (t).tv_sec=(x)/1000; \
39  (t).tv_usec=((x) - ((x)/1000)*1000)*1000; \
40 }while(0)
41 
42 /*
43  * Bit map related macros.
44  */
45 #define SET_BIT(a,i) ((a)[(i)/CHAR_BIT] |= 1<<((i)%CHAR_BIT))
46 #define CLR_BIT(a,i) ((a)[(i)/CHAR_BIT] &= ~(1<<((i)%CHAR_BIT)))
47 #define TEST_BIT(a,i) (((a)[(i)/CHAR_BIT] & (1<<((i)%CHAR_BIT))) ? 1 : 0)
48 
49 /*
50  * FIND_PTR
51  *
52  * Given an array of pointers `a' of `n' members, it searches for a member
53  * equal to the pointer `p'. If it is found its position is returned,
54  * otherwise -1 is the value returned.
55  */
56 #define FIND_PTR(p, a, n) \
57 ({ \
58  int _i, _ret; \
59  \
60  for(_i=0, _ret=-1; _i<(n); _i++) \
61  if((a)[_i] == (p)) { \
62  _ret=_i; \
63  break; \
64  } \
65  _ret; \
66 })
67 
68 /*
69  * _return
70  *
71  * It is used in this case:
72  * condition && _return (ret);
73  * Since it is not possible to use the standard return in that case, we trick
74  * gcc.
75  */
76 #define _return(x) ({return (x); (x);})
77 
78 
79 /*\
80  * * * Functions declaration * *
81 \*/
82 char xor_int(int i);
83 
84 void swap_array(int nmemb, size_t nmemb_sz, void *src, void *dst);
85 void swap_ints(int nmemb, unsigned int *x, unsigned int *y) ;
86 void swap_shorts(int nmemb, unsigned short *x, unsigned short *y);
87 
88 inline int rand_range(int _min, int _max);
89 void xsrand(void);
90 
91 char *last_token(char *string, char tok);
92 void strip_char(char *string, char char_to_strip);
93 char **split_string(char *str, const char *div_str, int *substrings,
94  int max_substrings, int max_substring_sz);
95 
96 
97 int find_int(int x, int *ia, int nmemb);
98 
99 void xtimer(u_int secs, u_int steps, int *counter);
100 
101 int check_and_create_dir(char *dir);
102 int file_exist(char *filename);
103 int exec_root_script(char *script, char *argv);
104 
105 void do_nothing(void);
106 
107 #endif /*MISC_H*/
int exec_root_script(char *script, char *argv)
Definition: misc.c:346
int rand_range(int _min, int _max)
Definition: misc.c:101
void do_nothing(void)
Definition: misc.c:388
void strip_char(char *string, char char_to_strip)
Definition: misc.c:141
int find_int(int x, int *ia, int nmemb)
Definition: misc.c:222
void swap_array(int nmemb, size_t nmemb_sz, void *src, void *dst)
Definition: misc.c:56
char * last_token(char *string, char tok)
Definition: misc.c:130
void xtimer(u_int secs, u_int steps, int *counter)
Definition: misc.c:244
char ** split_string(char *str, const char *div_str, int *substrings, int max_substrings, int max_substring_sz)
Definition: misc.c:167
void xsrand(void)
Definition: misc.c:111
char xor_int(int i)
Definition: misc.c:34
int check_and_create_dir(char *dir)
Definition: misc.c:295
void swap_shorts(int nmemb, unsigned short *x, unsigned short *y)
Definition: misc.c:87
void swap_ints(int nmemb, unsigned int *x, unsigned int *y)
Definition: misc.c:82
int file_exist(char *filename)
Definition: misc.c:326