/[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 280 by n-ki, Tue Nov 26 14:39:50 2002 UTC revision 520 by matthewc, Tue Oct 28 05:07:00 2003 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 21  Line 21 
21  #include <stdarg.h>             /* va_list va_start va_end */  #include <stdarg.h>             /* va_list va_start va_end */
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 */
 #include <errno.h>              /* save licence uses it. */  
24  #include <pwd.h>                /* getpwuid */  #include <pwd.h>                /* getpwuid */
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 <errno.h>
30  #include "rdesktop.h"  #include "rdesktop.h"
31    
32  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 40  Line 40 
40  #include "crypto/md5.h"  #include "crypto/md5.h"
41  #endif  #endif
42    
43  char title[32] = "";  char g_title[64] = "";
44  char username[16];  char g_username[64];
45  char hostname[16];  char hostname[16];
46  char keymapname[16];  char keymapname[16];
47  int keylayout = 0x409;          /* Defaults to US keyboard layout */  int keylayout = 0x409;          /* Defaults to US keyboard layout */
48  int width = 800;                /* If width or height are reset to zero, the geometry will  
49                                     be fetched from _NET_WORKAREA */  int g_width = 800;              /* width is special: If 0, the
50  int height = 600;                                     geometry will be fetched from
51                                       _NET_WORKAREA. If negative,
52                                       absolute value specifies the
53                                       percent of the whole screen. */
54    int g_height = 600;
55  int tcp_port_rdp = TCP_PORT_RDP;  int tcp_port_rdp = TCP_PORT_RDP;
56  BOOL bitmap_compression = True;  int g_server_bpp = 8;
57  BOOL sendmotion = True;  int g_win_button_size = 0;      /* If zero, disable single app mode */
58  BOOL orders = True;  BOOL g_bitmap_compression = True;
59  BOOL encryption = True;  BOOL g_sendmotion = True;
60  BOOL desktop_save = True;  BOOL g_orders = True;
61  BOOL fullscreen = False;  BOOL g_encryption = True;
62  BOOL grab_keyboard = True;  BOOL packet_encryption = True;
63  BOOL hide_decorations = False;  BOOL g_desktop_save = True;
64  extern BOOL owncolmap;  BOOL g_fullscreen = False;
65    BOOL g_grab_keyboard = True;
66    BOOL g_hide_decorations = False;
67    BOOL g_use_rdp5 = True;
68    BOOL g_console_session = False;
69    extern BOOL g_owncolmap;
70    
71    #ifdef WITH_RDPSND
72    BOOL g_rdpsnd = False;
73    #endif
74    
75    #ifdef RDP2VNC
76    extern int rfb_port;
77    extern int defer_time;
78    void
79    rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
80                    char *shell, char *directory);
81    #endif
82  /* Display usage information */  /* Display usage information */
83  static void  static void
84  usage(char *program)  usage(char *program)
85  {  {
86          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
87          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
88          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
89    
90          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
91    #ifdef RDP2VNC
92            fprintf(stderr, "   -V: vnc port\n");
93            fprintf(stderr, "   -Q: defer time (ms)\n");
94    #endif
95          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
96          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
97          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
98          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
99          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
100          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
101          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");
102          fprintf(stderr, "   -g: desktop geometry (WxH)\n");          fprintf(stderr, "   -g: desktop geometry (WxH)\n");
103          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
104          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
105          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
106            fprintf(stderr, "   -E: disable encryption from client to server\n");
107          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
108          fprintf(stderr, "   -C: use private colour map\n");          fprintf(stderr, "   -C: use private colour map\n");
109            fprintf(stderr, "   -D: hide window manager decorations\n");
110          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
111            fprintf(stderr, "   -S: caption button size (single application mode)\n");
112          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
113          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -a: connection colour depth\n");
114            fprintf(stderr, "   -r: enable specified device redirection (currently: sound)\n");
115            fprintf(stderr, "   -0: attach to console\n");
116            fprintf(stderr, "   -4: use RDP version 4\n");
117            fprintf(stderr, "   -5: use RDP version 5 (default)\n");
118  }  }
119    
120  static BOOL  static BOOL
# Line 122  read_password(char *password, int size) Line 153  read_password(char *password, int size)
153          return ret;          return ret;
154  }  }
155    
156    static void
157    parse_server_and_port(char *server)
158    {
159            char *p;
160    #ifdef IPv6
161            int addr_colons;
162    #endif
163    
164    #ifdef IPv6
165            p = server;
166            addr_colons = 0;
167            while (*p)
168                    if (*p++ == ':')
169                            addr_colons++;
170            if (addr_colons >= 2)
171            {
172                    /* numeric IPv6 style address format - [1:2:3::4]:port */
173                    p = strchr(server, ']');
174                    if (*server == '[' && p != NULL)
175                    {
176                            if (*(p + 1) == ':' && *(p + 2) != '\0')
177                                    tcp_port_rdp = strtol(p + 2, NULL, 10);
178                            /* remove the port number and brackets from the address */
179                            *p = '\0';
180                            strncpy(server, server + 1, strlen(server));
181                    }
182            }
183            else
184            {
185                    /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
186                    p = strchr(server, ':');
187                    if (p != NULL)
188                    {
189                            tcp_port_rdp = strtol(p + 1, NULL, 10);
190                            *p = 0;
191                    }
192            }
193    #else /* no IPv6 support */
194            p = strchr(server, ':');
195            if (p != NULL)
196            {
197                    tcp_port_rdp = strtol(p + 1, NULL, 10);
198                    *p = 0;
199            }
200    #endif /* IPv6 */
201    
202    }
203    
204  /* Client program */  /* Client program */
205  int  int
206  main(int argc, char *argv[])  main(int argc, char *argv[])
# Line 129  main(int argc, char *argv[]) Line 208  main(int argc, char *argv[])
208          char server[64];          char server[64];
209          char fullhostname[64];          char fullhostname[64];
210          char domain[16];          char domain[16];
211          char password[16];          char password[64];
212          char shell[128];          char shell[128];
213          char directory[32];          char directory[32];
214          BOOL prompt_password;          BOOL prompt_password, rdp_retval = False;
215          struct passwd *pw;          struct passwd *pw;
216          uint32 flags;          uint32 flags;
217          char *p;          char *p;
218          int c;          int c;
219            int username_option = 0;
220    
221          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
222          prompt_password = False;          prompt_password = False;
223          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
224          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
225    
226          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?")) != -1)  #ifdef RDP2VNC
227    #define VNCOPT "V:Q:"
228    #else
229    #define VNCOPT
230    #endif
231    
232            while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbeEmCDKS:T:a:r:045h?")) != -1)
233          {          {
234                  switch (c)                  switch (c)
235                  {                  {
236    #ifdef RDP2VNC
237                            case 'V':
238                                    rfb_port = strtol(optarg, NULL, 10);
239                                    if (rfb_port < 100)
240                                            rfb_port += 5900;
241                                    break;
242    
243                            case 'Q':
244                                    defer_time = strtol(optarg, NULL, 10);
245                                    if (defer_time < 0)
246                                            defer_time = 0;
247                                    break;
248    #endif
249    
250                          case 'u':                          case 'u':
251                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
252                                    username_option = 1;
253                                  break;                                  break;
254    
255                          case 'd':                          case 'd':
# Line 190  main(int argc, char *argv[]) Line 291  main(int argc, char *argv[])
291                          case 'g':                          case 'g':
292                                  if (!strcmp(optarg, "workarea"))                                  if (!strcmp(optarg, "workarea"))
293                                  {                                  {
294                                          width = height = 0;                                          g_width = g_height = 0;
295                                          break;                                          break;
296                                  }                                  }
297    
298                                  width = strtol(optarg, &p, 10);                                  g_width = strtol(optarg, &p, 10);
299                                    if (g_width <= 0)
300                                    {
301                                            error("invalid geometry\n");
302                                            return 1;
303                                    }
304    
305                                  if (*p == 'x')                                  if (*p == 'x')
306                                          height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, NULL, 10);
307    
308                                  if ((width == 0) || (height == 0))                                  if (g_height <= 0)
309                                  {                                  {
310                                          error("invalid geometry\n");                                          error("invalid geometry\n");
311                                          return 1;                                          return 1;
312                                  }                                  }
313    
314                                    if (*p == '%')
315                                            g_width = -g_width;
316    
317                                  break;                                  break;
318    
319                          case 'f':                          case 'f':
320                                  fullscreen = True;                                  g_fullscreen = True;
321                                  break;                                  break;
322    
323                          case 'b':                          case 'b':
324                                  orders = False;                                  g_orders = False;
325                                  break;                                  break;
326    
327                          case 'e':                          case 'e':
328                                  encryption = False;                                  g_encryption = False;
329                                    break;
330                            case 'E':
331                                    packet_encryption = False;
332                                  break;                                  break;
   
333                          case 'm':                          case 'm':
334                                  sendmotion = False;                                  g_sendmotion = False;
335                                  break;                                  break;
336    
337                          case 'C':                          case 'C':
338                                  owncolmap = True;                                  g_owncolmap = True;
339                                    break;
340    
341                            case 'D':
342                                    g_hide_decorations = True;
343                                  break;                                  break;
344    
345                          case 'K':                          case 'K':
346                                  grab_keyboard = False;                                  g_grab_keyboard = False;
347                                    break;
348    
349                            case 'S':
350                                    if (!strcmp(optarg, "standard"))
351                                    {
352                                            g_win_button_size = 18;
353                                            break;
354                                    }
355    
356                                    g_win_button_size = strtol(optarg, &p, 10);
357    
358                                    if (*p)
359                                    {
360                                            error("invalid button size\n");
361                                            return 1;
362                                    }
363    
364                                  break;                                  break;
365    
366                          case 'T':                          case 'T':
367                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(g_title, optarg, sizeof(g_title));
368                                  break;                                  break;
369    
370                          case 'D':                          case 'a':
371                                  hide_decorations = True;                                  g_server_bpp = strtol(optarg, NULL, 10);
372                                    if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
373                                        && g_server_bpp != 24)
374                                    {
375                                            error("invalid server bpp\n");
376                                            return 1;
377                                    }
378                                    break;
379    
380                            case 'r':
381                                    if (!strcmp(optarg, "sound"))
382    #ifdef WITH_RDPSND
383                                            g_rdpsnd = True;
384    #else
385                                            warning("Not compiled with sound support");
386    #endif
387                                    break;
388    
389                            case '0':
390                                    g_console_session = True;
391                                    break;
392    
393                            case '4':
394                                    g_use_rdp5 = False;
395                                    break;
396    
397                            case '5':
398                                    g_use_rdp5 = True;
399                                  break;                                  break;
400    
401                          case 'h':                          case 'h':
# Line 252  main(int argc, char *argv[]) Line 413  main(int argc, char *argv[])
413          }          }
414    
415          STRNCPY(server, argv[optind], sizeof(server));          STRNCPY(server, argv[optind], sizeof(server));
416          p = strchr(server, ':');          parse_server_and_port(server);
         if (p != NULL)  
         {  
                 tcp_port_rdp = strtol(p + 1, NULL, 10);  
                 *p = 0;  
         }  
