~ubuntu-branches/ubuntu/vivid/util-linux/vivid

« back to all changes in this revision

Viewing changes to misc-utils/rename.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-08-18 15:43:56 UTC
  • mfrom: (1.6.5) (4.1.16 experimental)
  • Revision ID: package-import@ubuntu.com-20140818154356-sqn436j3vndc62qb
Tags: 2.25-8ubuntu1
* Merge with Debian experimental.
  - This is now a non-ancient version. (LP: #1012081)
  - No longer uses /etc/blkid.tab by default, but a file in /run/.
    (LP: #1244595)
  - mkswap wipes fs signatures (LP: #1047666)
  - Fix "reatime" manpage typo (LP: #1047666)
  - wipefs properly cleans up fs signatures (LP: #1059872)
  Remaining Ubuntu changes:
  - Regularly trim SSDs automatically (core-1311-ssd-trimming):
    + Add debian/fstrim-all: Script to detect which mounted partitions
      need/support trimming, and call fstrim(8) on all of them.
      Install into /usr/sbin/.
    + Add debian/fstrim-all.8: Manpage for the above.
    + Add debian/fstrim-all.cron: Trivial shell script to call fstrim-all,
      so that admins can easily adjust/disable it. Installed as
      /etc/cron.weekly/fstrim.
  - Upstart support:
    + Add hwclock{-save}.upstart, and install them in debian/rules.
    + Drop initscripts dependency.
    + Drop debian/hwclock.rules and hwclock.default.
  - Add mountall-options.patch, see patch header.
  - uuid-runtime.postinst: Due to the way the uuidd account is created, it
    will get a uid/gid allocation for userns use. This isn't needed and is a
    waste of uid/gid so always clear uuidd from subuid/subgid.
* Drop /lib/init/fstab parsing fallback in mount. Patch does not apply at
  all any more, is specific to mountall (and thus should not be relied
  upon), and not very useful; all init systems, schroot, debootstrap etc.
  mount /sys, /proc/ and friends by themselves already.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include <stdlib.h>
19
19
#include <errno.h>
20
20
#include <getopt.h>
 
21
#include <unistd.h>
 
22
#include <sys/types.h>
 
23
#include <sys/stat.h>
21
24
 
22
25
#include "nls.h"
23
26
#include "xalloc.h"
24
27
#include "c.h"
 
28
#include "closestream.h"
25
29
 
26
 
static int do_rename(char *from, char *to, char *s, int verbose)
 
30
static int do_rename(char *from, char *to, char *s, int verbose, int symtarget)
27
31
{
28
 
        char *newname, *where, *p, *q;
 
32
        char *newname, *where, *p, *q, *target = NULL;
29
33
        int flen, tlen, slen;
30
 
 
31
 
        where = strstr(s, from);
32
 
        if (where == NULL)
 
34
        struct stat sb;
 
35
 
 
36
        if (symtarget) {
 
37
                if (lstat(s, &sb) == -1)
 
38
                        err(EXIT_FAILURE, _("%s: lstat failed"), s);
 
39
 
 
40
                if (!S_ISLNK(sb.st_mode))
 
41
                        errx(EXIT_FAILURE, _("%s: not a symbolic link"), s);
 
42
 
 
43
                target = xmalloc(sb.st_size + 1);
 
44
                if (readlink(s, target, sb.st_size + 1) < 0)
 
45
                        err(EXIT_FAILURE, _("%s: readlink failed"), s);
 
46
 
 
47
                target[sb.st_size] = '\0';
 
48
                where = strstr(target, from);
 
49
        } else
 
50
                where = strstr(s, from);
 
51
 
 
52
        if (where == NULL) {
 
53
                free(target);
33
54
                return 0;
 
55
        }
34
56
 
35
57
        flen = strlen(from);
36
58
        tlen = strlen(to);
37
 
        slen = strlen(s);
 
59
        if (symtarget) {
 
60
                slen = strlen(target);
 
61
                p = target;
 
62
        } else {
 
63
                slen = strlen(s);
 
64
                p = s;
 
65
        }
38
66
        newname = xmalloc(tlen + slen + 1);
39
67
 
40
 
        p = s;
41
68
        q = newname;
42
69
        while (p < where)
43
70
                *q++ = *p++;
49
76
                *q++ = *p++;
50
77
        *q = 0;
51
78
 
52
 
        if (rename(s, newname) != 0)
53
 
                err(EXIT_FAILURE, _("renaming %s to %s failed"),
54
 
                    s, newname);
55
 
        if (verbose)
56
 
                printf("`%s' -> `%s'\n", s, newname);
 
79
        if (symtarget) {
 
80
                if (0 > unlink(s))
 
81
                        err(EXIT_FAILURE, _("%s: unlink failed"), s);
 
82
                if (symlink(newname, s) != 0)
 
83
                        err(EXIT_FAILURE, _("%s: symlinking to %s failed"), s, newname);
 
84
                if (verbose)
 
85
                        printf("%s: `%s' -> `%s'\n", s, target, newname);
 
86
        } else {
 
87
                if (rename(s, newname) != 0)
 
88
                        err(EXIT_FAILURE, _("%s: rename to %s failed"), s, newname);
 
89
                if (verbose)
 
90
                        printf("`%s' -> `%s'\n", s, newname);
 
91
        }
57
92
 
58
93
        free(newname);
 
94
        free(target);
59
95
        return 1;
60
96
}
61
97
 
62
98
static void __attribute__ ((__noreturn__)) usage(FILE * out)
63
99
{
64
 
        fputs(_("\nUsage:\n"), out);
 
100
        fputs(USAGE_HEADER, out);
65
101
        fprintf(out,
66
102
              _(" %s [options] expression replacement file...\n"),
67
103
                program_invocation_short_name);
68
 
 
69
 
        fputs(_("\nOptions:\n"), out);
70
 
        fputs(_(" -v, --verbose    explain what is being done\n"
71
 
                " -V, --version    output version information and exit\n"
72
 
                " -h, --help       display this help and exit\n\n"), out);
73
 
 
 
104
        fputs(USAGE_OPTIONS, out);
 
105
        fputs(_(" -v, --verbose    explain what is being done\n"), out);
 
106
        fputs(_(" -s, --symlink    act on symlink target\n"), out);
 
107
        fputs(USAGE_SEPARATOR, out);
 
108
        fputs(USAGE_HELP, out);
 
109
        fputs(USAGE_VERSION, out);
 
110
        fprintf(out, USAGE_MAN_TAIL("rename(1)"));
74
111
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
75
112
}
76
113
 
77
114
int main(int argc, char **argv)
78
115
{
79
116
        char *from, *to;
80
 
        int i, c, verbose = 0;
 
117
        int i, c, symtarget=0, verbose = 0;
81
118
 
82
119
        static const struct option longopts[] = {
83
120
                {"verbose", no_argument, NULL, 'v'},
84
121
                {"version", no_argument, NULL, 'V'},
85
122
                {"help", no_argument, NULL, 'h'},
 
123
                {"symlink", no_argument, NULL, 's'},
86
124
                {NULL, 0, NULL, 0}
87
125
        };
88
126
 
89
127
        setlocale(LC_ALL, "");
90
128
        bindtextdomain(PACKAGE, LOCALEDIR);
91
129
        textdomain(PACKAGE);
 
130
        atexit(close_stdout);
92
131
 
93
 
        while ((c = getopt_long(argc, argv, "vVh", longopts, NULL)) != -1)
 
132
        while ((c = getopt_long(argc, argv, "vsVh", longopts, NULL)) != -1)
94
133
                switch (c) {
95
134
                case 'v':
96
135
                        verbose = 1;
97
136
                        break;
 
137
                case 's':
 
138
                        symtarget = 1;
 
139
                        break;
98
140
                case 'V':
99
 
                        printf(_("%s from %s\n"),
100
 
                               program_invocation_short_name,
101
 
                               PACKAGE_STRING);
 
141
                        printf(UTIL_LINUX_VERSION);
102
142
                        return EXIT_SUCCESS;
103
143
                case 'h':
104
144
                        usage(stdout);
110
150
        argv += optind;
111
151
 
112
152
        if (argc < 3) {
113
 
                warnx("not enough arguments");
 
153
                warnx(_("not enough arguments"));
114
154
                usage(stderr);
115
155
        }
116
156
 
118
158
        to = argv[1];
119
159
 
120
160
        for (i = 2; i < argc; i++)
121
 
                do_rename(from, to, argv[i], verbose);
 
161
                do_rename(from, to, argv[i], verbose, symtarget);
122
162
 
123
163
        return EXIT_SUCCESS;
124
164
}