/[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

Annotation of /sourceforge.net/branches/seamlessrdp-branch/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 444 - (hide annotations)
Sun Aug 17 07:31:36 2003 UTC (20 years, 9 months ago) by astrand
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 14854 byte(s)
Applied patch from Mike Dawson (slightly modified)
for handling numeric IPv6 addresses.

1 forsberg 350 /* -*- c-basic-offset: 8 -*-
2 matty 10 rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions
4 matthewc 304 Copyright (C) Matthew Chapman 1999-2003
5 jsorg71 100
6 matty 10 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
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 jsorg71 100
11 matty 10 This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     GNU General Public License for more details.
15 jsorg71 100
16 matty 10 You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19     */
20    
21 matty 30 #include <stdarg.h> /* va_list va_start va_end */
22 matty 24 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
23     #include <fcntl.h> /* open */
24     #include <pwd.h> /* getpwuid */
25 matthewc 211 #include <termios.h> /* tcgetattr tcsetattr */
26 matty 24 #include <sys/stat.h> /* stat */
27     #include <sys/time.h> /* gettimeofday */
28     #include <sys/times.h> /* times */
29 astrand 325 #include <errno.h>
30 matty 10 #include "rdesktop.h"
31    
32 matthewc 220 #ifdef EGD_SOCKET
33     #include <sys/socket.h> /* socket connect */
34     #include <sys/un.h> /* sockaddr_un */
35     #endif
36    
37     #ifdef WITH_OPENSSL
38     #include <openssl/md5.h>
39     #else
40     #include "crypto/md5.h"
41     #endif
42    
43 astrand 107 char title[32] = "";
44 jsorg71 437 char g_username[16];
45 matty 10 char hostname[16];
46 matthewc 38 char keymapname[16];
47 astrand 66 int keylayout = 0x409; /* Defaults to US keyboard layout */
48 jsorg71 309 int width = 800; /* If width or height are reset to zero, the geometry will
49 astrand 263 be fetched from _NET_WORKAREA */
50 matthewc 160 int height = 600;
51 jsorg71 58 int tcp_port_rdp = TCP_PORT_RDP;
52 jsorg71 438 int g_server_bpp = 8;
53 astrand 328 int win_button_size = 0; /* If zero, disable single app mode */
54 jsorg71 437 BOOL g_bitmap_compression = True;
55 matty 29 BOOL sendmotion = True;
56 jsorg71 437 BOOL g_orders = True;
57     BOOL g_encryption = True;
58 forsberg 427 BOOL packet_encryption = True;
59 jsorg71 438 BOOL g_desktop_save = True;
60 matty 28 BOOL fullscreen = False;
61 astrand 76 BOOL grab_keyboard = True;
62 astrand 262 BOOL hide_decorations = False;
63 jsorg71 438 BOOL g_use_rdp5 = False;
64 n-ki 279 extern BOOL owncolmap;
65 matty 10
66 astrand 333 #ifdef RDP2VNC
67     extern int rfb_port;
68     extern int defer_time;
69     void
70     rdp2vnc_connect(char *server, uint32 flags, char *domain, char *password,
71     char *shell, char *directory);
72     #endif
73 matty 10 /* Display usage information */
74 matty 25 static void
75     usage(char *program)
76 matty 10 {
77 matthewc 122 fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
78 matthewc 304 fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");
79 matthewc 122 fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
80    
81 matthewc 222 fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
82 astrand 333 #ifdef RDP2VNC
83     fprintf(stderr, " -V: vnc port\n");
84     fprintf(stderr, " -E: defer time (ms)\n");
85     #endif
86 astrand 111 fprintf(stderr, " -u: user name\n");
87     fprintf(stderr, " -d: domain\n");
88     fprintf(stderr, " -s: shell\n");
89 astrand 329 fprintf(stderr, " -S: caption button size (single application mode)\n");
90 astrand 111 fprintf(stderr, " -c: working directory\n");
91 matthewc 211 fprintf(stderr, " -p: password (- to prompt)\n");
92 astrand 111 fprintf(stderr, " -n: client hostname\n");
93 matthewc 223 fprintf(stderr, " -k: keyboard layout on terminal server (us,sv,gr,etc.)\n");
94 astrand 111 fprintf(stderr, " -g: desktop geometry (WxH)\n");
95     fprintf(stderr, " -f: full-screen mode\n");
96     fprintf(stderr, " -b: force bitmap updates\n");
97     fprintf(stderr, " -e: disable encryption (French TS)\n");
98 forsberg 427 fprintf(stderr, " -E: disable encryption of everything but the logon packet\n");
99 astrand 111 fprintf(stderr, " -m: do not send motion events\n");
100 n-ki 279 fprintf(stderr, " -C: use private colour map\n");
101 astrand 111 fprintf(stderr, " -K: keep window manager key bindings\n");
102 matthewc 223 fprintf(stderr, " -T: window title\n");
103 astrand 262 fprintf(stderr, " -D: hide window manager decorations\n");
104 jsorg71 309 fprintf(stderr, " -a: server bpp\n");
105 forsberg 350 fprintf(stderr, " -5: Use RDP5 (EXPERIMENTAL!)\n");
106 matty 10 }
107    
108 matthewc 211 static BOOL
109     read_password(char *password, int size)
110     {
111     struct termios tios;
112     BOOL ret = False;
113     int istty = 0;
114     char *p;
115    
116     if (tcgetattr(STDIN_FILENO, &tios) == 0)
117     {
118     fprintf(stderr, "Password: ");
119     tios.c_lflag &= ~ECHO;
120     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
121     istty = 1;
122     }
123    
124     if (fgets(password, size, stdin) != NULL)
125     {
126     ret = True;
127    
128     /* strip final newline */
129     p = strchr(password, '\n');
130     if (p != NULL)
131     *p = 0;
132     }
133    
134     if (istty)
135     {
136     tios.c_lflag |= ECHO;
137     tcsetattr(STDIN_FILENO, TCSANOW, &tios);
138     fprintf(stderr, "\n");
139     }
140    
141     return ret;
142     }
143    
144 astrand 444 static void
145     parse_server_and_port(char *server)
146     {
147     char *p;
148     #ifdef IPv6
149     int addr_colons;
150     #endif
151    
152     #ifdef IPv6
153     p = server;
154     addr_colons = 0;
155     while (*p)
156     if (*p++ == ':')
157     addr_colons++;
158     if (addr_colons >= 2)
159     {
160     /* numeric IPv6 style address format - [1:2:3::4]:port */
161     p = strchr(server, ']');
162     if (*server == '[' && p != NULL)
163     {
164     if (*(p + 1) == ':' && *(p + 2) != '\0')
165     tcp_port_rdp = strtol(p + 2, NULL, 10);
166     /* remove the port number and brackets from the address */
167     *p = '\0';
168     strncpy(server, server + 1, strlen(server));
169     }
170     }
171     else
172     {
173     /* dns name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
174     p = strchr(server, ':');
175     if (p != NULL)
176     {
177     tcp_port_rdp = strtol(p + 1, NULL, 10);
178     *p = 0;
179     }
180     }
181     #else /* no IPv6 support */
182     p = strchr(server, ':');
183     if (p != NULL)
184     {
185     tcp_port_rdp = strtol(p + 1, NULL, 10);
186     *p = 0;
187     }
188     #endif /* IPv6 */
189    
190     }
191    
192 matty 10 /* Client program */
193 matty 25 int
194     main(int argc, char *argv[])
195 matty 10 {
196 matthewc 222 char server[64];
197 matty 30 char fullhostname[64];
198 matty 21 char domain[16];
199     char password[16];
200 astrand 238 char shell[128];
201 matty 21 char directory[32];
202 forsberg 424 BOOL prompt_password, rdp_retval = False;
203 matty 30 struct passwd *pw;
204     uint32 flags;
205 matthewc 222 char *p;
206 matty 10 int c;
207 astrand 289 int username_option = 0;
208 matty 10
209 matty 21 flags = RDP_LOGON_NORMAL;
210 matthewc 211 prompt_password = False;
211 matty 21 domain[0] = password[0] = shell[0] = directory[0] = 0;
212 matthewc 240 strcpy(keymapname, "en-us");
213 matty 21
214 astrand 333 #ifdef RDP2VNC
215     #define VNCOPT "V:E:"
216     #else
217     #define VNCOPT
218     #endif
219    
220 forsberg 427 while ((c = getopt(argc, argv, VNCOPT "u:d:s:S:c:p:n:k:g:a:fbeEmCKT:Dh?54")) != -1)
221 matty 10 {
222     switch (c)
223     {
224 astrand 333 #ifdef RDP2VNC
225     case 'V':
226     rfb_port = strtol(optarg, NULL, 10);
227     if (rfb_port < 100)
228     rfb_port += 5900;
229     break;
230    
231     case 'E':
232     defer_time = strtol(optarg, NULL, 10);
233     if (defer_time < 0)
234     defer_time = 0;
235     break;
236     #endif
237    
238 matty 10 case 'u':
239 jsorg71 437 STRNCPY(g_username, optarg, sizeof(g_username));
240 astrand 289 username_option = 1;
241 matty 10 break;
242    
243 matty 21 case 'd':
244 matty 30 STRNCPY(domain, optarg, sizeof(domain));
245 matty 21 break;
246    
247     case 's':
248 matty 30 STRNCPY(shell, optarg, sizeof(shell));
249 matty 21 break;
250    
251 astrand 328 case 'S':
252     if (!strcmp(optarg, "standard"))
253     {
254     win_button_size = 18;
255     break;
256     }
257    
258     win_button_size = strtol(optarg, &p, 10);
259    
260     if (*p)
261     {
262     error("invalid button size\n");
263     return 1;
264     }
265    
266     break;
267    
268 matty 21 case 'c':
269 matty 30 STRNCPY(directory, optarg, sizeof(directory));
270 matty 21 break;
271    
272 matty 30 case 'p':
273 matthewc 211 if ((optarg[0] == '-') && (optarg[1] == 0))
274     {
275     prompt_password = True;
276     break;
277     }
278    
279 matty 30 STRNCPY(password, optarg, sizeof(password));
280     flags |= RDP_LOGON_AUTO;
281 matthewc 211
282     /* try to overwrite argument so it won't appear in ps */
283 n-ki 171 p = optarg;
284     while (*p)
285     *(p++) = 'X';
286 matty 30 break;
287    
288 matty 10 case 'n':
289 matty 30 STRNCPY(hostname, optarg, sizeof(hostname));
290 matty 10 break;
291    
292     case 'k':
293 astrand 82 STRNCPY(keymapname, optarg, sizeof(keymapname));
294 matty 10 break;
295    
296 matty 30 case 'g':
297 astrand 263 if (!strcmp(optarg, "workarea"))
298     {
299     width = height = 0;
300     break;
301     }
302    
303 matty 30 width = strtol(optarg, &p, 10);
304     if (*p == 'x')
305 astrand 64 height = strtol(p + 1, NULL, 10);
306 matty 30
307     if ((width == 0) || (height == 0))
308     {
309     error("invalid geometry\n");
310     return 1;
311     }
312 matty 10 break;
313    
314 matty 30 case 'f':
315     fullscreen = True;
316     break;
317    
318 matty 10 case 'b':
319 jsorg71 437 g_orders = False;
320 matty 10 break;
321    
322 matty 30 case 'e':
323 jsorg71 437 g_encryption = False;
324 matty 10 break;
325 astrand 435 case 'E':
326 forsberg 427 packet_encryption = False;
327     break;
328 matty 30 case 'm':
329     sendmotion = False;
330 matty 28 break;
331 matty 29
332 n-ki 279 case 'C':
333     owncolmap = True;
334     break;
335    
336 astrand 76 case 'K':
337     grab_keyboard = False;
338     break;
339    
340 matthewc 223 case 'T':
341 matthewc 222 STRNCPY(title, optarg, sizeof(title));
342 astrand 107 break;
343    
344 astrand 262 case 'D':
345     hide_decorations = True;
346     break;
347    
348 jsorg71 309 case 'a':
349 jsorg71 438 g_server_bpp = strtol(optarg, NULL, 10);
350     if (g_server_bpp != 8 && g_server_bpp != 16 && g_server_bpp != 15
351     && g_server_bpp != 24)
352 jsorg71 309 {
353     error("invalid server bpp\n");
354     return 1;
355     }
356     break;
357    
358 forsberg 350 case '5':
359 jsorg71 438 g_use_rdp5 = True;
360 forsberg 350 break;
361 matty 28 case 'h':
362 matty 10 case '?':
363     default:
364     usage(argv[0]);
365     return 1;
366     }
367     }
368    
369     if (argc - optind < 1)
370     {
371     usage(argv[0]);
372     return 1;
373     }
374    
375 matthewc 222 STRNCPY(server, argv[optind], sizeof(server));
376 astrand 444 parse_server_and_port(server);
377 matty 10
378 astrand 289 if (!username_option)
379 matty 10 {
380     pw = getpwuid(getuid());
381     if ((pw == NULL) || (pw->pw_name == NULL))
382     {
383 matty 30 error("could not determine username, use -u\n");
384 matty 10 return 1;
385     }
386    
387 jsorg71 437 STRNCPY(g_username, pw->pw_name, sizeof(g_username));
388 matty 10 }
389    
390     if (hostname[0] == 0)
391     {
392 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
393 matty 10 {
394 matty 30 error("could not determine local hostname, use -n\n");
395 matty 10 return 1;
396     }
397 matty 30
398     p = strchr(fullhostname, '.');
399     if (p != NULL)
400     *p = 0;
401    
402     STRNCPY(hostname, fullhostname, sizeof(hostname));
403 matty 10 }
404    
405 matthewc 211 if (prompt_password && read_password(password, sizeof(password)))
406     flags |= RDP_LOGON_AUTO;
407 matty 30
408 matthewc 211 if (title[0] == 0)
409 astrand 107 {
410     strcpy(title, "rdesktop - ");
411     strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
412     }
413 matty 12
414 astrand 333 #ifdef RDP2VNC
415     rdp2vnc_connect(server, flags, domain, password, shell, directory);
416 forsberg 424 return 0;
417 astrand 333 #else
418    
419 astrand 82 if (!ui_init())
420     return 1;
421 astrand 66
422 matthewc 432 /* rdpsnd_init(); */
423     /* rdpdr_init(); */
424 forsberg 416
425 matthewc 53 if (!rdp_connect(server, flags, domain, password, shell, directory))
426     return 1;
427    
428 forsberg 427 /* By setting encryption to False here, we have an encrypted login
429     packet but unencrypted transfer of other packets */
430 astrand 435 if (!packet_encryption)
431 jsorg71 437 g_encryption = False;
432 forsberg 427
433    
434 matthewc 122 DEBUG(("Connection successful.\n"));
435 matthewc 211 memset(password, 0, sizeof(password));
436 matthewc 53
437 jsorg71 100 if (ui_create_window())
438 matty 10 {
439 forsberg 424 rdp_retval = rdp_main_loop();
440 matty 10 ui_destroy_window();
441     }
442    
443 matthewc 122 DEBUG(("Disconnecting...\n"));
444 matthewc 53 rdp_disconnect();
445 matthewc 188 ui_deinit();
446 astrand 333
447 astrand 435 if (True == rdp_retval)
448 forsberg 424 return 0;
449     else
450     return 2;
451    
452 astrand 333 #endif
453    
454 matty 10 }
455    
456 matthewc 220 #ifdef EGD_SOCKET
457     /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
458     static BOOL
459     generate_random_egd(uint8 * buf)
460     {
461     struct sockaddr_un addr;
462     BOOL ret = False;
463     int fd;
464    
465     fd = socket(AF_UNIX, SOCK_STREAM, 0);
466     if (fd == -1)
467     return False;
468    
469     addr.sun_family = AF_UNIX;
470     memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
471 astrand 259 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
472 matthewc 220 goto err;
473    
474     /* PRNGD and EGD use a simple communications protocol */
475 astrand 259 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
476     buf[1] = 32; /* Number of requested random bytes */
477 matthewc 220 if (write(fd, buf, 2) != 2)
478     goto err;
479    
480 astrand 259 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
481 matthewc 220 goto err;
482    
483     if (read(fd, buf, 32) != 32)
484     goto err;
485    
486     ret = True;
487    
488 astrand 259 err:
489 matthewc 220 close(fd);
490     return ret;
491     }
492     #endif
493    
494 matty 10 /* Generate a 32-byte random for the secure transport code. */
495 matty 25 void
496 astrand 64 generate_random(uint8 * random)
497 matty 10 {
498     struct stat st;
499 matty 22 struct tms tmsbuf;
500 matthewc 220 MD5_CTX md5;
501     uint32 *r;
502     int fd, n;
503 matty 10
504 matthewc 220 /* If we have a kernel random device, try that first */
505 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
506     || ((fd = open("/dev/random", O_RDONLY)) != -1))
507 matty 10 {
508 matthewc 220 n = read(fd, random, 32);
509 matty 10 close(fd);
510 matthewc 220 if (n == 32)
511     return;
512 matty 10 }
513    
514 matthewc 220 #ifdef EGD_SOCKET
515     /* As a second preference use an EGD */
516     if (generate_random_egd(random))
517     return;
518     #endif
519    
520 matty 10 /* Otherwise use whatever entropy we can gather - ideas welcome. */
521 astrand 259 r = (uint32 *) random;
522 matty 10 r[0] = (getpid()) | (getppid() << 16);
523     r[1] = (getuid()) | (getgid() << 16);
524 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
525     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
526 matty 10 stat("/tmp", &st);
527     r[5] = st.st_atime;
528     r[6] = st.st_mtime;
529     r[7] = st.st_ctime;
530 matthewc 220
531     /* Hash both halves with MD5 to obscure possible patterns */
532     MD5_Init(&md5);
533 astrand 259 MD5_Update(&md5, random, 16);
534 matthewc 220 MD5_Final(random, &md5);
535 astrand 259 MD5_Update(&md5, random + 16, 16);
536     MD5_Final(random + 16, &md5);
537 matty 10 }
538    
539     /* malloc; exit if out of memory */
540 matty 25 void *
541     xmalloc(int size)
542 matty 10 {
543     void *mem = malloc(size);
544     if (mem == NULL)
545     {
546 matty 30 error("xmalloc %d\n", size);
547 matty 10 exit(1);
548     }
549     return mem;
550     }
551    
552     /* realloc; exit if out of memory */
553 matty 25 void *
554     xrealloc(void *oldmem, int size)
555 matty 10 {
556     void *mem = realloc(oldmem, size);
557     if (mem == NULL)
558     {
559 matty 30 error("xrealloc %d\n", size);
560 matty 10 exit(1);
561     }
562     return mem;
563     }
564    
565     /* free */
566 matty 25 void
567     xfree(void *mem)
568 matty 10 {
569     free(mem);
570     }
571    
572 matty 30 /* report an error */
573 matty 25 void
574 matty 30 error(char *format, ...)
575     {
576     va_list ap;
577    
578     fprintf(stderr, "ERROR: ");
579    
580     va_start(ap, format);
581     vfprintf(stderr, format, ap);
582     va_end(ap);
583     }
584    
585 matthewc 297 /* report a warning */
586     void
587     warning(char *format, ...)
588     {
589     va_list ap;
590    
591     fprintf(stderr, "WARNING: ");
592    
593     va_start(ap, format);
594     vfprintf(stderr, format, ap);
595     va_end(ap);
596     }
597    
598 matty 30 /* report an unimplemented protocol feature */
599     void
600     unimpl(char *format, ...)
601     {
602     va_list ap;
603    
604     fprintf(stderr, "NOT IMPLEMENTED: ");
605    
606     va_start(ap, format);
607     vfprintf(stderr, format, ap);
608     va_end(ap);
609     }
610    
611     /* produce a hex dump */
612     void
613 jsorg71 376 hexdump(unsigned char *p, int len)
614 matty 10 {
615     unsigned char *line = p;
616 jsorg71 376 int i, thisline, offset = 0;
617 matty 10
618     while (offset < len)
619     {
620 matthewc 169 printf("%04x ", offset);
621 matty 10 thisline = len - offset;
622     if (thisline > 16)
623     thisline = 16;
624    
625     for (i = 0; i < thisline; i++)
626 matthewc 169 printf("%02x ", line[i]);
627 matty 10
628 matty 30 for (; i < 16; i++)
629 matthewc 169 printf(" ");
630 matty 30
631 matty 10 for (i = 0; i < thisline; i++)
632 matthewc 169 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
633 matty 10
634 matthewc 169 printf("\n");
635 matty 10 offset += thisline;
636     line += thisline;
637     }
638     }
639 astrand 325
640    
641     int
642     load_licence(unsigned char **data)
643     {
644 matthewc 367 char *home, *path;
645 astrand 325 struct stat st;
646 matthewc 367 int fd, length;
647 astrand 325
648     home = getenv("HOME");
649     if (home == NULL)
650     return -1;
651    
652 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
653 astrand 325 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
654    
655     fd = open(path, O_RDONLY);
656     if (fd == -1)
657     return -1;
658    
659     if (fstat(fd, &st))
660     return -1;
661    
662 forsberg 416 *data = (uint8 *) xmalloc(st.st_size);
663 matthewc 367 length = read(fd, *data, st.st_size);
664     close(fd);
665     xfree(path);
666     return length;
667 astrand 325 }
668    
669     void
670     save_licence(unsigned char *data, int length)
671     {
672 matthewc 367 char *home, *path, *tmppath;
673     int fd;
674 astrand 325
675     home = getenv("HOME");
676     if (home == NULL)
677     return;
678    
679 forsberg 416 path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));
680 astrand 325
681 matthewc 367 sprintf(path, "%s/.rdesktop", home);
682     if ((mkdir(path, 0700) == -1) && errno != EEXIST)
683 astrand 325 {
684 matthewc 367 perror(path);
685     return;
686 astrand 325 }
687    
688 matthewc 367 /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
689 astrand 325
690 matthewc 367 sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
691 forsberg 416 tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
692 matthewc 367 strcpy(tmppath, path);
693     strcat(tmppath, ".new");
694    
695 forsberg 416 fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
696 matthewc 367 if (fd == -1)
697 astrand 325 {
698 matthewc 367 perror(tmppath);
699     return;
700 astrand 325 }
701    
702 matthewc 367 if (write(fd, data, length) != length)
703 astrand 325 {
704 matthewc 367 perror(tmppath);
705     unlink(tmppath);
706 astrand 325 }
707 matthewc 367 else if (rename(tmppath, path) == -1)
708 astrand 325 {
709 matthewc 367 perror(path);
710     unlink(tmppath);
711 astrand 325 }
712    
713 matthewc 367 close(fd);
714     xfree(tmppath);
715     xfree(path);
716 astrand 325 }

  ViewVC Help
Powered by ViewVC 1.1.26