+/* daemon() - detach process from user and disappear into the background
+ * returns -1 on failure, but you can't do much except exit in that case
+ * since we may already have forked. This is based on the BSD version,
+ * so the caller is responsible for things like the umask, etc.
+ */
+
+/* believed to work on all Posix systems */
+
+int daemon(int nochdir, int noclose)
+{
+ pid_t pid;
+ switch (pid = fork())
+ {
+ case 0: break;
+ case -1: return -1;
+ default: _exit(0); /* exit the original process */
+ }
+
+ if (setsid() < 0) /* shoudn't fail */
+ return -1;
+
+ /* dyke out this switch if you want to acquire a control tty in */
+ /* the future -- not normally advisable for daemons */
+
+ switch (pid = fork())
+ {
+ case 0: break;
+ case -1: return -1;
+ default:
+ ofstream outfile;
+ outfile.open(pidfile);
+ if (outfile.fail())
+ cerr << "Unable to open " << pidfile << endl;
+ outfile << pid << endl;
+ outfile.close();
+
+ _exit(0);
+ }
+
+ if (!nochdir)
+ chdir("/");
+
+ if (!noclose)
+ {
+ closeall(0);
+ open("/dev/null",O_RDWR);
+ dup(0); dup(0);
+ }
+
+ return 0;
+}
+
+
+/* closeall() -- close all FDs >= a specified value */
+
+void closeall(int fd)
+{
+ int fdlimit = sysconf(_SC_OPEN_MAX);
+
+ while (fd < fdlimit)
+ close(fd++);
+}
+
+void prettyIntro()
+{
+ cout << endl;
+ cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl;
+ cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl;
+ cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl;
+ cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl;
+ cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl;
+ cout << " GGGGG V\n\n" << endl;
+ cout << "Version: " << VERSION << endl;
+ cout << "http://www.gameserv.us - http://www.sourceforge.net/projects/gameservirc" << endl;
+}
+
+void load_lastrefresh()
+{
+ ifstream infile;
+ infile.open(".gsrefresh");
+ if (infile.fail())
+ {
+ #ifdef DEBUGMODE
+ log("Error opening .gsrefresh");
+ #endif
+
+ generate:
+ long mytime = time(NULL);
+ #ifdef DEBUGMODE
+ log("Generating new refresh time");
+ #endif
+
+ // Just a safety measure... tho no one should
+ // get anywhere near the actual time as their refreshperiod
+ if (refreshperiod >= mytime)
+ {
+ log("Refresh period is greater than or equal to the actual time... setting it to 86400");
+ refreshperiod = 86400;
+ }
+
+ lastrefresh = mytime - (mytime % refreshperiod);
+
+ refreshall();
+ save_lastrefresh();
+ return;
+ }
+ infile >> lastrefresh;
+
+ infile.close();
+ if (lastrefresh < 0)
+ goto generate;
+}
+
+void load_lastrollover()
+{
+ ifstream infile;
+ infile.open(".gsrollover");
+ if (infile.fail())
+ {
+#ifdef DEBUGMODE
+ log("Error opening .gsrollover");
+#endif
+
+ generate:
+ long mytime = time(NULL);
+#ifdef DEBUGMODE
+ log("Generating new rollover time");
+#endif
+ lastrollover = mytime;
+ return;
+ }
+ infile >> lastrollover;
+
+ infile.close();
+ if (lastrollover < 0)
+ goto generate;
+}
+
+void save_lastrefresh()
+{
+ ofstream outfile;
+
+ outfile.open(".gsrefresh");
+
+ if (outfile.fail())
+ {
+ log("Error creating new file .gsrefresh");
+ return;
+ }
+ outfile << lastrefresh << endl << lastrollover;
+
+ outfile.close();
+}
+
+void save_lastrollover()
+{
+ ofstream outfile;
+
+ outfile.open(".gsrollover");
+
+ if (outfile.fail())
+ {
+ log("Error creating new file .gsrollover");
+ return;
+ }
+ outfile << lastrollover << endl;
+ outfile.close();
+}