File: exch.c

package info (click to toggle)
util-linux 2.41-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 95,208 kB
  • sloc: ansic: 179,016; sh: 22,689; yacc: 1,284; makefile: 528; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (102 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download | duplicates (2)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Copyright (C) 2023 Red Hat, Inc. All rights reserved.
 * Written by Masatake YAMATO <yamato@redhat.com>
 *
 * exch(1) - a command line interface for RENAME_EXCHANGE of renameat2(2).
 */
#include "c.h"
#include "nls.h"

#include <fcntl.h>
#include <getopt.h>

#ifndef HAVE_RENAMEAT2
# include <sys/syscall.h>
# include <unistd.h>
#endif

#ifndef RENAME_EXCHANGE
# define RENAME_EXCHANGE (1 << 1)
#endif

static inline int rename_exchange(const char *oldpath, const char *newpath)
{
	int rc;

#if defined(HAVE_RENAMEAT2)
	rc = renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, RENAME_EXCHANGE);
#elif defined(SYS_renameat2)
	rc = syscall(SYS_renameat2,
		     AT_FDCWD, oldpath, AT_FDCWD, newpath, RENAME_EXCHANGE);
#else
	rc = -1;
	errno = ENOSYS;
#endif
	return rc;
}

static void __attribute__((__noreturn__)) usage(void)
{
	FILE *out = stdout;

	fputs(USAGE_HEADER, out);
	fprintf(out, _(" %s [options] oldpath newpath\n"), program_invocation_short_name);
	fputs(USAGE_SEPARATOR, out);
	fputs(_("Atomically exchanges paths between two files.\n"), out);

	fputs(USAGE_OPTIONS, out);
	fprintf(out, USAGE_HELP_OPTIONS(30));

	fprintf(out, USAGE_MAN_TAIL("exch(1)"));

	exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
	int c;

	static const struct option longopts[] = {
		{ "version",    no_argument, NULL, 'V' },
		{ "help",	no_argument, NULL, 'h' },
		{ NULL }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	while ((c = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) {
		switch (c) {
		case 'V':
			print_version(EXIT_SUCCESS);
		case 'h':
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}

	if (argc - optind < 2) {
		warnx(_("too few arguments"));
		errtryhelp(EXIT_FAILURE);
	} else if (argc - optind > 2) {
		warnx(_("too many arguments"));
		errtryhelp(EXIT_FAILURE);
	}

	if (rename_exchange(argv[optind], argv[optind + 1]) != 0) {
		warn(_("failed to exchange \"%s\" and \"%s\""),
		     argv[optind], argv[optind + 1]);
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}