/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/rdesktop/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 /sourceforge.net/branches/seamlessrdp-branch/rdesktop/rdesktop.c

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

revision 582 by n-ki, Fri Jan 23 14:37:51 2004 UTC revision 975 by astrand, Thu Aug 4 12:51:44 2005 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-2003     Copyright (C) Matthew Chapman 1999-2005
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 27  Line 27 
27  #include <sys/time.h>           /* gettimeofday */  #include <sys/time.h>           /* gettimeofday */
28  #include <sys/times.h>          /* times */  #include <sys/times.h>          /* times */
29  #include <ctype.h>              /* toupper */  #include <ctype.h>              /* toupper */
30    #include <limits.h>             /* PATH_MAX */
31  #include <errno.h>  #include <errno.h>
32  #include "rdesktop.h"  #include "rdesktop.h"
33    
34    #ifdef HAVE_LOCALE_H
35    #include <locale.h>
36    #endif
37    #ifdef HAVE_ICONV
38    #ifdef HAVE_LANGINFO_H
39    #include <langinfo.h>
40    #endif
41    #endif
42    
43  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
44    #include <sys/types.h>
45  #include <sys/socket.h>         /* socket connect */  #include <sys/socket.h>         /* socket connect */
46  #include <sys/un.h>             /* sockaddr_un */  #include <sys/un.h>             /* sockaddr_un */
47  #endif  #endif
48    
 #ifdef WITH_OPENSSL  
49  #include <openssl/md5.h>  #include <openssl/md5.h>
 #else  
 #include "crypto/md5.h"  
 #endif  
