ubusd: fix issue caused by an implicit cast
[project/ubus.git] / examples / count.c
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include "count.h"
18
19 char *count_to_number(uint32_t num)
20 {
21         uint32_t ptr = 0, size = 0;
22         uint32_t written = 0, i;
23         int new_line_every_n_numbers = 30;
24         char *s;
25
26         for (i=0; i < num; ++i) {
27                 size += snprintf(NULL, 0, "%u ", i);
28                 if (i > 0 && i % new_line_every_n_numbers == 0)
29                         size++;
30         }
31         size++; /* one for null char */
32
33         s = calloc(size, sizeof(char));
34         if (!s)
35                 goto out;
36
37         for (i=0; i < num; ++i) {
38                 written = sprintf(&s[ptr], "%u ", i);
39                 ptr  += written;
40                 if (i > 0 && i % new_line_every_n_numbers == 0) {
41                         sprintf(&s[ptr], "\n");
42                         ptr++;
43                 }
44         }
45
46 out:
47         return s;
48 }