[packages] upx: update to 3.08
[packages.git] / net / wavemon / patches / 000-upstream-wrapper.patch
1 From: Gerrit Renker <gerrit@erg.abdn.ac.uk>
2 Date: Sun, 13 Mar 2011 17:39:40 +0000 (+0100)
3 Subject: Configuration: decouple configuration items
4 X-Git-Url: http://eden-feed.erg.abdn.ac.uk/cgi-bin/gitweb.cgi?p=wavemon.git;a=commitdiff_plain;h=74610068abe331927f72aa617689566a2b2a7771
5
6 Configuration: decouple configuration items
7
8 This puts access to WiFi interface list and currently selected interface
9 into wrapper functions, to better separate the code blocks.
10 ---
11
12 diff --git a/conf.c b/conf.c
13 index 4004c73..45fa42e 100644
14 --- a/conf.c
15 +++ b/conf.c
16 @@ -22,6 +22,7 @@
17  #include <sys/types.h>
18  
19  /* GLOBALS */
20 +static char **if_list;         /* array of WiFi interface names */
21  int conf_items;                        /* index into array storing menu items */
22  
23  static char *on_off_names[] = { [false] = "Off", [true] = "On", NULL };
24 @@ -116,6 +117,39 @@ static void getargs(int argc, char *argv[])
25                 }
26  }
27  
28 +/** Populate interface list */
29 +void conf_get_interface_list(void)
30 +{
31 +       char *old_if = NULL;
32 +       int idx;
33 +
34 +       if (if_list) {
35 +               for (idx = 0; if_list[idx]; idx++)
36 +                       if (idx == conf.if_idx)
37 +                               old_if = if_list[idx];
38 +                       else
39 +                               free(if_list[idx]);
40 +               free(if_list);
41 +       }
42 +       if_list = iw_get_interface_list();
43 +       if (if_list == NULL)
44 +               err_quit("no wireless interfaces found!");
45 +
46 +       conf.if_idx = 0;
47 +       if (old_if) {
48 +               idx = argv_find(if_list, old_if);
49 +               if (idx > 0)
50 +                       conf.if_idx = idx;
51 +               free(old_if);
52 +       }
53 +}
54 +
55 +/** Return currently selected interface name */
56 +const char *conf_ifname(void)
57 +{
58 +       return if_list ? if_list[conf.if_idx] : "(none)";
59 +}
60 +
61  /* Return full path of rcfile. Allocates string which must bee free()-d. */
62  static char *get_confname(void)
63  {
64 @@ -520,7 +554,7 @@ static void init_conf_items(void)
65  
66  void getconf(int argc, char *argv[])
67  {
68 -       iw_get_interface_list();
69 +       conf_get_interface_list();
70         init_conf_items();
71         read_cf();
72         getargs(argc, argv);
73 diff --git a/conf_scr.c b/conf_scr.c
74 index a338771..674137c 100644
75 --- a/conf_scr.c
76 +++ b/conf_scr.c
77 @@ -159,7 +159,7 @@ static int m_pref(WINDOW *w_conf, int list_offset, int active_item, int num_item
78  
79  void scr_conf_init(void)
80  {
81 -       iw_get_interface_list();        /* may have changed in the meantime */
82 +       conf_get_interface_list();      /* may have changed in the meantime */
83  
84         num_items = ll_size(conf_items);
85         w_conf    = newwin_title(0, WAV_HEIGHT, "Preferences", false);
86 diff --git a/info_scr.c b/info_scr.c
87 index 8171373..d17fdfa 100644
88 --- a/info_scr.c
89 +++ b/info_scr.c
90 @@ -30,7 +30,7 @@ void sampling_init(void (*sampling_handler)(int))
91         div_t d = div(conf.stat_iv, 1000);      /* conf.stat_iv in msec */
92  
93         xsignal(SIGALRM, SIG_IGN);
94 -       iw_getinf_range(if_list[conf.if_idx], &cur.range);
95 +       iw_getinf_range(conf_ifname(), &cur.range);
96         i.it_interval.tv_sec  = i.it_value.tv_sec  = d.quot;
97         i.it_interval.tv_usec = i.it_value.tv_usec = d.rem * 1000;
98         xsignal(SIGALRM, sampling_handler);
99 @@ -139,7 +139,7 @@ static void display_stats(void)
100         struct if_stat nstat;
101         char tmp[0x100];
102  
103 -       if_getstat(if_list[conf.if_idx], &nstat);
104 +       if_getstat(conf_ifname(), &nstat);
105  
106         /*
107          * Interface RX stats
108 @@ -197,10 +197,10 @@ static void display_info(WINDOW *w_if, WINDOW *w_info)
109         char tmp[0x100];
110         int i;
111  
112 -       dyn_info_get(&info, if_list[conf.if_idx], &cur.range);
113 +       dyn_info_get(&info, conf_ifname(), &cur.range);
114  
115         wmove(w_if, 1, 1);
116 -       waddstr_b(w_if, if_list[conf.if_idx]);
117 +       waddstr_b(w_if, conf_ifname());
118         if (cur.range.enc_capa & IW_WPA_MASK)
119                 sprintf(tmp, " (%s, %s)", info.name, format_wpa(&cur.range));
120         else
121 @@ -416,11 +416,11 @@ static void display_netinfo(WINDOW *w_net)
122         struct if_info info;
123         char tmp[0x40];
124  
125 -       if_getinf(if_list[conf.if_idx], &info);
126 +       if_getinf(conf_ifname(), &info);
127  
128         wmove(w_net, 1, 1);
129         if (getmaxy(w_net) == WH_NET_MAX) {
130 -               waddstr(w_net, if_list[conf.if_idx]);
131 +               waddstr(w_net, conf_ifname());
132  
133                 waddstr_b(w_net, " (");
134                 waddstr(w_net, info.flags & IFF_UP ? "UP" : "DOWN");
135 diff --git a/iw_if.c b/iw_if.c
136 index 2008c14..708ff85 100644
137 --- a/iw_if.c
138 +++ b/iw_if.c
139 @@ -24,9 +24,6 @@
140  /* Fallback maximum quality level when using random samples. */
141  #define WAVE_RAND_QUAL_MAX     100
142  
143 -/* GLOBALS */
144 -char **if_list = NULL;         /* array of WiFi interface names */
145 -
146  /*
147   * Obtain network device information
148   */
149 @@ -65,7 +62,7 @@ int if_set_up(int skfd, const char *ifname)
150  }
151  
152  /* Interface information */
153 -void if_getinf(char *ifname, struct if_info *info)
154 +void if_getinf(const char *ifname, struct if_info *info)
155  {
156         struct ifreq ifr;
157         int skfd = socket(AF_INET, SOCK_DGRAM, 0);
158 @@ -114,49 +111,27 @@ static FILE *open_proc_net(const char *filename)
159  }
160  
161  /**
162 - * iw_get_interface_list  -  Populate NULL-terminated array of WiFi interfaces.
163 - * Rebuild if already set, exit if no interfaces present.
164 + * iw_get_interface_list  -  Return NULL-terminated array of WiFi interfaces.
165   */
166 -void iw_get_interface_list(void)
167 +char **iw_get_interface_list(void)
168  {
169 -       char *p, tmp[IFNAMSIZ], *old_if = NULL;
170 -       int idx, nifs;
171 +       char **if_list = NULL, *p, tmp[BUFSIZ];
172 +       int  nifs = 1;          /* if_list[nifs-1] = NULL */
173         FILE *fp = open_proc_net("wireless");
174  
175 -       if (if_list) {
176 -               for (idx = 0; if_list[idx]; idx++)
177 -                       if (idx == conf.if_idx)
178 -                               old_if = if_list[idx];
179 -                       else
180 -                               free(if_list[idx]);
181 -               free(if_list);
182 -       }
183 -
184 -       for (nifs = 0; fgets(tmp, sizeof(tmp), fp); )
185 -               nifs += strchr(tmp, ':') != NULL;
186 -       if (!nifs)
187 -               err_quit("no wireless interfaces found!");
188 -       rewind(fp);
189 -
190 -       if_list = calloc(nifs + 1, sizeof(*if_list));
191 -       if (if_list == NULL)
192 -               err_sys("unable to memorize %d interfaces", nifs);
193 -
194 -       for (conf.if_idx = idx = 0; fgets(tmp, sizeof(tmp), fp); ) {
195 +       while (fgets(tmp, sizeof(tmp), fp))
196                 if ((p = strchr(tmp, ':'))) {
197 +                       if_list = realloc(if_list, sizeof(char *) * (nifs + 1));
198                         for (*p = '\0', p = tmp; isspace(*p); )
199                                 p++;
200 -                       if (old_if && strcmp(old_if, p) == 0)
201 -                               conf.if_idx = idx;
202 -                       if_list[idx++] = strdup(p);
203 +                       if_list[nifs-1] = strdup(p);
204 +                       if_list[nifs++] = NULL;
205                 }
206 -       }
207 -       assert(idx == nifs);
208         fclose(fp);
209 -       free(old_if);
210 +       return if_list;
211  }
212  
213 -void if_getstat(char *ifname, struct if_stat *stat)
214 +void if_getstat(const char *ifname, struct if_stat *stat)
215  {
216         char line[0x100];
217         unsigned long d;
218 @@ -187,7 +162,8 @@ void if_getstat(char *ifname, struct if_stat *stat)
219   * @ifname: interface name
220   * @if:            range information to use (number of encryption keys)
221   */
222 -void dyn_info_get(struct iw_dyn_info *info, char *ifname, struct iw_range *ir)
223 +void dyn_info_get(struct iw_dyn_info *info,
224 +                 const char *ifname, struct iw_range *ir)
225  {
226         struct iwreq iwr;
227         int i, skfd = socket(AF_INET, SOCK_DGRAM, 0);
228 @@ -321,7 +297,7 @@ void dyn_info_cleanup(struct iw_dyn_info *info)
229  /*
230   * get range information
231   */
232 -void iw_getinf_range(char *ifname, struct iw_range *range)
233 +void iw_getinf_range(const char *ifname, struct iw_range *range)
234  {
235         struct iwreq iwr;
236         int skfd = socket(AF_INET, SOCK_DGRAM, 0);
237 @@ -395,7 +371,7 @@ static void iw_getstat_real(struct iw_statistics *stat)
238         wrq.u.data.pointer = (caddr_t) stat;
239         wrq.u.data.length  = sizeof(*stat);
240         wrq.u.data.flags   = 0;
241 -       strncpy(wrq.ifr_name, if_list[conf.if_idx], IFNAMSIZ);
242 +       strncpy(wrq.ifr_name, conf_ifname(), IFNAMSIZ);
243  
244         if (ioctl(skfd, SIOCGIWSTATS, &wrq) < 0) {
245                 /*
246 @@ -490,13 +466,13 @@ void dump_parameters(void)
247         struct if_stat nstat;
248         int i;
249  
250 -       iw_getinf_range(if_list[conf.if_idx], &iw.range);
251 -       dyn_info_get(&info, if_list[conf.if_idx], &iw.range);
252 +       iw_getinf_range(conf_ifname(), &iw.range);
253 +       dyn_info_get(&info, conf_ifname(), &iw.range);
254         iw_getstat(&iw);
255 -       if_getstat(if_list[conf.if_idx], &nstat);
256 +       if_getstat(conf_ifname(), &nstat);
257  
258         printf("\n");
259 -       printf("Configured device: %s (%s)\n", if_list[conf.if_idx], info.name);
260 +       printf("Configured device: %s (%s)\n", conf_ifname(), info.name);
261         printf("         Security: %s\n", iw.range.enc_capa ?
262                         format_enc_capab(iw.range.enc_capa, ", ") : "WEP");
263         if (iw.range.num_encoding_sizes &&
264 diff --git a/iw_if.h b/iw_if.h
265 index 80e6595..636a63a 100644
266 --- a/iw_if.h
267 +++ b/iw_if.h
268 @@ -71,7 +71,7 @@ struct if_info {
269  };
270  extern bool if_is_up(int skfd, const char *ifname);
271  extern int  if_set_up(int skfd, const char *ifname);
272 -extern void if_getinf(char *ifname, struct if_info *info);
273 +extern void if_getinf(const char *ifname, struct if_info *info);
274  
275  /**
276   * struct iw_key  -  Encoding information
277 @@ -174,7 +174,8 @@ static inline uint8_t dyn_info_wep_keys(struct iw_dyn_info *info)
278                                    info->keys[i].size == 13;
279         return num_wep;
280  }
281 -extern void dyn_info_get(struct iw_dyn_info *, char *ifname, struct iw_range *);
282 +extern void dyn_info_get(struct iw_dyn_info *info,
283 +                        const char *ifname, struct iw_range *ir);
284  extern void dyn_info_cleanup(struct iw_dyn_info *info);
285  
286  
287 @@ -188,7 +189,7 @@ struct if_stat {
288                                 tx_bytes;
289  };
290  
291 -extern void if_getstat(char *ifname, struct if_stat *stat);
292 +extern void if_getstat(const char *ifname, struct if_stat *stat);
293  
294  /*
295   *      Structs to communicate WiFi statistics
296 @@ -200,7 +201,7 @@ struct iw_levelstat {
297  };
298  #define IW_LSTAT_INIT { 0, 0, IW_QUAL_LEVEL_INVALID | IW_QUAL_NOISE_INVALID }
299  
300 -extern void iw_getinf_range(char *ifname, struct iw_range *range);
301 +extern void iw_getinf_range(const char *ifname, struct iw_range *range);
302  extern void iw_sanitize(struct iw_range *range,
303                         struct iw_quality *qual,
304                         struct iw_levelstat *dbm);
305 @@ -250,7 +251,7 @@ struct scan_result {
306  
307         struct scan_result *next;
308  };
309 -extern struct scan_result *get_scan_list(int skfd, char *ifname, int weversion);
310 +extern struct scan_result *get_scan_list(int skfd, const char *ifname, int ver);
311  extern void free_scan_result(struct scan_result *head);
312  
313  
314 diff --git a/iw_scan.c b/iw_scan.c
315 index d75ab6b..8423e6a 100644
316 --- a/iw_scan.c
317 +++ b/iw_scan.c
318 @@ -553,7 +553,7 @@ static int cmp_scan_sig(struct scan_result *a, struct scan_result *b)
319         return a->qual.level - b->qual.level;
320  }
321  
322 -struct scan_result *get_scan_list(int skfd, char *ifname, int we_version)
323 +struct scan_result *get_scan_list(int skfd, const char *ifname, int we_version)
324  {
325         struct scan_result *head = NULL;
326         struct iwreq wrq;
327 diff --git a/scan_scr.c b/scan_scr.c
328 index 879c2f6..b9a4eee 100644
329 --- a/scan_scr.c
330 +++ b/scan_scr.c
331 @@ -80,9 +80,9 @@ static void display_aplist(WINDOW *w_aplst)
332         if (skfd < 0)
333                 err_sys("%s: can not open socket", __func__);
334  
335 -       iw_getinf_range(if_list[conf.if_idx], &range);
336 +       iw_getinf_range(conf_ifname(), &range);
337  
338 -       head = get_scan_list(skfd, if_list[conf.if_idx], range.we_version_compiled);
339 +       head = get_scan_list(skfd, conf_ifname(), range.we_version_compiled);
340         if (head) {
341                 ;
342         } else if (errno == EPERM || !has_net_admin_capability()) {
343 @@ -93,13 +93,13 @@ static void display_aplist(WINDOW *w_aplst)
344         } else if (errno == EINTR || errno == EAGAIN || errno == EBUSY) {
345                 /* Ignore temporary errors */
346                 goto done;
347 -       } else if (!if_is_up(skfd, if_list[conf.if_idx])) {
348 -               sprintf(s, "Interface '%s' is down ", if_list[conf.if_idx]);
349 +       } else if (!if_is_up(skfd, conf_ifname())) {
350 +               sprintf(s, "Interface '%s' is down ", conf_ifname());
351                 if (!has_net_admin_capability())
352                         strcat(s, "- can not scan");
353 -               else if (if_set_up(skfd, if_list[conf.if_idx]) < 0)
354 +               else if (if_set_up(skfd, conf_ifname()) < 0)
355                         sprintf(s, "Can not bring up '%s' for scanning: %s",
356 -                               if_list[conf.if_idx], strerror(errno));
357 +                               conf_ifname(), strerror(errno));
358                 else
359                         strcat(s, "- setting it up ...");
360         } else if (errno == EFAULT) {
361 @@ -110,9 +110,9 @@ static void display_aplist(WINDOW *w_aplst)
362                  */
363                 goto done;
364         } else if (errno) {
365 -               sprintf(s, "No scan on %s: %s", if_list[conf.if_idx], strerror(errno));
366 +               sprintf(s, "No scan on %s: %s", conf_ifname(), strerror(errno));
367         } else {
368 -               sprintf(s, "No scan results on %s", if_list[conf.if_idx]);
369 +               sprintf(s, "No scan results on %s", conf_ifname());
370         }
371  
372         for (i = 1; i <= MAXYLEN; i++)
373 diff --git a/wavemon.h b/wavemon.h
374 index 22a783f..ad0b488 100644
375 --- a/wavemon.h
376 +++ b/wavemon.h
377 @@ -103,7 +103,7 @@ static inline void threshold_action(enum threshold_action action)
378   * Global in-memory representation of current wavemon configuration state
379   */
380  extern struct wavemon_conf {
381 -       int     if_idx;                 /* Index into if_list */
382 +       int     if_idx;                 /* Index into interface list */
383  
384         int     stat_iv,
385                 info_iv;
386 @@ -256,8 +256,9 @@ static inline int cp_from_scale(float value, const char *cscale, bool reverse)
387  /*
388   *     Wireless interfaces
389   */
390 -extern char **if_list;
391 -extern void iw_get_interface_list(void);
392 +extern const char *conf_ifname(void);
393 +extern void conf_get_interface_list(void);
394 +extern char **iw_get_interface_list(void);
395  extern void dump_parameters(void);
396  
397  /*