[sheepdog] [PATCH] tools/zk_control: add sort options for list subcommand
Liu Yuan
namei.unix at gmail.com
Wed Mar 4 09:00:35 CET 2015
On Wed, Mar 04, 2015 at 02:02:11PM +0800, Meng Lingkun wrote:
> From: Meng Lingkun <menglingkun at cmss.chinamobile.com>
>
> It's more useable to list the sequential znodes in queue with sort function.
>
> Signed-off-by: Meng Lingkun <menglingkun at cmss.chinamobile.com>
> ---
> tools/zk_control.c | 43 ++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 42 insertions(+), 1 deletion(-)
>
> diff --git a/tools/zk_control.c b/tools/zk_control.c
> index 75783e1..4bf47ff 100644
> --- a/tools/zk_control.c
> +++ b/tools/zk_control.c
> @@ -11,6 +11,8 @@
> * along with this program. If not, see <http://www.gnu.org/licenses/>.
> */
>
> +#include <getopt.h>
> +#include <stdlib.h>
> #include <zookeeper/zookeeper.h>
> #include <string.h>
> #include <arpa/inet.h>
> @@ -231,6 +233,16 @@ err:
> return -1;
> }
>
> +static int seq_cmp(const void *a, const void *b)
> +{
> + return strcmp(*(const char **)b, *(const char **)a);
> +}
> +
> +static int seq_cmp2(const void *a, const void *b)
> +{
> + return strcmp(*(const char **)a, *(const char **)b);
> +}
> +
> static int do_list(int argc, char **argv)
> {
> struct String_vector strs;
> @@ -242,6 +254,14 @@ static int do_list(int argc, char **argv)
> struct Stat stat;
> int32_t seq;
> struct node_id *nid;
> + char opts[] = "sS", ch;
> + int longindex;
> + struct option lopts[] = {
> + { "sort", 0, NULL, 's' },
> + { "SORT", 0, NULL, 'S' },
> + { 0, 0, 0, 0 }
> + };
> + int sort = 0;
>
> fprintf(stdout, " QUEUE ID TYPE"
> " SENDER MSG LEN NR BUF LEN"
> @@ -249,6 +269,26 @@ static int do_list(int argc, char **argv)
> rc = zk_get_children(QUEUE_ZNODE, &strs);
> switch (rc) {
> case ZOK:
> + while ((ch = getopt_long(argc, argv, opts, lopts,
> + &longindex)) >= 0) {
> + switch (ch) {
> + case 's':
> + sort = 1;
> + break;
> + case 'S':
> + sort = 2;
> + break;
> + default:
> + break;
> + }
> + }
> +
> + if (1 == sort) {
> + qsort(strs.data, strs.count, sizeof(char *), seq_cmp);
> + } else if (2 == sort) {
> + qsort(strs.data, strs.count, sizeof(char *), seq_cmp2);
Should use xqsort().
Yuan
More information about the sheepdog
mailing list