home *** CD-ROM | disk | FTP | other *** search
- /* restart program by Tom Almy. May 1990 */
- /* contributed to the public domain */
- /* compile with Turbo C using the options -B -1- -f- -G- -O -lt -mt -p */
- /* compile with Microsoft C v6.0 with -AT -G0rs -Osleazr */
- /* This will generate minimum size modules */
-
-
-
- #ifdef __TURBOC__
- #pragma inline
- #define _far far /* Turbo C has non-ANSI keywords */
- #define _cdecl cdecl
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
-
-
- #define COMSPEC "COMSPEC"
- #define CONFIG "config.sys"
- #define AUTOEXEC "autoexec.bat"
- #define CONFIGIN "configur.dat"
- #define READONLY "rt"
- #define WRITEONLY "wt"
- #define BOOTDRIVE "BOOTDRIVE"
- #define MAXLINESIZE 256
-
- FILE *infile;
- FILE *outfile = stdout;
-
- char root[] = "C:\\" ; /* typically "c:\" */
-
- char *progname;
- #if 1 /* for testing, can disable possibility of reboot */
- void boot(void)
- {
-
- /* trick system into soft reboot */
-
- /* first store 1234 into location 40:72 */
-
- *((unsigned int _far *) 0x400072) = 0x1234;
-
- /* perform a far call to the reboot location, ffff:0000 */
-
- ((void (_far *)(void)) 0xffff0000)();
-
- }
- #else
- void boot(void) {} ;
- #endif
-
- void die(char *string1, char *string2)
- {
-
- char c;
-
- fputs(progname,stderr);
- fputs(": ", stderr);
- while ((c= *string1++) != 0) {
- if (c == '%') fputs(string2,stderr);
- else fputc(c,stderr);
- }
- exit(1);
- }
-
-
- void sleep(int delay)
- {
- time_t etime;
-
- etime = delay + time(NULL); /* the ending time of the wait */
-
- while (time(NULL) < etime);
- }
-
-
- char linebuf[MAXLINESIZE]; /* buffer to put each line in */
- char option[80]; /* selected restart file "option" */
-
-
- int getline(void) /* get the next line of input */
- {
- int i;
-
- if (fgets(linebuf,MAXLINESIZE,infile) == NULL) {
- *linebuf = '\0';
- return 0;
- }
- i = strlen(linebuf);
- if (i== MAXLINESIZE-1) die("line too long: %", linebuf);
- return (i);
- }
-
- void dumpclean(void) /* output line less leading config code */
- {
- char *cp;
-
- if ((cp = strstr(linebuf," ")) == NULL) {
- /* handle a totally empty line */
- fputs("\n",outfile);
- }
- else {
- /* dispense with leading blanks, output rest */
- while (*cp == ' ') cp++;
- fputs(cp,outfile);
- }
- }
-
-
- void _cdecl main(int argc, char **argv)
- {
- char namebuf[80];
- char *cp;
- int i;
- int preview = 0;
- int reboot = 1;
- int delay = 0;
-
- progname = *argv;
- /* reduce to base name */
- if ((cp=strrchr(progname,'\\'))!=NULL) progname = cp+1;
- if ((cp=strrchr(progname,'/'))!=NULL) progname = cp+1;
- if ((cp=strchr(progname,'.'))!=NULL) *cp = '\0';
-
- if (argc == 1) {
- fputs("Usage: ", stderr);
- fputs(progname, stderr);
- fputs(" [-p|-n] [-delay] optionname", stderr);
- exit(1);
- }
-
- while (argc > 2) { /* check for options */
- argv++;
- argc--;
- if (strcmp(*argv,"-p") == 0) {
- preview = 1; /* preview mode -- output to stdout */
- reboot = 0;
- continue;
- }
-
- if (strcmp(*argv,"-n") == 0) {
- reboot = 0; /* disable reboot */
- continue;
- }
-
- if ((**argv=='-') && sscanf(*argv+1,"%d",&delay) == 1 && delay > 0) {
- /* delay the reboot */
- continue;
- }
- die("Command switch % not recognized",*argv);
- }
-
- strcpy(option,"'"); /* option string */
- strcat(option,*++argv);
- strcat(option,"'");
-
- if ((cp=getenv(BOOTDRIVE)) == NULL)
- cp = getenv(COMSPEC); /* drive letter to boot from */
- root[0] = cp[0];
-
- strcpy(namebuf,root);
- strcat(namebuf,CONFIGIN);
- if ((infile=fopen(namebuf,READONLY))==NULL)
- die("file % not found", namebuf);
-
- if (getline() == 0)
- die("empty input file", NULL);
-
- if (strstr(linebuf,option)==NULL)
- die("restart option % not found",option);
-
- if (getline() == 0 || *linebuf != '*') /* had better be file name */
- die("No files specified", NULL);
-
- while (*linebuf == '*') { /* continue while file being read */
-
- if ((cp=strchr(linebuf+1,' '))!=NULL) *cp = '\0';
- else linebuf[strlen(linebuf)-1] = '\0';
- if (preview) printf("\n%s:\n",linebuf+1);
- else if((outfile=fopen(linebuf+1, WRITEONLY)) == NULL)
- die("cannot write %",linebuf+1);
-
- i = getline();
-
- while (i>0 && *linebuf!= '*') {
- if (linebuf[0] == '\'' ) { /* conditional line */
- if (strstr(linebuf,option) != NULL) dumpclean();
- }
- else if (linebuf[0] == '~') {
- if (strstr(linebuf,option) == NULL) dumpclean();
- }
- else /* unconditional line */
- fputs(linebuf,outfile);
-
- i = getline(); /* read the next line */
- }
-
- if (preview == 0) fclose(outfile);
- }
-
- if (reboot) {
- if (delay != 0) sleep(delay);
- boot();
- }
-
- }
-