All pastes #2047360 Raw Edit

Untitled

public c v1 · immutable
#2047360 ·published 2011-04-17 05:16 UTC
rendered paste body
#include <sys/io.h> /* ioperm(), inb(), outb() */#include <stdio.h> /* printf(), fprintf() */#include <stdlib.h> /* exit(), EXIT_FAILURE */#define status (base + 1)#define control (base + 2)/* DOS scans these 3 downwards to detect parallel ports, so do we. */int scanp[4] = { 0x3BC, 0x378, 0x278, -1 };int ports[4];int nports;int lp_test(int base) {        int c, d;        if (ioperm(base, 4, 1)) {                fprintf(stderr, "warning: cannot probe port %i\n", base);        } else {                c = inb(base);                c++;                outb(c, base);                d = inb(base);                outb(--c, base);                ioperm(base, 4, 0);                if (d == c)                        return 1;        }        return 0;}int lp_detect(void) {        int i, nports = 0;        for (i = 0; scanp[i] != -1; ++i)                if (lp_test(scanp[i]))                        ports[nports++] = scanp[i];        ports[nports] = -1;        return nports;}void printb(char b) {        int bit = 7;        printf("0x%X ", b);        next:        printf("%i", (b & (1 << bit)) >> bit);        if (bit > 0) {                putchar(' ');                --bit;                goto next;        }        putchar('\n');}int main(int argc, char *argv[]) {        int base, c;        if (!lp_detect()) {                fprintf(stderr, "error: no parallel port found\n");                exit(EXIT_FAILURE);        }        base = ports[0];        if (ioperm(base, 4, 1)) {                fprintf(stderr, "error: failed to claim port %i\n");                exit(EXIT_FAILURE);        }        c = inb(control);        printb(c);        return EXIT_SUCCESS;}