/[rdesktop]/jpeg/rdesktop/trunk/rdesktop.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /jpeg/rdesktop/trunk/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1010 by astrand, Wed Aug 31 13:00:57 2005 UTC revision 1417 by jsorg71, Thu Aug 30 04:47:36 2007 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions     Entrypoint and utility functions
4     Copyright (C) Matthew Chapman 1999-2005     Copyright (C) Matthew Chapman 1999-2007
5    
6     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 45  Line 45 
45  #include <sys/un.h>             /* sockaddr_un */  #include <sys/un.h>             /* sockaddr_un */
46  #endif  #endif
47    
48  #include <openssl/md5.h>  #include "ssl.h"
49    
50  char g_title[64] = "";  char g_title[64] = "";
51  char g_username[64];  char g_username[64];
52  char g_hostname[16];  char g_hostname[16];
53  char g_keymapname[PATH_MAX] = "";  char g_keymapname[PATH_MAX] = "";
54  int g_keylayout = 0x409;        /* Defaults to US keyboard layout */  unsigned int g_keylayout = 0x409;       /* Defaults to US keyboard layout */
55  int g_keyboard_type = 0x4;      /* Defaults to US keyboard layout */  int g_keyboard_type = 0x4;      /* Defaults to US keyboard layout */
56  int g_keyboard_subtype = 0x0;   /* Defaults to US keyboard layout */  int g_keyboard_subtype = 0x0;   /* Defaults to US keyboard layout */
57  int g_keyboard_functionkeys = 0xc;      /* Defaults to US keyboard layout */  int g_keyboard_functionkeys = 0xc;      /* Defaults to US keyboard layout */
# Line 69  int g_pos = 0;                 /* 0 position unspecifi Line 69  int g_pos = 0;                 /* 0 position unspecifi
69                                     2 xpos neg,                                     2 xpos neg,
70                                     4 ypos neg  */                                     4 ypos neg  */
71  extern int g_tcp_port_rdp;  extern int g_tcp_port_rdp;
72  int g_server_bpp = 8;  int g_server_depth = -1;
73  int g_win_button_size = 0;      /* If zero, disable single app mode */  int g_win_button_size = 0;      /* If zero, disable single app mode */
74  BOOL g_bitmap_compression = True;  RD_BOOL g_bitmap_compression = True;
75  BOOL g_sendmotion = True;  RD_BOOL g_sendmotion = True;
76  BOOL g_bitmap_cache = True;  RD_BOOL g_bitmap_cache = True;
77  BOOL g_bitmap_cache_persist_enable = False;  RD_BOOL g_bitmap_cache_persist_enable = False;
78  BOOL g_bitmap_cache_precache = True;  RD_BOOL g_bitmap_cache_precache = True;
79  BOOL g_encryption = True;  RD_BOOL g_encryption = True;
80  BOOL packet_encryption = True;  RD_BOOL g_packet_encryption = True;
81  BOOL g_desktop_save = True;     /* desktop save order */  RD_BOOL g_desktop_save = True;  /* desktop save order */
82  BOOL g_polygon_ellipse_orders = True;   /* polygon / ellipse orders */  RD_BOOL g_polygon_ellipse_orders = True;        /* polygon / ellipse orders */
83  BOOL g_fullscreen = False;  RD_BOOL g_fullscreen = False;
84  BOOL g_grab_keyboard = True;  RD_BOOL g_grab_keyboard = True;
85  BOOL g_hide_decorations = False;  RD_BOOL g_hide_decorations = False;
86  BOOL g_use_rdp5 = True;  RD_BOOL g_use_rdp5 = True;
87  BOOL g_console_session = False;  RD_BOOL g_rdpclip = True;
88  BOOL g_numlock_sync = False;  RD_BOOL g_console_session = False;
89  BOOL g_owncolmap = False;  RD_BOOL g_numlock_sync = False;
90  BOOL g_ownbackstore = True;     /* We can't rely on external BackingStore */  RD_BOOL g_lspci_enabled = False;
91    RD_BOOL g_owncolmap = False;
92    RD_BOOL g_ownbackstore = True;  /* We can't rely on external BackingStore */
93    RD_BOOL g_seamless_rdp = False;
94  uint32 g_embed_wnd;  uint32 g_embed_wnd;
95  uint32 g_rdp5_performanceflags =  uint32 g_rdp5_performanceflags =
96          RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;          RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;
97  /* Session Directory redirection */  /* Session Directory redirection */
98  BOOL g_redirect = False;  RD_BOOL g_redirect = False;
99  char g_redirect_server[64];  char g_redirect_server[64];
100  char g_redirect_domain[16];  char g_redirect_domain[16];
101  char g_redirect_password[64];  char g_redirect_password[64];
# Line 101  char g_redirect_cookie[128]; Line 104  char g_redirect_cookie[128];
104  uint32 g_redirect_flags = 0;  uint32 g_redirect_flags = 0;
105    
106  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
107  BOOL g_rdpsnd = False;  RD_BOOL g_rdpsnd = False;
108  #endif  #endif
109    
110  #ifdef HAVE_ICONV  #ifdef HAVE_ICONV
# Line 124  static void Line 127  static void
127  usage(char *program)  usage(char *program)
128  {  {
129          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
130          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2005 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2007 Matt Chapman.\n");
131          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
132    
133          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
# Line 145  usage(char *program) Line 148  usage(char *program)
148  #ifdef HAVE_ICONV  #ifdef HAVE_ICONV
149          fprintf(stderr, "   -L: local codepage\n");          fprintf(stderr, "   -L: local codepage\n");
150  #endif  #endif
151            fprintf(stderr, "   -A: enable SeamlessRDP mode\n");
152          fprintf(stderr, "   -B: use BackingStore of X-server (if available)\n");          fprintf(stderr, "   -B: use BackingStore of X-server (if available)\n");
153          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
154          fprintf(stderr, "   -E: disable encryption from client to server\n");          fprintf(stderr, "   -E: disable encryption from client to server\n");
# Line 175  usage(char *program) Line 179  usage(char *program)
179          fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");          fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");
180          fprintf(stderr,          fprintf(stderr,
181                  "             or      mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");                  "             or      mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
182          fprintf(stderr, "         '-r sound:[local|off|remote]': enable sound redirection\n");  #ifdef WITH_RDPSND
183            fprintf(stderr,
184                    "         '-r sound:[local[:driver[:device]]|off|remote]': enable sound redirection\n");
185          fprintf(stderr, "                     remote would leave sound on server\n");          fprintf(stderr, "                     remote would leave sound on server\n");
186            fprintf(stderr, "                     available drivers for 'local':\n");
187            rdpsnd_show_help();
188    #endif
189            fprintf(stderr,
190                    "         '-r clipboard:[off|PRIMARYCLIPBOARD|CLIPBOARD]': enable clipboard\n");
191            fprintf(stderr, "                      redirection.\n");
192            fprintf(stderr,
193                    "                      'PRIMARYCLIPBOARD' looks at both PRIMARY and CLIPBOARD\n");
194            fprintf(stderr, "                      when sending data to server.\n");
195            fprintf(stderr, "                      'CLIPBOARD' looks at only CLIPBOARD.\n");
196    #ifdef WITH_SCARD
197            fprintf(stderr, "         '-r scard[:\"Scard Name\"=\"Alias Name[;Vendor Name]\"[,...]]\n");
198            fprintf(stderr, "          example: -r scard:\"eToken PRO 00 00\"=\"AKS ifdh 0\"\n");
199            fprintf(stderr,
200                    "                   \"eToken PRO 00 00\" -> Device in Linux/Unix enviroment\n");
201            fprintf(stderr,
202                    "                   \"AKS ifdh 0\"       -> Device shown in Windows enviroment \n");
203            fprintf(stderr, "          example: -r scard:\"eToken PRO 00 00\"=\"AKS ifdh 0;AKS\"\n");
204            fprintf(stderr,
205                    "                   \"eToken PRO 00 00\" -> Device in Linux/Unix enviroment\n");
206            fprintf(stderr,
207                    "                   \"AKS ifdh 0\"       -> Device shown in Windows enviroment \n");
208            fprintf(stderr,
209                    "                   \"AKS\"              -> Device vendor name                 \n");
210    #endif
211          fprintf(stderr, "   -0: attach to console\n");          fprintf(stderr, "   -0: attach to console\n");
212          fprintf(stderr, "   -4: use RDP version 4\n");          fprintf(stderr, "   -4: use RDP version 4\n");
213          fprintf(stderr, "   -5: use RDP version 5 (default)\n");          fprintf(stderr, "   -5: use RDP version 5 (default)\n");
# Line 288  rdesktop_reset_state(void) Line 319  rdesktop_reset_state(void)
319          rdp_reset_state();          rdp_reset_state();
320  }  }
321    
322  static BOOL  static RD_BOOL
323  read_password(char *password, int size)  read_password(char *password, int size)
324  {  {
325          struct termios tios;          struct termios tios;
326          BOOL ret = False;          RD_BOOL ret = False;
327          int istty = 0;          int istty = 0;
328          char *p;          char *p;
329    
# Line 380  main(int argc, char *argv[]) Line 411  main(int argc, char *argv[])
411          char fullhostname[64];          char fullhostname[64];
412          char domain[16];          char domain[16];
413          char password[64];          char password[64];
414          char shell[128];          char shell[256];
415          char directory[32];          char directory[256];
416          BOOL prompt_password, deactivated;          RD_BOOL prompt_password, deactivated;
417          struct passwd *pw;          struct passwd *pw;
418          uint32 flags, ext_disc_reason = 0;          uint32 flags, ext_disc_reason = 0;
419          char *p;          char *p;
420          int c;          int c;
421          char *locale = NULL;          char *locale = NULL;
422          int username_option = 0;          int username_option = 0;
423            RD_BOOL geometry_option = False;
424          int run_count = 0;      /* Session Directory support */          int run_count = 0;      /* Session Directory support */
425          BOOL continue_connect = True;   /* Session Directory support */          RD_BOOL continue_connect = True;        /* Session Directory support */
426    #ifdef WITH_RDPSND
427            char *rdpsnd_optarg = NULL;
428    #endif
429    
430  #ifdef HAVE_LOCALE_H  #ifdef HAVE_LOCALE_H
431          /* Set locale according to environment */          /* Set locale according to environment */
# Line 415  main(int argc, char *argv[]) Line 450  main(int argc, char *argv[])
450  #endif  #endif
451    
452          while ((c = getopt(argc, argv,          while ((c = getopt(argc, argv,
453                             VNCOPT "u:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1)                             VNCOPT "Au:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1)
454          {          {
455                  switch (c)                  switch (c)
456                  {                  {
# Line 433  main(int argc, char *argv[]) Line 468  main(int argc, char *argv[])
468                                  break;                                  break;
469  #endif  #endif
470    
471                            case 'A':
472                                    g_seamless_rdp = True;
473                                    break;
474    
475                          case 'u':                          case 'u':
476                                  STRNCPY(g_username, optarg, sizeof(g_username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
477                                  username_option = 1;                                  username_option = 1;
# Line 483  main(int argc, char *argv[]) Line 522  main(int argc, char *argv[])
522                                  break;                                  break;
523    
524                          case 'g':                          case 'g':
525                                    geometry_option = True;
526                                  g_fullscreen = False;                                  g_fullscreen = False;
527                                  if (!strcmp(optarg, "workarea"))                                  if (!strcmp(optarg, "workarea"))
528                                  {                                  {
# Line 542  main(int argc, char *argv[]) Line 582  main(int argc, char *argv[])
582                                  g_encryption = False;                                  g_encryption = False;
583                                  break;                                  break;
584                          case 'E':                          case 'E':
585                                  packet_encryption = False;                                  g_packet_encryption = False;
586                                  break;                                  break;
587                          case 'm':                          case 'm':
588                                  g_sendmotion = False;                                  g_sendmotion = False;
# Line 590  main(int argc, char *argv[]) Line 630  main(int argc, char *argv[])
630                                  break;                                  break;
631    
632                          case 'a':                          case 'a':
633                                  g_server_bpp = strtol(optarg, NULL, 10);                                  g_server_depth = strtol(optarg, NULL, 10);
634                                  if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15                                  if (g_server_depth != 8 &&
635                                      && g_server_bpp != 24)                                      g_server_depth != 16 &&
636                                        g_server_depth != 15 && g_server_depth != 24
637                                        && g_server_depth != 32)
638                                  {                                  {
639                                          error("invalid server bpp\n");                                          error("Invalid server colour depth.\n");
640                                          return 1;                                          return 1;
641                                  }                                  }
642                                  break;                                  break;
# Line 637  main(int argc, char *argv[]) Line 679  main(int argc, char *argv[])
679    
680                                          if (*optarg == ':')                                          if (*optarg == ':')
681                                          {                                          {
682                                                  *optarg++;                                                  optarg++;
683                                                  while ((p = next_arg(optarg, ',')))                                                  while ((p = next_arg(optarg, ',')))
684                                                  {                                                  {
685                                                          if (str_startswith(optarg, "remote"))                                                          if (str_startswith(optarg, "remote"))
# Line 645  main(int argc, char *argv[]) Line 687  main(int argc, char *argv[])
687    
688                                                          if (str_startswith(optarg, "local"))                                                          if (str_startswith(optarg, "local"))
689  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
690                                                            {
691                                                                    rdpsnd_optarg =
692                                                                            next_arg(optarg, ':');
693                                                                  g_rdpsnd = True;                                                                  g_rdpsnd = True;
694                                                            }
695    
696  #else  #else
697                                                                  warning("Not compiled with sound support\n");                                                                  warning("Not compiled with sound support\n");
698  #endif  #endif
# Line 678  main(int argc, char *argv[]) Line 725  main(int argc, char *argv[])
725                                  {                                  {
726                                          serial_enum_devices(&g_num_devices, optarg + 7);                                          serial_enum_devices(&g_num_devices, optarg + 7);
727                                  }                                  }
728                                    else if (str_startswith(optarg, "lspci"))
729                                    {
730                                            g_lspci_enabled = True;
731                                    }
732                                  else if (str_startswith(optarg, "lptport"))                                  else if (str_startswith(optarg, "lptport"))
733                                  {                                  {
734                                          parallel_enum_devices(&g_num_devices, optarg + 7);                                          parallel_enum_devices(&g_num_devices, optarg + 7);
# Line 691  main(int argc, char *argv[]) Line 742  main(int argc, char *argv[])
742                                          g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1);                                          g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1);
743                                          strcpy(g_rdpdr_clientname, optarg + 11);                                          strcpy(g_rdpdr_clientname, optarg + 11);
744                                  }                                  }
745                                    else if (str_startswith(optarg, "clipboard"))
746                                    {
747                                            optarg += 9;
748    
749                                            if (*optarg == ':')
750                                            {
751                                                    optarg++;
752    
753                                                    if (str_startswith(optarg, "off"))
754                                                            g_rdpclip = False;
755                                                    else
756                                                            cliprdr_set_mode(optarg);
757                                            }
758                                            else
759                                                    g_rdpclip = True;
760                                    }
761                                    else if (strncmp("scard", optarg, 5) == 0)
762                                    {
763    #ifdef WITH_SCARD
764                                            scard_enum_devices(&g_num_devices, optarg + 5);
765    #else
766                                            warning("Not compiled with smartcard support\n");
767    #endif
768                                    }
769                                  else                                  else
770                                  {                                  {
771                                          warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");                                          warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound, clipboard, scard\n");
772                                  }                                  }
773                                  break;                                  break;
774    
# Line 726  main(int argc, char *argv[]) Line 801  main(int argc, char *argv[])
801          STRNCPY(server, argv[optind], sizeof(server));          STRNCPY(server, argv[optind], sizeof(server));
802          parse_server_and_port(server);          parse_server_and_port(server);
803    
804            if (g_seamless_rdp)
805            {
806                    if (g_win_button_size)
807                    {
808                            error("You cannot use -S and -A at the same time\n");
809                            return 1;
810                    }
811                    g_rdp5_performanceflags &= ~RDP5_NO_FULLWINDOWDRAG;
812                    if (geometry_option)
813                    {
814                            error("You cannot use -g and -A at the same time\n");
815                            return 1;
816                    }
817                    if (g_fullscreen)
818                    {
819                            error("You cannot use -f and -A at the same time\n");
820                            return 1;
821                    }
822                    if (g_hide_decorations)
823                    {
824                            error("You cannot use -D and -A at the same time\n");
825                            return 1;
826                    }
827                    if (g_embed_wnd)
828                    {
829                            error("You cannot use -X and -A at the same time\n");
830                            return 1;
831                    }
832                    if (!g_use_rdp5)
833                    {
834                            error("You cannot use -4 and -A at the same time\n");
835                            return 1;
836                    }
837                    g_width = -100;
838                    g_grab_keyboard = False;
839            }
840    
841          if (!username_option)          if (!username_option)
842          {          {
843                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
# Line 801  main(int argc, char *argv[]) Line 913  main(int argc, char *argv[])
913    
914  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
915          if (g_rdpsnd)          if (g_rdpsnd)
916                  rdpsnd_init();          {
917                    if (!rdpsnd_init(rdpsnd_optarg))
918                    {
919                            warning("Initializing sound-support failed!\n");
920                    }
921            }
922  #endif  #endif
923    
924            if (g_lspci_enabled)
925                    lspci_init();
926    
927          rdpdr_init();          rdpdr_init();
928    
929          while (run_count < 2 && continue_connect)       /* add support for Session Directory; only reconnect once */          while (run_count < 2 && continue_connect)       /* add support for Session Directory; only reconnect once */
# Line 818  main(int argc, char *argv[]) Line 939  main(int argc, char *argv[])
939    
940                  /* By setting encryption to False here, we have an encrypted login                  /* By setting encryption to False here, we have an encrypted login
941                     packet but unencrypted transfer of other packets */                     packet but unencrypted transfer of other packets */
942                  if (!packet_encryption)                  if (!g_packet_encryption)
943                          g_encryption = False;                          g_encryption = False;
944    
945    
# Line 890  main(int argc, char *argv[]) Line 1011  main(int argc, char *argv[])
1011    
1012  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
1013  /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */  /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
1014  static BOOL  static RD_BOOL
1015  generate_random_egd(uint8 * buf)  generate_random_egd(uint8 * buf)
1016  {  {
1017          struct sockaddr_un addr;          struct sockaddr_un addr;
1018          BOOL ret = False;          RD_BOOL ret = False;
1019          int fd;          int fd;
1020    
1021          fd = socket(AF_UNIX, SOCK_STREAM, 0);          fd = socket(AF_UNIX, SOCK_STREAM, 0);
# Line 932  generate_random(uint8 * random) Line 1053  generate_random(uint8 * random)
1053  {  {
1054          struct stat st;          struct stat st;
1055          struct tms tmsbuf;          struct tms tmsbuf;
1056          MD5_CTX md5;          SSL_MD5 md5;
1057          uint32 *r;          uint32 *r;
1058          int fd, n;          int fd, n;
1059    
# Line 964  generate_random(uint8 * random) Line 1085  generate_random(uint8 * random)
1085          r[7] = st.st_ctime;          r[7] = st.st_ctime;
1086    
1087          /* Hash both halves with MD5 to obscure possible patterns */          /* Hash both halves with MD5 to obscure possible patterns */
1088          MD5_Init(&md5);          ssl_md5_init(&md5);
1089          MD5_Update(&md5, random, 16);          ssl_md5_update(&md5, random, 16);
1090          MD5_Final(random, &md5);          ssl_md5_final(&md5, random);
1091          MD5_Update(&md5, random + 16, 16);          ssl_md5_update(&md5, random + 16, 16);
1092          MD5_Final(random + 16, &md5);          ssl_md5_final(&md5, random + 16);
1093  }  }
1094    
1095  /* malloc; exit if out of memory */  /* malloc; exit if out of memory */
# Line 984  xmalloc(int size) Line 1105  xmalloc(int size)
1105          return mem;          return mem;
1106  }  }
1107    
1108    /* Exit on NULL pointer. Use to verify result from XGetImage etc */
1109    void
1110    exit_if_null(void *ptr)
1111    {
1112            if (ptr == NULL)
1113            {
1114                    error("unexpected null pointer. Out of memory?\n");
1115                    exit(1);
1116            }
1117    }
1118    
1119  /* strdup */  /* strdup */
1120  char *  char *
1121  xstrdup(const char *s)  xstrdup(const char *s)
# Line 1132  next_arg(char *src, char needle) Line 1264  next_arg(char *src, char needle)
1264                          while (*(mvp + 1) != (char) 0x00)                          while (*(mvp + 1) != (char) 0x00)
1265                          {                          {
1266                                  *mvp = *(mvp + 1);                                  *mvp = *(mvp + 1);
1267                                  *mvp++;                                  mvp++;
1268                          }                          }
1269                          *mvp = (char) 0x00;                          *mvp = (char) 0x00;
1270                          p = nextval;                          p = nextval;
# Line 1170  toupper_str(char *p) Line 1302  toupper_str(char *p)
1302  }  }
1303    
1304    
1305  BOOL  RD_BOOL
1306  str_startswith(const char *s, const char *prefix)  str_startswith(const char *s, const char *prefix)
1307  {  {
1308          return (strncmp(s, prefix, strlen(prefix)) == 0);          return (strncmp(s, prefix, strlen(prefix)) == 0);
1309  }  }
1310    
1311    
1312    /* Split input into lines, and call linehandler for each
1313       line. Incomplete lines are saved in the rest variable, which should
1314       initially point to NULL. When linehandler returns False, stop and
1315       return False. Otherwise, return True.  */
1316    RD_BOOL
1317    str_handle_lines(const char *input, char **rest, str_handle_lines_t linehandler, void *data)
1318    {
1319            char *buf, *p;
1320            char *oldrest;
1321            size_t inputlen;
1322            size_t buflen;
1323            size_t restlen = 0;
1324            RD_BOOL ret = True;
1325    
1326            /* Copy data to buffer */
1327            inputlen = strlen(input);
1328            if (*rest)
1329                    restlen = strlen(*rest);
1330            buflen = restlen + inputlen + 1;
1331            buf = (char *) xmalloc(buflen);
1332            buf[0] = '\0';
1333            if (*rest)
1334                    STRNCPY(buf, *rest, buflen);
1335            strncat(buf, input, inputlen);
1336            p = buf;
1337    
1338            while (1)
1339            {
1340                    char *newline = strchr(p, '\n');
1341                    if (newline)
1342                    {
1343                            *newline = '\0';
1344                            if (!linehandler(p, data))
1345                            {
1346                                    p = newline + 1;
1347                                    ret = False;
1348                                    break;
1349                            }
1350                            p = newline + 1;
1351                    }
1352                    else
1353                    {
1354                            break;
1355    
1356                    }
1357            }
1358    
1359            /* Save in rest */
1360            oldrest = *rest;
1361            restlen = buf + buflen - p;
1362            *rest = (char *) xmalloc(restlen);
1363            STRNCPY((*rest), p, restlen);
1364            xfree(oldrest);
1365    
1366            xfree(buf);
1367            return ret;
1368    }
1369    
1370    /* Execute the program specified by argv. For each line in
1371       stdout/stderr output, call linehandler. Returns false on failure. */
1372    RD_BOOL
1373    subprocess(char *const argv[], str_handle_lines_t linehandler, void *data)
1374    {
1375            pid_t child;
1376            int fd[2];
1377            int n = 1;
1378            char output[256];
1379            char *rest = NULL;
1380    
1381            if (pipe(fd) < 0)
1382            {
1383                    perror("pipe");
1384                    return False;
1385            }
1386    
1387            if ((child = fork()) < 0)
1388            {
1389                    perror("fork");
1390                    return False;
1391            }
1392    
1393            /* Child */
1394            if (child == 0)
1395            {
1396                    /* Close read end */
1397                    close(fd[0]);
1398    
1399                    /* Redirect stdout and stderr to pipe */
1400                    dup2(fd[1], 1);
1401                    dup2(fd[1], 2);
1402    
1403                    /* Execute */
1404                    execvp(argv[0], argv);
1405                    perror("Error executing child");
1406                    _exit(128);
1407            }
1408    
1409            /* Parent. Close write end. */
1410            close(fd[1]);
1411            while (n > 0)
1412            {
1413                    n = read(fd[0], output, 255);
1414                    output[n] = '\0';
1415                    str_handle_lines(output, &rest, linehandler, data);
1416            }
1417            xfree(rest);
1418    
1419            return True;
1420    }
1421    
1422    
1423  /* not all clibs got ltoa */  /* not all clibs got ltoa */
1424  #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)  #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
1425    
# Line 1292  save_licence(unsigned char *data, int le Line 1535  save_licence(unsigned char *data, int le
1535  }  }
1536    
1537  /* Create the bitmap cache directory */  /* Create the bitmap cache directory */
1538  BOOL  RD_BOOL
1539  rd_pstcache_mkdir(void)  rd_pstcache_mkdir(void)
1540  {  {
1541          char *home;          char *home;
# Line 1369  rd_lseek_file(int fd, int offset) Line 1612  rd_lseek_file(int fd, int offset)
1612  }  }
1613    
1614  /* do a write lock on a file */  /* do a write lock on a file */
1615  BOOL  RD_BOOL
1616  rd_lock_file(int fd, int start, int len)  rd_lock_file(int fd, int start, int len)
1617  {  {
1618          struct flock lock;          struct flock lock;

Legend:
Removed from v.1010  
changed lines
  Added in v.1417

  ViewVC Help
Powered by ViewVC 1.1.26