+int main (int argc, char *argv[])
+{
+ if (argc==1) showHelp(2,1);
+ if (strcmp(argv[1], "--help")==0) showHelp(1,0);
+ if (strcmp(argv[1], "--version")==0) showVersion();
+ if (argc!=3) showHelp(2,1);
+
+ {
+ int const runlevel = atoi(argv[1]);
+ char const * const fname = argv[2];
+ int fd;
+ struct utmp ut;
+
+ gid_t gid;
+ char *gid_str = getenv("UTMP_GID");
+
+ if (runlevel<0 || runlevel>6) showHelp(2,1);
+
+ Echroot(".");
+ Echdir("/");
+
+ // Real NSS is too expensive/insecure in this state; therefore, use the
+ // value detected at ./configure stage or overridden by $UTMP_GID
+ // env-variable
+ gid = gid_str ? atoi(gid_str) : UTMP_GID;
+ Esetgroups(1, &gid);
+ Esetgid(gid);
+
+ if (getgid()!=gid) {
+ WRITE_MSG(2, "fakerunlevel: Failed to set group\n");
+ return EXIT_FAILURE;
+ }
+
+ umask(002);
+ fd = EopenD(fname, O_WRONLY|O_CREAT|O_APPEND, 0664);
+ Eclose(fd);
+
+ utmpname (fname);
+ setutent();
+ memset (&ut,0,sizeof(ut));
+ ut.ut_type = RUN_LVL;
+ ut.ut_pid = ('#' << 8) + runlevel+'0';
+ pututline (&ut);
+ endutent();
+ }
+
+ return EXIT_SUCCESS;
+}