Added dummy scripts that just write into logfiles
[mtetherd] / device.c
index 9dbe424..022f8cb 100644 (file)
--- a/device.c
+++ b/device.c
@@ -1,21 +1,21 @@
 /*
-maemo-tethering
-(c) 2010 Gregor Riepl <onitake@gmail.com>
-
-Tethering utility for Maemo
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  mtetherd
+  (c) 2010 Gregor Riepl <onitake@gmail.com>
+  
+  Tethering utility for Maemo
+  
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+  
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+  
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include <stdio.h>
@@ -35,14 +35,11 @@ Device *device_new(const char *name) {
                return NULL;
        }
        memset(ret, 0, sizeof(Device));
-       size_t length = strlen(name) + 1;
-       ret->name = malloc(length);
+       device_set_name(ret, name);
        if (!ret->name) {
-               fprintf(stderr, "Error allocating memory for device name.\n");
                free(ret);
                return NULL;
        }
-       memcpy(ret->name, name, length);
        return ret;
 }
 
@@ -92,10 +89,31 @@ Device *device_set_name(Device *device, const char *name) {
                return NULL;
        }
        free(device->name);
-       size_t length = strlen(name) + 1;
-       device->name = malloc(length);
+       size_t length = strlen(name);
+       device->name = malloc(length + 1);
        if (!device->name) {
                fprintf(stderr, "Error allocating memory for device name.\n");
+       } else {
+               int filtered = 0;
+               size_t i, j;
+               for (i = 0, j = 0; i < length; i++) {
+                       if ((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= '0' && name[i] <= '9')) {
+                               device->name[j] = name[i];
+                               j++;
+                       } else {
+                               filtered = 1;
+                       }
+               }
+               device->name[j] = '\0';
+               if (j == 0) {
+                       fprintf(stderr, "Error: Invalid device name.\n");
+                       free(device->name);
+                       device->name = NULL;
+               } else {
+                       if (filtered) {
+                               fprintf(stderr, "Warning, device name contained invalid characters. They have been removed.\n");
+                       }
+               }
        }
        return device;
 }
@@ -192,7 +210,7 @@ int device_validate(Device *device) {
 }
 
 Device *device_search(Device *start, const char *name) {
-       while (start) {
+       for (; start; start = start->next) {
                if (strcmp(start->name, name) == 0) {
                        return start;
                }