Initial import of popen_ls.c
[eleutheria.git] / fileops / readwd.c
blob0a1fcbc2f4bb41eb8f1f999b68277de4d9276f99
1 /*
2 * WARNING: I got spontaneous hangups with the following code in NetBSD 4.99.20
4 * Compile with:
5 * gcc readwd.c -o readwd -Wall -W -Wextra -ansi -pedantic
6 */
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <dev/ata/atareg.h>
14 #include <sys/ataio.h>
15 #include <sys/ioctl.h>
16 #include <sys/param.h>
18 int main(int argc, char *argv[])
20 int fd;
21 unsigned int i;
22 struct atareq req;
23 union {
24 unsigned char inbuf[DEV_BSIZE];
25 struct ataparams inqbuf;
26 } inbuf;
27 u_int16_t *p;
29 /* Check argument count */
30 if (argc != 2) {
31 fprintf(stderr, "usage: %s /dev/file\n", argv[0]);
32 exit(EXIT_FAILURE);
35 /* Open device file descriptor */
36 if ((fd = open(argv[1], O_RDONLY)) == -1) {
37 perror("open");
38 exit(EXIT_FAILURE);
41 /* Construct an ata request */
42 memset(&req, 0, sizeof req);
43 req.flags = ATACMD_READ;
44 req.command = WDCC_IDENTIFY;
45 req.databuf = (caddr_t) &inbuf;
46 req.datalen = sizeof inbuf;
47 req.timeout = 1000; /* 1 sec */
49 /* Make the ioctl call */
50 if (ioctl(fd, ATAIOCCOMMAND, &req) == -1) {
51 perror("ioctl");
52 exit(EXIT_FAILURE);
55 /* Handle ata request return status */
56 switch (req.retsts) {
57 case ATACMD_OK:
58 break;
59 case ATACMD_TIMEOUT:
60 fprintf(stderr, "ata request timed out\n");
61 exit(EXIT_FAILURE);
62 case ATACMD_DF:
63 fprintf(stderr, "ata device returned a device fault\n");
64 exit(EXIT_FAILURE);
65 case ATACMD_ERROR:
66 fprintf(stderr, "ata device returned error code: %0x\n", req.error);
67 exit(EXIT_FAILURE);
68 default:
69 fprintf(stderr, "unknown ata request return status: %d\n", req.retsts);
70 exit(EXIT_FAILURE);
74 * Magic for little endian archs
75 * FIXME: add #ifdef condition for little endian archs
77 for (i = 0; i < sizeof inbuf.inqbuf.atap_model; i+=2) {
78 p = (u_int16_t *) (inbuf.inqbuf.atap_model + i);
79 *p = ntohs(*p);
82 /* Print the model (trim spaces when printing) */
83 for (i = 0; i < sizeof inbuf.inqbuf.atap_model; i++)
84 if (inbuf.inqbuf.atap_model[i] != ' ')
85 printf("%c", inbuf.inqbuf.atap_model[i]);
86 printf("\n");
88 /* Close file descriptor */
89 close(fd);
91 return EXIT_SUCCESS;