sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

init-msys1-helper.c (1846B)


      1 // For MSYS 1.0
      2 #include <stdio.h>
      3 #include <windows.h>
      4 #include <sys/stat.h>
      5 #include <signal.h>
      6 
      7 BOOL is_process_alive(HANDLE handle) {
      8   DWORD result;
      9   return GetExitCodeProcess(handle, &result) && result == STILL_ACTIVE;
     10 }
     11 
     12 BOOL is_file(const char* filename) {
     13   struct stat st;
     14   return stat(filename, &st) == 0 && S_ISREG(st.st_mode);
     15 }
     16 
     17 int main(int argc, char** argv) {
     18   const char* winpid = argv[1];
     19   const char* fname_buff = argv[2];
     20   const char* fname_read = argv[3];
     21 
     22   signal(SIGINT, SIG_IGN);
     23   //signal(SIGQUIT, SIG_IGN);
     24 
     25   int ppid = atoi(winpid);
     26   if (!ppid) {
     27     fprintf(stderr, "ble.sh (msys1): invalid process ID '%s'\n", winpid);
     28     return 1;
     29   }
     30   HANDLE parent_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ppid);
     31   if (parent_process == NULL) {
     32     fprintf(stderr, "ble.sh (msys1): failed to open the parent process '%s'\n", winpid);
     33     return 1;
     34   }
     35 
     36   int exit_code = 0;
     37   BOOL terminate = FALSE;
     38   while (!terminate) {
     39     unlink(fname_read);
     40     if (rename(fname_buff, fname_read) != 0) {
     41       perror("ble.sh (msys1)");
     42       fprintf(stderr, "ble.sh (msys1): failed to move the file '%s' -> '%s'\n", fname_buff, fname_read);
     43       terminate = TRUE;
     44       exit_code = 1;
     45       break;
     46     }
     47 
     48     FILE* f = fopen(fname_read, "r");
     49     if (!f) {
     50       fprintf(stderr, "ble.sh (msys1): failed to open the file '%s'\n", fname_read);
     51       terminate = TRUE;
     52       exit_code = 1;
     53       break;
     54     }
     55 
     56     for (;;) {
     57       if (!is_process_alive(parent_process)) {
     58         terminate = TRUE;
     59         break;
     60       }
     61       if (is_file(fname_buff)) break;
     62 
     63       int count = 0;
     64       char buff[4096];
     65       while (count = fread(&buff, 1, sizeof buff, f))
     66         fwrite(buff, 1, count, stdout);
     67       fflush(stdout);
     68       Sleep(20);
     69     }
     70     fclose(f);
     71   }
     72 
     73   CloseHandle(parent_process);
     74   return exit_code;
     75 }