You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.h 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef MISC_H
  19. #define MISC_H
  20. /*
  21. * NMEMB: returns the number of members of the `x' array
  22. */
  23. #define NMEMB(x) (sizeof((x))/sizeof(typeof((x)[0])))
  24. /*
  25. * MILLISEC: converts a timeval struct to a int. The time will be returned in
  26. * milliseconds.
  27. */
  28. #define MILLISEC(x) (((x).tv_sec*1000)+((x).tv_usec/1000))
  29. /*
  30. * MILLISEC_TO_TV: Converts `x', which is an int into `t', a timeval struct
  31. */
  32. #define MILLISEC_TO_TV(x,t) \
  33. do{ \
  34. (t).tv_sec=(x)/1000; \
  35. (t).tv_usec=((x) - ((x)/1000)*1000)*1000; \
  36. }while(0)
  37. /*
  38. * Bit map related macros.
  39. */
  40. #define SET_BIT(a,i) ((a)[(i)/CHAR_BIT] |= 1<<((i)%CHAR_BIT))
  41. #define CLR_BIT(a,i) ((a)[(i)/CHAR_BIT] &= ~(1<<((i)%CHAR_BIT)))
  42. #define TEST_BIT(a,i) (((a)[(i)/CHAR_BIT] & (1<<((i)%CHAR_BIT))) ? 1 : 0)
  43. /*
  44. * FIND_PTR
  45. *
  46. * Given an array of pointers `a' of `n' members, it searches for a member
  47. * equal to the pointer `p'. If it is found its position is returned,
  48. * otherwise -1 is the value returned.
  49. */
  50. #define FIND_PTR(p, a, n) \
  51. ({ \
  52. int _i, _ret; \
  53. \
  54. for(_i=0, _ret=-1; _i<(n); _i++) \
  55. if((a)[_i] == (p)) { \
  56. _ret=_i; \
  57. break; \
  58. } \
  59. _ret; \
  60. })
  61. /*
  62. * _return
  63. *
  64. * It is used in this case:
  65. * condition && _return (ret);
  66. * Since it is not possible to use the standard return in that case, we trick
  67. * gcc.
  68. */
  69. #define _return(x) ({return (x); (x);})
  70. /*\
  71. * * * Functions declaration * *
  72. \*/
  73. char xor_int(int i);
  74. void swap_array(int nmemb, size_t nmemb_sz, void *src, void *dst);
  75. void swap_ints(int nmemb, unsigned int *x, unsigned int *y);
  76. void swap_shorts(int nmemb, unsigned short *x, unsigned short *y);
  77. int rand_range(int _min, int _max);
  78. void xsrand(void);
  79. char *last_token(char *string, char tok);
  80. void strip_char(char *string, char char_to_strip);
  81. char **split_string(char *str, const char *div_str, int *substrings,
  82. int max_substrings, int max_substring_sz);
  83. int find_int(int x, int *ia, int nmemb);
  84. void xtimer(u_int secs, u_int steps, int *counter);
  85. int check_and_create_dir(char *dir);
  86. int file_exist(char *filename);
  87. int exec_root_script(char *script, char *argv);
  88. void do_nothing(void);
  89. #endif /*MISC_H */