chiark / gitweb /
libudev: export udev_util_encode_string()
[elogind.git] / extras / scsi_id / scsi_id.c
index d630cf902b16de4531aab7fefbb6a14d03458ba5..26b92e3fcda70a6f6626da90dd4b93f551275f66 100644 (file)
@@ -1,17 +1,19 @@
 /*
- * scsi_id.c
- *
- * Main section of the scsi_id program
- *
  * Copyright (C) IBM Corp. 2003
  * Copyright (C) SUSE Linux Products GmbH, 2006
  *
- * Author:
- *     Patrick Mansfield<patmans@us.ibm.com>
+ * 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>
@@ -27,7 +29,8 @@
 #include <getopt.h>
 #include <sys/stat.h>
 
-#include "../../udev/udev.h"
+#include "libudev.h"
+#include "libudev-private.h"
 #include "scsi_id.h"
 
 static const struct option options[] = {
@@ -105,7 +108,7 @@ static void set_type(const char *from, char *to, size_t len)
                        break;
                }
        }
-       util_strlcpy(to, type, len);
+       util_strscpy(to, len, type);
 }
 
 /*
@@ -196,6 +199,7 @@ static int get_file_options(struct udev *udev,
         */
        buffer = malloc(MAX_BUFFER_LEN);
        if (!buffer) {
+               fclose(fd);
                err(udev, "can't allocate memory\n");
                return -1;
        }
@@ -354,7 +358,7 @@ static int set_options(struct udev *udev,
 
                case 'd':
                        dev_specified = 1;
-                       util_strlcpy(maj_min_dev, optarg, MAX_PATH_LEN);
+                       util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
                        break;
 
                case 'e':
@@ -362,7 +366,7 @@ static int set_options(struct udev *udev,
                        break;
 
                case 'f':
-                       util_strlcpy(config_file, optarg, MAX_PATH_LEN);
+                       util_strscpy(config_file, MAX_PATH_LEN, optarg);
                        break;
 
                case 'g':
@@ -428,7 +432,7 @@ static int set_options(struct udev *udev,
        }
        if (optind < argc && !dev_specified) {
                dev_specified = 1;
-               util_strlcpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
+               util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
        }
        return 0;
 }
@@ -506,41 +510,16 @@ static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, co
        udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
        udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
 
-       udev_util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
-       udev_util_replace_chars(vendor_str, NULL);
-       udev_util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
-       udev_util_replace_chars(model_str, NULL);
+       util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
+       util_replace_chars(vendor_str, NULL);
+       util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
+       util_replace_chars(model_str, NULL);
        set_type(dev_scsi->type, type_str, sizeof(type_str));
-       udev_util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
-       udev_util_replace_chars(revision_str, NULL);
+       util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
+       util_replace_chars(revision_str, NULL);
        return 0;
 }
 
-/*
- * format_serial: replace to whitespaces by underscores for calling
- * programs that use the serial for device naming (multipath, Suse
- * naming, etc...)
- */
-static void format_serial(char *serial)
-{
-       char *p = serial, *q;
-
-       q = p;
-       while (*p != '\0') {
-               if (isspace(*p)) {
-                       if (q > serial && q[-1] != '_') {
-                               *q = '_';
-                               q++;
-                       }
-               } else {
-                       *q = *p;
-                       q++;
-               }
-               p++;
-       }
-       *q = '\0';
-}
-
 /*
  * scsi_id: try to get an id, if one is found, printf it to stdout.
  * returns a value passed to exit() - 0 if printed an id, else 1.
@@ -573,6 +552,7 @@ static int scsi_id(struct udev *udev, char *maj_min_dev)
        if (export) {
                char serial_str[MAX_SERIAL_LEN];
 
+               printf("ID_SCSI=1\n");
                printf("ID_VENDOR=%s\n", vendor_str);
                printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
                printf("ID_MODEL=%s\n", model_str);
@@ -580,13 +560,28 @@ static int scsi_id(struct udev *udev, char *maj_min_dev)
                printf("ID_REVISION=%s\n", revision_str);
                printf("ID_TYPE=%s\n", type_str);
                if (dev_scsi.serial[0] != '\0') {
-                       udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
-                       udev_util_replace_chars(serial_str, NULL);
+                       util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
+                       util_replace_chars(serial_str, NULL);
                        printf("ID_SERIAL=%s\n", serial_str);
-                       udev_util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
-                       udev_util_replace_chars(serial_str, NULL);
+                       util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
+                       util_replace_chars(serial_str, NULL);
                        printf("ID_SERIAL_SHORT=%s\n", serial_str);
                }
+               if (dev_scsi.wwn[0] != '\0') {
+                       printf("ID_WWN=0x%s\n", dev_scsi.wwn);
+                       if (dev_scsi.wwn_vendor_extension[0] != '\0') {
+                               printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
+                               printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
+                       } else {
+                               printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
+                       }
+               }
+               if (dev_scsi.tgpt_group[0] != '\0') {
+                       printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
+               }
+               if (dev_scsi.unit_serial_number[0] != '\0') {
+                       printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
+               }
                goto out;
        }
 
@@ -594,8 +589,16 @@ static int scsi_id(struct udev *udev, char *maj_min_dev)
                retval = 1;
                goto out;
        }
-       if (reformat_serial)
-               format_serial(dev_scsi.serial);
+
+       if (reformat_serial) {
+               char serial_str[MAX_SERIAL_LEN];
+
+               util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
+               util_replace_chars(serial_str, NULL);
+               printf("%s\n", serial_str);
+               goto out;
+       }
+
        printf("%s\n", dev_scsi.serial);
 out:
        return retval;
@@ -613,7 +616,7 @@ int main(int argc, char **argv)
        if (udev == NULL)
                goto exit;
 
-       logging_init("scsi_id");
+       udev_log_init("scsi_id");
        udev_set_log_fn(udev, log_fn);
 
        /*
@@ -649,6 +652,6 @@ int main(int argc, char **argv)
 
 exit:
        udev_unref(udev);
-       logging_close();
+       udev_log_close();
        return retval;
 }