chiark / gitweb /
libudev: require LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
[elogind.git] / extras / cdrom_id / cdrom_id.c
index 315d27610f1bcc91a9d10886f451fc9f440d7637..9e274718631e38ac0b431ede4578be792fbac1b4 100644 (file)
@@ -37,7 +37,8 @@
 #include <sys/time.h>
 #include <sys/ioctl.h>
 #include <linux/cdrom.h>
-#include "../../udev.h"
+
+#include "../../udev/udev.h"
 
 #ifndef ARRAY_SIZE
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
@@ -71,7 +72,6 @@ void log_message(int priority, const char *format, ...)
        if (debug) {
                fprintf(stderr, "[%d] ", (int) getpid());
                vfprintf(stderr, format, args);
-               fprintf(stderr, "\n");
        } else
                vsyslog(priority, format, args);
        va_end(args);
@@ -123,10 +123,11 @@ static unsigned int cd_media_mrw;
 static unsigned int cd_media_mrw_w;
 
 static const char *cd_media_state;
-static unsigned int cd_media_has_audio;
 static unsigned int cd_media_session_next;
 static unsigned int cd_media_session_count;
 static unsigned int cd_media_track_count;
+static unsigned int cd_media_track_count_data;
+static unsigned int cd_media_track_count_audio;
 static unsigned long long int cd_media_session_last_offset;
 
 #define ERRCODE(s)     ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
@@ -137,10 +138,10 @@ static unsigned long long int cd_media_session_last_offset;
 static void info_scsi_cmd_err(const char *cmd, int err)
 {
        if (err == -1) {
-               info("%s failed", cmd);
+               info("%s failed\n", cmd);
                return;
        }
-       info("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
+       info("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
 }
 
 struct scsi_cmd {
@@ -199,7 +200,7 @@ static int cd_capability_compat(int fd)
 
        capabilty = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
        if (capabilty < 0) {
-               info("CDROM_GET_CAPABILITY failed");
+               info("CDROM_GET_CAPABILITY failed\n");
                return -1;
        }
 
@@ -235,11 +236,11 @@ static int cd_inquiry(int fd) {
        }
 
        if ((inq[0] & 0x1F) != 5) {
-               info("not an MMC unit");
+               info("not an MMC unit\n");
                return -1;
        }
 
-       info("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32);
+       info("INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
        return 0;
 }
 
@@ -264,9 +265,9 @@ static int cd_profiles(int fd)
        }
 
        len = 4 + (header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3]);
-       info("GET CONFIGURATION: number of profiles %i", len);
+       info("GET CONFIGURATION: number of profiles %i\n", len);
        if (len > sizeof(profiles)) {
-               info("invalid number of profiles");
+               info("invalid number of profiles\n");
                return -1;
        }
 
@@ -287,7 +288,7 @@ static int cd_profiles(int fd)
                unsigned int profile = (profiles[i] << 8 | profiles[i + 1]);
                if (profile == 0)
                        continue;
-               info("profile 0x%02x", profile);
+               info("profile 0x%02x\n", profile);
 
                switch (profile) {
                case 0x03:
@@ -343,9 +344,9 @@ static int cd_profiles(int fd)
 
        /* current media profile */
        cur_profile = header[6] << 8 | header[7];
-       info("current profile 0x%02x", cur_profile);
+       info("current profile 0x%02x\n", cur_profile);
        if (cur_profile == 0) {
-               info("no current profile, assuming no media");
+               info("no current profile, assuming no media\n");
                return -1;
        }
 
@@ -435,7 +436,7 @@ static int cd_media_info(int fd)
                return -1;
        };
 
-       info("disk type %02x", header[8]);
+       info("disk type %02x\n", header[8]);
 
        if ((header[2] & 3) < 4)
                cd_media_state = media_status[header[2] & 3];
@@ -467,13 +468,15 @@ static int cd_media_toc(int fd)
        }
 
        len = (header[0] << 8 | header[1]) + 2;
-       info("READ TOC: len: %d", len);
+       info("READ TOC: len: %d\n", len);
        if (len > sizeof(toc))
                return -1;
+       if (len < 2)
+               return -1;
 
-       /* check if we have a data track */
-       info("ctl %02x (0x04 is data/audio)", header[5]);
-       cd_media_has_audio = (header[5] & 0x04) == 0;
+       /* empty media has no tracks */
+       if (len < 8)
+               return 0;
 
        scsi_cmd_set(&sc, 0, 0x43);
        scsi_cmd_set(&sc, 6, header[2]); /* First Track/Session Number */
@@ -488,9 +491,18 @@ static int cd_media_toc(int fd)
 
        for (p = toc+4, i = 4; i < len-8; i += 8, p += 8) {
                unsigned int block;
+               unsigned int is_data_track;
+
+               is_data_track = (p[1] & 0x04) != 0;
 
                block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
-               info("track %u starts at block %u", p[2], block);
+               info("track=%u info=0x%x(%s) start_block=%u\n",
+                    p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
+
+               if (is_data_track)
+                       cd_media_track_count_data++;
+               else
+                       cd_media_track_count_audio++;
        }
 
        scsi_cmd_set(&sc, 0, 0x43);
@@ -503,7 +515,7 @@ static int cd_media_toc(int fd)
                return -1;
        }
        len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
-       info("last track %u starts at block %u", header[4+2], len);
+       info("last track %u starts at block %u\n", header[4+2], len);
        cd_media_session_last_offset = (unsigned long long int)len * 2048;
        return 0;
 }
@@ -539,8 +551,9 @@ int main(int argc, char *argv[])
                        break;
                case 'h':
                        printf("Usage: cdrom_id [options] <device>\n"
-                           " --export        export key/value pairs\n"
-                           " --help\n\n");
+                              "  --export        export key/value pairs\n"
+                              "  --debug         debug to stderr\n"
+                              "  --help          print this help text\n\n");
                        goto exit;
                default:
                        rc = 1;
@@ -550,7 +563,7 @@ int main(int argc, char *argv[])
 
        node = argv[optind];
        if (!node) {
-               err("no device");
+               err("no device\n");
                fprintf(stderr, "no device\n");
                rc = 1;
                goto exit;
@@ -558,11 +571,11 @@ int main(int argc, char *argv[])
 
        fd = open(node, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
-               info("unable to open '%s'", node);
+               info("unable to open '%s'\n", node);
                rc = 1;
                goto exit;
        }
-       info("probing: '%s'", node);
+       info("probing: '%s'\n", node);
 
        /* same data as original cdrom_id */
        if (cd_capability_compat(fd) < 0) {
@@ -674,15 +687,17 @@ print:
 
        if (cd_media_state != NULL)
                printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
-       if (cd_media_has_audio)
-               printf("ID_CDROM_MEDIA_HAS_AUDIO=1\n");
        if (cd_media_session_next > 0)
                printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
        if (cd_media_session_count > 0)
                printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
        if (cd_media_track_count > 0)
                printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
-       if (cd_media_session_last_offset)
+       if (cd_media_track_count_audio > 0)
+               printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
+       if (cd_media_track_count_data > 0)
+               printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);
+       if (cd_media_session_last_offset > 0)
                printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
 exit:
        if (fd >= 0)