]> git.decadent.org.uk Git - ap-utils.git/blobdiff - lib/file.c
Imported Upstream version 1.5~pre1
[ap-utils.git] / lib / file.c
index e39d7d3431b3ca645dc812f53d2cd1614db15e95..22dbf7a0f5deb303406f711e5ea87e280498fbb3 100644 (file)
 #include <string.h>
 #include <sys/types.h>
 #include "ap-utils.h"
-#include "ap-curses.h"
 
 extern WINDOW *main_sub, *win_for_help, *main_win;
-extern char *ap_types[];
-extern short ap_type;
+extern char *ap_types[], *ap_vendorexts[][3];
+extern short ap_type, ap_vendorext;
+extern struct in_addr ap_ip;
 extern int atmel410_filter;
 #define MAX_LINES LINES-6
 
 struct APList {
     char *ip;
     char *passwd;
-    int  type;
-    char *name;
+    int type;
+    int vendorext;
+    char *label;
 
     struct APList * next;
 };
@@ -48,7 +49,7 @@ _scroll_rows(struct APList *first, int begin, int end)
 {
     int i = 1;
     struct APList *curr = first;
-    char message[56];
+    char message[80];
 
     clear_main(3);
 
@@ -57,9 +58,10 @@ _scroll_rows(struct APList *first, int begin, int end)
 
     i = 0;
     while (end-- > begin) {
-       sprintf(message, "%5u  %15s   %s    %s", begin + i, curr->ip,
-           ap_types[curr->type], curr->name);
-       mvwaddstr(main_sub, 2 + i, 1, message);
+       sprintf(message, "  %3u  %-15s  %-10s  %-8s  %-16s", begin + i,
+           curr->ip, ap_types[curr->type],
+           ap_vendorexts[curr->type][curr->vendorext], curr->label);
+       mvwaddstr(main_sub, 2 + i, 0, message);
        i++;
        curr = curr->next;
     }
@@ -69,7 +71,7 @@ _scroll_rows(struct APList *first, int begin, int end)
 struct APList *parse_db_str(char *str)
 {
     struct APList *curr=NULL;
-    char *ip=NULL, *passwd=NULL, *name=NULL, mess[1024];
+    char *ip=NULL, *passwd=NULL, *label=NULL, *aptype=NULL, mess[1024];
     int i=0, pos=0, j=0;
 
     while(str[i] != '\0') {
@@ -86,9 +88,14 @@ struct APList *parse_db_str(char *str)
                    passwd[j] = '\0';
                    break;
                case 2:
-                   name = (char *) malloc(j+1);
-                   memcpy(name, mess, j+1);
-                   name[j] = '\0';
+                   label = (char *) malloc(j+1);
+                   memcpy(label, mess, j+1);
+                   label[j > 16 ? 16 : j] = '\0';
+                   break;
+               case 3:
+                   aptype = (char *) malloc(j+1);
+                   memcpy(aptype, mess, j+1);
+                   aptype[j] = '\0';
                    break;
            }
            i++;
@@ -100,27 +107,31 @@ struct APList *parse_db_str(char *str)
     }
 
     mess[j]='\0';
-    if (pos==3 && ip && passwd && ((atmel410_filter && atoi(mess) == ATMEL410)
+    if (pos==4 && ip && passwd && ((atmel410_filter && atoi(aptype) == ATMEL410)
        || !atmel410_filter)) {
        curr = (struct APList *) malloc(sizeof(struct APList));
-       curr->type = atoi(mess);
+       curr->type = atoi(aptype);
+       curr->vendorext = atoi(mess);
        curr->next = NULL;
        curr->ip = (char *) malloc(strlen(ip) + 1);
        strcpy(curr->ip, ip);
        curr->passwd = (char *) malloc(strlen(passwd) + 1);
        strcpy(curr->passwd, passwd);
-       curr->name = (char *) malloc(strlen(name) + 1);
-       strcpy(curr->name, name);
+       curr->label = (char *) malloc(strlen(label) + 1);
+       strcpy(curr->label, label);
     }
-       
+
     if (ip)
        free(ip);
 
     if (passwd)
        free(passwd);
 
-    if (name)
-       free(name);
+    if (label)
+       free(label);
+
+    if (aptype)
+       free(aptype);
 
     return curr;
 }
@@ -133,9 +144,9 @@ int get_opts()
        
     char *home_dir, buf[1024], mess[64];
     char message[50];
-    int c, fd, rval=0, pos;
-    signed int j, i, begin, end, record_num=0;
-    struct APList *first=NULL, *curr=NULL, *pmac; 
+    int c, fd, rval = 0, pos;
+    signed int j, i, begin, end, record_num = 0;
+    struct APList *first = NULL, *curr = NULL, *pmac; 
     struct sockaddr_in client;
 
 
@@ -158,8 +169,8 @@ int get_opts()
                mess[pos]='\0';
                if (first == NULL) {
                    if ((first = parse_db_str(mess)) != NULL) {
-                       curr=first;
-                       record_num=1;
+                       curr = first;
+                       record_num = 1;
                    }
                } else {
                    if ((curr->next = parse_db_str(mess)) != NULL) {
@@ -176,8 +187,8 @@ int get_opts()
     mess[pos]='\0';
     if (first == NULL) {
        if ((first = parse_db_str(mess)) != NULL) {
-           curr=first;
-           record_num=1;
+           curr = first;
+           record_num = 1;
        }
     } else {
        if ((curr->next = parse_db_str(mess)) != NULL) 
@@ -190,15 +201,15 @@ int get_opts()
     if (!record_num)
        return 0;
 
-    mvwaddstr(main_sub, 0, 3,
-       _("NUM       IP ADDRESS   TYPE        NAME (in ~/.ap-config)"));
-    print_title(_("Choose an AP to connect to"));
+    mvwaddstr(main_sub, 0, 2,
+       _("NUM  IP ADDRESS       MIB TYPE    MIB EXT.  LABEL"));
+    print_top(NULL, _("Choose an AP to connect to"));
     begin = 1;
     end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
     _scroll_rows(first, begin, end);
     noecho();
-    print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll"));
     while (1) {
+       print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll"));
        switch (c = getch()) {
            case 'q':
            case 'Q':
@@ -234,18 +245,18 @@ int get_opts()
                community = (char *) malloc(i);
                strncpy(community, curr->passwd, i);
                ap_type = curr->type;
-               rval=1;
+               ap_vendorext = curr->vendorext;
+               rval = 1;
                if (sockfd)
                    close(sockfd);
 
                if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-                   rval=0;
+                   rval = 0;
 
                if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
-                   rval=0;
+                   rval = 0;
 
                print_bottom(inet_ntoa(ap_ip));
-               get_mib_details();
                goto quit;
 
            case 'c':
@@ -270,18 +281,18 @@ int get_opts()
                community = (char *) malloc(i);
                strncpy(community, curr->passwd, i);
                ap_type = curr->type;
-               rval=1;
+               ap_vendorext = curr->vendorext;
+               rval = 1;
                if (sockfd)
                    close(sockfd);
 
                if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-                   rval=0;
+                   rval = 0;
 
                if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
-                   rval=0;
+                   rval = 0;
 
                print_bottom(inet_ntoa(ap_ip));
-               get_mib_details();
                goto quit;
 
            case 'd':
@@ -297,6 +308,9 @@ int get_opts()
                if (i == 1) {
                    curr = first;
                    first = first->next;
+                   free(curr->ip);
+                   free(curr->passwd);
+                   free(curr->label);
                    free(curr);
                } else {
                    curr = first;
@@ -305,6 +319,9 @@ int get_opts()
 
                    pmac = curr->next;
                    curr->next = pmac->next;
+                   free(pmac->ip);
+                   free(pmac->passwd);
+                   free(pmac->label);
                    free(pmac);
                }
                record_num--;
@@ -345,15 +362,22 @@ wrong_num:
                if ((fd = creat(buf, 0600)) != -1) {
                    curr=first;
                    while (curr) {
-                       sprintf(buf, "%s:%s:%d\n", curr->ip, curr->passwd,
-                           curr->type);
+                       sprintf(buf, "%s:%s:%s:%d:%d\n", curr->ip, curr->passwd,
+                           curr->label,curr->type, curr->vendorext);
                        write(fd, buf, strlen(buf));
                        curr = curr->next;
                    }
                    close(fd);
+                   print_help(
+                       _("AP list file ~/.ap-config successfully written. "
+                         "Press any key."));
+               } else {
+                   print_helperr(
+                       _("Unable to write AP list file ~/.ap-config. "
+                         "Press any key."));
                }
+               getch();
                continue;
-
        }
     }
 /*
@@ -364,10 +388,11 @@ wrong_num:
        first = curr->next;
        free(curr->ip);
        free(curr->passwd);
+       free(curr->label);
        free(curr);
     }
     print_help("");
-    print_title("");
+    print_top(NULL, NULL);
     wclear(main_sub);
     wrefresh(main_sub);
     return rval;
@@ -375,11 +400,12 @@ wrong_num:
 
 void save_Stations(struct MacListStat *curr)
 {
-    int fd;
+    int fd, err_f = 0;
     char *home_dir;
     char message[1024];
     if ((home_dir = getenv("HOME"))) {
-       sprintf(message, "%s/ap-%s.stations", home_dir, ap_types[ap_type]);
+       sprintf(message, "%s/ap-%s-%s-%s.stations", home_dir, inet_ntoa(ap_ip),
+           ap_types[ap_type],ap_vendorexts[ap_type][ap_vendorext]);
        if ((fd = creat(message, 0600)) != -1) {
            while (curr) {
                sprintf(message, "%02X%02X%02X%02X%02X%02X\n",
@@ -390,7 +416,17 @@ void save_Stations(struct MacListStat *curr)
                curr = curr->next;
            }
            close(fd);
+       } else {
+           err_f = 1;
        }
+    } else {
+       err_f = 1;
     }
+    if (err_f)
+       print_helperr(_("Unable to write stations file. Press any key."));
+    else
+       print_help(_("Stations file succesfully written. Press any key."));
+    getch();
+    print_help("");
 }