/* $Id: logrotate.c,v 1.2 2007/05/07 19:32:36 dmitri Exp $ */ /* * Copyright (c) 2007 Dmitri Alenitchev * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include extern char *__progname; __dead void usage(void); int main(int argc, char** argv) { time_t tval; int ch; char buf[1024], file[FILENAME_MAX], *format; format = "%Y_%m_%d"; while ((ch = getopt(argc, argv, "f:")) != -1) switch (ch) { case 'f': format = optarg; break; default: usage(); } argc -= optind; argv += optind; if (argc == 0) usage(); (void)time(&tval); (void)strftime(buf, sizeof(buf), format, localtime(&tval)); (void)strlcpy(file, argv[0], sizeof(file)); (void)strlcat(file, ".", sizeof(file)); (void)strlcat(file, buf, sizeof(file)); if (rename(argv[0], file)) { perror(__progname); exit(1); } exit(0); } __dead void usage(void) { (void)fprintf(stderr, "usage: %s [-f format] filename\n", __progname); exit(1); }