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

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

revision 279 by n-ki, Tue Nov 26 10:09:14 2002 UTC revision 636 by stargo, Sat Mar 13 12:08:18 2004 UTC
# Line 1  Line 1 
1  /*  /* -*- 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-2002     Copyright (C) Matthew Chapman 1999-2003
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 22  Line 22 
22  #include <unistd.h>             /* read close getuid getgid getpid getppid gethostname */  #include <unistd.h>             /* read close getuid getgid getpid getppid gethostname */
23  #include <fcntl.h>              /* open */  #include <fcntl.h>              /* open */
24  #include <pwd.h>                /* getpwuid */  #include <pwd.h>                /* getpwuid */
 #include <limits.h>             /* PATH_MAX */  
25  #include <termios.h>            /* tcgetattr tcsetattr */  #include <termios.h>            /* tcgetattr tcsetattr */
26  #include <sys/stat.h>           /* stat */  #include <sys/stat.h>           /* stat */
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 */
30    #include <errno.h>
31  #include "rdesktop.h"  #include "rdesktop.h"
32    
33  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 40  Line 41 
41  #include "crypto/md5.h"  #include "crypto/md5.h"
42  #endif  #endif
43    
44  char title[32] = "";  char g_title[64] = "";
45  char username[16];  char g_username[64];
46  char hostname[16];  char hostname[16];
47  char keymapname[16];  char keymapname[16];
48  int keylayout = 0x409;          /* Defaults to US keyboard layout */  int keylayout = 0x409;          /* Defaults to US keyboard layout */
49  int width = 800;                /* If width or height are reset to zero, the geometry will  
50                                     be fetched from _NET_WORKAREA */  int g_width = 800;              /* width is special: If 0, the
51  int height = 600;                                     geometry will be fetched from
52                                       _NET_WORKAREA. If negative,
53                                       absolute value specifies the
54                                       percent of the whole screen. */
55    int g_height = 600;
56  int tcp_port_rdp = TCP_PORT_RDP;  int tcp_port_rdp = TCP_PORT_RDP;
57  BOOL bitmap_compression = True;  int g_server_bpp = 8;
58  BOOL sendmotion = True;  int g_win_button_size = 0;      /* If zero, disable single app mode */
59  BOOL orders = True;  BOOL g_bitmap_compression = True;
60  BOOL encryption = True;  BOOL g_sendmotion = True;
61  BOOL desktop_save = True;  BOOL g_orders = True;
62  BOOL fullscreen = False;  BOOL g_encryption = True;
63  BOOL grab_keyboard = True;  BOOL packet_encryption = True;
64  BOOL hide_decorations = False;  BOOL g_desktop_save = True;
65  extern BOOL owncolmap;  BOOL g_fullscreen = False;
66    BOOL g_grab_keyboard = True;
67    BOOL g_hide_decorations = False;
68    BOOL g_use_rdp5 = True;
69    BOOL g_console_session = False;
70    BOOL g_numlock_sync = False;
71    extern BOOL g_owncolmap;
72    extern BOOL g_ownbackstore;
73    extern uint32 g_embed_wnd;
74    
75    #ifdef WITH_RDPSND
76    BOOL g_rdpsnd = False;
77    #endif
78    
79    extern RDPDR_DEVICE g_rdpdr_device[];
80    extern uint32 g_num_devices;
81    
82    #ifdef RDP2VNC
83    extern int rfb_port;
84    extern int defer_time;
85    void
86    rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
87                    char *shell, char *directory);
88    #endif
89  /* Display usage information */  /* Display usage information */
90  static void  static void
91  usage(char *program)  usage(char *program)
92  {  {
93          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
94          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
95          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
96    
97          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
98    #ifdef RDP2VNC
99            fprintf(stderr, "   -V: vnc port\n");
100            fprintf(stderr, "   -Q: defer time (ms)\n");
101    #endif
102          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
103          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
104          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
105          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
106          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
107          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
108          fprintf(stderr, "   -k: keyboard layout on terminal server (us,sv,gr,etc.)\n");          fprintf(stderr, "   -k: keyboard layout on server (en-us, de, sv, etc.)\n");
109          fprintf(stderr, "   -g: desktop geometry (WxH)\n");          fprintf(stderr, "   -g: desktop geometry (WxH)\n");
110          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
111          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
112            fprintf(stderr, "   -B: use BackingStore of X-server (if available)\n");
113          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
114            fprintf(stderr, "   -E: disable encryption from client to server\n");
115          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
116          fprintf(stderr, "   -C: use private colour map\n");          fprintf(stderr, "   -C: use private colour map\n");
117            fprintf(stderr, "   -D: hide window manager decorations\n");
118          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
119            fprintf(stderr, "   -S: caption button size (single application mode)\n");
120          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
121          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -N: enable numlock syncronization\n");
122            fprintf(stderr, "   -X: embed into another window with a given id.\n");
123            fprintf(stderr, "   -a: connection colour depth\n");
124            fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");
125            fprintf(stderr,
126                    "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
127            fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
128            fprintf(stderr,
129                    "         '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n");
130            fprintf(stderr, "             or   A=/mnt/floppy,D=/mnt/cdrom'\n");
131            fprintf(stderr,
132                    "         '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
133            fprintf(stderr, "             or      LPT1=/dev/lp0,LPT2=/dev/lp1\n");
134            fprintf(stderr, "         '-r printer:mydeskjet': enable printer redirection\n");
135            fprintf(stderr,
136                    "             or      mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
137            fprintf(stderr, "         '-r sound:[local|off|remote]': enable sound redirection\n");
138            fprintf(stderr, "                     remote would leave sound on server\n");
139            fprintf(stderr, "   -0: attach to console\n");
140            fprintf(stderr, "   -4: use RDP version 4\n");
141            fprintf(stderr, "   -5: use RDP version 5 (default)\n");
142  }  }
143    
144  static BOOL  static BOOL
# Line 122  read_password(char *password, int size) Line 177  read_password(char *password, int size)
177          return ret;          return ret;
178  }  }
179    
180    static void
181    parse_server_and_port(char *server)
182    {
183            char *p;
184    #ifdef IPv6
185            int addr_colons;
186    #endif
187    
188    #ifdef IPv6
189            p = server;
190            addr_colons = 0;
191            while (*p)
192                    if (*p++ == ':')
193                            addr_colons++;
194            if (addr_colons >= 2)
195            {
196                    /* numeric IPv6 style address format - [1:2:3::4]:port */
197                    p = strchr(server, ']');
198                    if (*server == '[' && p != NULL)
199                    {
200                            if (*(p + 1) == ':' && *(p + 2) != '\0')
201                                    tcp_port_rdp = strtol(p + 2, NULL, 10);
202                            /* remove the port number and brackets from the address */
203                            *p = '\0';
204                            strncpy(server, server + 1, strlen(server));
205                    }
206            }
207            else
208            {
209                    /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
210                    p = strchr(server, ':');
211                    if (p != NULL)
212                    {
213                            tcp_port_rdp = strtol(p + 1, NULL, 10);
214                            *p = 0;
215                    }
216            }
217    #else /* no IPv6 support */
218            p = strchr(server, ':');
219            if (p != NULL)
220            {
221                    tcp_port_rdp = strtol(p + 1, NULL, 10);
222                    *p = 0;
223            }
224    #endif /* IPv6 */
225    
226    }
227    
228  /* Client program */  /* Client program */
229  int  int
230  main(int argc, char *argv[])  main(int argc, char *argv[])
# Line 129  main(int argc, char *argv[]) Line 232  main(int argc, char *argv[])
232          char server[64];          char server[64];
233          char fullhostname[64];          char fullhostname[64];
234          char domain[16];          char domain[16];
235          char password[16];          char password[64];
236          char shell[128];          char shell[128];
237          char directory[32];          char directory[32];
238          BOOL prompt_password;          BOOL prompt_password, rdp_retval = False;
239          struct passwd *pw;          struct passwd *pw;
240          uint32 flags;          uint32 flags;
241          char *p;          char *p;
242          int c;          int c;
243    
244            int username_option = 0;
245    
246          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
247          prompt_password = False;          prompt_password = False;
248          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
249          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
250            g_embed_wnd = 0;
251    
252            g_num_devices = 0;
253    
254          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?")) != -1)  #ifdef RDP2VNC
255    #define VNCOPT "V:Q:"
256    #else
257    #define VNCOPT
258    #endif
259    
260            while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbBeEmCDKS:T:NX:a:r:045h?")) != -1)
261          {          {
262                  switch (c)                  switch (c)
263                  {                  {
264    #ifdef RDP2VNC
265                            case 'V':
266                                    rfb_port = strtol(optarg, NULL, 10);
267                                    if (rfb_port < 100)
268                                            rfb_port += 5900;
269                                    break;
270    
271                            case 'Q':
272                                    defer_time = strtol(optarg, NULL, 10);
273                                    if (defer_time < 0)
274                                            defer_time = 0;
275                                    break;
276    #endif
277    
278                          case 'u':                          case 'u':
279                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
280                                    username_option = 1;
281                                  break;                                  break;
282    
283                          case 'd':                          case 'd':
# Line 188  main(int argc, char *argv[]) Line 317  main(int argc, char *argv[])
317                                  break;                                  break;
318    
319                          case 'g':                          case 'g':
320                                    g_fullscreen = False;
321                                  if (!strcmp(optarg, "workarea"))                                  if (!strcmp(optarg, "workarea"))
322                                  {                                  {
323                                          width = height = 0;                                          g_width = g_height = 0;
324                                          break;                                          break;
325                                  }                                  }
326    
327                                  width = strtol(optarg, &p, 10);                                  g_width = strtol(optarg, &p, 10);
328                                    if (g_width <= 0)
329                                    {
330                                            error("invalid geometry\n");
331                                            return 1;
332                                    }
333    
334                                  if (*p == 'x')                                  if (*p == 'x')
335                                          height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, NULL, 10);
336    
337                                  if ((width == 0) || (height == 0))                                  if (g_height <= 0)
338                                  {                                  {
339                                          error("invalid geometry\n");                                          error("invalid geometry\n");
340                                          return 1;                                          return 1;
341                                  }                                  }
342    
343                                    if (*p == '%')
344                                            g_width = -g_width;
345    
346                                  break;                                  break;
347    
348                          case 'f':                          case 'f':
349                                  fullscreen = True;                                  g_fullscreen = True;
350                                  break;                                  break;
351    
352                          case 'b':                          case 'b':
353                                  orders = False;                                  g_orders = False;
354                                  break;                                  break;
355    
356                          case 'e':                          case 'B':
357                                  encryption = False;                                  g_ownbackstore = False;
358                                  break;                                  break;
359    
360                            case 'e':
361                                    g_encryption = False;
362                                    break;
363                            case 'E':
364                                    packet_encryption = False;
365                                    break;
366                          case 'm':                          case 'm':
367                                  sendmotion = False;                                  g_sendmotion = False;
368                                  break;                                  break;
369    
370                          case 'C':                          case 'C':
371                                  owncolmap = True;                                  g_owncolmap = True;
372                                    break;
373    
374                            case 'D':
375                                    g_hide_decorations = True;
376                                  break;                                  break;
377    
378                          case 'K':                          case 'K':
379                                  grab_keyboard = False;                                  g_grab_keyboard = False;
380                                    break;
381    
382                            case 'S':
383                                    if (!strcmp(optarg, "standard"))
384                                    {
385                                            g_win_button_size = 18;
386                                            break;
387                                    }
388    
389                                    g_win_button_size = strtol(optarg, &p, 10);
390    
391                                    if (*p)
392                                    {
393                                            error("invalid button size\n");
394                                            return 1;
395                                    }
396    
397                                  break;                                  break;
398    
399                          case 'T':                          case 'T':
400                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(g_title, optarg, sizeof(g_title));
401                                  break;                                  break;
402    
403                          case 'D':                          case 'N':
404                                  hide_decorations = True;                                  g_numlock_sync = True;
405                                    break;
406    
407                            case 'X':
408                                    g_embed_wnd = strtol(optarg, NULL, 10);
409                                    break;
410                                    
411                            case 'a':
412                                    g_server_bpp = strtol(optarg, NULL, 10);
413                                    if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
414                                        && g_server_bpp != 24)
415                                    {
416                                            error("invalid server bpp\n");
417                                            return 1;
418                                    }
419                                    break;
420    
421                            case 'r':
422    
423                                    if (strncmp("sound", optarg, 5) == 0)
424                                    {
425                                            optarg += 5;
426    
427                                            if (*optarg == ':')
428                                            {
429                                                    *optarg++;
430                                                    while ((p = next_arg(optarg, ',')))
431                                                    {
432                                                            if (strncmp("remote", optarg, 6) == 0)
433                                                                    flags |= RDP_LOGON_LEAVE_AUDIO;
434    
435                                                            if (strncmp("local", optarg, 5) == 0)
436    #ifdef WITH_RDPSND
437                                                                    g_rdpsnd = True;
438    #else
439                                                                    warning("Not compiled with sound support");
440    #endif
441    
442                                                            if (strncmp("off", optarg, 3) == 0)
443                                                                    g_rdpsnd = False;
444    
445                                                            optarg = p;
446                                                    }
447                                            }
448                                            else
449                                            {
450    #ifdef WITH_RDPSND
451                                                    g_rdpsnd = True;
452    #else
453                                                    warning("Not compiled with sound support");
454    #endif
455                                            }
456                                    }
457                                    else if (strncmp("disk", optarg, 4) == 0)
458                                    {
459                                            /* -r disk:h:=/mnt/floppy */
460                                            disk_enum_devices(&g_num_devices, optarg + 4);
461                                    }
462                                    else if (strncmp("comport", optarg, 7) == 0)
463                                    {
464                                            serial_enum_devices(&g_num_devices, optarg + 7);
465                                    }
466                                    else if (strncmp("lptport", optarg, 7) == 0)
467                                    {
468                                            parallel_enum_devices(&g_num_devices, optarg + 7);
469                                    }
470                                    else if (strncmp("printer", optarg, 7) == 0)
471                                    {
472                                            printer_enum_devices(&g_num_devices, optarg + 7);
473                                    }
474                                    else
475                                    {
476                                            warning("Unknown -r argument\n\n\tPossible arguments are: comport, disk, lptport, printer, sound\n");
477                                    }
478                                    break;
479    
480                            case '0':
481                                    g_console_session = True;
482                                    break;
483    
484                            case '4':
485                                    g_use_rdp5 = False;
486                                    break;
487    
488                            case '5':
489                                    g_use_rdp5 = True;
490                                  break;                                  break;
491    
492                          case 'h':                          case 'h':
# Line 245  main(int argc, char *argv[]) Line 497  main(int argc, char *argv[])
497                  }                  }
498          }          }
499    
500          if (argc - optind < 1)          if (argc - optind != 1)
501          {          {
502                  usage(argv[0]);                  usage(argv[0]);
503                  return 1;                  return 1;
504          }          }
505    
506          STRNCPY(server, argv[optind], sizeof(server));          STRNCPY(server, argv[optind], sizeof(server));
507          p = strchr(server, ':');          parse_server_and_port(server);
         if (p != NULL)  
         {  
                 tcp_port_rdp = strtol(p + 1, NULL, 10);  
                 *p = 0;  
         }  
