~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to misc-utils/rename.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include <string.h>
18
18
#include <stdlib.h>
19
19
#include <errno.h>
 
20
#include <getopt.h>
20
21
 
21
22
#include "nls.h"
22
23
#include "xalloc.h"
23
 
 
24
 
static char *progname;
25
 
 
26
 
static int
27
 
do_rename(char *from, char *to, char *s) {
 
24
#include "c.h"
 
25
 
 
26
static int do_rename(char *from, char *to, char *s, int verbose)
 
27
{
28
28
        char *newname, *where, *p, *q;
29
29
        int flen, tlen, slen;
30
30
 
35
35
        flen = strlen(from);
36
36
        tlen = strlen(to);
37
37
        slen = strlen(s);
38
 
        newname = xmalloc(tlen+slen+1);
 
38
        newname = xmalloc(tlen + slen + 1);
39
39
 
40
40
        p = s;
41
41
        q = newname;
44
44
        p = to;
45
45
        while (*p)
46
46
                *q++ = *p++;
47
 
        p = where+flen;
 
47
        p = where + flen;
48
48
        while (*p)
49
49
                *q++ = *p++;
50
50
        *q = 0;
51
51
 
52
 
        if (rename(s, newname) != 0) {
53
 
                int errsv = errno;
54
 
                fprintf(stderr, _("%s: renaming %s to %s failed: %s\n"),
55
 
                                  progname, s, newname, strerror(errsv));
56
 
                exit(1);
57
 
        }
 
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);
58
57
 
 
58
        free(newname);
59
59
        return 1;
60
60
}
61
61
 
62
 
int
63
 
main(int argc, char **argv) {
64
 
        char *from, *to, *p;
65
 
        int i;
66
 
 
67
 
        progname = argv[0];
68
 
        if ((p = strrchr(progname, '/')) != NULL)
69
 
                progname = p+1;
 
62
static void __attribute__ ((__noreturn__)) usage(FILE * out)
 
63
{
 
64
        fputs(_("\nUsage:\n"), out);
 
65
        fprintf(out,
 
66
              _(" %s [options] expression replacement file...\n"),
 
67
                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
 
 
74
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 
75
}
 
76
 
 
77
int main(int argc, char **argv)
 
78
{
 
79
        char *from, *to;
 
80
        int i, c, verbose = 0;
 
81
 
 
82
        static const struct option longopts[] = {
 
83
                {"verbose", no_argument, NULL, 'v'},
 
84
                {"version", no_argument, NULL, 'V'},
 
85
                {"help", no_argument, NULL, 'h'},
 
86
                {NULL, 0, NULL, 0}
 
87
        };
70
88
 
71
89
        setlocale(LC_ALL, "");
72
90
        bindtextdomain(PACKAGE, LOCALEDIR);
73
91
        textdomain(PACKAGE);
74
92
 
75
 
        if (argc == 2) {
76
 
                if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
77
 
                        printf(_("%s (%s)\n"),
78
 
                               progname, PACKAGE_STRING);
79
 
                        return 0;
 
93
        while ((c = getopt_long(argc, argv, "vVh", longopts, NULL)) != -1)
 
94
                switch (c) {
 
95
                case 'v':
 
96
                        verbose = 1;
 
97
                        break;
 
98
                case 'V':
 
99
                        printf(_("%s from %s\n"),
 
100
                               program_invocation_short_name,
 
101
                               PACKAGE_STRING);
 
102
                        return EXIT_SUCCESS;
 
103
                case 'h':
 
104
                        usage(stdout);
 
105
                default:
 
106
                        usage(stderr);
80
107
                }
81
 
        }
 
108
 
 
109
        argc -= optind;
 
110
        argv += optind;
82
111
 
83
112
        if (argc < 3) {
84
 
                fprintf(stderr, _("call: %s from to files...\n"), progname);
85
 
                exit(1);
 
113
                warnx("not enough arguments");
 
114
                usage(stderr);
86
115
        }
87
116
 
88
 
        from = argv[1];
89
 
        to = argv[2];
90
 
 
91
 
        for (i=3; i<argc; i++)
92
 
                do_rename(from, to, argv[i]);
93
 
        return 0;
 
117
        from = argv[0];
 
118
        to = argv[1];
 
119
 
 
120
        for (i = 2; i < argc; i++)
 
121
                do_rename(from, to, argv[i], verbose);
 
122
 
 
123
        return EXIT_SUCCESS;
94
124
}