+static void export(int p)
+{
+ char path[256];
+ FILE *fp;
+ int rp;
+
+ if((rp = mappin(p)) < 0)
+ errx(1, "%i: no such port\n", p);
+ sprintf(path, "/sys/class/gpio/gpio%i", rp);
+ if(!access(path, R_OK | X_OK))
+ return;
+ if(preserve)
+ errx(2, "gpio%i: not exported", rp);
+ sprintf(path, "/sys/class/gpio/export");
+ if((fp = fopen(path, "w")) == NULL)
+ err(1, "%s", path);
+ fprintf(fp, "%i\n", rp); fflush(fp);
+ if(ferror(fp))
+ errx(1, "gpio%i: could not export", rp);
+ fclose(fp);
+ sprintf(path, "/sys/class/gpio/gpio%i", rp);
+ if(access(path, R_OK | X_OK))
+ errx(1, "gpio%i: still not available after export", rp);
+}
+