Created
June 13, 2015 22:13
-
-
Save 0x5742/92199a8f7a26ed4a91d1 to your computer and use it in GitHub Desktop.
a cheap substitute for util-linux rename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| int main(int argc, char **argv) | |
| { | |
| int n, diff, fromlen, tolen, leftlen; | |
| const char *name, *from, *to, *pos; | |
| char *buf; | |
| if (argc < 4) { | |
| fprintf(stderr, "usage: %s from to files...\n", argv[0]); | |
| return 1; | |
| } | |
| from = argv[1]; | |
| to = argv[2]; | |
| fromlen = strlen(from); | |
| tolen = strlen(to); | |
| diff = tolen - fromlen; | |
| for (n = 3; n < argc; n++) { | |
| name = argv[n]; | |
| pos = strstr(name, from); | |
| if (!pos) continue; | |
| leftlen = pos - name; | |
| buf = malloc(strlen(name) + diff + 1); | |
| memcpy(buf, name, leftlen); | |
| memcpy(buf + leftlen, to, tolen); | |
| memcpy(buf + leftlen + tolen, pos + fromlen, strlen(name) + diff - leftlen - tolen + 1); | |
| rename(name, buf); | |
| free(buf); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment