fix compilation warnings
[asterisk-bristuff.git] / funcs / func_rand.c
blob53952d341033478422c22a3abfebbedc4a48d4bc
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Digium, Inc.
5 * Copyright (C) 2006, Claude Patry
7 * See http://www.asterisk.org for more information about
8 * the Asterisk project. Please do not directly contact
9 * any of the maintainers of this project for assistance;
10 * the project provides a web site, mailing lists and IRC
11 * channels for your use.
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License Version 2. See the LICENSE file
15 * at the top of the source tree.
18 /*! \file
20 * \brief Generate Random Number
22 * \author Claude Patry <cpatry@gmail.com>
23 * \author Tilghman Lesher ( http://asterisk.drunkcoder.com/ )
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <sys/types.h>
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/app.h"
42 static int acf_rand_exec(struct ast_channel *chan, char *cmd,
43 char *parse, char *buffer, size_t buflen)
45 struct ast_module_user *u;
46 int min_int, response_int, max_int;
47 AST_DECLARE_APP_ARGS(args,
48 AST_APP_ARG(min);
49 AST_APP_ARG(max);
52 u = ast_module_user_add(chan);
54 AST_STANDARD_APP_ARGS(args, parse);
56 if (ast_strlen_zero(args.min) || sscanf(args.min, "%d", &min_int) != 1)
57 min_int = 0;
59 if (ast_strlen_zero(args.max) || sscanf(args.max, "%d", &max_int) != 1)
60 max_int = RAND_MAX;
62 if (max_int < min_int) {
63 int tmp = max_int;
65 max_int = min_int;
66 min_int = tmp;
67 ast_log(LOG_DEBUG, "max<min\n");
70 response_int = min_int + (ast_random() % (max_int - min_int + 1));
71 ast_log(LOG_DEBUG, "%d was the lucky number in range [%d,%d]\n",
72 response_int, min_int, max_int);
73 snprintf(buffer, buflen, "%d", response_int);
75 ast_module_user_remove(u);
77 return 0;
80 static struct ast_custom_function acf_rand = {
81 .name = "RAND",
82 .synopsis = "Choose a random number in a range",
83 .syntax = "RAND([min][|max])",
84 .desc =
85 "Choose a random number between min and max. Min defaults to 0, if not\n"
86 "specified, while max defaults to RAND_MAX (2147483647 on many systems).\n"
87 " Example: Set(junky=${RAND(1|8)}); \n"
88 " Sets junky to a random number between 1 and 8, inclusive.\n",
89 .read = acf_rand_exec,
92 static int unload_module(void)
94 ast_custom_function_unregister(&acf_rand);
96 return 0;
99 static int load_module(void)
101 return ast_custom_function_register(&acf_rand);
104 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function");