13 #include <sys/types.h>
14 #include <sys/socket.h>
17 const char *const pccardd_file = "/var/tmp/.pccardd";
18 const char *prog = "pccardq";
19 const char *tmp_dir = "/tmp";
20 unsigned slot_map = ~0;
25 fprintf(stderr, "usage: %s [-a] [-n] [-s slot]\n", prog);
29 proc_arg(int ac, char **av)
34 char *p = strrchr(av[0], '/');
35 prog = p ? p + 1 : av[0];
37 tmp_dir = getenv("TMPDIR") ? getenv("TMPDIR") : tmp_dir;
39 while ((ch = getopt(ac, av, "ans:")) != -1) {
50 if (n < 0 || n >= CHAR_BIT * sizeof slot_map) {
51 warnc(0, "Invalid slot number.");
72 connect_to_pccardd(char **path)
76 struct sockaddr_un pccardq;
77 struct sockaddr_un pccardd;
79 if ((so = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
84 snprintf(pccardq.sun_path, sizeof pccardq.sun_path,
85 "%s/%s%ld%ld", tmp_dir, prog, (long) getpid(), (long) time(0));
86 pccardq.sun_family = AF_UNIX;
87 pccardq.sun_len = offsetof(struct sockaddr_un, sun_path) + strlen(pccardq.sun_path);
88 if (bind(so, (struct sockaddr *) &pccardq, pccardq.sun_len) < 0) {
89 warn("bind: %s", pccardq.sun_path);
92 if ((*path = strdup(pccardq.sun_path)) == NULL) {
97 pccardd_len = strlen(pccardd_file) + 1;
98 if (pccardd_len > sizeof pccardd.sun_path) {
99 warnc(0, "%s: too long", pccardd_file);
102 pccardd.sun_len = offsetof(struct sockaddr_un, sun_path) + pccardd_len;
103 pccardd.sun_family = AF_UNIX;
104 strcpy(pccardd.sun_path, pccardd_file);
105 if (connect(so, (struct sockaddr *) &pccardd, pccardd.sun_len) < 0) {
106 warn("connect: %s", pccardd_file);
117 get_slot_number(int so)
123 if ((rv = write(so, "S", 1)) < 1) {
126 } else if (rv != 1) {
127 warnc(0, "write: fail.");
131 if ((rv = read(so, buf, sizeof buf)) < 0) {
135 buf[sizeof buf - 1] = 0;
136 if (sscanf(buf, "%d", &nslot) != 1) {
137 warnc(0, "Invalid response.");
153 get_slot_info(int so, int slot, char **manuf, char **version, char
154 **device, int *state)
158 static char buf[1024];
167 if ((slen = snprintf(buf, sizeof buf, "N%d", slot)) < 0) {
172 if ((rv = write(so, buf, slen)) < 0) {
175 } else if (rv != slen) {
180 if ((rv = read(so, buf, sizeof buf)) < 0) {
186 if ((sl = strsep(&s, "~")) == NULL)
188 if (atoi(sl) != slot)
190 if ((_manuf = strsep(&s, "~")) == NULL)
192 if ((_version = strsep(&s, "~")) == NULL)
194 if ((_device = strsep(&s, "~")) == NULL)
196 if (sscanf(s, "%1d", state) != 1)
198 if (s != NULL && strchr(s, '~') != NULL)
201 if ((*manuf = strdup(_manuf)) == NULL) {
205 if ((*version = strdup(_version)) == NULL) {
209 if ((*device = strdup(_device)) == NULL) {
213 if (*manuf == NULL || *version == NULL || *device == NULL) {
222 warnc(0, "Invalid response: %*s", rv, buf);
242 main(int ac, char **av)
249 if (proc_arg(ac, av) < 0)
251 if ((so = connect_to_pccardd(&path)) < 0)
253 if ((nslot = get_slot_number(so)) < 0)
256 printf("%d\n", nslot);
258 for (i = 0; i < nslot; i++) {
259 if ((slot_map & (1 << i))) {
265 if (get_slot_info(so, i, &manuf, &version, &device,
268 if (manuf == NULL || version == NULL || device == NULL)
270 printf("%d~%s~%s~%s~%s\n",
271 i, manuf, version, device, strstate(state));