+static int getport(int p)
+{
+ char path[256], line[256], *ep;
+ FILE *fp;
+ int rv, rp;
+ size_t ln;
+
+ if((rp = mappin(p)) < 0)
+ errx(1, "%i: no such port", p);
+ checkdir(rp, "in");
+ sprintf(path, "/sys/class/gpio/gpio%i/value", rp);
+ if((fp = fopen(path, "r")) == NULL)
+ err(1, "%s", path);
+ rv = !!fgets(line, sizeof(line), fp);
+ fclose(fp);
+ if(!rv || ((ln = strlen(line)) < 1) || (line[ln - 1] != '\n'))
+ errx(1, "gpio%i: could not read direction", rp);
+ line[ln - 1] = 0;
+ rv = (int)strtol(line, &ep, 10);
+ if(*ep)
+ errx(1, "gpio%i: unexpected contents: %s", rp, line);
+ return(rv);
+}
+