/* * purge.cpp * Code to handle a purge. * ____ _ * | _ \ ___ __ _| |_ __ ___ ___ * | |_) / _ \/ _` | | '_ ` _ \/ __| * | _ < __/ (_| | | | | | | \__ \ * |_| \_\___|\__,_|_|_| |_| |_|___/ * * Permission to use, modify and distribute is granted via the * Creative Commons - Attribution - Non Commercial - Share Alike 3.0 License * http://creativecommons.org/licenses/by-nc-sa/3.0/ * * Copyright (C) 2007-2009 Jason Mitchell, Randi Mitchell * Contributions by Tim Callahan, Jonathan Hseu * Based on Mordor (C) Brooke Paul, Brett J. Vickers, John P. Freeman * */ void log(const char *fmt,...); int PURGED=0; #define ACC 00660 #define PATH "/home/realms/mud/player" int main() { DIR *dirfd; struct dirent *dirp; if((dirfd = opendir(PATH)) == NULL) { printf("Directory could not be opened.\n"); return; } while((dirp = readdir(dirfd)) != NULL) { if(dirp->d_name[0] == '.') continue; if(!isupper(dirp->d_name[0])) continue; do_stat(dirp->d_name); } return; } int do_stat(char *name) { struct stat stats; char temp[160]; stat(name, &stats); // printf("%20s last logged on %s", name, ctime(&stats.st_mtime)); if(stats.st_mtime < 915000000) { unlink(name); sprintf(temp,"%20s: %s", name, ctime(&stats.st_mtime)); log("deleted", temp); } else log("skipped", "%20s: %s", name, ctime(&stats.st_mtime)); return; } void log(const char *fmt,...) { char file[80]; char str[2048]; int fd; va_list ap; va_start(ap, fmt); sprintf(file, "/home/realms/mud/%s", name); fd = open(file, O_RDWR | O_APPEND, 0); if(fd < 0) { fd = open(file, O_RDWR | O_CREAT, ACC); if(fd < 0) return; } lseek(fd, 0L, 2); // prevent string overruns with vsn vsnprintf(str, 2000, fmt, ap); va_end(ap); write(fd, str, strlen(str)); close(fd); }