Revert ".c files don't need to be +x"
[eleutheria.git] / fileops / readwd.c
blob04dda496059e5c5981b85b0c8909dc64a0863a94
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 <dev/ata/atareg.h>
10 #include <sys/ataio.h>
11 #include <sys/ioctl.h>
12 #include <sys/param.h>
14 int main(int argc, char *argv[])
16 int fd;
17 unsigned int i;
18 struct atareq req;
19 union {
20 unsigned char inbuf[DEV_BSIZE];
21 struct ataparams inqbuf;
22 } inbuf;
23 u_int16_t *p;
25 /* check argument count */
26 if (argc != 2) {
27 fprintf(stderr, "usage: %s /dev/file\n", argv[0]);
28 exit(EXIT_FAILURE);
31 /* open device file descriptor */
32 if ((fd = open(argv[1], O_RDONLY)) == -1) {
33 perror("open");
34 exit(EXIT_FAILURE);
37 /* construct an ata request */
38 memset(&req, 0, sizeof req);
39 req.flags = ATACMD_READ;
40 req.command = WDCC_IDENTIFY;
41 req.databuf = (caddr_t) &inbuf;
42 req.datalen = sizeof(inbuf);
43 req.timeout = 1000; /* 1 sec */
45 /* make the ioctl call */
46 if (ioctl(fd, ATAIOCCOMMAND, &req) == -1) {
47 perror("ioctl");
48 exit(EXIT_FAILURE);
51 /* handle ata request return status */
52 switch (req.retsts) {
53 case ATACMD_OK:
54 break;
55 case ATACMD_TIMEOUT:
56 fprintf(stderr, "ata request timed out\n");
57 exit(EXIT_FAILURE);
58 case ATACMD_DF:
59 fprintf(stderr, "ata device returned a device fault\n");
60 exit(EXIT_FAILURE);
61 case ATACMD_ERROR:
62 fprintf(stderr, "ata device returned error code: %0x\n", req.error);
63 exit(EXIT_FAILURE);
64 default:
65 fprintf(stderr, "unknown ata request return status: %d\n", req.retsts);
66 exit(EXIT_FAILURE);
69 /* print the model */
70 for (i = 0; i < sizeof(inbuf.inqbuf.atap_model); i+=2) {
71 p = (u_int16_t *) (inbuf.inqbuf.atap_model + i);
72 *p = ntohs(*p);
75 for (i = 0; i < sizeof(inbuf.inqbuf.atap_model); i++)
76 printf("%c", inbuf.inqbuf.atap_model[i]);
78 /* close file descriptor */
79 close(fd);
81 return EXIT_SUCCESS;