1 /* utility functions for 'patch' */
2 
3 /* Copyright (C) 1986 Larry Wall
4 
5    Copyright (C) 1992-1993, 1997-1999, 2001-2003, 2009-2012 Free Software
6    Foundation, Inc.
7 
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20 
21 #include <timespec.h>
22 #include <stat-time.h>
23 #include <backupfile.h>
24 
25 /* An upper bound on the print length of a signed decimal line number.
26    Add one for the sign.  */
27 #define LINENUM_LENGTH_BOUND (sizeof (lin) * CHAR_BIT / 3 + 1)
28 
29 enum file_id_type { UNKNOWN, CREATED, DELETE_LATER, OVERWRITTEN };
30 
31 XTERN enum backup_type backup_type;
32 
33 bool ok_to_reverse (char const *, ...) __attribute__ ((format (printf, 1, 2)));
34 void ask (char const *, ...) __attribute__ ((format (printf, 1, 2)));
35 void say (char const *, ...) __attribute__ ((format (printf, 1, 2)));
36 
37 void fatal (char const *, ...)
38 	__attribute__ ((noreturn, format (printf, 1, 2)));
39 void pfatal (char const *, ...)
40 	__attribute__ ((noreturn, format (printf, 1, 2)));
41 
42 void fetchname (char const *, int, char **, char **, struct timespec *);
43 char *parse_name (char const *, int, char const **);
44 char *savebuf (char const *, size_t);
45 char *savestr (char const *);
46 char const *version_controller (char const *, bool, struct stat const *, char **, char **);
47 bool version_get (char const *, char const *, bool, bool, char const *, struct stat *);
48 int create_file (char const *, int, mode_t, bool);
49 int systemic (char const *);
50 char *format_linenum (char[LINENUM_LENGTH_BOUND + 1], lin);
51 void Fseek (FILE *, file_offset, int);
52 void copy_file (char const *, char const *, struct stat *, int, mode_t, bool);
53 void append_to_file (char const *, char const *);
54 void exit_with_signal (int) __attribute__ ((noreturn));
55 void ignore_signals (void);
56 void init_backup_hash_table (void);
57 void init_time (void);
58 void xalloc_die (void) __attribute__ ((noreturn));
59 void create_backup (char const *, const struct stat *, bool);
60 void move_file (char const *, bool *, struct stat const *, char const *, mode_t, bool);
61 void read_fatal (void) __attribute__ ((noreturn));
62 void remove_prefix (char *, size_t);
63 void removedirs (char const *);
64 void set_signals (bool);
65 void write_fatal (void) __attribute__ ((noreturn));
66 void insert_file_id (struct stat const *, enum file_id_type);
67 enum file_id_type lookup_file_id (struct stat const *);
68 void set_queued_output (struct stat const *, bool);
69 bool has_queued_output (struct stat const *);
70 int stat_file (char const *, struct stat *);
71 bool filename_is_safe (char const *) _GL_ATTRIBUTE_PURE;
72 bool cwd_is_root (char const *);
73 
74 enum file_attributes {
75   FA_TIMES = 1,
76   FA_IDS = 2,
77   FA_MODE = 4,
78   FA_XATTRS = 8
79 };
80 
81 void set_file_attributes (char const *, enum file_attributes, char const *,
82 			  const struct stat *, mode_t, struct timespec *);
83 
84 static inline char const * _GL_ATTRIBUTE_PURE
skip_spaces(char const * str)85 skip_spaces (char const *str)
86 {
87   while (ISSPACE ((unsigned char) *str))
88     str++;
89   return str;
90 }
91 
92 int make_tempfile(char const **, char, char const *, int, mode_t);
93