inp.c
上传用户:wudi5211
上传日期:2010-01-21
资源大小:607k
文件大小:3k
- /*
- * inp.c -- read all the ports specified in hex on the command line.
- * The program uses the faster ioperm/iopl calls on x86, /dev/port
- * on other platforms. The program acts as inb/inw/inl according
- * to its own name
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <asm/io.h> /* linux-specific */
- #ifdef __GLIBC__
- # include <sys/perm.h>
- #endif
- #define PORT_FILE "/dev/port"
- char *prgname;
- #ifdef __i386__
- static int read_and_print_one(unsigned int port,int size)
- {
- static int iopldone = 0;
- if (port > 1024) {
- if (!iopldone && iopl(3)) {
- fprintf(stderr, "%s: iopl(): %sn", prgname, strerror(errno));
- return 1;
- }
- iopldone++;
- } else if (ioperm(port,size,1)) {
- fprintf(stderr, "%s: ioperm(%x): %sn", prgname,
- port, strerror(errno));
- return 1;
- }
- if (size == 4)
- printf("%04x: %08xn", port, inl(port));
- else if (size == 2)
- printf("%04x: %04xn", port, inw(port));
- else
- printf("%04x: %02xn", port, inb(port));
- return 0;
- }
- #else /* not i386 */
- static int read_and_print_one(unsigned int port,int size)
- {
- static int fd = -1;
- unsigned char b; unsigned short w; unsigned int l;
- if (fd < 0)
- fd = open(PORT_FILE, O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "%s: %s: %sn", prgname, PORT_FILE, strerror(errno));
- return 1;
- }
- lseek(fd, port, SEEK_SET);
-
- if (size == 4) {
- read(fd, &l, 4);
- printf("%04x: 0x%08xn", port, l);
- } else if (size == 2) {
- read(fd, &w, 2);
- printf("%04x: 0x%04xn", port, w & 0xffff);
- } else {
- read(fd, &b, 1);
- printf("%04x: 0x%02xn", port, b & 0xff);
- }
- return 0;
- }
- #endif /* i386 */
- int main(int argc, char **argv)
- {
- unsigned int i, n, port, size, error = 0;
-
- prgname = argv[0];
- /* find the data size */
- switch (prgname[strlen(prgname)-1]) {
- case 'w': size = 2; break;
- case 'l': size = 4; break;
- case 'b': case 'p': default:
- size = 1;
- }
- setuid(0); /* if we're setuid, force it on */
- for (i = 1; i < argc; i++) {
- if ( sscanf(argv[i], "%x%n", &port, &n) < 1
- || n != strlen(argv[i]) ) {
- fprintf(stderr, "%s: argument "%s" is not a hex numbern",
- argv[0], argv[i]);
- error++; continue;
- }
- if (port & (size-1)) {
- fprintf(stderr, "%s: argument "%s" is not properly alignedn",
- argv[0], argv[i]);
- error++; continue;
- }
- error += read_and_print_one(port, size);
- }
- exit (error ? 1 : 0);
- }