417    
418          if (username[0] == 0)          if (!username_option)
419          {          {
420                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
421                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
# Line 268  main(int argc, char *argv[]) Line 424  main(int argc, char *argv[])
424                          return 1;                          return 1;
425                  }                  }
426    
427                  STRNCPY(username, pw->pw_name, sizeof(username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
428          }          }
429    
430          if (hostname[0] == 0)          if (hostname[0] == 0)
# Line 289  main(int argc, char *argv[]) Line 445  main(int argc, char *argv[])
445          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
446                  flags |= RDP_LOGON_AUTO;                  flags |= RDP_LOGON_AUTO;
447    
448          if (title[0] == 0)          if (g_title[0] == 0)
449          {          {
450                  strcpy(title, "rdesktop - ");                  strcpy(g_title, "rdesktop - ");
451                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
452          }          }
453    
454    #ifdef RDP2VNC
455            rdp2vnc_connect(server, flags, domain, password, shell, directory);
456            return 0;
457    #else
458    
459          if (!ui_init())          if (!ui_init())
460                  return 1;                  return 1;
461    
462    #ifdef WITH_RDPSND
463            if (g_rdpsnd)
464                    rdpsnd_init();
465    #endif
466            /* rdpdr_init(); */
467    
468          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
469                  return 1;                  return 1;
470    
471            /* By setting encryption to False here, we have an encrypted login
472               packet but unencrypted transfer of other packets */
473            if (!packet_encryption)
474                    g_encryption = False;
475    
476    
477          DEBUG(("Connection successful.\n"));          DEBUG(("Connection successful.\n"));
478          memset(password, 0, sizeof(password));          memset(password, 0, sizeof(password));
479    
480          if (ui_create_window())          if (ui_create_window())
481          {          {
482                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
483                  ui_destroy_window();                  ui_destroy_window();
484          }          }
485    
486          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
487          rdp_disconnect();          rdp_disconnect();
488          ui_deinit();          ui_deinit();
489          return 0;  
490            if (True == rdp_retval)
491                    return 0;
492            else
493                    return 2;
494    
495    #endif
496    
497  }  }
498    
499  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 445  error(char *format, ...) Line 625  error(char *format, ...)
625          va_end(ap);          va_end(ap);
626  }  }
627    
628    /* report a warning */
629    void
630    warning(char *format, ...)
631    {
632            va_list ap;
633    
634            fprintf(stderr, "WARNING: ");
635    
636            va_start(ap, format);
637            vfprintf(stderr, format, ap);
638            va_end(ap);
639    }
640    
641  /* report an unimplemented protocol feature */  /* report an unimplemented protocol feature */
642  void  void
643  unimpl(char *format, ...)  unimpl(char *format, ...)
# Line 460  unimpl(char *format, ...) Line 653  unimpl(char *format, ...)
653    
654  /* produce a hex dump */  /* produce a hex dump */
655  void  void
656  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, int len)
657  {  {
658          unsigned char *line = p;          unsigned char *line = p;
659          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
660    
661          while (offset < len)          while (offset < len)
662          {          {
# Line 488  hexdump(unsigned char *p, unsigned int l Line 680  hexdump(unsigned char *p, unsigned int l
680          }          }
681  }  }
682    
683  #ifdef SAVE_LICENCE  
684  int  int
685  load_licence(unsigned char **data)  load_licence(unsigned char **data)
686  {  {
687          char *path;          char *home, *path;
         char *home;  
688          struct stat st;          struct stat st;
689          int fd;          int fd, length;
690    
691          home = getenv("HOME");          home = getenv("HOME");
692          if (home == NULL)          if (home == NULL)
693                  return -1;                  return -1;
694    
695          path = xmalloc(strlen(home) + strlen(hostname) + 20);          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
696          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
697    
698          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
# Line 511  load_licence(unsigned char **data) Line 702  load_licence(unsigned char **data)
702          if (fstat(fd, &st))          if (fstat(fd, &st))
703                  return -1;                  return -1;
704    
705          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
706          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
707            close(fd);
708            xfree(path);
709            return length;
710  }  }
711    
712  void  void
713  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
714  {  {
715          char *fpath;            /* file path for licence */          char *home, *path, *tmppath;
716          char *fname, *fnamewrk; /* file name for licence .inkl path. */          int fd;
         char *home;  
         uint32 y;  
         struct flock fnfl;  
         int fnfd, fnwrkfd, i, wlen;  
         struct stream s, *s_ptr;  
         uint32 len;  
   
         /* Construct a stream, so that we can use macros to extract the  
          * licence.  
          */  
         s_ptr = &s;  
         s_ptr->p = data;  
         /* Skip first two bytes */  
         in_uint16(s_ptr, len);  
   
         /* Skip three strings */  
         for (i = 0; i < 3; i++)  
         {  
                 in_uint32(s_ptr, len);  
                 s_ptr->p += len;  
                 /* Make sure that we won't be past the end of data after  
                  * reading the next length value  
                  */  
                 if ((s_ptr->p) + 4 > data + length)  
                 {  
                         printf("Error in parsing licence key.\n");  
                         printf("Strings %d end value %x > supplied length (%x)\n",  
                                i, s_ptr->p, data + length);  
                         return;  
                 }  
         }  
         in_uint32(s_ptr, len);  
         if (s_ptr->p + len > data + length)  
         {  
                 printf("Error in parsing licence key.\n");  
                 printf("End of licence %x > supplied length (%x)\n", s_ptr->p + len, data + length);  
                 return;  
         }  
717    
718          home = getenv("HOME");          home = getenv("HOME");
719          if (home == NULL)          if (home == NULL)
720                  return;                  return;
721    
722          /* set and create the directory -- if it doesn't exist. */          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
         fpath = xmalloc(strlen(home) + 11);  
         STRNCPY(fpath, home, strlen(home) + 1);  
723    
724          sprintf(fpath, "%s/.rdesktop", fpath);          sprintf(path, "%s/.rdesktop", home);
725          if (mkdir(fpath, 0700) == -1 && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
726          {          {
727                  perror("mkdir");                  perror(path);
728                  exit(1);                  return;
729          }          }
730    
731          /* set the real licence filename, and put a write lock on it. */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
732          fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);  
733          sprintf(fname, "%s/licence.%s", fpath, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
734          fnfd = open(fname, O_RDONLY);          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
735          if (fnfd != -1)          strcpy(tmppath, path);
736          {          strcat(tmppath, ".new");
                 fnfl.l_type = F_WRLCK;  
                 fnfl.l_whence = SEEK_SET;  
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
         }  
   
         /* create a temporary licence file */  
         fnamewrk = xmalloc(strlen(fname) + 12);  
         for (y = 0;; y++)  
         {  
                 sprintf(fnamewrk, "%s.%lu", fname, y);  
                 fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);  
                 if (fnwrkfd == -1)  
                 {  
                         if (errno == EINTR || errno == EEXIST)  
                                 continue;  
                         perror("create");  
                         exit(1);  
                 }  
                 break;  
         }  
         /* write to the licence file */  
         for (y = 0; y < len;)  
         {  
                 do  
                 {  
                         wlen = write(fnwrkfd, s_ptr->p + y, len - y);  
                 }  
                 while (wlen == -1 && errno == EINTR);  
                 if (wlen < 1)  
                 {  
                         perror("write");  
                         unlink(fnamewrk);  
                         exit(1);  
                 }  
                 y += wlen;  
         }  
737    
738          /* close the file and rename it to fname */          fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
739          if (close(fnwrkfd) == -1)          if (fd == -1)
740          {          {
741                  perror("close");                  perror(tmppath);
742                  unlink(fnamewrk);                  return;
                 exit(1);  
743          }          }
744          if (rename(fnamewrk, fname) == -1)  
745            if (write(fd, data, length) != length)
746          {          {
747                  perror("rename");                  perror(tmppath);
748                  unlink(fnamewrk);                  unlink(tmppath);
                 exit(1);  
749          }          }
750          /* close the file lock on fname */          else if (rename(tmppath, path) == -1)
         if (fnfd != -1)  
751          {          {
752                  fnfl.l_type = F_UNLCK;                  perror(path);
753                  fnfl.l_whence = SEEK_SET;                  unlink(tmppath);
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
                 close(fnfd);  
754          }          }
755    
756            close(fd);
757            xfree(tmppath);
758            xfree(path);
759  }  }
 #endif  

Legend:
Removed from v.280  
changed lines
  Added in v.520

  ViewVC Help
Powered by ViewVC 1.1.26