summaryrefslogtreecommitdiff
path: root/rsync/fileutil.c
Unidiff
Diffstat (limited to 'rsync/fileutil.c') (more/less context) (ignore whitespace changes)
-rw-r--r--rsync/fileutil.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/rsync/fileutil.c b/rsync/fileutil.c
new file mode 100644
index 0000000..75a6bb4
--- a/dev/null
+++ b/rsync/fileutil.c
@@ -0,0 +1,70 @@
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- library for network deltas
4 * $Id$
5 *
6 * Copyright (C) 1999, 2000, 2001 by Martin Pool <mbp@samba.org>
7 * Copyright (C) 1999 by Andrew Tridgell <tridge@samba.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <config_rsync.h>
25
26#include <assert.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <stdio.h>
30#include <fcntl.h>
31#include <sys/file.h>
32#include <string.h>
33#include <errno.h>
34
35#include "rsync.h"
36#include "fileutil.h"
37#include "trace.h"
38
39
40
41/**
42 * \brief Open a file, with special handling for `-' or unspecified
43 * parameters on input and output.
44 *
45 * \param fopen-style mode string.
46 */
47FILE *
48rs_file_open(char const *filename, char const *mode)
49{
50 FILE *f;
51 int is_write;
52
53 is_write = mode[0] == 'w';
54
55 if (!filename || !strcmp("-", filename)) {
56 if (is_write)
57 return stdout;
58 else
59 return stdin;
60 }
61
62 if (!(f = fopen(filename, mode))) {
63 rs_error("Error opening \"%s\" for %s: %s", filename,
64 is_write ? "write" : "read",
65 strerror(errno));
66 exit(RS_IO_ERROR);
67 }
68
69 return f;
70}