#include #include #include #ifdef __linux__ #include #include #elif defined(__sgi) #include #include #include #endif int main(int argc, char *argv[]) { struct stat target; char ext[] = ".old"; char *newfile; size_t length; int rotatesize; if (argc != 3) { fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, " is the name of the file to rotate.\n"); fprintf(stderr, " is the size (in bytes) the file must\n"); fprintf(stderr, " be greater than for rotation.\n"); exit(-1); } if (stat(argv[1], &target)) { perror("Unable to open file"); exit(-1); } rotatesize = atoi(argv[2]); if (rotatesize <= target.st_size) { length = strlen(argv[1]); if ((newfile = (char *) malloc(length * sizeof(char))) == NULL) { fprintf(stderr, "Malloc failed, don't ask me why.\n"); exit(-1); } strcpy(newfile, argv[1]); strcat(newfile, ext); if (rename(argv[1], newfile)) { perror("Error renaming original file"); exit(-1); } if ((creat(argv[1], target.st_mode)) < 0) { perror("Error creating new file"); exit(-1); } exit(0); } else { exit(1); } }