/[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 500 by astrand, Wed Oct 15 14:32:43 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 */
 int width = 800;                /* If width or height are reset to zero, the geometry will  
                                    be fetched from _NET_WORKAREA */  
 int height = 600;  
 int tcp_port_rdp = TCP_PORT_RDP;  
 BOOL bitmap_compression = True;  
 BOOL sendmotion = True;  
 BOOL orders = True;  
 BOOL encryption = True;  
 BOOL desktop_save = True;  
 BOOL fullscreen = False;  
 BOOL grab_keyboard = True;  
 BOOL hide_decorations = False;  
 extern BOOL owncolmap;  
48    
49    int g_width = 800;              /* width is special: If 0, the
50                                       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;
56    int g_server_bpp = 8;
57    int g_win_button_size = 0;      /* If zero, disable single app mode */
58    BOOL g_bitmap_compression = True;
59    BOOL g_sendmotion = True;
60    BOOL g_orders = True;
61    BOOL g_encryption = True;
62    BOOL packet_encryption = True;
63    BOOL g_desktop_save = True;
64    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 RDP2VNC
72    extern int rfb_port;
73    extern int defer_time;
74    void
75    rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
76                    char *shell, char *directory);
77    #endif
78  /* Display usage information */  /* Display usage information */
79  static void  static void
80  usage(char *program)  usage(char *program)
81  {  {
82          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
83          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2002 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
84          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
85    
86          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
87    #ifdef RDP2VNC
88            fprintf(stderr, "   -V: vnc port\n");
89            fprintf(stderr, "   -Q: defer time (ms)\n");
90    #endif
91          fprintf(stderr, "   -u: user name\n");          fprintf(stderr, "   -u: user name\n");
92          fprintf(stderr, "   -d: domain\n");          fprintf(stderr, "   -d: domain\n");
93          fprintf(stderr, "   -s: shell\n");          fprintf(stderr, "   -s: shell\n");
94            fprintf(stderr, "   -S: caption button size (single application mode)\n");
95          fprintf(stderr, "   -c: working directory\n");          fprintf(stderr, "   -c: working directory\n");
96          fprintf(stderr, "   -p: password (- to prompt)\n");          fprintf(stderr, "   -p: password (- to prompt)\n");
97          fprintf(stderr, "   -n: client hostname\n");          fprintf(stderr, "   -n: client hostname\n");
# Line 79  usage(char *program) Line 100  usage(char *program)
100          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
101          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
102          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
103            fprintf(stderr, "   -E: disable encryption from client to server\n");
104          fprintf(stderr, "   -m: do not send motion events\n");          fprintf(stderr, "   -m: do not send motion events\n");
105          fprintf(stderr, "   -C: use private colour map\n");          fprintf(stderr, "   -C: use private colour map\n");
106          fprintf(stderr, "   -K: keep window manager key bindings\n");          fprintf(stderr, "   -K: keep window manager key bindings\n");
107          fprintf(stderr, "   -T: window title\n");          fprintf(stderr, "   -T: window title\n");
108          fprintf(stderr, "   -D: hide window manager decorations\n");          fprintf(stderr, "   -D: hide window manager decorations\n");
109            fprintf(stderr, "   -a: server bpp\n");
110            fprintf(stderr, "   -0: attach to console\n");
111            fprintf(stderr, "   -4: use RDP version 4\n");
112            fprintf(stderr, "   -5: use RDP version 5 (default)\n");
113  }  }
114    
115  static BOOL  static BOOL
# Line 122  read_password(char *password, int size) Line 148  read_password(char *password, int size)
148          return ret;          return ret;
149  }  }
150    
151    static void
152    parse_server_and_port(char *server)
153    {
154            char *p;
155    #ifdef IPv6
156            int addr_colons;
157    #endif
158    
159    #ifdef IPv6
160            p = server;
161            addr_colons = 0;
162            while (*p)
163                    if (*p++ == ':')
164                            addr_colons++;
165            if (addr_colons >= 2)
166            {
167                    /* numeric IPv6 style address format - [1:2:3::4]:port */
168                    p = strchr(server, ']');
169                    if (*server == '[' && p != NULL)
170                    {
171                            if (*(p + 1) == ':' && *(p + 2) != '\0')
172                                    tcp_port_rdp = strtol(p + 2, NULL, 10);
173                            /* remove the port number and brackets from the address */
174                            *p = '\0';
175                            strncpy(server, server + 1, strlen(server));
176                    }
177            }
178            else
179            {
180                    /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
181                    p = strchr(server, ':');
182                    if (p != NULL)
183                    {
184                            tcp_port_rdp = strtol(p + 1, NULL, 10);
185                            *p = 0;
186                    }
187            }
188    #else /* no IPv6 support */
189            p = strchr(server, ':');
190            if (p != NULL)
191            {
192                    tcp_port_rdp = strtol(p + 1, NULL, 10);
193                    *p = 0;
194            }
195    #endif /* IPv6 */
196    
197    }
198    
199  /* Client program */  /* Client program */
200  int  int
201  main(int argc, char *argv[])  main(int argc, char *argv[])
# Line 129  main(int argc, char *argv[]) Line 203  main(int argc, char *argv[])
203          char server[64];          char server[64];
204          char fullhostname[64];          char fullhostname[64];
205          char domain[16];          char domain[16];
206          char password[16];          char password[64];
207          char shell[128];          char shell[128];
208          char directory[32];          char directory[32];
209          BOOL prompt_password;          BOOL prompt_password, rdp_retval = False;
210          struct passwd *pw;          struct passwd *pw;
211          uint32 flags;          uint32 flags;
212          char *p;          char *p;
213          int c;          int c;
214            int username_option = 0;
215    
216          flags = RDP_LOGON_NORMAL;          flags = RDP_LOGON_NORMAL;
217          prompt_password = False;          prompt_password = False;
218          domain[0] = password[0] = shell[0] = directory[0] = 0;          domain[0] = password[0] = shell[0] = directory[0] = 0;
219          strcpy(keymapname, "en-us");          strcpy(keymapname, "en-us");
220    
221          while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemCKT:Dh?")) != -1)  #ifdef RDP2VNC
222    #define VNCOPT "V:Q:"
223    #else
224    #define VNCOPT
225    #endif
226    
227            while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbeEmCKT:D045h?")) != -1)
228          {          {
229                  switch (c)                  switch (c)
230                  {                  {
231    #ifdef RDP2VNC
232                            case 'V':
233                                    rfb_port = strtol(optarg, NULL, 10);
234                                    if (rfb_port < 100)
235                                            rfb_port += 5900;
236                                    break;
237    
238                            case 'Q':
239                                    defer_time = strtol(optarg, NULL, 10);
240                                    if (defer_time < 0)
241                                            defer_time = 0;
242                                    break;
243    #endif
244    
245                          case 'u':                          case 'u':
246                                  STRNCPY(username, optarg, sizeof(username));                                  STRNCPY(g_username, optarg, sizeof(g_username));
247                                    username_option = 1;
248                                  break;                                  break;
249    
250                          case 'd':                          case 'd':
# Line 159  main(int argc, char *argv[]) Line 255  main(int argc, char *argv[])
255                                  STRNCPY(shell, optarg, sizeof(shell));                                  STRNCPY(shell, optarg, sizeof(shell));
256                                  break;                                  break;
257    
258                            case 'S':
259                                    if (!strcmp(optarg, "standard"))
260                                    {
261                                            g_win_button_size = 18;
262                                            break;
263                                    }
264    
265                                    g_win_button_size = strtol(optarg, &p, 10);
266    
267                                    if (*p)
268                                    {
269                                            error("invalid button size\n");
270                                            return 1;
271                                    }
272    
273                                    break;
274    
275                          case 'c':                          case 'c':
276                                  STRNCPY(directory, optarg, sizeof(directory));                                  STRNCPY(directory, optarg, sizeof(directory));
277                                  break;                                  break;
# Line 190  main(int argc, char *argv[]) Line 303  main(int argc, char *argv[])
303                          case 'g':                          case 'g':
304                                  if (!strcmp(optarg, "workarea"))                                  if (!strcmp(optarg, "workarea"))
305                                  {                                  {
306                                          width = height = 0;                                          g_width = g_height = 0;
307                                          break;                                          break;
308                                  }                                  }
309    
310                                  width = strtol(optarg, &p, 10);                                  g_width = strtol(optarg, &p, 10);
311                                    if (g_width <= 0)
312                                    {
313                                            error("invalid geometry\n");
314                                            return 1;
315                                    }
316    
317                                  if (*p == 'x')                                  if (*p == 'x')
318                                          height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, NULL, 10);
319    
320                                  if ((width == 0) || (height == 0))                                  if (g_height <= 0)
321                                  {                                  {
322                                          error("invalid geometry\n");                                          error("invalid geometry\n");
323                                          return 1;                                          return 1;
324                                  }                                  }
325    
326                                    if (*p == '%')
327                                            g_width = -g_width;
328    
329                                  break;                                  break;
330    
331                          case 'f':                          case 'f':
332                                  fullscreen = True;                                  g_fullscreen = True;
333                                  break;                                  break;
334    
335                          case 'b':                          case 'b':
336                                  orders = False;                                  g_orders = False;
337                                  break;                                  break;
338    
339                          case 'e':                          case 'e':
340                                  encryption = False;                                  g_encryption = False;
341                                    break;
342                            case 'E':
343                                    packet_encryption = False;
344                                  break;                                  break;
   
345                          case 'm':                          case 'm':
346                                  sendmotion = False;                                  g_sendmotion = False;
347                                  break;                                  break;
348    
349                          case 'C':                          case 'C':
350                                  owncolmap = True;                                  g_owncolmap = True;
351                                  break;                                  break;
352    
353                          case 'K':                          case 'K':
354                                  grab_keyboard = False;                                  g_grab_keyboard = False;
355                                  break;                                  break;
356    
357                          case 'T':                          case 'T':
358                                  STRNCPY(title, optarg, sizeof(title));                                  STRNCPY(g_title, optarg, sizeof(g_title));
359                                  break;                                  break;
360    
361                          case 'D':                          case 'D':
362                                  hide_decorations = True;                                  g_hide_decorations = True;
363                                    break;
364    
365                            case 'a':
366                                    g_server_bpp = strtol(optarg, NULL, 10);
367                                    if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
368                                        && g_server_bpp != 24)
369                                    {
370                                            error("invalid server bpp\n");
371                                            return 1;
372                                    }
373                                    break;
374    
375                            case '0':
376                                    g_console_session = True;
377                                    break;
378    
379                            case '4':
380                                    g_use_rdp5 = False;
381                                    break;
382    
383                            case '5':
384                                    g_use_rdp5 = True;
385                                  break;                                  break;
386    
387                          case 'h':                          case 'h':
# Line 252  main(int argc, char *argv[]) Line 399  main(int argc, char *argv[])
399          }          }
400    
401          STRNCPY(server, argv[optind], sizeof(server));          STRNCPY(server, argv[optind], sizeof(server));
402          p = strchr(server, ':');          parse_server_and_port(server);
         if (p != NULL)  
         {  
                 tcp_port_rdp = strtol(p + 1, NULL, 10);  
                 *p = 0;  
         }  
