Skip to content

Instantly share code, notes, and snippets.

@0x5742
Created June 13, 2015 22:13
Show Gist options
  • Select an option

  • Save 0x5742/92199a8f7a26ed4a91d1 to your computer and use it in GitHub Desktop.

Select an option

Save 0x5742/92199a8f7a26ed4a91d1 to your computer and use it in GitHub Desktop.
a cheap substitute for util-linux rename
#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