508    
509          if (username[0] == 0)          if (!username_option)
510          {          {
511                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
512                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
# Line 268  main(int argc, char *argv[]) Line 515  main(int argc, char *argv[])
515                          return 1;                          return 1;
516                  }                  }
517    
518                  STRNCPY(username, pw->pw_name, sizeof(username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
519          }          }
520    
521          if (hostname[0] == 0)          if (hostname[0] == 0)
# Line 289  main(int argc, char *argv[]) Line 536  main(int argc, char *argv[])
536          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
537                  flags |= RDP_LOGON_AUTO;                  flags |= RDP_LOGON_AUTO;
538    
539          if (title[0] == 0)          if (g_title[0] == 0)
540          {          {
541                  strcpy(title, "rdesktop - ");                  strcpy(g_title, "rdesktop - ");
542                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
543          }          }
544    
545    #ifdef RDP2VNC
546            rdp2vnc_connect(server, flags, domain, password, shell, directory);
547            return 0;
548    #else
549    
550          if (!ui_init())          if (!ui_init())
551                  return 1;                  return 1;
552    
553    #ifdef WITH_RDPSND
554            if (g_rdpsnd)
555                    rdpsnd_init();
556    #endif
557            rdpdr_init();
558    
559          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
560                  return 1;                  return 1;
561    
562            /* By setting encryption to False here, we have an encrypted login
563               packet but unencrypted transfer of other packets */
564            if (!packet_encryption)
565                    g_encryption = False;
566    
567    
568          DEBUG(("Connection successful.\n"));          DEBUG(("Connection successful.\n"));
569          memset(password, 0, sizeof(password));          memset(password, 0, sizeof(password));
570    
571          if (ui_create_window())          if (ui_create_window())
572          {          {
573                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
574                  ui_destroy_window();                  ui_destroy_window();
575          }          }
576    
577          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
578          rdp_disconnect();          rdp_disconnect();
579          ui_deinit();          ui_deinit();
580          return 0;  
581            if (True == rdp_retval)
582                    return 0;
583            else
584                    return 2;
585    
586    #endif
587    
588  }  }
589    
590  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 445  error(char *format, ...) Line 716  error(char *format, ...)
716          va_end(ap);          va_end(ap);
717  }  }
718    
719    /* report a warning */
720    void
721    warning(char *format, ...)
722    {
723            va_list ap;
724    
725            fprintf(stderr, "WARNING: ");
726    
727            va_start(ap, format);
728            vfprintf(stderr, format, ap);
729            va_end(ap);
730    }
731    
732  /* report an unimplemented protocol feature */  /* report an unimplemented protocol feature */
733  void  void
734  unimpl(char *format, ...)  unimpl(char *format, ...)
# Line 463  void Line 747  void
747  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, unsigned int len)
748  {  {
749          unsigned char *line = p;          unsigned char *line = p;
750          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
751    
752          while (offset < len)          while (offset < len)
753          {          {
# Line 488  hexdump(unsigned char *p, unsigned int l Line 771  hexdump(unsigned char *p, unsigned int l
771          }          }
772  }  }
773    
774  #ifdef SAVE_LICENCE  /*
775      input: src is the string we look in for needle.
776             Needle may be escaped by a backslash, in
777             that case we ignore that particular needle.
778      return value: returns next src pointer, for
779            succesive executions, like in a while loop
780            if retval is 0, then there are no more args.
781      pitfalls:
782            src is modified. 0x00 chars are inserted to
783            terminate strings.
784            return val, points on the next val chr after ins
785            0x00
786    
787            example usage:
788            while( (pos = next_arg( optarg, ',')) ){
789                    printf("%s\n",optarg);
790                    optarg=pos;
791            }
792    
793    */
794    char *
795    next_arg(char *src, char needle)
796    {
797            char *nextval;
798            char *p;
799            char *mvp = 0;
800    
801            /* EOS */
802            if (*src == (char) 0x00)
803                    return 0;
804    
805            p = src;
806            /*  skip escaped needles */
807            while ((nextval = strchr(p, needle)))
808            {
809                    mvp = nextval - 1;
810                    /* found backslashed needle */
811                    if (*mvp == '\\' && (mvp > src))
812                    {
813                            /* move string one to the left */
814                            while (*(mvp + 1) != (char) 0x00)
815                            {
816                                    *mvp = *(mvp + 1);
817                                    *mvp++;
818                            }
819                            *mvp = (char) 0x00;
820                            p = nextval;
821                    }
822                    else
823                    {
824                            p = nextval + 1;
825                            break;
826                    }
827    
828            }
829    
830            /* more args available */
831            if (nextval)
832            {
833                    *nextval = (char) 0x00;
834                    return ++nextval;
835            }
836    
837            /* no more args after this, jump to EOS */
838            nextval = src + strlen(src);
839            return nextval;
840    }
841    
842    
843    void
844    toupper_str(char *p)
845    {
846            while (*p)
847            {
848                    if ((*p >= 'a') && (*p <= 'z'))
849                            *p = toupper((int) *p);
850                    p++;
851            }
852    }
853    
854    
855    /* not all clibs got ltoa */
856    #define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
857    
858    char *
859    l_to_a(long N, int base)
860    {
861            static char ret[LTOA_BUFSIZE];
862    
863            char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
864    
865            register int divrem;
866    
867            if (base < 36 || 2 > base)
868                    base = 10;
869    
870            if (N < 0)
871            {
872                    *head++ = '-';
873                    N = -N;
874            }
875    
876            tail = buf + sizeof(buf);
877            *--tail = 0;
878    
879            do
880            {
881                    divrem = N % base;
882                    *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
883                    N /= base;
884            }
885            while (N);
886    
887            strcpy(head, tail);
888            return ret;
889    }
890    
891    
892  int  int
893  load_licence(unsigned char **data)  load_licence(unsigned char **data)
894  {  {
895          char path[PATH_MAX];          char *home, *path;
         char *home;  
896          struct stat st;          struct stat st;
897          int fd;          int fd, length;
898    
899          home = getenv("HOME");          home = getenv("HOME");
900          if (home == NULL)          if (home == NULL)
901                  return -1;                  return -1;
902    
903          STRNCPY(path, home, sizeof(path));          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
904          strncat(path, "/.rdesktop/licence", sizeof(path) - strlen(path) - 1);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
905    
906          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
907          if (fd == -1)          if (fd == -1)
# Line 511  load_licence(unsigned char **data) Line 910  load_licence(unsigned char **data)
910          if (fstat(fd, &st))          if (fstat(fd, &st))
911                  return -1;                  return -1;
912    
913          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
914          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
915            close(fd);
916            xfree(path);
917            return length;
918  }  }
919    
920  void  void
921  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
922  {  {
923          char path[PATH_MAX];          char *home, *path, *tmppath;
         char *home;  
924          int fd;          int fd;
925    
926          home = getenv("HOME");          home = getenv("HOME");
927          if (home == NULL)          if (home == NULL)
928                  return;                  return;
929    
930          STRNCPY(path, home, sizeof(path));          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
         strncat(path, "/.rdesktop", sizeof(path) - strlen(path) - 1);  
         mkdir(path, 0700);  
931    
932          strncat(path, "/licence", sizeof(path) - strlen(path) - 1);          sprintf(path, "%s/.rdesktop", home);
933            if ((mkdir(path, 0700) == -1) && errno != EEXIST)
934            {
935                    perror(path);
936                    return;
937            }
938    
939          fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
940    
941            sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
942            tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
943            strcpy(tmppath, path);
944            strcat(tmppath, ".new");
945    
946            fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
947          if (fd == -1)          if (fd == -1)
948          {          {
949                  perror("open");                  perror(tmppath);
950                  return;                  return;
951          }          }
952    
953          write(fd, data, length);          if (write(fd, data, length) != length)
954            {
955                    perror(tmppath);
956                    unlink(tmppath);
957            }
958            else if (rename(tmppath, path) == -1)
959            {
960                    perror(path);
961                    unlink(tmppath);
962            }
963    
964          close(fd);          close(fd);
965            xfree(tmppath);
966            xfree(path);
967  }  }
 #endif  

Legend:
Removed from v.279  
changed lines
  Added in v.636

  ViewVC Help
Powered by ViewVC 1.1.26