403    
404          if (username[0] == 0)          if (!username_option)
405          {          {
406                  pw = getpwuid(getuid());                  pw = getpwuid(getuid());
407                  if ((pw == NULL) || (pw->pw_name == NULL))                  if ((pw == NULL) || (pw->pw_name == NULL))
# Line 268  main(int argc, char *argv[]) Line 410  main(int argc, char *argv[])
410                          return 1;                          return 1;
411                  }                  }
412    
413                  STRNCPY(username, pw->pw_name, sizeof(username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
414          }          }
415    
416          if (hostname[0] == 0)          if (hostname[0] == 0)
# Line 289  main(int argc, char *argv[]) Line 431  main(int argc, char *argv[])
431          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
432                  flags |= RDP_LOGON_AUTO;                  flags |= RDP_LOGON_AUTO;
433    
434          if (title[0] == 0)          if (g_title[0] == 0)
435          {          {
436                  strcpy(title, "rdesktop - ");                  strcpy(g_title, "rdesktop - ");
437                  strncat(title, server, sizeof(title) - sizeof("rdesktop - "));                  strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
438          }          }
439    
440    #ifdef RDP2VNC
441            rdp2vnc_connect(server, flags, domain, password, shell, directory);
442            return 0;
443    #else
444    
445          if (!ui_init())          if (!ui_init())
446                  return 1;                  return 1;
447    
448    #ifdef WITH_RDPSND
449            rdpsnd_init();
450    #endif
451            /* rdpdr_init(); */
452    
453          if (!rdp_connect(server, flags, domain, password, shell, directory))          if (!rdp_connect(server, flags, domain, password, shell, directory))
454                  return 1;                  return 1;
455    
456            /* By setting encryption to False here, we have an encrypted login
457               packet but unencrypted transfer of other packets */
458            if (!packet_encryption)
459                    g_encryption = False;
460    
461    
462          DEBUG(("Connection successful.\n"));          DEBUG(("Connection successful.\n"));
463          memset(password, 0, sizeof(password));          memset(password, 0, sizeof(password));
464    
465          if (ui_create_window())          if (ui_create_window())
466          {          {
467                  rdp_main_loop();                  rdp_retval = rdp_main_loop();
468                  ui_destroy_window();                  ui_destroy_window();
469          }          }
470    
471          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
472          rdp_disconnect();          rdp_disconnect();
473          ui_deinit();          ui_deinit();
474          return 0;  
475            if (True == rdp_retval)
476                    return 0;
477            else
478                    return 2;
479    
480    #endif
481    
482  }  }
483    
484  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
# Line 445  error(char *format, ...) Line 610  error(char *format, ...)
610          va_end(ap);          va_end(ap);
611  }  }
612    
613    /* report a warning */
614    void
615    warning(char *format, ...)
616    {
617            va_list ap;
618    
619            fprintf(stderr, "WARNING: ");
620    
621            va_start(ap, format);
622            vfprintf(stderr, format, ap);
623            va_end(ap);
624    }
625    
626  /* report an unimplemented protocol feature */  /* report an unimplemented protocol feature */
627  void  void
628  unimpl(char *format, ...)  unimpl(char *format, ...)
# Line 460  unimpl(char *format, ...) Line 638  unimpl(char *format, ...)
638    
639  /* produce a hex dump */  /* produce a hex dump */
640  void  void
641  hexdump(unsigned char *p, unsigned int len)  hexdump(unsigned char *p, int len)
642  {  {
643          unsigned char *line = p;          unsigned char *line = p;
644          unsigned int thisline, offset = 0;          int i, thisline, offset = 0;
         int i;  
645    
646          while (offset < len)          while (offset < len)
647          {          {
# Line 488  hexdump(unsigned char *p, unsigned int l Line 665  hexdump(unsigned char *p, unsigned int l
665          }          }
666  }  }
667    
668  #ifdef SAVE_LICENCE  
669  int  int
670  load_licence(unsigned char **data)  load_licence(unsigned char **data)
671  {  {
672          char *path;          char *home, *path;
         char *home;  
673          struct stat st;          struct stat st;
674          int fd;          int fd, length;
675    
676          home = getenv("HOME");          home = getenv("HOME");
677          if (home == NULL)          if (home == NULL)
678                  return -1;                  return -1;
679    
680          path = xmalloc(strlen(home) + strlen(hostname) + 20);          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
681          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
682    
683          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
# Line 511  load_licence(unsigned char **data) Line 687  load_licence(unsigned char **data)
687          if (fstat(fd, &st))          if (fstat(fd, &st))
688                  return -1;                  return -1;
689    
690          *data = xmalloc(st.st_size);          *data = (uint8 *) xmalloc(st.st_size);
691          return read(fd, *data, st.st_size);          length = read(fd, *data, st.st_size);
692            close(fd);
693            xfree(path);
694            return length;
695  }  }
696    
697  void  void
698  save_licence(unsigned char *data, int length)  save_licence(unsigned char *data, int length)
699  {  {
700          char *fpath;            /* file path for licence */          char *home, *path, *tmppath;
701          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;  
         }  
702    
703          home = getenv("HOME");          home = getenv("HOME");
704          if (home == NULL)          if (home == NULL)
705                  return;                  return;
706    
707          /* 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);  
708    
709          sprintf(fpath, "%s/.rdesktop", fpath);          sprintf(path, "%s/.rdesktop", home);
710          if (mkdir(fpath, 0700) == -1 && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
711          {          {
712                  perror("mkdir");                  perror(path);
713                  exit(1);                  return;
714          }          }
715    
716          /* set the real licence filename, and put a write lock on it. */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
717          fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);  
718          sprintf(fname, "%s/licence.%s", fpath, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
719          fnfd = open(fname, O_RDONLY);          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
720          if (fnfd != -1)          strcpy(tmppath, path);
721          {          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;  
         }  
722    
723          /* close the file and rename it to fname */          fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
724          if (close(fnwrkfd) == -1)          if (fd == -1)
725          {          {
726                  perror("close");                  perror(tmppath);
727                  unlink(fnamewrk);                  return;
                 exit(1);  
728          }          }
729          if (rename(fnamewrk, fname) == -1)  
730            if (write(fd, data, length) != length)
731          {          {
732                  perror("rename");                  perror(tmppath);
733                  unlink(fnamewrk);                  unlink(tmppath);
                 exit(1);  
734          }          }
735          /* close the file lock on fname */          else if (rename(tmppath, path) == -1)
         if (fnfd != -1)  
736          {          {
737                  fnfl.l_type = F_UNLCK;                  perror(path);
738                  fnfl.l_whence = SEEK_SET;                  unlink(tmppath);
                 fnfl.l_start = 0;  
                 fnfl.l_len = 1;  
                 fcntl(fnfd, F_SETLK, &fnfl);  
                 close(fnfd);  
739          }          }
740    
741            close(fd);
742            xfree(tmppath);
743            xfree(path);
744  }  }
 #endif  

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

  ViewVC Help
Powered by ViewVC 1.1.26