Date: Thu, 27 Jan 2000 00:30:05 -0500 (EST) From: Mike Heffner <mheffner@mailandnews.com> To: FreeBSD-audit <FreeBSD-audit@freebsd.org>, Mike Haertel <mike@gnu.ai.mit.edu> Subject: use mkstemp(3) for sort Message-ID: <XFMail.20000127003005.mheffner@mailandnews.com>
next in thread | raw e-mail | index | archive | help
This patch uses mkstemp(3), instead of a pid + sequence number(usually zero), for a tempfile name...reviewers? Index: sort.c =================================================================== RCS file: /home/ncvs/src/gnu/usr.bin/sort/sort.c,v retrieving revision 1.15 diff -u -r1.15 sort.c --- sort.c 1999/04/25 22:14:05 1.15 +++ sort.c 2000/01/27 05:17:26 @@ -276,6 +276,7 @@ static struct tempnode { char *name; + int fd; struct tempnode *next; } temphead; @@ -336,9 +337,15 @@ xtmpfopen (const char *file) { FILE *fp; - int fd; + int fd=-1; + struct tempnode *temp; - fd = open (file, O_EXCL | O_WRONLY | O_CREAT | O_TRUNC, 0600); + /* find the fd for file */ + for(temp = &temphead; temp->next; temp = temp->next) + if( strcmp(temp->next->name, file) == 0 ) + break; + if( temp->next ) + fd = temp->next->fd; if (fd < 0 || (fp = fdopen (fd, "w")) == NULL) { error (0, errno, "%s", file); @@ -418,23 +425,16 @@ static char * tempname (void) { - static unsigned int seq; int len = strlen (temp_file_prefix); - char *name = xmalloc (len + 1 + sizeof ("sort") - 1 + 5 + 5 + 1); + char *name; struct tempnode *node; node = (struct tempnode *) xmalloc (sizeof (struct tempnode)); - sprintf (name, - "%s%ssort%5.5d%5.5d", - temp_file_prefix, - (len && temp_file_prefix[len - 1] != '/') ? "/" : "", - (unsigned int) getpid () & 0xffff, seq); - - /* Make sure that SEQ's value fits in 5 digits. */ - ++seq; - if (seq >= 100000) - seq = 0; - + asprintf(&name, + "%s%ssortXXXXXXXXXX", + temp_file_prefix, + (len && temp_file_prefix[len - 1] != '/') ? "/" : ""); + node->fd = mkstemp(name); node->name = name; node->next = temphead.next; temphead.next = node; --------------------------------- Mike Heffner <spock@techfour.net> Fredericksburg, VA ICQ# 882073 Date: 27-Jan-2000 Time: 00:20:59 --------------------------------- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.20000127003005.mheffner>