]> err.no Git - moreutils/commitdiff
move code into a function
authorJoey Hess <joey@kodama.kitenet.net>
Fri, 11 Apr 2008 20:15:29 +0000 (16:15 -0400)
committerJoey Hess <joey@kodama.kitenet.net>
Fri, 11 Apr 2008 20:15:29 +0000 (16:15 -0400)
sponge.c

index 948edf0cc70ef5295e9b8fb5b27a724f9b9535c7..df1b7a15e88a481d262b3e31d0aeea8e56267499 100644 (file)
--- a/sponge.c
+++ b/sponge.c
@@ -218,6 +218,29 @@ static void copy_tmpfile(FILE *tmpfile, FILE *outfd) {
        fclose(outfd);
 }
 
+FILE *open_tmpfile(void) {
+       /* umask(077); FIXME: Should we be setting umask, or using default?  */
+       struct cs_status cs;
+       int tmpfd;
+       FILE *tmpfile;
+
+       trapsignals();
+       cs = cs_enter();
+       tmpfd = mkstemp(tmpname);
+       atexit(onexit_cleanup); // solaris on_exit(onexit_cleanup, 0);
+       cs_leave(cs);
+       if (tmpfd < 0) {
+               perror("mkstemp failed");
+               exit(1);
+       }
+       tmpfile = fdopen(tmpfd, "w+");
+       if (! tmpfile) {
+               perror("fdopen");
+               exit(1);
+       }
+       return tmpfile;
+}
+
 int main (int argc, char **argv) {
        char *buf, *bufstart, *outname = NULL;
        size_t bufsize = BUFF_SIZE;
@@ -239,20 +262,7 @@ int main (int argc, char **argv) {
                if (bufused == bufsize) {
                        if ((bufsize*2) >= mem_available) {
                                if (!tmpfile) {
-                                       /* umask(077); FIXME: Should we be setting umask, or using default?  */
-                                       struct cs_status cs;
-                                       int tmpfd;
-
-                                       trapsignals();
-                                       cs = cs_enter();
-                                       tmpfd = mkstemp(tmpname);
-                                       atexit(onexit_cleanup); // solaris on_exit(onexit_cleanup, 0);
-                                       cs_leave(cs);
-                                       if (tmpfd < 0) {
-                                               perror("mkstemp failed");
-                                               exit(1);
-                                       }
-                                       tmpfile = fdopen(tmpfd, "w+");
+                                       tmpfile=open_tmpfile();
                                }
                                write_buff_tmp(bufstart, bufused, tmpfile);
                                bufused = 0;