annotate lib/tmpfile-safer.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 10eb9086bea0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Invoke tmpfile, but avoid some glitches.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2006, 2009-2019 Free Software Foundation, Inc.
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
4 This program is free software: you can redistribute it and/or modify
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
7 (at your option) any later version.
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake, based on ideas from Paul Eggert. */
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 7036
diff changeset
19 #include <config.h>
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include "stdio-safer.h"
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23 #include <errno.h>
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24 #include <unistd.h>
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25 #include "unistd-safer.h"
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27 #include "binary-io.h"
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29 /* Like tmpfile, but do not return stdin, stdout, or stderr.
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31 Remember that tmpfile can leave files behind if your program calls _exit,
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 so this function should not be mixed with the close_stdout module. */
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 FILE *
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35 tmpfile_safer (void)
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
36 {
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37 FILE *fp = tmpfile ();
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
38
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39 if (fp)
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 {
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
41 int fd = fileno (fp);
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 if (0 <= fd && fd <= STDERR_FILENO)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
44 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
45 int f = dup_safer (fd);
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
47 if (f < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
48 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
49 int e = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
50 fclose (fp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
51 errno = e;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
52 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
53 }
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
55 /* Keep the temporary file in binary mode, on platforms
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
56 where that matters. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
57 if (fclose (fp) != 0
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
58 || ! (fp = fdopen (f, O_BINARY ? "wb+" : "w+")))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
59 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
60 int e = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
61 close (f);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
62 errno = e;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
63 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
64 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11049
diff changeset
65 }
7036
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66 }
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
67
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68 return fp;
a57aa913b52c * tmpfile-safer.c: New file.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
69 }