]> git.sur5r.net Git - ngadmin/blob - cli/src/common.c
Factorize string related functions
[ngadmin] / cli / src / common.c
1
2 #include "common.h"
3
4
5 void printErrCode (int err)
6 {
7         const char *str;
8         
9         str = ngadmin_errorStr(err);
10         
11         if (str == NULL)
12                 printf("unknown status code (%i)\n", err);
13         else if (err != ERR_OK)
14                 puts(str);
15 }
16
17
18 void displaySwitchTab (const struct swi_attr *sa, int nb)
19 {
20         int i=0;
21         
22         if (nb == 0) {
23                 printf("no switch found\n");
24                 return;
25         }
26         
27         printf("Num\tMac\t\t\tProduct\t\t\tName\t\t\t\t\tIP\t\tPorts\tFirmware\n");
28         
29         for (i = 0; i < nb; i++)
30                 printf("% 3d\t%-17s\t%-16.16s\t%-32.32s\t%-15s\t% 5d\t%s\n", i, ether_ntoa(&sa[i].mac), sa[i].product, sa[i].name, inet_ntoa(sa[i].nc.ip), sa[i].ports, sa[i].firmware);
31         
32         printf("\nfound %i switch(es)\n", nb);
33 }
34
35
36 int explode (const char *commande, char** tab, int maximum)
37 {
38         const char *start, *end;
39         int n = 0, len;
40         
41         
42         for (end = commande; ; n++) {
43                 for (start = end; *start == ' ' && *start != 0; start++);
44                 for (end = start; (*end != ' ' || n >= maximum - 1 ) && *end != 0; end++);
45                 
46                 len = end - start;
47                 if (len == 0)
48                         break;
49                 
50                 tab[n] = malloc(sizeof(char) * (len + 1));
51                 memcpy(tab[n], start, len);
52                 tab[n][len] = 0;
53         }
54         
55         
56         return n;
57 }
58
59