tpage: remove temporary file at program exit; fixes reading from stdin - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 2fb0c4447ef493da81703dd20090676ed8cae834
 (DIR) parent 8ff457f56db9f94f4baf68f53d87e791584d1dfe
 (HTM) Author: J.R. Mauro <jrm8005@gmail.com>
       Date:   Thu, 20 Aug 2009 14:38:31 -0400
       
       page: remove temporary file at program exit; fixes reading from stdin
       
       http://codereview.appspot.com/105097
       
       Diffstat:
         M src/cmd/page/page.c                 |       9 +++++++++
         M src/cmd/page/page.h                 |       3 +++
         M src/cmd/page/util.c                 |       9 ++++-----
       
       3 files changed, 16 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/page/page.c b/src/cmd/page/page.c
       t@@ -21,6 +21,7 @@ int truecolor;
        int imagemode;
        int notewatcher;
        int notegp;
       +char tempfile[40];
        
        int
        watcher(void *v, char *x)
       t@@ -84,6 +85,12 @@ usage(void)
        }
        
        void
       +cleanup(void)
       +{
       +        remove(tempfile);
       +}
       +
       +void
        threadmain(int argc, char **argv)
        {
                Document *doc;
       t@@ -173,6 +180,8 @@ threadmain(int argc, char **argv)
                                fprint(2, "page: short read reading %s\n", argv[0]);
                                wexits("read");
                        }
       +                
       +                atexit(cleanup);
                }else if(argc != 0){
                        if(!(b = Bopen(argv[0], OREAD))) {
                                fprint(2, "page: cannot open \"%s\"\n", argv[0]);
 (DIR) diff --git a/src/cmd/page/page.h b/src/cmd/page/page.h
       t@@ -99,9 +99,12 @@ Image*        convert(Graphic *g);
        Image*        cachedpage(Document*, int, int);
        void        cacheflush(void);
        
       +extern char tempfile[40];
       +
        extern int stdinfd;
        extern int truecolor;
        
       +
        /* BUG BUG BUG BUG BUG: cannot use new draw operations in drawterm,
         * or in vncs, and there is a bug in the kernel for copying images
         * from cpu memory -> video memory (memmove is not being used).
 (DIR) diff --git a/src/cmd/page/util.c b/src/cmd/page/util.c
       t@@ -50,12 +50,11 @@ spooltodisk(uchar *ibuf, int in, char **name)
        {
                uchar buf[8192];
                int fd, n;
       -        char temp[40];
       -
       -        strcpy(temp, "/tmp/pagespoolXXXXXXXXX");
       -        fd = opentemp(temp, ORDWR|ORCLOSE);
       +        
       +        strcpy(tempfile, "/tmp/pagespoolXXXXXXXXX");
       +        fd = opentemp(tempfile, ORDWR);
                if(name)
       -                *name = estrdup(temp);
       +                *name = estrdup(tempfile);
        
                if(write(fd, ibuf, in) != in){
                        fprint(2, "error writing temporary file\n");