Yes, there is a problem. Your use perror()is incorrect.
You must first check if the system call indicates a problem before calling perror. The personal page is in sufficient detail devoted to the topic:
Note that errno is undefined after a successful library call: this call
may well change this variable, even though it succeeds, for example
because it internally used some other library function that failed.
Thus, if a failing call is not immediately followed by a call to per‐
ror(), the value of errno should be saved.
You should check return codes for each system and only call perror if they fail. Something like that:
fd = open("/dev/mtd0", O_RDWR);
if (fd < 0) {
perror("open: ");
return 1;
}
if (lseek(fd, 1, SEEK_SET) < 0) {
perror("lseek: ");
return 1;
}
if (write(fd, &buf, 4) < 0) {
perror("write: ");
return 1;
}
close(fd);
source
share