by Guest »
Hi Eli,
Thanks for your explanation. I have changed the data type from unsigned char to long for the 4th argument (argv[3]) in memwrite.c. And the write function is modified as allwrite(fd, &data, 8). The whole code is as follows.
However, I can only send data to the least 32-bit address (4 bytes). It is still not effective for the higher 4 bytes address. Can you give me some advice?
void allwrite(int fd, long *buf, int len);
int main(int argc, char *argv[]) {
int fd;
// int address;
long address;
// unsigned char data;
long data;
if (argc!=4) {
fprintf(stderr, "Usage: %s devfile address data\n", argv[0]);
exit(1);
}
address = (long) atoi(argv[2]);
// data = (unsigned char) atoi(argv[3]);
data = (long) atoi(argv[3]);
fd = open(argv[1], O_WRONLY);
if (fd < 0) {
if (errno == ENODEV)
fprintf(stderr, "(Maybe %s a read-only file?)\n", argv[1]);
perror("Failed to open devfile");
exit(1);
}
if (lseek(fd, address, SEEK_SET) < 0) {
perror("Failed to seek");
exit(1);
}
allwrite(fd, &data, 8);//1
return 0;
}
void allwrite(int fd, long *buf, int len) {
int sent = 0;
int rc;
while (sent < len) {
rc = write(fd, buf + sent, len - sent);
if ((rc < 0) && (errno == EINTR))
continue;
if (rc < 0) {
perror("allwrite() failed to write");
exit(1);
}
if (rc == 0) {
fprintf(stderr, "Reached write EOF (?!)\n");
exit(1);
}
sent += rc;
}
}
Hi Eli,
Thanks for your explanation. I have changed the data type from unsigned char to long for the 4th argument (argv[3]) in memwrite.c. And the write function is modified as allwrite(fd, &data, 8). The whole code is as follows.
However, I can only send data to the least 32-bit address (4 bytes). It is still not effective for the higher 4 bytes address. Can you give me some advice?
void allwrite(int fd, long *buf, int len);
int main(int argc, char *argv[]) {
int fd;
// int address;
long address;
// unsigned char data;
long data;
if (argc!=4) {
fprintf(stderr, "Usage: %s devfile address data\n", argv[0]);
exit(1);
}
address = (long) atoi(argv[2]);
// data = (unsigned char) atoi(argv[3]);
data = (long) atoi(argv[3]);
fd = open(argv[1], O_WRONLY);
if (fd < 0) {
if (errno == ENODEV)
fprintf(stderr, "(Maybe %s a read-only file?)\n", argv[1]);
perror("Failed to open devfile");
exit(1);
}
if (lseek(fd, address, SEEK_SET) < 0) {
perror("Failed to seek");
exit(1);
}
allwrite(fd, &data, 8);//1
return 0;
}
void allwrite(int fd, long *buf, int len) {
int sent = 0;
int rc;
while (sent < len) {
rc = write(fd, buf + sent, len - sent);
if ((rc < 0) && (errno == EINTR))
continue;
if (rc < 0) {
perror("allwrite() failed to write");
exit(1);
}
if (rc == 0) {
fprintf(stderr, "Reached write EOF (?!)\n");
exit(1);
}
sent += rc;
}
}