Skip to content

Instantly share code, notes, and snippets.

@Surge1223
Last active May 31, 2018 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Surge1223/333413b0018dea1c834b4b3308a76b80 to your computer and use it in GitHub Desktop.
Save Surge1223/333413b0018dea1c834b4b3308a76b80 to your computer and use it in GitHub Desktop.
./blkpath boot boot.img returns block path to boot
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/system_properties.h>
#define BUFFER_SIZE 4096
#define devblockpath "/dev/block/"
#define byname "/by-name/"
int find_blockdev(const char *bootdevice, const char *outfname, const char *infname) {
char buf[256], path[128];
pid_t child;
int status, n, a, d, diff;
char *blockdev;
char *pch;
char realpath[]="/dev/block/";
char c[1024];
char *b=c;
char *f=buf;
n = readlink(bootdevice, path, sizeof(path) - 1);
if (n > 0) {
path[n] = 0
if (!memcmp(path, "/dev/block/", 11));
blockdev = path;
strcat(path, byname);
if (strncmp(bootdevice, "/by-name/", 9) == 0) {
blockdev = path;
}
}
pch=strchr(path,'/')
while (pch!=NULL) {
diff=pch-path+1
bootdevice = pch
pch=strchr(pch+1,'/')
status = readlink(bootdevice, path, sizeof(path));
}
strcat(path, outfname);
a=readlink(path, c, sizeof(c) - 1);
c[a]=0;
b=c;
printf("\n%s is symlinked to: %s\n", c, path);
return 0;
}
int main(int argc, char** argv) {
char *infname, *outfname;
char path[128];
char *abpath;
int i;
char *bootdevice="/dev/block/bootdevice";
if (argc != 3) {
printf("Usage: %s by-name-dir input img \n", argv[0]);
exit(0);
}
infname = argv[2];
abpath = path;
outfname = argv[1];
find_blockdev(bootdevice, outfname, infname);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment