summaryrefslogtreecommitdiff
blob: 372cfc9ee10843b3e95fee755ac2f0d91a57a236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Fix the tempfile handling to avoid race conditions.  Also use /tmp as
a fallback if $PWD is read-only for whatever reason.

Patch by Mike Frysinger <vapier@gentoo.org>

--- pcalc-000/pcalc.y
+++ pcalc-000/pcalc.y
@@ -151,7 +151,10 @@
 int     main(int argc, char *argv[])
 
 {
-    int     comm = 0, args;
+    int     args;
+    char    template_local[] = "pcalc.tmp.XXXXXX",
+            template_global[] = "/tmp/pcalc.tmp.XXXXXX",
+            *template;
     char    *env;
 
     args = parse_comline(argc, argv);
@@ -198,6 +199,7 @@
 
         char    *commandline;
         int     len, cnt;
+        int     tmpfile;
 
         for(cnt = args+1; cnt < argc; cnt++)
             {
@@ -207,19 +209,27 @@
         //printf("CMDLINE='%s'\n", buff);
 
         len = strlen(buff);
-        yyin = fopen("pcalc.tmp", "w");
+        template = template_local;
+        tmpfile = mkstemp(template);
 
-        if(!yyin)
+        if(tmpfile == -1)
             {
-            fprintf(stderr, "cannot create tmp file\n"); exit(0);
+            template = template_global;
+            tmpfile = mkstemp(template);
+            if(tmpfile == -1)
+                {
+                fprintf(stderr, "cannot create tmp file\n"); exit(0);
+                }
             }
-        fwrite(buff, len, 1, yyin);
-        fputc('\n', yyin);
-	//fputc(0x1a,  yyin);
-        fclose(yyin);
+        write(tmpfile, buff, len);
+        write(tmpfile, "\n", 1);
+        //write(tmpfile, "\x1a", 1);
+        lseek(tmpfile, 0, SEEK_SET);
 
-        yyin = fopen("pcalc.tmp", "r");
-        comm = 1;
+        yyin = fdopen(tmpfile, "r");
+        /* XXX: hack! unlink here because if parsing fails, flex will
+         * exit and we won't be able to unlink the file below */
+        unlink(template);
         }
 
     init_sym() ;
@@ -228,10 +229,10 @@
     yyparse() ;
 
     if(yyin)
-        fclose(yyin);
-
-    if(comm)
-        unlink("pcalc.tmp");
+        {
+        unlink(template); /* unlink before we close to avoid race */
+        fclose(yyin); /* this closes tmpfile too */
+        }
 
     return 0 ;
 }