utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*****************************************************************************
  2. *
  3. * utils.c
  4. *
  5. * Library of useful functions for plugins
  6. *
  7. * Copyright (c) 2000 Karl DeBisschop (karl@debisschop.net)
  8. * License: GPL
  9. *
  10. * $Revision$
  11. * $Date$
  12. ****************************************************************************/
  13. #include "config.h"
  14. #include "common.h"
  15. #include "version.h"
  16. #include <stdarg.h>
  17. #include <limits.h>
  18. extern int timeout_interval;
  19. char *my_basename (char *);
  20. void support (void);
  21. char *clean_revstring (const char *);
  22. void print_revision (char *, const char *);
  23. void terminate (int, const char *fmt, ...);
  24. RETSIGTYPE timeout_alarm_handler (int);
  25. int is_host (char *);
  26. int is_dotted_quad (char *);
  27. int is_hostname (char *);
  28. int is_integer (char *);
  29. int is_intpos (char *);
  30. int is_intneg (char *);
  31. int is_intnonneg (char *);
  32. int is_intpercent (char *);
  33. int is_numeric (char *);
  34. int is_positive (char *);
  35. int is_negative (char *);
  36. int is_nonnegative (char *);
  37. int is_percentage (char *);
  38. int is_option (char *str);
  39. void strip (char *);
  40. char *strscpy (char *dest, const char *src);
  41. char *strscat (char *dest, const char *src);
  42. char *strnl (char *str);
  43. char *ssprintf (char *str, const char *fmt, ...);
  44. char *strpcpy (char *dest, const char *src, const char *str);
  45. char *strpcat (char *dest, const char *src, const char *str);
  46. #define LABELLEN 63
  47. #define STRLEN 64
  48. #define TXTBLK 128
  49. #define max(a,b) ((a)>(b))?(a):(b)
  50. char *
  51. my_basename (char *path)
  52. {
  53. if (!strstr (path, "/"))
  54. return path;
  55. else
  56. return 1 + strrchr (path, '/');
  57. }
  58. void
  59. support (void)
  60. {
  61. printf
  62. ("Send email to nagios-users@lists.sourceforge.net if you have questions\n"
  63. "regarding use of this software. To submit patches or suggest improvements,\n"
  64. "send email to nagiosplug-devel@lists.sourceforge.net\n");
  65. }
  66. char *
  67. clean_revstring (const char *revstring)
  68. {
  69. char plugin_revision[STRLEN];
  70. if (sscanf (revstring,"$Revision: %[0-9.]",plugin_revision) == 1)
  71. return strscpy (NULL, plugin_revision);
  72. else
  73. return strscpy (NULL, "N/A");
  74. }
  75. void
  76. print_revision (char *command_name, const char *revision_string)
  77. {
  78. char plugin_revision[STRLEN];
  79. if (sscanf (revision_string, "$Revision: %[0-9.]", plugin_revision) != 1)
  80. strncpy (plugin_revision, "N/A", STRLEN);
  81. printf ("%s (nagios-plugins %s) %s\n",
  82. my_basename (command_name), VERSION, plugin_revision);
  83. printf
  84. ("The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n"
  85. "copies of the plugins under the terms of the GNU General Public License.\n"
  86. "For more information about these matters, see the file named COPYING.\n");
  87. }
  88. void
  89. terminate (int result, const char *fmt, ...)
  90. {
  91. va_list ap;
  92. va_start (ap, fmt);
  93. vprintf (fmt, ap);
  94. va_end (ap);
  95. exit (result);
  96. }
  97. void
  98. timeout_alarm_handler (int signo)
  99. {
  100. if (signo == SIGALRM) {
  101. printf ("CRITICAL - Plugin timed out after %d seconds\n",
  102. timeout_interval);
  103. exit (STATE_CRITICAL);
  104. }
  105. }
  106. int
  107. is_host (char *address)
  108. {
  109. if (is_dotted_quad (address) || is_hostname (address))
  110. return (TRUE);
  111. return (FALSE);
  112. }
  113. int
  114. is_dotted_quad (char *address)
  115. {
  116. int o1, o2, o3, o4;
  117. char c[1];
  118. if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4)
  119. return FALSE;
  120. else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255)
  121. return FALSE;
  122. else if (o1 < 0 || o2 < 0 || o3 < 0 || o4 < 0)
  123. return FALSE;
  124. else
  125. return TRUE;
  126. }
  127. /* from RFC-1035
  128. *
  129. * The labels must follow the rules for ARPANET host names. They must
  130. * start with a letter, end with a letter or digit, and have as interior
  131. * characters only letters, digits, and hyphen. There are also some
  132. * restrictions on the length. Labels must be 63 characters or less. */
  133. int
  134. is_hostname (char *s1)
  135. {
  136. if (strlen (s1) > 63)
  137. return FALSE;
  138. if (strcspn
  139. (s1,
  140. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") !=
  141. 0) return FALSE;
  142. if (strspn (s1, "0123456789-.") == 1)
  143. return FALSE;
  144. while ((s1 = index (s1, '.'))) {
  145. s1++;
  146. if (strspn (s1, "0123456789-.") == 1) {
  147. printf ("%s\n", s1);
  148. return FALSE;
  149. }
  150. }
  151. return TRUE;
  152. }
  153. int
  154. is_numeric (char *number)
  155. {
  156. char tmp[1];
  157. float x;
  158. if (sscanf (number, "%f%c", &x, tmp) == 1)
  159. return (TRUE);
  160. return (FALSE);
  161. }
  162. int
  163. is_positive (char *number)
  164. {
  165. if (is_numeric (number) && atof (number) > 0.0)
  166. return (TRUE);
  167. return (FALSE);
  168. }
  169. int
  170. is_negative (char *number)
  171. {
  172. if (is_numeric (number) && atof (number) < 0.0)
  173. return (TRUE);
  174. return (FALSE);
  175. }
  176. int
  177. is_nonnegative (char *number)
  178. {
  179. if (is_numeric (number) && atof (number) >= 0.0)
  180. return (TRUE);
  181. return (FALSE);
  182. }
  183. int
  184. is_percentage (char *number)
  185. {
  186. int x;
  187. if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100)
  188. return (TRUE);
  189. return (FALSE);
  190. }
  191. int
  192. is_integer (char *number)
  193. {
  194. long int n;
  195. if (strspn (number, "-0123456789 ") != strlen (number))
  196. return (FALSE);
  197. n = strtol (number, NULL, 10);
  198. if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX)
  199. return (TRUE);
  200. return (FALSE);
  201. }
  202. int
  203. is_intpos (char *number)
  204. {
  205. if (is_integer (number) && atoi (number) > 0)
  206. return (TRUE);
  207. return (FALSE);
  208. }
  209. int
  210. is_intneg (char *number)
  211. {
  212. if (is_integer (number) && atoi (number) < 0)
  213. return (TRUE);
  214. return (FALSE);
  215. }
  216. int
  217. is_intnonneg (char *number)
  218. {
  219. if (is_integer (number) && atoi (number) >= 0)
  220. return (TRUE);
  221. return (FALSE);
  222. }
  223. int
  224. is_intpercent (char *number)
  225. {
  226. int i;
  227. if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100)
  228. return (TRUE);
  229. return (FALSE);
  230. }
  231. int
  232. is_option (char *str)
  233. {
  234. if (strspn (str, "-") == 1 || strspn (str, "-") == 2)
  235. return TRUE;
  236. return FALSE;
  237. }
  238. void
  239. strip (char *buffer)
  240. {
  241. size_t x;
  242. int i;
  243. for (x = strlen (buffer); x >= 1; x--) {
  244. i = x - 1;
  245. if (buffer[i] == ' ' ||
  246. buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t')
  247. buffer[i] = '\0';
  248. else
  249. break;
  250. }
  251. return;
  252. }
  253. /******************************************************************************
  254. *
  255. * Copies one string to another
  256. *
  257. * Given a pointer destination string, which may or may not already
  258. * hold some text, and a source string with additional text (possibly
  259. * NULL or empty), returns a pointer to a a copy of the source
  260. * string. Uses realloc to free memory held by the dest argument if
  261. * new storage space is required, and any previously existing data in
  262. * the destination string is lost.
  263. *
  264. * Example:
  265. *
  266. * char *str=NULL;
  267. * str = strscpy("This is a line of text with no trailing newline");
  268. *
  269. *****************************************************************************/
  270. char *
  271. strscpy (char *dest, const char *src)
  272. {
  273. size_t len;
  274. if (src == NULL)
  275. return dest;
  276. len = strlen (src) + 1;
  277. if (dest == NULL)
  278. dest = malloc (len);
  279. else if (strlen (dest) < len)
  280. dest = realloc (dest, len);
  281. if (dest == NULL)
  282. terminate (STATE_UNKNOWN, "failed realloc in strscpy\n");
  283. strncpy (dest, src, len);
  284. return dest;
  285. }
  286. /******************************************************************************
  287. *
  288. * Concatenates one string to the end of another
  289. *
  290. * Given a pointer destination string, which may or may not already
  291. * hold some text, and a source string with additional text (possibly
  292. * NULL or empty), returns a pointer to a string that is the first
  293. * string with the second concatenated to it. Uses realloc to free
  294. * memory held by the dest argument if new storage space is required.
  295. *
  296. * Example:
  297. *
  298. * char *str=NULL;
  299. * str = strscpy("This is a line of text with no trailing newline");
  300. * str = strscat(str,"\n");
  301. *
  302. *****************************************************************************/
  303. char *
  304. strscat (char *dest, const char *src)
  305. {
  306. size_t len, l2;
  307. if (src)
  308. l2 = strlen (src);
  309. else
  310. return dest;
  311. if (dest)
  312. len = strlen (dest);
  313. else
  314. len = 0;
  315. dest = realloc (dest, len + l2 + 1);
  316. if (dest == NULL)
  317. terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
  318. strncpy (dest + len, src, l2);
  319. dest[len + l2] = '\0';
  320. return dest;
  321. }
  322. /******************************************************************************
  323. *
  324. * Returns a pointer to the next line of a multiline string buffer
  325. *
  326. * Given a pointer string, find the text following the next sequence
  327. * of \r and \n characters. This has the effect of skipping blank
  328. * lines as well
  329. *
  330. * Example:
  331. *
  332. * Given text as follows:
  333. *
  334. * ==============================
  335. * This
  336. * is
  337. * a
  338. *
  339. * multiline string buffer
  340. * ==============================
  341. *
  342. * int i=0;
  343. * char *str=NULL;
  344. * char *ptr=NULL;
  345. * str = strscpy(str,"This\nis\r\na\n\nmultiline string buffer\n");
  346. * ptr = str;
  347. * while (ptr) {
  348. * printf("%d %s",i++,firstword(ptr));
  349. * ptr = strnl(ptr);
  350. * }
  351. *
  352. * Produces the following:
  353. *
  354. * 1 This
  355. * 2 is
  356. * 3 a
  357. * 4 multiline
  358. *
  359. * NOTE: The 'firstword()' function is conceptual only and does not
  360. * exist in this package.
  361. *
  362. * NOTE: Although the second 'ptr' variable is not strictly needed in
  363. * this example, it is good practice with these utilities. Once
  364. * the * pointer is advance in this manner, it may no longer be
  365. * handled with * realloc(). So at the end of the code fragment
  366. * above, * strscpy(str,"foo") work perfectly fine, but
  367. * strscpy(ptr,"foo") will * cause the the program to crash with
  368. * a segmentation fault.
  369. *
  370. *****************************************************************************/
  371. char *
  372. strnl (char *str)
  373. {
  374. size_t len;
  375. if (str == NULL)
  376. return NULL;
  377. str = strpbrk (str, "\r\n");
  378. if (str == NULL)
  379. return NULL;
  380. len = strspn (str, "\r\n");
  381. if (str[len] == '\0')
  382. return NULL;
  383. str += len;
  384. if (strlen (str) == 0)
  385. return NULL;
  386. return str;
  387. }
  388. /******************************************************************************
  389. *
  390. * Does a formatted print to a string variable
  391. *
  392. * Given a pointer destination string, which may or may not already
  393. * hold some text, and a source string with additional text (possibly
  394. * NULL or empty), returns a pointer to a string that cntains the
  395. * results of the specified formatted print
  396. *
  397. * Example:
  398. *
  399. * char *str=NULL;
  400. * str = ssprintf(str,"%d %s",1,"string");
  401. *
  402. *****************************************************************************/
  403. char *
  404. ssprintf (char *ptr, const char *fmt, ...)
  405. {
  406. va_list ap;
  407. int nchars;
  408. size_t size;
  409. char *str = NULL;
  410. if (str == NULL) {
  411. str = malloc (TXTBLK);
  412. if (str == NULL)
  413. terminate (STATE_UNKNOWN, "malloc failed in ssprintf");
  414. size = TXTBLK;
  415. }
  416. else
  417. size = max (strlen (str), TXTBLK);
  418. va_start (ap, fmt);
  419. while (1) {
  420. nchars = vsnprintf (str, size, fmt, ap);
  421. if (nchars > -1)
  422. if (nchars < (int) size) {
  423. va_end (ap);
  424. str[nchars] = '\0';
  425. if (ptr)
  426. free (ptr);
  427. return str;
  428. }
  429. else {
  430. size = (size_t) (nchars + 1);
  431. }
  432. else
  433. size *= 2;
  434. str = realloc (str, size);
  435. if (str == NULL)
  436. terminate (STATE_UNKNOWN, "realloc failed in ssprintf");
  437. }
  438. }
  439. /******************************************************************************
  440. *
  441. * Like strscpy, except only the portion of the source string up to
  442. * the provided delimiter is copied.
  443. *
  444. * Example:
  445. *
  446. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  447. * printf("%s\n",str);
  448. *
  449. * Produces:
  450. *
  451. *This is a line of te
  452. *
  453. *****************************************************************************/
  454. char *
  455. strpcpy (char *dest, const char *src, const char *str)
  456. {
  457. size_t len;
  458. if (src)
  459. len = strcspn (src, str);
  460. else
  461. return NULL;
  462. if (dest == NULL || strlen (dest) < len)
  463. dest = realloc (dest, len + 1);
  464. if (dest == NULL)
  465. terminate (STATE_UNKNOWN, "failed realloc in strpcpy\n");
  466. strncpy (dest, src, len);
  467. dest[len] = '\0';
  468. return dest;
  469. }
  470. /******************************************************************************
  471. *
  472. * Like strscat, except only the portion of the source string up to
  473. * the provided delimiter is copied.
  474. *
  475. * str = strpcpy(str,"This is a line of text with no trailing newline","x");
  476. * str = strpcat(str,"This is a line of text with no trailing newline","x");
  477. * printf("%s\n",str);
  478. *
  479. *This is a line of texThis is a line of tex
  480. *
  481. *****************************************************************************/
  482. char *
  483. strpcat (char *dest, const char *src, const char *str)
  484. {
  485. size_t len, l2;
  486. if (dest)
  487. len = strlen (dest);
  488. else
  489. len = 0;
  490. if (src) {
  491. l2 = strcspn (src, str);
  492. }
  493. else {
  494. return dest;
  495. }
  496. dest = realloc (dest, len + l2 + 1);
  497. if (dest == NULL)
  498. terminate (STATE_UNKNOWN, "failed malloc in strscat\n");
  499. strncpy (dest + len, src, l2);
  500. dest[len + l2] = '\0';
  501. return dest;
  502. }