1 /* vi: set sw=4 ts=4: */
3 * lsscsi implementation for busybox
5 * Copyright (C) 2017 Markus Gothe <nietzsche@lysator.liu.se>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "lsscsi (2.4 kb)"
12 //config: #select PLATFORM_LINUX
14 //config: lsscsi is a utility for displaying information about SCSI buses in the
15 //config: system and devices connected to them.
17 //config: This version uses sysfs (/sys/bus/scsi/devices) only.
19 //applet:IF_LSSCSI(APPLET(lsscsi, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_LSSCSI) += lsscsi.o
23 //usage:#define lsscsi_trivial_usage NOUSAGE_STR
24 //usage:#define lsscsi_full_usage ""
28 static char *get_line(const char *filename, char *buf, unsigned *bufsize_p)
30 unsigned bufsize = *bufsize_p;
33 if ((int)(bufsize - 2) <= 0)
36 sz = open_read_close(filename, buf, bufsize - 2);
51 int lsscsi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
52 int lsscsi_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
57 xchdir("/sys/bus/scsi/devices");
60 while ((de = readdir(dir)) != NULL) {
66 const char *type_name;
71 if (!isdigit(de->d_name[0]))
73 if (!strchr(de->d_name, ':'))
75 if (chdir(de->d_name) != 0)
78 bufsize = sizeof(buf);
80 ptr = get_line("vendor", buf, &bufsize);
82 ptr = get_line("type", ptr, &bufsize);
84 ptr = get_line("model", ptr, &bufsize);
86 ptr = get_line("rev", ptr, &bufsize);
88 printf("[%s]\t", de->d_name);
90 #define scsi_device_types \
91 "disk\0" "tape\0" "printer\0" "process\0" \
92 "worm\0" "\0" "scanner\0" "optical\0" \
93 "mediumx\0" "comms\0" "\0" "\0" \
94 "storage\0" "enclosu\0" "sim dsk\0" "opti rd\0" \
95 "bridge\0" "osd\0" "adi\0" "\0" \
98 "\0" "\0" "wlun\0" "no dev"
99 type = bb_strtou(type_str, NULL, 10);
102 || (type_name = nth_string(scsi_device_types, type))[0] == '\0'
104 printf("(%s)\t", type_str);
106 printf("%s\t", type_name);
109 printf("%s\t""%s\t""%s\n",
114 /* TODO: also output device column, e.g. "/dev/sdX" */
119 if (ENABLE_FEATURE_CLEAN_UP)