50    
51  char g_title[64] = "";  char g_title[64] = "";
52  char g_username[64];  char g_username[64];
53  char hostname[16];  char g_hostname[16];
54  char keymapname[16];  char g_keymapname[PATH_MAX] = "";
55  int keylayout = 0x409;          /* Defaults to US keyboard layout */  int g_keylayout = 0x409;        /* Defaults to US keyboard layout */
56    int g_keyboard_type = 0x4;      /* Defaults to US keyboard layout */
57    int g_keyboard_subtype = 0x0;   /* Defaults to US keyboard layout */
58    int g_keyboard_functionkeys = 0xc;      /* Defaults to US keyboard layout */
59    
60  int g_width = 800;              /* width is special: If 0, the  int g_width = 800;              /* width is special: If 0, the
61                                     geometry will be fetched from                                     geometry will be fetched from
# Line 53  int g_width = 800;             /* width is special: Line 63  int g_width = 800;             /* width is special:
63                                     absolute value specifies the                                     absolute value specifies the
64                                     percent of the whole screen. */                                     percent of the whole screen. */
65  int g_height = 600;  int g_height = 600;
66  int tcp_port_rdp = TCP_PORT_RDP;  int g_xpos = 0;
67    int g_ypos = 0;
68    int g_pos = 0;                  /* 0 position unspecified,
69                                       1 specified,
70                                       2 xpos neg,
71                                       4 ypos neg  */
72    extern int g_tcp_port_rdp;
73  int g_server_bpp = 8;  int g_server_bpp = 8;
74  int g_win_button_size = 0;      /* If zero, disable single app mode */  int g_win_button_size = 0;      /* If zero, disable single app mode */
75  BOOL g_bitmap_compression = True;  BOOL g_bitmap_compression = True;
76  BOOL g_sendmotion = True;  BOOL g_sendmotion = True;
77  BOOL g_orders = True;  BOOL g_bitmap_cache = True;
78    BOOL g_bitmap_cache_persist_enable = False;
79    BOOL g_bitmap_cache_precache = True;
80  BOOL g_encryption = True;  BOOL g_encryption = True;
81  BOOL packet_encryption = True;  BOOL packet_encryption = True;
82  BOOL g_desktop_save = True;  BOOL g_desktop_save = True;     /* desktop save order */
83    BOOL g_polygon_ellipse_orders = True;   /* polygon / ellipse orders */
84  BOOL g_fullscreen = False;  BOOL g_fullscreen = False;
85  BOOL g_grab_keyboard = True;  BOOL g_grab_keyboard = True;
86  BOOL g_hide_decorations = False;  BOOL g_hide_decorations = False;
87  BOOL g_use_rdp5 = True;  BOOL g_use_rdp5 = True;
88  BOOL g_console_session = False;  BOOL g_console_session = False;
89  BOOL g_numlock_sync = False;  BOOL g_numlock_sync = False;
90  extern BOOL g_owncolmap;  BOOL g_owncolmap = False;
91    BOOL g_ownbackstore = True;     /* We can't rely on external BackingStore */
92    uint32 g_embed_wnd;
93    uint32 g_rdp5_performanceflags =
94            RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS;
95    
96  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
97  BOOL g_rdpsnd = False;  BOOL g_rdpsnd = False;
98  #endif  #endif
99    
100    #ifdef HAVE_ICONV
101    char g_codepage[16] = "";
102    #endif
103    
104  extern RDPDR_DEVICE g_rdpdr_device[];  extern RDPDR_DEVICE g_rdpdr_device[];
105  extern uint32 g_num_devices;  extern uint32 g_num_devices;
106    extern char *g_rdpdr_clientname;
107    
108  #ifdef RDP2VNC  #ifdef RDP2VNC
109  extern int rfb_port;  extern int rfb_port;
# Line 89  static void Line 117  static void
117  usage(char *program)  usage(char *program)
118  {  {
119          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
120          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2005 Matt Chapman.\n");
121          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
122    
123          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
# Line 107  usage(char *program) Line 135  usage(char *program)
135          fprintf(stderr, "   -g: desktop geometry (WxH)\n");          fprintf(stderr, "   -g: desktop geometry (WxH)\n");
136          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
137          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
138    #ifdef HAVE_ICONV
139            fprintf(stderr, "   -L: local codepage\n");
140    #endif
141            fprintf(stderr, "   -B: use BackingStore of X-server (if available)\n");
142          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
143          fprintf(stderr, "   -E: disable encryption from client to server\n");          fprintf(stderr, "   -E: disable encryption from client to server\n");
144          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
# Line 116  usage(char *program) Line 148  usage(char *program)
148          fprintf(stderr, "   -S: caption button size (single application mode)\n");          fprintf(stderr, "   -S: caption button size (single application mode)\n");
149          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
150          fprintf(stderr, "   -N: enable numlock syncronization\n");          fprintf(stderr, "   -N: enable numlock syncronization\n");
151            fprintf(stderr, "   -X: embed into another window with a given id.\n");
152          fprintf(stderr, "   -a: connection colour depth\n");          fprintf(stderr, "   -a: connection colour depth\n");
153            fprintf(stderr, "   -z: enable rdp compression\n");
154            fprintf(stderr, "   -x: RDP5 experience (m[odem 28.8], b[roadband], l[an] or hex nr.)\n");
155            fprintf(stderr, "   -P: use persistent bitmap caching\n");
156          fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");          fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");
157          fprintf(stderr,          fprintf(stderr,
158                  "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");                  "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
159          fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");          fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
160          fprintf(stderr,          fprintf(stderr,
161                  "         '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n");                  "         '-r disk:floppy=/mnt/floppy': enable redirection of /mnt/floppy to 'floppy' share\n");
162          fprintf(stderr, "             or   A=/mnt/floppy,D=/mnt/cdrom'\n");          fprintf(stderr, "             or   'floppy=/mnt/floppy,cdrom=/mnt/cdrom'\n");
163            fprintf(stderr, "         '-r clientname=<client name>': Set the client name displayed\n");
164            fprintf(stderr, "             for redirected disks\n");
165          fprintf(stderr,          fprintf(stderr,
166                  "         '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");                  "         '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
167          fprintf(stderr, "             or      LPT1=/dev/lp0,LPT2=/dev/lp1\n");          fprintf(stderr, "             or      LPT1=/dev/lp0,LPT2=/dev/lp1\n");
168          fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");          fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");
169          fprintf(stderr,          fprintf(stderr,
170                  "             or       mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");                  "             or      mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
171          fprintf(stderr, "         '-r sound': enable sound redirection\n");          fprintf(stderr, "         '-r sound:[local|off|remote]': enable sound redirection\n");
172            fprintf(stderr, "                     remote would leave sound on server\n");
173          fprintf(stderr, "   -0: attach to console\n");          fprintf(stderr, "   -0: attach to console\n");
174          fprintf(stderr, "   -4: use RDP version 4\n");          fprintf(stderr, "   -4: use RDP version 4\n");
175          fprintf(stderr, "   -5: use RDP version 5 (default)\n");          fprintf(stderr, "   -5: use RDP version 5 (default)\n");
176  }  }
177    
178    static void
179    print_disconnect_reason(uint16 reason)
180    {
181            char *text;
182    
183            switch (reason)
184            {
185                    case exDiscReasonNoInfo:
186                            text = "No information available";
187                            break;
188    
189                    case exDiscReasonAPIInitiatedDisconnect:
190                            text = "Server initiated disconnect";
191                            break;
192    
193                    case exDiscReasonAPIInitiatedLogoff:
194                            text = "Server initiated logoff";
195                            break;
196    
197                    case exDiscReasonServerIdleTimeout:
198                            text = "Server idle timeout reached";
199                            break;
200    
201                    case exDiscReasonServerLogonTimeout:
202                            text = "Server logon timeout reached";
203                            break;
204    
205                    case exDiscReasonReplacedByOtherConnection:
206                            text = "The session was replaced";
207                            break;
208    
209                    case exDiscReasonOutOfMemory:
210                            text = "The server is out of memory";
211                            break;
212    
213                    case exDiscReasonServerDeniedConnection:
214                            text = "The server denied the connection";
215                            break;
216    
217                    case exDiscReasonServerDeniedConnectionFips:
218                            text = "The server denied the connection for security reason";
219                            break;
220    
221                    case exDiscReasonLicenseInternal:
222                            text = "Internal licensing error";
223                            break;
224    
225                    case exDiscReasonLicenseNoLicenseServer:
226                            text = "No license server available";
227                            break;
228    
229                    case exDiscReasonLicenseNoLicense:
230                            text = "No valid license available";
231                            break;
232    
233                    case exDiscReasonLicenseErrClientMsg:
234                            text = "Invalid licensing message";
235                            break;
236    
237                    case exDiscReasonLicenseHwidDoesntMatchLicense:
238                            text = "Hardware id doesn't match software license";
239                            break;
240    
241                    case exDiscReasonLicenseErrClientLicense:
242                            text = "Client license error";
243                            break;
244    
245                    case exDiscReasonLicenseCantFinishProtocol:
246                            text = "Network error during licensing protocol";
247                            break;
248    
249                    case exDiscReasonLicenseClientEndedProtocol:
250                            text = "Licensing protocol was not completed";
251                            break;
252    
253                    case exDiscReasonLicenseErrClientEncryption:
254                            text = "Incorrect client license enryption";
255                            break;
256    
257                    case exDiscReasonLicenseCantUpgradeLicense:
258                            text = "Can't upgrade license";
259                            break;
260    
261                    case exDiscReasonLicenseNoRemoteConnections:
262                            text = "The server is not licensed to accept remote connections";
263                            break;
264    
265                    default:
266                            if (reason > 0x1000 && reason < 0x7fff)
267                            {
268                                    text = "Internal protocol error";
269                            }
270                            else
271                            {
272                                    text = "Unknown reason";
273                            }
274            }
275            fprintf(stderr, "disconnect: %s.\n", text);
276    }
277    
278  static BOOL  static BOOL
279  read_password(char *password, int size)  read_password(char *password, int size)
280  {  {
# Line 193  parse_server_and_port(char *server) Line 332  parse_server_and_port(char *server)
332                  if (*server == '[' && p != NULL)                  if (*server == '[' && p != NULL)
333                  {                  {
334                          if (*(p + 1) == ':' && *(p + 2) != '\0')                          if (*(p + 1) == ':' && *(p + 2) != '\0')
335                                  tcp_port_rdp = strtol(p + 2, NULL, 10);                                  g_tcp_port_rdp = strtol(p + 2, NULL, 10);
336                          /* remove the port number and brackets from the address */                          /* remove the port number and brackets from the address */
337                          *p = '\0';                          *p = '\0';
338                          strncpy(server, server + 1, strlen(server));                          strncpy(server, server + 1, strlen(server));
# Line 205  parse_server_and_port(char *server) Line 344  parse_server_and_port(char *server)
344                  p = strchr(server, ':');                  p = strchr(server, ':');
345                  if (p != NULL)                  if (p != NULL)
346                  {                  {
347                          tcp_port_rdp = strtol(p + 1, NULL, 10);                          g_tcp_port_rdp = strtol(p + 1, NULL, 10);
348                          *p = 0;                          *p = 0;
349                  }                  }
350          }          }
# Line 213  parse_server_and_port(char *server) Line 352  parse_server_and_port(char *server)
352          p = strchr(server, ':');          p = strchr(server, ':');
353          if (p != NULL)          if (p != NULL)
354          {          {
355                  tcp_port_rdp = strtol(p + 1, NULL, 10);                  g_tcp_port_rdp = strtol(p + 1, NULL, 10);
356                  *p = 0;                  *p = 0;
357          }          }
358  #endif /* IPv6 */  #endif /* IPv6 */
# Line 230  main(int argc, char *argv[]) Line 369  main(int argc, char *argv[])
369          char password[64];          char password[64];
370          char shell[128];          char shell[128];
371          char directory[32];          char directory[32];
372          BOOL prompt_password, rdp_retval = False;          BOOL prompt_password, deactivated;
373          struct passwd *pw;          struct passwd *pw;
374          uint32 flags;          uint32 flags, ext_disc_reason = 0;
375          char *p;          char *p;
376          int c;          int c;
377            char *locale = NULL;
378          int username_option = 0;          int username_option = 0;
379    
380    #ifdef HAVE_LOCALE_H
381            /* Set locale according to environment */
382            locale = setlocale(LC_ALL, "");
383            if (locale)
384            {
385                    locale = xstrdup(locale);
386            }
387    
388    #endif
389          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
390          prompt_password = False;          prompt_password = False;
391          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
392          strcpy(keymapname, "en-us");          g_embed_wnd = 0;
393    
394          g_num_devices = 0;          g_num_devices = 0;
395    
# Line 251  main(int argc, char *argv[]) Line 399  main(int argc, char *argv[])
399  #define VNCOPT  #define VNCOPT
400  #endif  #endif
401    
402          while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbeEmCDKS:T:Na:r:045h?")) != -1)          while ((c = getopt(argc, argv,
403                               VNCOPT "u:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1)
404          {          {
405                  switch (c)                  switch (c)
406                  {                  {
# Line 274  main(int argc, char *argv[]) Line 423  main(int argc, char *argv[])
423                                  username_option = 1;                                  username_option = 1;
424                                  break;                                  break;
425    
426                            case 'L':
427    #ifdef HAVE_ICONV
428                                    STRNCPY(g_codepage, optarg, sizeof(g_codepage));
429    #else
430                                    error("iconv support not available\n");
431    #endif
432                                    break;
433    
434                          case 'd':                          case 'd':
435                                  STRNCPY(domain, optarg, sizeof(domain));                                  STRNCPY(domain, optarg, sizeof(domain));
436                                  break;                                  break;
# Line 303  main(int argc, char *argv[]) Line 460  main(int argc, char *argv[])
460                                  break;                                  break;
461    
462                          case 'n':                          case 'n':
463                                  STRNCPY(hostname, optarg, sizeof(hostname));                                  STRNCPY(g_hostname, optarg, sizeof(g_hostname));
464                                  break;                                  break;
465    
466                          case 'k':                          case 'k':
467                                  STRNCPY(keymapname, optarg, sizeof(keymapname));                                  STRNCPY(g_keymapname, optarg, sizeof(g_keymapname));
468                                  break;                                  break;
469    
470                          case 'g':                          case 'g':
# Line 326  main(int argc, char *argv[]) Line 483  main(int argc, char *argv[])
483                                  }                                  }
484    
485                                  if (*p == 'x')                                  if (*p == 'x')
486                                          g_height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, &p, 10);
487    
488                                  if (g_height <= 0)                                  if (g_height <= 0)
489                                  {                                  {
# Line 335  main(int argc, char *argv[]) Line 492  main(int argc, char *argv[])
492                                  }                                  }
493    
494                                  if (*p == '%')                                  if (*p == '%')
495                                    {
496                                          g_width = -g_width;                                          g_width = -g_width;
497                                            p++;
498                                    }
499    
500                                    if (*p == '+' || *p == '-')
501                                    {
502                                            g_pos |= (*p == '-') ? 2 : 1;
503                                            g_xpos = strtol(p, &p, 10);
504    
505                                    }
506                                    if (*p == '+' || *p == '-')
507                                    {
508                                            g_pos |= (*p == '-') ? 4 : 1;
509                                            g_ypos = strtol(p, NULL, 10);
510                                    }
511    
512                                  break;                                  break;
513    
# Line 344  main(int argc, char *argv[]) Line 516  main(int argc, char *argv[])
516                                  break;                                  break;
517    
518                          case 'b':                          case 'b':
519                                  g_orders = False;                                  g_bitmap_cache = False;
520                                    break;
521    
522                            case 'B':
523                                    g_ownbackstore = False;
524                                  break;                                  break;
525    
526                          case 'e':                          case 'e':
# Line 394  main(int argc, char *argv[]) Line 570  main(int argc, char *argv[])
570                                  g_numlock_sync = True;                                  g_numlock_sync = True;
571                                  break;                                  break;
572    
573                            case 'X':
574                                    g_embed_wnd = strtol(optarg, NULL, 0);
575                                    break;
576    
577                          case 'a':                          case 'a':
578                                  g_server_bpp = strtol(optarg, NULL, 10);                                  g_server_bpp = strtol(optarg, NULL, 10);
579                                  if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15                                  if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
# Line 404  main(int argc, char *argv[]) Line 584  main(int argc, char *argv[])
584                                  }                                  }
585                                  break;                                  break;
586    
587                            case 'z':
588                                    DEBUG(("rdp compression enabled\n"));
589                                    flags |= (RDP_LOGON_COMPRESSION | RDP_LOGON_COMPRESSION2);
590                                    break;
591    
592                            case 'x':
593                                    if (strncmp("modem", optarg, 1) == 0)
594                                    {
595                                            g_rdp5_performanceflags =
596                                                    RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG |
597                                                    RDP5_NO_MENUANIMATIONS | RDP5_NO_THEMING;
598                                    }
599                                    else if (strncmp("broadband", optarg, 1) == 0)
600                                    {
601                                            g_rdp5_performanceflags = RDP5_NO_WALLPAPER;
602                                    }
603                                    else if (strncmp("lan", optarg, 1) == 0)
604                                    {
605                                            g_rdp5_performanceflags = RDP5_DISABLE_NOTHING;
606                                    }
607                                    else
608                                    {
609                                            g_rdp5_performanceflags = strtol(optarg, NULL, 16);
610                                    }
611                                    break;
612    
613                            case 'P':
614                                    g_bitmap_cache_persist_enable = True;
615                                    break;
616    
617                          case 'r':                          case 'r':
618    
619                                  if (strncmp("sound", optarg, 5) == 0)                                  if (strncmp("sound", optarg, 5) == 0)
620                                  {                                  {
621                                            optarg += 5;
622    
623                                            if (*optarg == ':')
624                                            {
625                                                    *optarg++;
626                                                    while ((p = next_arg(optarg, ',')))
627                                                    {
628                                                            if (strncmp("remote", optarg, 6) == 0)
629                                                                    flags |= RDP_LOGON_LEAVE_AUDIO;
630    
631                                                            if (strncmp("local", optarg, 5) == 0)
632    #ifdef WITH_RDPSND
633                                                                    g_rdpsnd = True;
634    #else
635                                                                    warning("Not compiled with sound support\n");
636    #endif
637    
638                                                            if (strncmp("off", optarg, 3) == 0)
639  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
640                                          g_rdpsnd = True;                                                                  g_rdpsnd = False;
641  #else  #else
642                                          warning("Not compiled with sound support");                                                                  warning("Not compiled with sound support\n");
643  #endif  #endif
644    
645                                                            optarg = p;
646                                                    }
647                                            }
648                                            else
649                                            {
650    #ifdef WITH_RDPSND
651                                                    g_rdpsnd = True;
652    #else
653                                                    warning("Not compiled with sound support\n");
654    #endif
655                                            }
656                                  }                                  }
657                                  else if (strncmp("disk", optarg, 4) == 0)                                  else if (strncmp("disk", optarg, 4) == 0)
658                                  {                                  {
# Line 431  main(int argc, char *argv[]) Line 671  main(int argc, char *argv[])
671                                  {                                  {
672                                          printer_enum_devices(&g_num_devices, optarg + 7);                                          printer_enum_devices(&g_num_devices, optarg + 7);
673                                  }                                  }
674                                    else if (strncmp("clientname", optarg, 7) == 0)
675                                    {
676                                            g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1);
677                                            strcpy(g_rdpdr_clientname, optarg + 11);
678                                    }
679                                  else                                  else
680                                  {                                  {
681                                          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\n");
# Line 457  main(int argc, char *argv[]) Line 702  main(int argc, char *argv[])
702                  }                  }
703          }          }
704    
705          if (argc - optind < 1)          if (argc - optind != 1)
706          {          {
707                  usage(argv[0]);                  usage(argv[0]);
708                  return 1;                  return 1;
# Line 478  main(int argc, char *argv[]) Line 723  main(int argc, char *argv[])
723                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
724          }          }
725    
726          if (hostname[0] == 0)  #ifdef HAVE_ICONV
727            if (g_codepage[0] == 0)
728            {
729                    if (setlocale(LC_CTYPE, ""))
730                    {
731                            STRNCPY(g_codepage, nl_langinfo(CODESET), sizeof(g_codepage));
732                    }
733                    else
734                    {
735                            STRNCPY(g_codepage, DEFAULT_CODEPAGE, sizeof(g_codepage));
736                    }
737            }
738    #endif
739    
740            if (g_hostname[0] == 0)
741          {          {
742                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
743                  {                  {
# Line 490  main(int argc, char *argv[]) Line 749  main(int argc, char *argv[])
749                  if (p != NULL)                  if (p != NULL)
750                          *p = 0;                          *p = 0;
751    
752                  STRNCPY(hostname, fullhostname, sizeof(hostname));                  STRNCPY(g_hostname, fullhostname, sizeof(g_hostname));
753          }          }
754    
755            if (g_keymapname[0] == 0)
756            {
757                    if (locale && xkeymap_from_locale(locale))
758                    {
759                            fprintf(stderr, "Autoselected keyboard map %s\n", g_keymapname);
760                    }
761                    else
762                    {
763                            STRNCPY(g_keymapname, "en-us", sizeof(g_keymapname));
764                    }
765            }
766            if (locale)
767                    xfree(locale);
768    
769    
770          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
771                  flags |= RDP_LOGON_AUTO;                  flags |= RDP_LOGON_AUTO;
772    
# Line 530  main(int argc, char *argv[]) Line 804  main(int argc, char *argv[])
804    
805          if (ui_create_window())          if (ui_create_window())
806          {          {
807                  rdp_retval = rdp_main_loop();                  rdp_main_loop(&deactivated, &ext_disc_reason);
808                  ui_destroy_window();                  ui_destroy_window();
809          }          }
810    
811          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
812          rdp_disconnect();          rdp_disconnect();
813            cache_save_state();
814          ui_deinit();          ui_deinit();
815    
816          if (True == rdp_retval)          if (ext_disc_reason >= 2)
817                    print_disconnect_reason(ext_disc_reason);
818    
819            if (deactivated)
820            {
821                    /* clean disconnect */
822                  return 0;                  return 0;
823            }
824          else          else
825                  return 2;          {
826                    if (ext_disc_reason == exDiscReasonAPIInitiatedDisconnect
827                        || ext_disc_reason == exDiscReasonAPIInitiatedLogoff)
828                    {
829                            /* not so clean disconnect, but nothing to worry about */
830                            return 0;
831                    }
832                    else
833                    {
834                            /* return error */
835                            return 2;
836                    }
837            }
838    
839  #endif  #endif
840    
# Line 643  xmalloc(int size) Line 936  xmalloc(int size)
936          return mem;          return mem;
937  }  }
938    
939    /* strdup */
940    char *
941    xstrdup(const char *s)
942    {
943            char *mem = strdup(s);
944            if (mem == NULL)
945            {
946                    perror("strdup");
947                    exit(1);
948            }
949            return mem;
950    }
951    
952  /* realloc; exit if out of memory */  /* realloc; exit if out of memory */
953  void *  void *
954  xrealloc(void *oldmem, int size)  xrealloc(void *oldmem, int size)
955  {  {
956          void *mem = realloc(oldmem, size);          void *mem;
957    
958            if (size < 1)
959                    size = 1;
960            mem = realloc(oldmem, size);
961          if (mem == NULL)          if (mem == NULL)
962          {          {
963                  error("xrealloc %d\n", size);                  error("xrealloc %d\n", size);
# Line 732  hexdump(unsigned char *p, unsigned int l Line 1042  hexdump(unsigned char *p, unsigned int l
1042  }  }
1043    
1044  /*  /*
1045    input: src is the string we look in for needle    input: src is the string we look in for needle.
1046             Needle may be escaped by a backslash, in
1047             that case we ignore that particular needle.
1048    return value: returns next src pointer, for    return value: returns next src pointer, for
1049          succesive executions, like in a while loop          succesive executions, like in a while loop
1050          if retval is 0, then there are no more args.          if retval is 0, then there are no more args.
# Line 814  toupper_str(char *p) Line 1126  toupper_str(char *p)
1126  #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)  #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
1127    
1128  char *  char *
1129  ltoa(long N, int base)  l_to_a(long N, int base)
1130  {  {
1131          static char ret[LTOA_BUFSIZE];          static char ret[LTOA_BUFSIZE];
1132    
1133          register int i = 2;          char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
         long uarg;  
         char *tail, *head = ret, buf[LTOA_BUFSIZE];  
1134    
1135          if (36 < base || 2 > base)          register int divrem;
                 base = 10;  
1136    
1137          tail = &buf[LTOA_BUFSIZE - 1];          if (base < 36 || 2 > base)
1138          *tail-- = '\0';                  base = 10;
1139    
1140          if (10 == base && N < 0L)          if (N < 0)
1141          {          {
1142                  *head++ = '-';                  *head++ = '-';
1143                  uarg = -N;                  N = -N;
1144          }          }
         else  
                 uarg = N;  
1145    
1146          if (uarg)          tail = buf + sizeof(buf);
1147          {          *--tail = 0;
                 for (i = 1; uarg; ++i)  
                 {  
                         register ldiv_t r;  
1148    
1149                          r = ldiv(uarg, base);          do
1150                          *tail-- = (char) (r.rem + ((9L < r.rem) ? ('A' - 10L) : '0'));          {
1151                          uarg = r.quot;                  divrem = N % base;
1152                  }                  *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
1153                    N /= base;
1154          }          }
1155          else          while (N);
                 *tail-- = '0';  
1156    
1157          memcpy(head, ++tail, i);          strcpy(head, tail);
1158          return ret;          return ret;
1159  }  }
1160    
# Line 866  load_licence(unsigned char **data) Line 1170  load_licence(unsigned char **data)
1170          if (home == NULL)          if (home == NULL)
1171                  return -1;                  return -1;
1172    
1173          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));          path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1174          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1175    
1176          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
1177          if (fd == -1)          if (fd == -1)
# Line 893  save_licence(unsigned char *data, int le Line 1197  save_licence(unsigned char *data, int le
1197          if (home == NULL)          if (home == NULL)
1198                  return;                  return;
1199    
1200          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));          path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1201    
1202          sprintf(path, "%s/.rdesktop", home);          sprintf(path, "%s/.rdesktop", home);
1203          if ((mkdir(path, 0700) == -1) && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
# Line 904  save_licence(unsigned char *data, int le Line 1208  save_licence(unsigned char *data, int le
1208    
1209          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
1210    
1211          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1212          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
1213          strcpy(tmppath, path);          strcpy(tmppath, path);
1214          strcat(tmppath, ".new");          strcat(tmppath, ".new");
# Line 931  save_licence(unsigned char *data, int le Line 1235  save_licence(unsigned char *data, int le
1235          xfree(tmppath);          xfree(tmppath);
1236          xfree(path);          xfree(path);
1237  }  }
1238    
1239    /* Create the bitmap cache directory */
1240    BOOL
1241    rd_pstcache_mkdir(void)
1242    {
1243            char *home;
1244            char bmpcache_dir[256];
1245    
1246            home = getenv("HOME");
1247    
1248            if (home == NULL)
1249                    return False;
1250    
1251            sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop");
1252    
1253            if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1254            {
1255                    perror(bmpcache_dir);
1256                    return False;
1257            }
1258    
1259            sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache");
1260    
1261            if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1262            {
1263                    perror(bmpcache_dir);
1264                    return False;
1265            }
1266    
1267            return True;
1268    }
1269    
1270    /* open a file in the .rdesktop directory */
1271    int
1272    rd_open_file(char *filename)
1273    {
1274            char *home;
1275            char fn[256];
1276            int fd;
1277    
1278            home = getenv("HOME");
1279            if (home == NULL)
1280                    return -1;
1281            sprintf(fn, "%s/.rdesktop/%s", home, filename);
1282            fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1283            if (fd == -1)
1284                    perror(fn);
1285            return fd;
1286    }
1287    
1288    /* close file */
1289    void
1290    rd_close_file(int fd)
1291    {
1292            close(fd);
1293    }
1294    
1295    /* read from file*/
1296    int
1297    rd_read_file(int fd, void *ptr, int len)
1298    {
1299            return read(fd, ptr, len);
1300    }
1301    
1302    /* write to file */
1303    int
1304    rd_write_file(int fd, void *ptr, int len)
1305    {
1306            return write(fd, ptr, len);
1307    }
1308    
1309    /* move file pointer */
1310    int
1311    rd_lseek_file(int fd, int offset)
1312    {
1313            return lseek(fd, offset, SEEK_SET);
1314    }
1315    
1316    /* do a write lock on a file */
1317    BOOL
1318    rd_lock_file(int fd, int start, int len)
1319    {
1320            struct flock lock;
1321    
1322            lock.l_type = F_WRLCK;
1323            lock.l_whence = SEEK_SET;
1324            lock.l_start = start;
1325            lock.l_len = len;
1326            if (fcntl(fd, F_SETLK, &lock) == -1)
1327                    return False;
1328            return True;
1329    }

Legend:
Removed from v.582  
changed lines
  Added in v.975

  ViewVC Help
Powered by ViewVC 1.1.26