0

I would like to verify the serial number of my device with C program, so the command to get the serial number is in shell:

dd if=/dev/mtd0 bs=1 skip=$((0x1fc30)) count=16 2>/dev/null

This will result something like this:

1866203214226041

Here's basically what I want to do:

#include <stdio.h>
#include <string.h>

int main ()
{
    int sn;
    sn=1866203214226041;

    system("dd if=/dev/mtd0 bs=1 skip=$((0x1fc30)) count=16 2>/dev/null");

    if (output of system == sn) {
        printf("The serial number is verified\n");
    }
    else {
        printf("The serial number is not verified\n");
    }
    return(0);
}

How can I do this ?

hillz
  • 467
  • 5
  • 9
  • 17
  • 3
    [popen](https://linux.die.net/man/3/popen) – kaylum Nov 12 '16 at 03:57
  • Is there a special reason you don't want to/can read the file `/dev/mtd0` directly? – deamentiaemundi Nov 12 '16 at 03:57
  • @deamentiaemundi I actually would like to do that in C but I don't know how, I'm still new in this programming language – hillz Nov 12 '16 at 03:59
  • 2
    @hillz there is `fopen` to open the file and `fseek()` to set the file pointer to the right position, and `fread()` to read teh 16 bytes of interest – deamentiaemundi Nov 12 '16 at 04:01
  • @deamentiaemundi but `/dev/mtd0` is in binary, I have to convert it to a human readable format, in shell it's basically using `cat /dev/mtd0 | strings` How can I convert it with C program ? – hillz Nov 12 '16 at 04:10
  • @hillz the shell comman you used is nothing more than reading 16 bytes from `/dev/mtd0` from position `0x1fc30` on upwards. Just implement it the way I told you (you might need to add a "b" with `fopen()` if on Windows) and print those 16 bytes. Would make me wonder if it doesn't do what you wanted. – deamentiaemundi Nov 12 '16 at 04:18
  • Why do you need to use dd, why don't you open /dev/mtd0, seek to 0x1fc30 and read 16 bytes yourself? – user253751 Nov 12 '16 at 04:58

0 Answers0