Advertisement
Guest User

bleh

a guest
Feb 28th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include <sys/mman.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8.  
  9. #define IMG     "./english.dsk"
  10. const off_t offset = 0x0468c00;
  11. #define S_OFFSET "OFFSET"
  12. #define SIZE    60
  13.  
  14. int edit(char *v) {
  15.   int fd;
  16.   char *mem;
  17.   struct stat s;
  18.   off_t pa_offset, diff;
  19.   pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
  20.   printf("PA_OFFSET = %d, offset = %d\n", pa_offset, offset);
  21.   diff = offset - pa_offset;
  22.   if ((fd  = open(IMG, O_RDWR)) == -1) {
  23.    perror("open");
  24.   } else {
  25.    fstat(fd, &s);
  26.    if ((mem = mmap(NULL, SIZE + diff, PROT_READ | PROT_WRITE, MAP_SHARED, fd, pa_offset)) == MAP_FAILED) {
  27.     perror("mmap");
  28.   }
  29.    memset(&mem[diff], 0, SIZE);
  30.    memcpy(&mem[diff], v, strlen(v));
  31.    if (msync(mem, SIZE + diff, MS_SYNC|MS_INVALIDATE)) {
  32.     perror("msync");
  33.    }
  34.    if (munmap(mem, SIZE + diff)) {
  35.     perror("munmap");
  36.    }
  37.    close(fd);
  38.   }
  39. }
  40. int usage(char *v) {
  41.   return printf("usage: %s BLAH\n", v);
  42. }
  43.  
  44. int main(char c, char **v) {
  45.   if (c < 2) return usage(v[0]);
  46.   if (strlen(v[1]) > SIZE) {
  47.     return(printf("Sorry, %d is too big.\n", strlen(v[1])));
  48.   }
  49.   printf("edit_img %s\n", v[1]);
  50.   return (edit(v[1]));
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement