home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / SYSTEM / RESTART2.ZIP / RESTART2.C next >
Encoding:
C/C++ Source or Header  |  1990-06-04  |  4.5 KB  |  209 lines

  1. /* restart program by Tom Almy.  May 1990 */
  2. /* contributed to the public domain */
  3. /* compile with Turbo C using the options -B -1- -f- -G- -O -lt -mt -p */
  4. /* compile with Microsoft C v6.0 with -AT -G0rs -Osleazr  */
  5. /* This will generate minimum size modules */
  6.  
  7.  
  8.  
  9. #ifdef __TURBOC__
  10. #pragma inline
  11. #define _far far        /* Turbo C has non-ANSI keywords */
  12. #define _cdecl cdecl
  13. #endif
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18.  
  19.  
  20. #define COMSPEC        "COMSPEC"
  21. #define CONFIG        "config.sys"
  22. #define AUTOEXEC    "autoexec.bat"
  23. #define CONFIGIN    "configur.dat"
  24. #define READONLY    "rt"
  25. #define WRITEONLY    "wt"
  26. #define BOOTDRIVE    "BOOTDRIVE"
  27. #define MAXLINESIZE 256
  28.  
  29. FILE *infile;
  30. FILE *outfile = stdout;
  31.  
  32. char root[] = "C:\\" ;    /* typically "c:\" */
  33.  
  34. char *progname;
  35. #if 1        /* for testing, can disable possibility of reboot */
  36. void boot(void)
  37. {
  38.  
  39. /* trick system into soft reboot */
  40.  
  41. /* first store 1234 into location 40:72  */
  42.  
  43. *((unsigned int _far *) 0x400072) = 0x1234;
  44.  
  45. /*  perform a far call to the reboot location, ffff:0000 */
  46.  
  47.     ((void (_far *)(void)) 0xffff0000)();
  48.  
  49. }
  50. #else
  51. void boot(void) {} ;
  52. #endif
  53.  
  54. void die(char *string1, char *string2)
  55. {
  56.  
  57.     char c;
  58.  
  59.     fputs(progname,stderr);
  60.     fputs(": ", stderr);
  61.     while ((c= *string1++) != 0) {
  62.         if (c == '%') fputs(string2,stderr);
  63.         else fputc(c,stderr);
  64.     }
  65.     exit(1);
  66. }
  67.  
  68.  
  69. void sleep(int delay)
  70. {
  71.     time_t etime;
  72.  
  73.     etime = delay + time(NULL);    /* the ending time of the wait */
  74.  
  75.     while (time(NULL) < etime);
  76. }
  77.  
  78.  
  79. char linebuf[MAXLINESIZE];    /* buffer to put each line in */
  80. char option[80];            /* selected restart file "option" */
  81.  
  82.  
  83. int getline(void)    /* get the next line of input */
  84. {
  85.     int i;
  86.  
  87.     if (fgets(linebuf,MAXLINESIZE,infile) == NULL) {
  88.         *linebuf = '\0';
  89.         return 0;
  90.     }
  91.     i = strlen(linebuf);
  92.     if (i== MAXLINESIZE-1) die("line too long: %", linebuf);
  93.     return (i);
  94. }
  95.  
  96. void dumpclean(void)    /* output line less leading config code */
  97. {
  98.     char *cp;
  99.     
  100.     if ((cp = strstr(linebuf," ")) == NULL) {
  101.         /* handle a totally empty line */
  102.         fputs("\n",outfile);
  103.     }
  104.     else {
  105.         /* dispense with leading blanks, output rest */
  106.         while (*cp == ' ') cp++;
  107.         fputs(cp,outfile);
  108.     }
  109. }
  110.     
  111.  
  112. void _cdecl main(int argc, char **argv)
  113. {
  114.     char namebuf[80];
  115.     char *cp;
  116.     int i;
  117.     int preview = 0;
  118.     int reboot = 1;
  119.     int delay = 0;
  120.  
  121.     progname = *argv;
  122.     /* reduce to base name */
  123.     if ((cp=strrchr(progname,'\\'))!=NULL) progname = cp+1;
  124.     if ((cp=strrchr(progname,'/'))!=NULL) progname = cp+1;
  125.     if ((cp=strchr(progname,'.'))!=NULL) *cp = '\0';
  126.  
  127.     if (argc == 1) {
  128.         fputs("Usage: ", stderr);
  129.         fputs(progname, stderr);
  130.         fputs(" [-p|-n] [-delay] optionname", stderr);
  131.         exit(1);
  132.     }
  133.  
  134.     while (argc > 2) {    /* check for options */
  135.         argv++;
  136.         argc--;
  137.         if (strcmp(*argv,"-p") == 0) {
  138.             preview = 1;    /* preview mode -- output to stdout */
  139.             reboot = 0;
  140.             continue;
  141.         }
  142.  
  143.         if (strcmp(*argv,"-n") == 0) {
  144.             reboot = 0; /* disable reboot */
  145.             continue;
  146.         }
  147.  
  148.         if ((**argv=='-') && sscanf(*argv+1,"%d",&delay) == 1 && delay > 0) {
  149.             /* delay the reboot */
  150.             continue;
  151.         }
  152.         die("Command switch % not recognized",*argv);
  153.     }
  154.  
  155.     strcpy(option,"'");        /* option string */
  156.     strcat(option,*++argv);
  157.     strcat(option,"'");
  158.  
  159.     if ((cp=getenv(BOOTDRIVE)) == NULL)
  160.         cp = getenv(COMSPEC);    /* drive letter to boot from */
  161.     root[0] = cp[0];
  162.  
  163.     strcpy(namebuf,root);
  164.     strcat(namebuf,CONFIGIN);
  165.     if ((infile=fopen(namebuf,READONLY))==NULL)
  166.         die("file % not found", namebuf);
  167.  
  168.     if (getline() == 0)
  169.         die("empty input file", NULL);
  170.  
  171.     if (strstr(linebuf,option)==NULL)
  172.         die("restart option % not found",option);
  173.  
  174.     if (getline() == 0 || *linebuf != '*')    /* had better be file name */
  175.         die("No files specified", NULL);
  176.  
  177.     while (*linebuf == '*')    { /* continue while file being read */
  178.  
  179.         if ((cp=strchr(linebuf+1,' '))!=NULL) *cp = '\0';
  180.         else linebuf[strlen(linebuf)-1] = '\0';
  181.         if (preview) printf("\n%s:\n",linebuf+1);
  182.         else if((outfile=fopen(linebuf+1, WRITEONLY)) == NULL)
  183.             die("cannot write %",linebuf+1);
  184.  
  185.         i = getline();
  186.  
  187.         while (i>0 && *linebuf!= '*') {
  188.             if (linebuf[0] == '\'' ) {    /* conditional line */
  189.                 if (strstr(linebuf,option) != NULL) dumpclean();
  190.             }
  191.             else if (linebuf[0] == '~') {
  192.                 if (strstr(linebuf,option) == NULL) dumpclean();
  193.             }
  194.             else /* unconditional line */
  195.                 fputs(linebuf,outfile);
  196.             
  197.             i = getline();    /* read the next line */
  198.         }
  199.  
  200.         if (preview == 0) fclose(outfile);
  201.     }
  202.  
  203.     if (reboot)    {
  204.         if (delay != 0) sleep(delay);
  205.         boot();
  206.     }
  207.  
  208. }
  209.