chiark / gitweb /
rules: implement TAGS== match
[elogind.git] / extras / floppy / create_floppy_devices.c
index b9fa4ef41d7e5a9061a14729da7e250548f95815..47724f8b0484f0a4793594f48a07fb6676d0d640 100644 (file)
@@ -1,17 +1,23 @@
 /*
- * create_floppy_devices
- *
  * Create all possible floppy device based on the CMOS type.
  * Based upon code from drivers/block/floppy.c
  *
  * Copyright(C) 2005, SUSE Linux Products GmbH
  *
- * Author:
- *     Hannes Reinecke <hare@suse.de>
+ * Author: Hannes Reinecke <hare@suse.de>
+ *
+ * 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 2 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.
  *
- *     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 version 2 of the License.
+ * 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>
@@ -24,8 +30,8 @@
 #include <pwd.h>
 #include <grp.h>
 
-#include "../../udev.h"
-#include "../../udev_selinux.h"
+#include "libudev.h"
+#include "libudev-private.h"
 
 static char *table[] = {
        "", "d360", "h1200", "u360", "u720", "h360", "h720",
@@ -41,34 +47,18 @@ static int t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23, 0 };
 static int t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13, 17, 21, 22, 30, 0 };
 static int *table_sup[] = { NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in };
 
-#ifdef USE_LOG
-void log_message(int priority, const char *format, ...)
+static void log_fn(struct udev *udev, int priority,
+                  const char *file, int line, const char *fn,
+                  const char *format, va_list args)
 {
-       va_list args;
-       static int udev_log = -1;
-
-       if (udev_log == -1) {
-               const char *value;
-
-               value = getenv("UDEV_LOG");
-               if (value)
-                       udev_log = log_priority(value);
-               else
-                       udev_log = LOG_ERR;
-       }
-
-       if (priority > udev_log)
-               return;
-
-       va_start(args, format);
        vsyslog(priority, format, args);
-       va_end(args);
 }
-#endif
 
 int main(int argc, char **argv)
 {
+       struct udev *udev;
        char *dev;
+       char *devname;
        char node[64];
        int type = 0, i, fdnum, c;
        int major = 2, minor;
@@ -79,6 +69,14 @@ int main(int argc, char **argv)
        int print_nodes = 0;
        int is_err = 0;
 
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
+       udev_log_init("create_floppy_devices");
+       udev_set_log_fn(udev, log_fn);
+       udev_selinux_init(udev);
+
        while ((c = getopt(argc, argv, "cudm:U:G:M:t:")) != -1) {
                switch (c) {
                case 'c':
@@ -88,10 +86,10 @@ int main(int argc, char **argv)
                        print_nodes = 1;
                        break;
                case 'U':
-                       uid = lookup_user(optarg);
+                       uid = util_lookup_user(udev, optarg);
                        break;
                case 'G':
-                       gid = lookup_group(optarg);
+                       gid = util_lookup_group(udev, optarg);
                        break;
                case 'M':
                        mode = strtol(optarg, NULL, 0);
@@ -123,25 +121,30 @@ int main(int argc, char **argv)
        }
 
        dev = argv[optind];
-       if (dev[strlen(dev) - 3] != 'f' || dev[strlen(dev) -2 ] != 'd') {
+       devname = strrchr(dev, '/');
+       if (devname != NULL)
+               devname = &devname[1];
+       else
+               devname = dev;
+       if (strncmp(devname, "fd", 2) != 0) {
                fprintf(stderr,"Device '%s' is not a floppy device\n", dev);
                return 1;
        }
 
-       fdnum = strtol(dev + 2, NULL, 10);
+       fdnum = strtol(&devname[2], NULL, 10);
        if (fdnum < 0 || fdnum > 7) {
                fprintf(stderr,"Floppy device number %d out of range (0-7)\n", fdnum);
                return 1;
        }
        if (fdnum > 3)
-               fdnum += 128;
+               fdnum += 124;
 
        if (major < 1) {
                fprintf(stderr,"Invalid major number %d\n", major);
                return 1;
        }
 
-       if (type < 0 || type > (int) sizeof(table)) {
+       if (type < 0 || type >= (int) ARRAY_SIZE(table_sup)) {
                fprintf(stderr,"Invalid CMOS type %d\n", type);
                return 1;
        }
@@ -149,26 +152,26 @@ int main(int argc, char **argv)
        if (type == 0)
                return 0;
 
-       udev_config_init();
-       selinux_init();
-
        i = 0;
        while (table_sup[type][i]) {
                sprintf(node, "%s%s", dev, table[table_sup[type][i]]);
                minor = (table_sup[type][i] << 2) + fdnum;
                if (print_nodes)
-                       printf("%s b %d %d %d\n", node, mode, major, minor);
+                       printf("%s b %.4o %d %d\n", node, mode, major, minor);
                if (create_nodes) {
                        unlink(node);
-                       selinux_setfscreatecon(node, NULL, S_IFBLK | mode);
+                       udev_selinux_setfscreatecon(udev, node, S_IFBLK | mode);
                        mknod(node, S_IFBLK | mode, makedev(major,minor));
-                       selinux_resetfscreatecon();
+                       udev_selinux_resetfscreatecon(udev);
                        chown(node, uid, gid);
                        chmod(node, S_IFBLK | mode);
                }
                i++;
        }
 
-       selinux_exit();
+       udev_selinux_exit(udev);
+       udev_unref(udev);
+       udev_log_close();
+exit:
        return 0;
 }