Merge branch 'exp-hash'
[eleutheria.git] / fileops / readwd.c
blob3cb77dbd53f5df47a09bfb49e6bedca2b794794e
1 /* WARNING: I got spontaneous hangups with the following code in NetBSD 4.99.20
2 compile with:
3 gcc readwd.c -o readwd -Wall -W -Wextra -ansi -pedantic */
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <dev/ata/atareg.h>
11 #include <sys/ataio.h>
12 #include <sys/ioctl.h>
13 #include <sys/param.h>
15 int main(int argc, char *argv[])
17 int fd;
18 unsigned int i;
19 struct atareq req;
20 union {
21 unsigned char inbuf[DEV_BSIZE];
22 struct ataparams inqbuf;
23 } inbuf;
24 u_int16_t *p;
26 /* check argument count */
27 if (argc != 2) {
28 fprintf(stderr, "usage: %s /dev/file\n", argv[0]);
29 exit(EXIT_FAILURE);
32 /* open device file descriptor */
33 if ((fd = open(argv[1], O_RDONLY)) == -1) {
34 perror("open");
35 exit(EXIT_FAILURE);
38 /* construct an ata request */
39 memset(&req, 0, sizeof req);
40 req.flags = ATACMD_READ;
41 req.command = WDCC_IDENTIFY;
42 req.databuf = (caddr_t) &inbuf;
43 req.datalen = sizeof inbuf;
44 req.timeout = 1000; /* 1 sec */
46 /* make the ioctl call */
47 if (ioctl(fd, ATAIOCCOMMAND, &req) == -1) {
48 perror("ioctl");
49 exit(EXIT_FAILURE);
52 /* handle ata request return status */
53 switch (req.retsts) {
54 case ATACMD_OK:
55 break;
56 case ATACMD_TIMEOUT:
57 fprintf(stderr, "ata request timed out\n");
58 exit(EXIT_FAILURE);
59 case ATACMD_DF:
60 fprintf(stderr, "ata device returned a device fault\n");
61 exit(EXIT_FAILURE);
62 case ATACMD_ERROR:
63 fprintf(stderr, "ata device returned error code: %0x\n", req.error);
64 exit(EXIT_FAILURE);
65 default:
66 fprintf(stderr, "unknown ata request return status: %d\n", req.retsts);
67 exit(EXIT_FAILURE);
70 /* magic for little endian archs
71 * FIXME: add #ifdef condition for little endian archs
73 for (i = 0; i < sizeof inbuf.inqbuf.atap_model; i+=2) {
74 p = (u_int16_t *) (inbuf.inqbuf.atap_model + i);
75 *p = ntohs(*p);
78 /* print the model (trim spaces when printing) */
79 for (i = 0; i < sizeof inbuf.inqbuf.atap_model; i++)
80 if (inbuf.inqbuf.atap_model[i] != ' ')
81 printf("%c", inbuf.inqbuf.atap_model[i]);
82 printf("\n");
84 /* close file descriptor */
85 close(fd);
87 return EXIT_SUCCESS;