home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / WHAT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  7KB  |  261 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /****************************************************************************
  4. *     WHAT.c
  5. *  @(#)
  6. *     @(#) Scans binary or text files for a token and prints the line following
  7. *     @(#) the token on stdout (normally the screen).
  8. *     @(#) The token is by default @(#) like in the UNIX version of WHAT.
  9. *  @(#)
  10. *
  11. *
  12. *
  13. *** CORRECTIONS *************************************************************
  14. *1995-07-30/Bac:
  15. *-    FindStringInFile() included explicit in project and recompiled.
  16. *-    DEBUGTRACE inserted and iDebugFlag added
  17. *1995-09-20/Bac:  v. 0.03
  18. *-    fsif() ajusted of searching in last block of a file.
  19. *1995-10-26/Bac: v. 0.04
  20. *-    Rewritten for public use
  21. *
  22. *** BUGS & FLAWS **********************************************************
  23. *
  24. *
  25. *****************************************************************************
  26. *@(#)1995 Erik Bachmann                                                      *
  27. *
  28. * Released to public domain 27-Oct-95
  29. *****************************************************************************/
  30. #include    "bacstd.h"                                      /* */
  31. #include    <string.h>                                      /* strlen(),  */
  32. #include    <stdio.h>                                       /* FILE */
  33. #include    "dirport.h"
  34.  
  35.  
  36. PROGRAMINF("WHAT", "0.04", "1995-06-18", "(p)1995 Erik Bachmann");
  37.  
  38. /*** MACROS *****************************************************************/
  39. #define     MAXPATH     80
  40. #define     MAXLINE     256
  41.  
  42. /* TRACE for debugging is NOT implemented in this version */
  43. #define     TRACEFILE               NULL
  44. #define     ON_DEBUG                NULL;
  45. #define     TRACE                   _0
  46. #define     DEBUGTRACE              _0
  47.  
  48. char  *pszErrMsg  = NULL;
  49.  
  50.  
  51.  
  52. /*** PROTOTYPES ************************************************************/
  53. int main(int argc, char *argv[]);
  54.  
  55.  
  56. /*** GLOBAL VARABLES *******************************************************/
  57.  
  58. static char szInputFile[MAXPATH];
  59. static char szToken[MAXLINE] = "\x40(#)"; /* Token to identify comments */
  60.  
  61.  
  62. const char  *HELPFORMAT =
  63. {
  64.       "Scans binary or text files for a token and prints the line following\n"
  65.       "the token on stdout (normally the screen).\n"
  66.       "The token is by default \x40(#) like in the UNIX version of WHAT.\n\n"
  67.       "CALL: %Fs <filename> {/Ttoken}\n\n"
  68.       "\tfilename\tfilename or wildcard\n"
  69.       "\t/T\tToken if standard \x40(#) is not used.\n\n"
  70. };
  71.  
  72. /*** FUNCTIONS *************************************************************/
  73. /* Dummy function */
  74.  
  75. int _0(char *fmt, ...)
  76. {     }     /*** _0 ***/
  77.  
  78. /*
  79.  /=======================================\
  80. |      SEARCH_FILE
  81. |-------------------------------------|
  82. |\=======================================/
  83. |
  84. |     Scanning through a binary file for a token. Prints the following line on
  85. |  stdout.
  86. |
  87. |
  88. |-----------------------------------------------------------------------------|
  89. | CALL:
  90. |    search_file(ffblk.ff_name);
  91. |
  92. | HEADER:
  93. |    %
  94. |
  95. | GLOBALE VARIABLES:
  96. |    szToken                  The Token to search for
  97. |
  98. | ARGUMENTS:
  99. |    *szInputFile       Name of a file to scan for template strings
  100. |
  101. |
  102. | PROTOTYPE:
  103. |    int  search_file(char *szInputFile)
  104. |
  105. | RETURN VALUE:
  106. |    int
  107. |
  108. | MODULE:
  109. |    what.c
  110. |
  111. |-KNOWN ERRORS AND FLAWS------------------------------------------------------|
  112. |
  113. |
  114. |
  115. |
  116. |-CORRECTIONS-----------------------------------------------------------------|
  117. |
  118. |
  119. |
  120. |
  121. |-----------------------------------------------------------------------------|
  122. |1995-06-20/Erik Bachmann
  123. \===========================================================================|*/
  124.  
  125. int  search_file(char *szInputFile)
  126. {
  127.       int         iStatusFlag       = 0;                    /* Return value */
  128.       int         iTokenLength      = 0;
  129.       char        szStr[MAXLINE+1];
  130.       long        lStartOffset      = 0L;
  131.  
  132.       FILE        *fpInFile;
  133.  
  134.       /*-------------------------------------*/
  135.  
  136.       iTokenLength = strlen(szToken);
  137.  
  138.       if (0 >= iTokenLength)
  139.       {
  140.             pszErrMsg   = "Token is too short (ie. < 1)";
  141.             fprintf(stderr, pszErrMsg);
  142.             DEBUGTRACE(TRACEFILE, pszErrMsg);
  143.             return(1);
  144.       }
  145.  
  146.       if (0 == (fpInFile = fopen(szInputFile, "rb")))
  147.       {
  148.             fprintf(stderr, "Cannot open input file: %s", szInputFile);
  149.             return(1);
  150.       }
  151.  
  152.       lStartOffset = (long) find_str_in_file(fpInFile, lStartOffset, szToken);
  153.  
  154.       if (0 <= lStartOffset)
  155.       {
  156.             fprintf(stdout, "\n - %s\n", szInputFile);
  157.             /* First found -> write filename */
  158.       }
  159.  
  160.       while (0 <= lStartOffset)
  161.       {
  162.             ON_DEBUG { TRACE(TRACEFILE, "\n\t%s found in %s offset %lu",
  163.                         szToken, szInputFile, lStartOffset); }
  164.  
  165.             /* Write to file if debug flag in active */
  166.  
  167.             lStartOffset += (long)strlen(szToken);
  168.  
  169.             /* Go beyond token */
  170.  
  171.             fseek(fpInFile, lStartOffset, SEEK_SET);
  172.  
  173.             /* Go to start offset */
  174.  
  175.             fgets(szStr, MAXLINE, fpInFile);
  176.  
  177.             /* Get the string after token */
  178.  
  179.             strtrimr(&szStr);                   /* Remove trailing line feed */
  180.  
  181.             fprintf(stdout, "%s\n", szStr);
  182.  
  183.             /* Write string */
  184.  
  185.             lStartOffset += (long)strlen(szStr);
  186.  
  187.             /* Next search starts from end of string */
  188.  
  189.             lStartOffset = (long)find_str_in_file(fpInFile,
  190.                   (long)lStartOffset, szToken);
  191.  
  192.             /* Find next token */
  193.       }
  194.       fclose(fpInFile);
  195.  
  196.       return(iStatusFlag);                      /* Return default value */
  197. }
  198.  
  199.  
  200. /**** Main *****************************************************************/
  201.  
  202. /*
  203.  /=======================================\
  204. |   MAIN
  205. |-------------------------------------|
  206. |\=======================================/
  207. |
  208. | Looping through files matching the first argument
  209. |
  210. |
  211. |
  212. |
  213. | Virtual main()
  214. |
  215. | See template.c
  216. |
  217. |-KNOWN ERRORS AND FLAWS------------------------------------------------------|
  218. |
  219. |
  220. |
  221. |
  222. |-CORRECTIONS-----------------------------------------------------------------|
  223. |
  224. |
  225. |
  226. |
  227. |-----------------------------------------------------------------------------|
  228. |1995-06-19/Erik Bachmann
  229. \===========================================================================|*/
  230.  
  231. int main(int argc, char *argv[])
  232. {
  233.       DOSFileData       ffblk;                        /* File control block */
  234.       int                     done;                   /* Done flag */
  235.  
  236.       /*--------------------------------*/
  237.  
  238.       fprintf(stderr, "\n%Fs v. %Fs : %s\n", PROGNAME, PROGVER,
  239.               stModulInfo.pszCopyright);
  240.       fprintf(stdout, "Argument: %s\n", argv[1]);
  241.  
  242.       if ('?' == argv[1][1])
  243.       {
  244.             fprintf(stderr, HELPFORMAT, argv[0]);
  245.             exit();
  246.       }
  247.       strcpy(szInputFile, argv[1]);
  248.  
  249.       done = FIND_FIRST(szInputFile, 0, &ffblk);
  250.  
  251.       /* Find first file */
  252.  
  253.       while (!done)
  254.       {
  255.             search_file(ff_name(&ffblk));  /* Search file for tokens */
  256.             done = FIND_NEXT(&ffblk);      /* Find next matching file */
  257.       }
  258.  
  259.       return(0);
  260. }
  261.