home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 042.lha / c / ask2.1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-25  |  3.5 KB  |  95 lines

  1. /*  ASK v2.1        A CLI command written in C.           June 25,1987
  2. *   (c)Copyright 1987 by Ed Holzknecht
  3. *
  4. *   This is based on a assembly program by Dewi Williams called "ask".
  5. *   I wrote Ask1.0 as a C version of the assembly program.
  6. *   This is my new version of ASK. It has one main difference from the old
  7. *     version. This one is user-definable. In other words, you can define
  8. *     any test string you want, without the need to re-compile the code.
  9. *    
  10. *   EXAMPLE:   ask d newtest
  11. *          Ask would now be set to test for 'newtest' in a batch file:
  12. *
  13. *                  echo "Load oldtest or newtest"
  14. *                  failat 20
  15. *                  ask
  16. *                  if not error
  17. *                     run newtest
  18. *                  else
  19. *                     run oldtest
  20. *   NOTE: The test string should be 7 characters or less in length.
  21. *         When ASK is re-defined, it MUST be in the root directory, or the
  22. *           file won't be found!!
  23. *         The test string IS case dependent - Test is NOT the same as test!
  24. *         A carriage return as a response will NOT return an error from ASK.
  25. *
  26. *   Compiled with Manx Aztec C, version 3.20. 
  27. *   Use, abuse, misuse, or copy this to your heart's content; as long as it
  28. *   is a non-commercial application. If you can re-write it so the program
  29. *   is better or smaller, let me know how you did it!
  30. *
  31. *                                         Ed Holzknecht
  32. *                                         CIS  70357,3331
  33. *       or call Blacksmith's Shoppe BBS   502-776-4313   11:00pm - 9:00am
  34. *               Louisville, Ky.
  35. ***************************************************************************/
  36. #include "stdio.h"
  37.  
  38. char *defstr = "\0\0\0\0\0\0\0\0";  /* The string that gets re-defined */
  39. FILE *fp;
  40.  
  41. main(argc,argv)
  42. int  argc;
  43. char *argv[];
  44. {
  45.    int   c,i=0;
  46.    char  a[8];
  47.    void  rewrite_it();
  48.  
  49.    if(argc > 1) {
  50.       if(strcmp(argv[1], "d")) {
  51.          puts("USAGE...ASK d newtest");
  52.          puts("Newtest must be 7 characters or less.");
  53.          _exit(10);
  54.       }
  55.       rewrite_it(argv);
  56.    }
  57.    while((a[i++] = getchar()) != '\n')    /* This gets the string to be  */
  58.       ;                                   /* tested and NULL terminates  */
  59.    a[--i] = '\0';                         /* it. Then strncmp tests the  */
  60.    if((strncmp(defstr,a,i)) == 0)         /* string against 'defstr' for */
  61.       _exit(0);                           /* a length of 'i' characters. */
  62.    else
  63.       _exit(10);
  64. }
  65.  
  66. void rewrite_it(argv)
  67. char *argv[];
  68. {
  69.    long offset = 0x002e;             /* Offset for the re-write from start*/
  70.    int  count,space;                 /* of file.                          */
  71.  
  72.    count = strlen(argv[2]);          /*  This may be sloppy, but it's the */
  73.    if(count > 7) {                   /*  only way I could figure out how  */
  74.       puts("Too many characters. Limit 7.");
  75.       _exit(10);                     /*  to pad the string with NULLs so  */
  76.    }                                 /*  the string test would work.      */ 
  77.    space = 8-count;
  78.    for(;space == 0; space--)
  79.          argv[2][++count] = '\0';
  80.    if((fp = fopen("ask", "r+")) == NULL) {
  81.       puts("Can't open ASK for re-definition.");
  82.       _exit(10);
  83.    }
  84.    fseek(fp,offset,0);                     /* This finds the place on the */
  85.    if(fwrite(argv[2],1,8,fp)== -1) {       /* disk where the string lives */
  86.       puts("ERROR. Write failed.");        /* and writes the replacement. */
  87.       exit(10);
  88.    }
  89.    puts("ASK re-defined.");
  90.    exit(0);
  91.  
  92. }
  93.  
  94.  
  95.