Commit 2a4860b9 2a4860b9fd39c563de2eac81bc91fe518b6f3fcc by Sergey Poznyakoff

Added extra argument to util_find_entry(): a pointer to the table where

to look up. Added util_error(), util_help(), util_tempfile().
1 parent 727d451f
Showing 1 changed file with 166 additions and 182 deletions
...@@ -179,8 +179,8 @@ util_expand_msglist (const int argc, char **argv, int **list) ...@@ -179,8 +179,8 @@ util_expand_msglist (const int argc, char **argv, int **list)
179 pattern[k] = toupper ((int)pattern[k]); 179 pattern[k] = toupper ((int)pattern[k]);
180 180
181 if (pattern && subject && strstr (subject, pattern)) 181 if (pattern && subject && strstr (subject, pattern))
182 { 182 {
183 current = util_ll_add (current, j); 183 current = util_ll_add (current, j);
184 } 184 }
185 185
186 free (pattern); 186 free (pattern);
...@@ -242,7 +242,7 @@ util_expand_msglist (const int argc, char **argv, int **list) ...@@ -242,7 +242,7 @@ util_expand_msglist (const int argc, char **argv, int **list)
242 ret = calloc (1, sizeof (int)); 242 ret = calloc (1, sizeof (int));
243 if (!ret) 243 if (!ret)
244 { 244 {
245 fprintf (ofile, "not enough memory\n"); 245 util_error("not enough memory");
246 exit (1); 246 exit (1);
247 } 247 }
248 ret [0] = cursor; 248 ret [0] = cursor;
...@@ -253,7 +253,7 @@ util_expand_msglist (const int argc, char **argv, int **list) ...@@ -253,7 +253,7 @@ util_expand_msglist (const int argc, char **argv, int **list)
253 ret = malloc (lc * sizeof (int)); 253 ret = malloc (lc * sizeof (int));
254 if (!ret) 254 if (!ret)
255 { 255 {
256 fprintf (ofile, "not enough memory\n"); 256 util_error("not enough memory");
257 exit (1); 257 exit (1);
258 } 258 }
259 lc = 0; 259 lc = 0;
...@@ -279,14 +279,14 @@ util_do_command (const char *c, ...) ...@@ -279,14 +279,14 @@ util_do_command (const char *c, ...)
279 function_t *command; 279 function_t *command;
280 char *cmd = NULL; 280 char *cmd = NULL;
281 va_list ap; 281 va_list ap;
282 int i, zcnt = 0; 282 static const char *delim = "=";
283 283
284 va_start (ap, c); 284 va_start (ap, c);
285 status = vasprintf (&cmd, c, ap); 285 status = vasprintf (&cmd, c, ap);
286 va_end (ap); 286 va_end (ap);
287 if (status < 0) 287 if (status < 0)
288 return 0; 288 return 0;
289 289
290 if (cmd) 290 if (cmd)
291 { 291 {
292 struct mail_command_entry entry; 292 struct mail_command_entry entry;
...@@ -294,34 +294,12 @@ util_do_command (const char *c, ...) ...@@ -294,34 +294,12 @@ util_do_command (const char *c, ...)
294 if (cmd[0] == '#') 294 if (cmd[0] == '#')
295 return 0; 295 return 0;
296 296
297 if (argcv_get (cmd, &argc, &argv) != 0) 297 if (argcv_get (cmd, delim, &argc, &argv) != 0)
298 return argcv_free (argc, argv); 298 return argcv_free (argc, argv);
299 299
300 /* Eliminate empty strings */ 300 entry = util_find_entry (mail_command_table, argv[0]);
301 for (i = 0; i < argc; i++)
302 {
303 if (argv[i][0] == 0)
304 {
305 int d;
306 for (d = i; d < argc && argv[d][0] == 0; d++)
307 ;
308 if (d == argc)
309 {
310 break;
311 }
312 else
313 {
314 char *s = argv[d];
315 argv[d] = argv[i];
316 argv[i] = s;
317 }
318 zcnt++;
319 }
320 }
321
322 entry = util_find_entry (argv[0]);
323 301
324 if (if_cond() == 0 && entry.isflow == 0) 302 if (if_cond() == 0 && (entry.flags & EF_FLOW) == 0)
325 { 303 {
326 argcv_free (argc, argv); 304 argcv_free (argc, argv);
327 return 0; 305 return 0;
...@@ -332,10 +310,10 @@ util_do_command (const char *c, ...) ...@@ -332,10 +310,10 @@ util_do_command (const char *c, ...)
332 command = util_command_get ("quit"); 310 command = util_command_get ("quit");
333 311
334 if (command != NULL) 312 if (command != NULL)
335 status = command (argc - zcnt, argv); 313 status = command (argc, argv);
336 else 314 else
337 { 315 {
338 fprintf (ofile, "Unknown command: %s\n", argv[0]); 316 util_error("Unknown command: %s", argv[0]);
339 status = 1; 317 status = 1;
340 } 318 }
341 319
...@@ -348,7 +326,7 @@ util_do_command (const char *c, ...) ...@@ -348,7 +326,7 @@ util_do_command (const char *c, ...)
348 * func is the function to run 326 * func is the function to run
349 * argc is the number of arguments inculding the command and msglist 327 * argc is the number of arguments inculding the command and msglist
350 * argv is the list of strings containing the command and msglist 328 * argv is the list of strings containing the command and msglist
351 * set_cursor means whether the function should set the cursor to 329 * set_cursor means whether the function should set the cursor to
352 * the number of the last message processed. If set_cursor = 0, the 330 * the number of the last message processed. If set_cursor = 0, the
353 * cursor is not altered. 331 * cursor is not altered.
354 */ 332 */
...@@ -383,7 +361,7 @@ util_msglist_command (function_t *func, int argc, char **argv, int set_cursor) ...@@ -383,7 +361,7 @@ util_msglist_command (function_t *func, int argc, char **argv, int set_cursor)
383 function_t * 361 function_t *
384 util_command_get (char *cmd) 362 util_command_get (char *cmd)
385 { 363 {
386 struct mail_command_entry entry = util_find_entry (cmd); 364 struct mail_command_entry entry = util_find_entry (mail_command_table, cmd);
387 return entry.func; 365 return entry.func;
388 } 366 }
389 367
...@@ -391,24 +369,24 @@ util_command_get (char *cmd) ...@@ -391,24 +369,24 @@ util_command_get (char *cmd)
391 * returns the mail_command_entry structure for the command matching cmd 369 * returns the mail_command_entry structure for the command matching cmd
392 */ 370 */
393 struct mail_command_entry 371 struct mail_command_entry
394 util_find_entry (char *cmd) 372 util_find_entry (const struct mail_command_entry *table, char *cmd)
395 { 373 {
396 int i = 0, ll = 0, sl = 0; 374 int i = 0, ll = 0, sl = 0;
397 int len = strlen (cmd); 375 int len = strlen (cmd);
398 376
399 while (mail_command_table[i].shortname != 0) 377 while (table[i].shortname != 0)
400 { 378 {
401 sl = strlen (mail_command_table[i].shortname); 379 sl = strlen (table[i].shortname);
402 ll = strlen (mail_command_table[i].longname); 380 ll = strlen (table[i].longname);
403 if (sl > ll && !strncmp (mail_command_table[i].shortname, cmd, sl)) 381 if (sl > ll && !strncmp (table[i].shortname, cmd, sl))
404 return mail_command_table[i]; 382 return table[i];
405 else if (sl == len && !strcmp (mail_command_table[i].shortname, cmd)) 383 else if (sl == len && !strcmp (table[i].shortname, cmd))
406 return mail_command_table[i]; 384 return table[i];
407 else if (sl < len && !strncmp (mail_command_table[i].longname, cmd, len)) 385 else if (sl < len && !strncmp (table[i].longname, cmd, len))
408 return mail_command_table[i]; 386 return table[i];
409 i++; 387 i++;
410 } 388 }
411 return mail_command_table[i]; 389 return table[i];
412 } 390 }
413 391
414 /* 392 /*
...@@ -452,7 +430,7 @@ util_screen_lines() ...@@ -452,7 +430,7 @@ util_screen_lines()
452 { 430 {
453 struct mail_env_entry *ep = util_find_env("screen"); 431 struct mail_env_entry *ep = util_find_env("screen");
454 size_t n; 432 size_t n;
455 433
456 if (ep && ep->set && (n = atoi(ep->value)) != 0) 434 if (ep && ep->set && (n = atoi(ep->value)) != 0)
457 return n; 435 return n;
458 return util_getlines(); 436 return util_getlines();
...@@ -523,7 +501,7 @@ util_printenv (int set) ...@@ -523,7 +501,7 @@ util_printenv (int set)
523 { 501 {
524 fprintf (ofile, "%s", env_cursor->env_entry.var); 502 fprintf (ofile, "%s", env_cursor->env_entry.var);
525 if (env_cursor->env_entry.value != NULL) 503 if (env_cursor->env_entry.value != NULL)
526 fprintf (ofile, "=%s", env_cursor->env_entry.value); 504 fprintf (ofile, "=\"%s\"", env_cursor->env_entry.value);
527 fprintf (ofile, "\n"); 505 fprintf (ofile, "\n");
528 } 506 }
529 } 507 }
...@@ -546,101 +524,6 @@ util_isdeleted (int n) ...@@ -546,101 +524,6 @@ util_isdeleted (int n)
546 return 0; 524 return 0;
547 } 525 }
548 526
549 /*
550 * readline tab completion
551 */
552 #ifdef WITH_READLINE
553 char **
554 util_command_completion (char *cmd, int start, int end)
555 {
556 if (start == 0)
557 return completion_matches (cmd, util_command_generator);
558 return NULL;
559 }
560
561 /*
562 * more readline
563 */
564 char *
565 util_command_generator (char *text, int state)
566 {
567 static int i, len;
568 char *name;
569
570 if (!state)
571 {
572 i = 0;
573 len = strlen (text);
574 }
575
576 while ((name = mail_command_table[i].longname))
577 {
578 if (strlen (mail_command_table[i].shortname) > strlen(name))
579 name = mail_command_table[i].shortname;
580 i++;
581 if (strncmp (name, text, len) == 0)
582 return (strdup(name));
583 }
584
585 return NULL;
586 }
587
588 #else
589
590 char *
591 readline (const char *prompt)
592 {
593 char *line;
594 char *p;
595 size_t alloclen, linelen;
596
597 if (prompt)
598 {
599 fprintf (ofile, "%s",prompt);
600 fflush (ofile);
601 }
602
603 p = line = calloc (1, 255);
604 alloclen = 255;
605 linelen = 0;
606 for (;;)
607 {
608 size_t n;
609 p = fgets (p, alloclen - linelen, stdin);
610 n = (p) ? strlen (p) : 0;
611
612 linelen += n;
613
614 /* Error. */
615 if (linelen == 0)
616 {
617 free (line);
618 return NULL;
619 }
620
621 /* Ok. */
622 if (line[linelen - 1] == '\n')
623 {
624 line[linelen - 1] = '\0';
625 return line;
626 }
627 else
628 {
629 char *tmp;
630 alloclen *= 2;
631 tmp = realloc (line, alloclen);
632 if (tmp == NULL)
633 {
634 free (line);
635 return NULL;
636 }
637 line = tmp;
638 p = line + linelen;
639 }
640 }
641 }
642 #endif
643
644 char * 527 char *
645 util_get_homedir() 528 util_get_homedir()
646 { 529 {
...@@ -648,12 +531,12 @@ util_get_homedir() ...@@ -648,12 +531,12 @@ util_get_homedir()
648 if (!homedir) 531 if (!homedir)
649 { 532 {
650 /* Shouldn't happen, but one never knows */ 533 /* Shouldn't happen, but one never knows */
651 fprintf(ofile, "can't get homedir\n"); 534 util_error("can't get homedir");
652 exit (EXIT_FAILURE); 535 exit (EXIT_FAILURE);
653 } 536 }
654 return strdup(homedir); 537 return strdup(homedir);
655 } 538 }
656 539
657 char * 540 char *
658 util_fullpath(char *inpath) 541 util_fullpath(char *inpath)
659 { 542 {
...@@ -667,7 +550,7 @@ util_get_sender(int msgno, int strip) ...@@ -667,7 +550,7 @@ util_get_sender(int msgno, int strip)
667 address_t addr = NULL; 550 address_t addr = NULL;
668 message_t msg = NULL; 551 message_t msg = NULL;
669 char buffer[512], *p; 552 char buffer[512], *p;
670 553
671 mailbox_get_message (mbox, msgno, &msg); 554 mailbox_get_message (mbox, msgno, &msg);
672 message_get_header (msg, &header); 555 message_get_header (msg, &header);
673 if (header_get_value (header, MU_HEADER_FROM, buffer, sizeof(buffer), NULL) 556 if (header_get_value (header, MU_HEADER_FROM, buffer, sizeof(buffer), NULL)
...@@ -678,25 +561,25 @@ util_get_sender(int msgno, int strip) ...@@ -678,25 +561,25 @@ util_get_sender(int msgno, int strip)
678 if (envelope_sender (env, buffer, sizeof (buffer), NULL) 561 if (envelope_sender (env, buffer, sizeof (buffer), NULL)
679 || address_create (&addr, buffer)) 562 || address_create (&addr, buffer))
680 { 563 {
681 fprintf (ofile, "can't determine sender name (msg %d)\n", msgno); 564 util_error("can't determine sender name (msg %d)", msgno);
682 return NULL; 565 return NULL;
683 } 566 }
684 } 567 }
685 568
686 if (address_get_email (addr, 1, buffer, sizeof(buffer), NULL)) 569 if (address_get_email (addr, 1, buffer, sizeof(buffer), NULL))
687 { 570 {
688 fprintf (ofile, "can't determine sender name (msg %d)\n", msgno); 571 util_error("can't determine sender name (msg %d)", msgno);
689 address_destroy (&addr); 572 address_destroy (&addr);
690 return NULL; 573 return NULL;
691 } 574 }
692 575
693 if (strip) 576 if (strip)
694 { 577 {
695 p = strchr (buffer, '@'); 578 p = strchr (buffer, '@');
696 if (p) 579 if (p)
697 *p = 0; 580 *p = 0;
698 } 581 }
699 582
700 p = strdup (buffer); 583 p = strdup (buffer);
701 address_destroy (&addr); 584 address_destroy (&addr);
702 return p; 585 return p;
...@@ -707,15 +590,15 @@ util_slist_print(list_t list, int nl) ...@@ -707,15 +590,15 @@ util_slist_print(list_t list, int nl)
707 { 590 {
708 iterator_t itr; 591 iterator_t itr;
709 char *name; 592 char *name;
710 593
711 if (!list || iterator_create (&itr, list)) 594 if (!list || iterator_create (&itr, list))
712 return; 595 return;
713 596
714 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) 597 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
715 { 598 {
716 iterator_current (itr, (void **)&name); 599 iterator_current (itr, (void **)&name);
717 fprintf (ofile, "%s%c", name, nl ? '\n' : ' '); 600 fprintf (ofile, "%s%c", name, nl ? '\n' : ' ');
718 601
719 } 602 }
720 iterator_destroy (&itr); 603 iterator_destroy (&itr);
721 } 604 }
...@@ -726,10 +609,10 @@ util_slist_lookup(list_t list, char *str) ...@@ -726,10 +609,10 @@ util_slist_lookup(list_t list, char *str)
726 iterator_t itr; 609 iterator_t itr;
727 char *name; 610 char *name;
728 int rc = 0; 611 int rc = 0;
729 612
730 if (!list || iterator_create (&itr, list)) 613 if (!list || iterator_create (&itr, list))
731 return 0; 614 return 0;
732 615
733 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) 616 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
734 { 617 {
735 iterator_current (itr, (void **)&name); 618 iterator_current (itr, (void **)&name);
...@@ -747,13 +630,13 @@ void ...@@ -747,13 +630,13 @@ void
747 util_slist_add (list_t *list, char *value) 630 util_slist_add (list_t *list, char *value)
748 { 631 {
749 char *p; 632 char *p;
750 633
751 if (!*list && list_create (list)) 634 if (!*list && list_create (list))
752 return; 635 return;
753 636
754 if ((p = strdup(value)) == NULL) 637 if ((p = strdup(value)) == NULL)
755 { 638 {
756 fprintf (ofile, "not enough memory\n"); 639 util_error("not enough memory\n");
757 return; 640 return;
758 } 641 }
759 list_append (*list, p); 642 list_append (*list, p);
...@@ -764,10 +647,10 @@ util_slist_destroy (list_t *list) ...@@ -764,10 +647,10 @@ util_slist_destroy (list_t *list)
764 { 647 {
765 iterator_t itr; 648 iterator_t itr;
766 char *name; 649 char *name;
767 650
768 if (!*list || iterator_create (&itr, *list)) 651 if (!*list || iterator_create (&itr, *list))
769 return; 652 return;
770 653
771 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) 654 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
772 { 655 {
773 iterator_current (itr, (void **)&name); 656 iterator_current (itr, (void **)&name);
...@@ -783,10 +666,10 @@ util_slist_to_string (list_t list, char *delim) ...@@ -783,10 +666,10 @@ util_slist_to_string (list_t list, char *delim)
783 iterator_t itr; 666 iterator_t itr;
784 char *name; 667 char *name;
785 char *str = NULL; 668 char *str = NULL;
786 669
787 if (!list || iterator_create (&itr, list)) 670 if (!list || iterator_create (&itr, list))
788 return NULL; 671 return NULL;
789 672
790 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr)) 673 for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
791 { 674 {
792 iterator_current (itr, (void **)&name); 675 iterator_current (itr, (void **)&name);
...@@ -816,14 +699,14 @@ util_strcat(char **dest, char *str) ...@@ -816,14 +699,14 @@ util_strcat(char **dest, char *str)
816 memcpy (newp + dlen - 1, str, slen); 699 memcpy (newp + dlen - 1, str, slen);
817 } 700 }
818 } 701 }
819 702
820 void 703 void
821 util_escape_percent (char **str) 704 util_escape_percent (char **str)
822 { 705 {
823 int count; 706 int count;
824 char *p, *q; 707 char *p, *q;
825 char *newstr; 708 char *newstr;
826 709
827 /* Count ocurrences of % in the string */ 710 /* Count ocurrences of % in the string */
828 count = 0; 711 count = 0;
829 for (p = *str; *p; p++) 712 for (p = *str; *p; p++)
...@@ -833,15 +716,15 @@ util_escape_percent (char **str) ...@@ -833,15 +716,15 @@ util_escape_percent (char **str)
833 if (!count) 716 if (!count)
834 return; /* nothing to do */ 717 return; /* nothing to do */
835 718
836 /* expand the string */ 719 /* expand the string */
837 newstr = malloc (strlen (*str) + 1 + count); 720 newstr = malloc (strlen (*str) + 1 + count);
838 if (!newstr) 721 if (!newstr)
839 { 722 {
840 fprintf (ofile, "not enough memory\n"); 723 util_error("not enough memory");
841 exit (1); /* be on the safe side */ 724 exit (1); /* be on the safe side */
842 } 725 }
843 726
844 /* and escape percent signs */ 727 /* and escape percent signs */
845 p = newstr; 728 p = newstr;
846 q = *str; 729 q = *str;
847 while (*p = *q++) 730 while (*p = *q++)
...@@ -865,7 +748,7 @@ util_outfolder_name (char *str) ...@@ -865,7 +748,7 @@ util_outfolder_name (char *str)
865 case '+': 748 case '+':
866 str = util_fullpath (str); 749 str = util_fullpath (str);
867 break; 750 break;
868 751
869 default: 752 default:
870 if (ep && ep->set) 753 if (ep && ep->set)
871 { 754 {
...@@ -881,13 +764,6 @@ util_outfolder_name (char *str) ...@@ -881,13 +764,6 @@ util_outfolder_name (char *str)
881 return str; 764 return str;
882 } 765 }
883 766
884 char *
885 util_whoami()
886 {
887 struct passwd *pw = getpwuid(getuid());
888 return pw ? pw->pw_name : "unknown";
889 }
890
891 /* Save an outgoing message. "savefile" allows to override the setting 767 /* Save an outgoing message. "savefile" allows to override the setting
892 of the "record" variable. */ 768 of the "record" variable. */
893 void 769 void
...@@ -902,24 +778,24 @@ util_save_outgoing (message_t msg, char *savefile) ...@@ -902,24 +778,24 @@ util_save_outgoing (message_t msg, char *savefile)
902 outfile = fopen (filename, "a"); 778 outfile = fopen (filename, "a");
903 if (!outfile) 779 if (!outfile)
904 { 780 {
905 fprintf (outfile, "can't open save file %s: %s", 781 util_error("can't open save file %s: %s",
906 filename, strerror (errno)); 782 filename, strerror (errno));
907 } 783 }
908 else 784 else
909 { 785 {
910 char *buf; 786 char *buf;
911 size_t bsize = 0; 787 size_t bsize = 0;
912 788
913 message_size (msg, &bsize); 789 message_size (msg, &bsize);
914 790
915 /* Try to allocate large buffer */ 791 /* Try to allocate large buffer */
916 for (; bsize > 1; bsize /= 2) 792 for (; bsize > 1; bsize /= 2)
917 if ((buf = malloc (bsize))) 793 if ((buf = malloc (bsize)))
918 break; 794 break;
919 795
920 if (!bsize) 796 if (!bsize)
921 { 797 {
922 fprintf (ofile, "not enough memory for creating save file\n"); 798 util_error("not enough memory for creating save file");
923 } 799 }
924 else 800 else
925 { 801 {
...@@ -932,7 +808,7 @@ util_save_outgoing (message_t msg, char *savefile) ...@@ -932,7 +808,7 @@ util_save_outgoing (message_t msg, char *savefile)
932 time(&t); 808 time(&t);
933 tm = gmtime(&t); 809 tm = gmtime(&t);
934 strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y%n", tm); 810 strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y%n", tm);
935 fprintf (outfile, "From %s %s\n", util_whoami(), date); 811 fprintf (outfile, "From %s %s\n", mail_whoami(), date);
936 812
937 message_get_stream (msg, &stream); 813 message_get_stream (msg, &stream);
938 while (stream_read (stream, buf, bsize, off, &n) == 0 814 while (stream_read (stream, buf, bsize, off, &n) == 0
...@@ -948,3 +824,111 @@ util_save_outgoing (message_t msg, char *savefile) ...@@ -948,3 +824,111 @@ util_save_outgoing (message_t msg, char *savefile)
948 free (filename); 824 free (filename);
949 } 825 }
950 } 826 }
827
828 #ifdef HAVE_STDARG_H
829 void
830 util_error (const char *format, ...)
831 #else
832 void
833 util_error (va_alist)
834 va_dcl
835 #endif
836 {
837 va_list ap;
838
839 #ifdef HAVE_STDARG_H
840 va_start(ap, format);
841 #else
842 char *format;
843
844 va_start(ap);
845 format = va_arg(ap, char *);
846 #endif
847
848 vfprintf(stderr, format, ap);
849 fprintf(stderr, "\n");
850
851 va_end(ap);
852 }
853
854 int
855 util_help (const struct mail_command_entry *table, char *word)
856 {
857 if (!word)
858 {
859 int i = 0;
860 FILE *out = stdout;
861
862 if ((util_find_env("crt"))->set)
863 out = popen (getenv("PAGER"), "w");
864
865 while (table[i].synopsis != 0)
866 fprintf (out, "%s\n", table[i++].synopsis);
867
868 if (out != stdout)
869 pclose (out);
870
871 return 0;
872 }
873 else
874 {
875 int status = 0;
876 struct mail_command_entry entry = util_find_entry(table, word);
877 if (entry.synopsis != NULL)
878 fprintf (stdout, "%s\n", entry.synopsis);
879 else
880 {
881 status = 1;
882 fprintf (stdout, "Unknown command: %s\n", word);
883 }
884 return status;
885 }
886 return 1;
887 }
888
889 int
890 util_tempfile(char **namep)
891 {
892 char *filename;
893 char *tmpdir;
894 int fd;
895
896 /* We have to be extra careful about opening temporary files, since we
897 may be running with extra privilege i.e setgid(). */
898 tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : "/tmp";
899
900 filename = malloc (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
901 if (!filename)
902 return -1;
903 sprintf (filename, "%s/muXXXXXX", tmpdir);
904
905 #ifdef HAVE_MKSTEMP
906 {
907 int save_mask = umask(077);
908 fd = mkstemp (filename);
909 umask(save_mask);
910 }
911 #else
912 if (mktemp (filename))
913 fd = open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
914 else
915 fd = -1;
916 #endif
917
918 if (fd == -1)
919 {
920 util_error("Can not open temporary file: %s", strerror(errno));
921 free(filename);
922 return -1;
923 }
924
925 if (namep)
926 *namep = filename;
927 else
928 {
929 unlink(filename);
930 free(filename);
931 }
932
933 return fd;
934 }
......