chiark / gitweb /
add ID_BUS to *_id programs
[elogind.git] / extras / ata_id / ata_id.c
index 59e2b3987b376b6dc58aed5144c0b32d345c393b..1e99b9df9c6129e8e81490af2d10dd7038bc1795 100644 (file)
@@ -67,25 +67,30 @@ static void set_str(char *to, const unsigned char *from, int count)
        int i, j;
        int len;
 
        int i, j;
        int len;
 
+       /* strip trailing whitespace */
        len = strnlen(from, count);
        while (isspace(from[len-1]))
                len--;
 
        len = strnlen(from, count);
        while (isspace(from[len-1]))
                len--;
 
+       /* strip leading whitespace */
        i = 0;
        while (isspace(from[i]) && (i < len))
                i++;
 
        j = 0;
        while (i < len) {
        i = 0;
        while (isspace(from[i]) && (i < len))
                i++;
 
        j = 0;
        while (i < len) {
-               switch(from[i]) {
-               case '/':
-               case ' ':
+               /* substitute multiple whitespace */
+               if (isspace(from[i])) {
+                       while (isspace(from[i]))
+                               i++;
                        to[j++] = '_';
                        to[j++] = '_';
-                       break;
-               default:
-                       to[j++] = from[i];
                }
                }
-               i++;
+               /* skip chars */
+               if (from[i] == '/') {
+                       i++;
+                       continue;
+               }
+               to[j++] = from[i++];
        }
        to[j] = '\0';
 }
        }
        to[j] = '\0';
 }
@@ -102,6 +107,8 @@ int main(int argc, char *argv[])
        int fd;
        int rc = 0;
 
        int fd;
        int rc = 0;
 
+       logging_init("ata_id");
+
        for (i = 1 ; i < argc; i++) {
                char *arg = argv[i];
 
        for (i = 1 ; i < argc; i++) {
                char *arg = argv[i];
 
@@ -137,11 +144,38 @@ int main(int argc, char *argv[])
        set_str(revision, id.fw_rev, 8);
 
        if (export) {
        set_str(revision, id.fw_rev, 8);
 
        if (export) {
+               if ((id.config >> 8) & 0x80) {
+                       /* This is an ATAPI device */
+                       switch ((id.config >> 8) & 0x1f) {
+                       case 0:
+                               printf("ID_TYPE=cd\n");
+                               break;
+                       case 1:
+                               printf("ID_TYPE=tape\n");
+                               break;
+                       case 5:
+                               printf("ID_TYPE=cd\n");
+                               break;
+                       case 7:
+                               printf("ID_TYPE=optical\n");
+                               break;
+                       default:
+                               printf("ID_TYPE=generic\n");
+                               break;
+                       }
+               } else {
+                       printf("ID_TYPE=disk\n");
+               }
                printf("ID_MODEL=%s\n", model);
                printf("ID_SERIAL=%s\n", serial);
                printf("ID_REVISION=%s\n", revision);
                printf("ID_MODEL=%s\n", model);
                printf("ID_SERIAL=%s\n", serial);
                printf("ID_REVISION=%s\n", revision);
-       } else
-               printf("%s_%s\n", model, serial);
+               printf("ID_BUS=ata\n");
+       } else {
+               if (serial[0] != '\0')
+                       printf("%s_%s\n", model, serial);
+               else
+                       printf("%s\n", model);
+       }
 
 close:
        close(fd);
 
 close:
        close(fd);