Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. void rebuild_if_modified(int argc, char *argv[])
  2. {
  3. assert(argc > 0);
  4. const char *binary = argv[0];
  5. const char *source = __FILE__;
  6.  
  7. struct stat source_stat;
  8. stat(source, &source_stat);
  9.  
  10. struct stat binary_stat;
  11. stat(binary, &binary_stat);
  12.  
  13. if (source_stat.st_mtime > binary_stat.st_mtime) {
  14. printf("%s is modified. Rebuilding myself...\n", source);
  15. pid_t pid = fork();
  16. if (pid) {
  17. int status = 0;
  18. waitpid(pid, &status, 0);
  19. execlp(binary, binary, NULL);
  20. } else {
  21. printf("Building build\n");
  22. execlp("gcc", "gcc", "-o", binary, source, NULL);
  23. }
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement