localename: Fix gettext test failures on mingw.
[gnulib.git] / lib / backup-find.c
blob477a018b23c68a7d84eea95b61b7b5fa99a2173a
1 /* backupfile.c -- make Emacs style backup file names
3 Copyright 2017-2018 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include "backup-internal.h"
22 #include "argmatch.h"
23 #include "xalloc.h"
25 #include <stdlib.h>
27 /* Relative to DIR_FD, return the name of a backup file for the
28 existing file FILE, allocated with malloc. Report an error and
29 exit if out of memory. Do not call this function if
30 backup_type == no_backups. */
32 char *
33 find_backup_file_name (int dir_fd, char const *file,
34 enum backup_type backup_type)
36 char *result = backupfile_internal (dir_fd, file, backup_type, false);
37 if (!result)
38 xalloc_die ();
39 return result;
42 static char const *const backup_args[] =
44 /* In a series of synonyms, present the most meaningful first, so
45 that argmatch_valid be more readable. */
46 "none", "off",
47 "simple", "never",
48 "existing", "nil",
49 "numbered", "t",
50 NULL
53 static const enum backup_type backup_types[] =
55 no_backups, no_backups,
56 simple_backups, simple_backups,
57 numbered_existing_backups, numbered_existing_backups,
58 numbered_backups, numbered_backups
61 /* Ensure that these two vectors have the same number of elements,
62 not counting the final NULL in the first one. */
63 ARGMATCH_VERIFY (backup_args, backup_types);
65 /* Return the type of backup specified by VERSION.
66 If VERSION is NULL or the empty string, return numbered_existing_backups.
67 If VERSION is invalid or ambiguous, fail with a diagnostic appropriate
68 for the specified CONTEXT. Unambiguous abbreviations are accepted. */
70 enum backup_type
71 get_version (char const *context, char const *version)
73 if (version == 0 || *version == 0)
74 return numbered_existing_backups;
75 else
76 return XARGMATCH (context, version, backup_args, backup_types);
80 /* Return the type of backup specified by VERSION.
81 If VERSION is NULL, use the value of the envvar VERSION_CONTROL.
82 If the specified string is invalid or ambiguous, fail with a diagnostic
83 appropriate for the specified CONTEXT.
84 Unambiguous abbreviations are accepted. */
86 enum backup_type
87 xget_version (char const *context, char const *version)
89 if (version && *version)
90 return get_version (context, version);
91 else
92 return get_version ("$VERSION_CONTROL", getenv ("VERSION_CONTROL"));