home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / wget153.zip / os2 / os2.c < prev    next >
C/C++ Source or Header  |  1998-09-23  |  4KB  |  154 lines

  1. #define  INCL_WINSWITCHLIST
  2. #include <os2emx.h>
  3. #include <string.h>
  4. #include <process.h>
  5. #include <sys/types.h>
  6.  
  7. #include <wget.h>
  8. #include <utils.h>
  9.  
  10. /* change wget process title in PM tasklist */
  11.  
  12. void os2title(init, url)
  13. int init;
  14. const char *url;
  15. {
  16.   static char new_title[MAXNAMEL];
  17.   static char *org_title;
  18.   static HSWITCH hswitch;
  19.   static SWCNTRL sw;
  20.   static PSWCNTRL psw = (PSWCNTRL) &sw;
  21.   switch(init) {
  22.     case 1:
  23.       /* save current process title */
  24.       hswitch = WinQuerySwitchHandle(0, getpid());
  25.       WinQuerySwitchEntry(hswitch, psw);
  26.       if(psw->uchVisibility) {
  27.     org_title = (char *) malloc(strlen(psw->szSwtitle)+1);
  28.     strcpy(org_title, psw->szSwtitle);
  29.       }    
  30.       break;
  31.     case -1:
  32.       /* restore original task title */
  33.       if(psw->uchVisibility) {
  34.     strcpy(psw->szSwtitle, org_title);
  35.     WinChangeSwitchEntry(hswitch, psw);
  36.       }    
  37.       break;
  38.     case 0:
  39.       /* show current url in title */
  40.       if(psw->uchVisibility && url != NULL && *url) {
  41.     strncpy(new_title, url, MAXNAMEL-9);
  42.     sprintf(psw->szSwtitle, "WGet [%s]", new_title);
  43.     WinChangeSwitchEntry(hswitch, psw);
  44.       }    
  45.       break;
  46.   }
  47. }
  48.  
  49. /* before termination, restore process title */
  50.  
  51. void os2break(arg)
  52. int arg;
  53. {
  54.   os2title(-1, NULL);
  55.   printf("\nTerminated by signal.\n");
  56.   exit(3);
  57. }
  58.  
  59. /* Encodes the '?' characters in a given string,
  60.    returning a malloc-ed %3F encoded string.     */
  61.  
  62. char *replace_question(s)
  63. const char *s;
  64. {
  65.   const char *b;
  66.   char *p, *res;
  67.   int i;
  68.   b = s;
  69.   for (i = 0; *s; s++, i++)
  70.     if (*s == '?' || *s == '\\')
  71.       i += 2;
  72.   res = (char *)xmalloc (i + 1);
  73.   s = b;
  74.   for (p = res; *s; s++)
  75.     if (*s == '?' || *s == '\\') {
  76.       sprintf(p,"%%%.2X", *s);
  77.       p += 3;
  78.     /*
  79.       *p++ = '%';
  80.       *p++ = '3';
  81.       *p++ = 'F';
  82.     */
  83.     } else
  84.       *p++ = *s;
  85.   *p = '\0';
  86.   return res;
  87. }
  88.  
  89. /* replace '%' character with other ('_'). for those who uses 4OS2,
  90.    where '%' is the prefix to environment variables */
  91.  
  92. void replace_percent(s)
  93. char *s;
  94. {
  95.   char *p;
  96.   for (p = s; *p; p++)
  97.     if (*p == '%')
  98.       *p = '_';
  99. }
  100.  
  101. /* make '--background' working under os/2
  102.    spawning new wget process detached.     */
  103.  
  104. void spawn_process(argc, argv)
  105. int argc;
  106. char *const *argv;
  107. {
  108.   int i;
  109.   char **newargs;
  110.   pid_t pid = 0;
  111.   HSWITCH hswitch;
  112.   SWCNTRL sw;
  113.   PSWCNTRL psw = (PSWCNTRL) &sw;
  114.  
  115.   /* Whether we arrange our own version of opt.lfilename here.  */
  116.   int changedp = 0;
  117.  
  118.   if (!opt.lfilename) {
  119.     opt.lfilename = unique_name (DEFAULT_LOGFILE);
  120.     changedp = 1;
  121.   }
  122.  
  123.   /* test where wget process is detached.
  124.      if it is not, new detached process will be spawned, continue otherwise.
  125.      so you can still use 'detach' command
  126.      and there will be no new processes */
  127.  
  128.   hswitch = WinQuerySwitchHandle(0, getpid());
  129.   WinQuerySwitchEntry(hswitch, psw);
  130.   if(psw->uchVisibility) {
  131.     /* parent here */
  132.     newargs = (char **) malloc(sizeof(char *) * (argc + 1));
  133.     for(i = 0; i <= argc; i++)
  134.         newargs[i] = (i == argc) ? NULL : strdup(argv[i]);
  135.     pid = spawnv(P_NOWAIT | P_DETACH, argv[0], newargs);
  136.     for(i = 0; newargs[i]; i++)
  137.       free(newargs[i]);
  138.     free(newargs);
  139.   }
  140.   if (pid == -1) {
  141.     /* parent, error */
  142.     perror ("spawn");
  143.     exit (1);
  144.   } else
  145.     if (pid != 0) {
  146.       /* parent, no error */
  147.       printf (_("Continuing in background. pid=%d\n"), pid);
  148.       if (changedp)
  149.     printf (_("Output will be written to `%s'.\n"), opt.lfilename);
  150.       exit (0);
  151.     }
  152.   /* child: keep running */
  153. }
  154.