home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / ffld.zip / FFLD.C < prev    next >
C/C++ Source or Header  |  1991-08-05  |  6KB  |  241 lines

  1.  
  2. /*
  3.  
  4.     ffld.c--
  5.  
  6.     Display structure of a dBASE data file.
  7.     Copyright (C) 1991 by Mark W. Schumann
  8.  
  9.     Mark W. Schumann
  10.     November 1990-August 1991
  11.  
  12.     Added noextension() December 1990 to more accurately
  13.     determine whether an extension needs to be added to
  14.     the argument.
  15.  
  16.     July 1991, added wildcard logic and informative
  17.     introduction message.  August 1991, fixed bug in
  18.     error output that caused system to lock up.
  19.  
  20.     -------------------------------------------------------------------
  21.  
  22.     To create FFLD.COM with Turbo C, Turbo C++, or Borland C++:
  23.  
  24.                                 tcc -lt -mt ffld.c
  25.  
  26.                                         or
  27.  
  28.                                 bcc -lt -mt ffld.c
  29.  
  30.     -------------------------------------------------------------------
  31.  
  32.  
  33.     This program is free software; you can redistribute it and/or modify
  34.     it under the terms of the GNU General Public License as published by
  35.     the Free Software Foundation; either version 1, or (at your option)
  36.     any later version.
  37.  
  38.     This program is distributed in the hope that it will be useful,
  39.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  40.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  41.     GNU General Public License for more details.
  42.  
  43.     You should have received a copy of the GNU General Public License
  44.     along with this program; if not, write to the Free Software
  45.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  46.  
  47. */
  48.  
  49. #include <dir.h>
  50. #include <errno.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53.  
  54. #define PATHLEN 79         /* Maximum DOS file specification size */
  55.  
  56. int datadict = 0;
  57.  
  58. int noextension (char *);
  59. int ffmatch (char *);
  60. int ff (FILE *, char *);
  61. void info (void);
  62.  
  63. int main (int argc, char *argv[])
  64.  
  65. {
  66.  
  67. char database[PATHLEN];
  68. char *u;
  69. int okay;
  70. struct ffblk fff;
  71. char path[PATHLEN];
  72. char target[PATHLEN];
  73. char *w;
  74. int i;
  75. int dashflag = 0;
  76.  
  77.     if (argc < 2) {
  78.         fprintf (stderr, "FFLD ■ Mark W. Schumann, 1991\n");
  79.         fprintf (stderr, "Usenet: catfood@ncoast.org; CI$: 73750,3527\n");
  80.         fprintf (stderr, " Display field structure of a dBASE file.\n");
  81.         fprintf (stderr, " Syntax: FFLD [-d] <database>\n\n");
  82.         fprintf (stderr, " Wildcards and path specifications are valid.\n");
  83.         fprintf (stderr, " The -d option creates a columnar output format.\n");
  84.         fprintf (stderr, " FFLD comes with ABSOLUTELY NO WARRANTY.  It is free software, and\n");
  85.         fprintf (stderr, "  you are welcome to redistribute it under certain conditions; read\n");
  86.         fprintf (stderr, "  the file COPYING for details.\n\n");
  87.         printf ("Database? ");
  88.         fgets (database, PATHLEN, stdin);
  89.         /* Trim the carriage return off the database name. */
  90.         for (u = database; *u; u++) if (*u == '\n') *u = 0;
  91.         ffmatch (database);
  92.         }
  93.  
  94.     else for (i = 1; i < argc; i++) {
  95.         if (strcmp (argv[i], "--") == 0) {
  96.             dashflag = 1;
  97.             continue;
  98.             }
  99.         if (!dashflag && strcmp (argv[i], "-d") == 0) {
  100.             datadict = 1;
  101.             continue;
  102.             }
  103.         strcpy (database, argv[i]);
  104.         ffmatch (database);
  105.         }
  106.  
  107.     return 0;
  108.     }
  109.  
  110. int ffmatch (char *database)
  111.  
  112. {
  113.  
  114. FILE *f;
  115. int okay;
  116. struct ffblk fff;
  117. char path[PATHLEN];
  118. char target[PATHLEN];
  119. char *u;
  120. char *w;
  121.  
  122.     strupr (database);
  123.  
  124.     /* if (strchr (database, '.') == NULL) */
  125.     if (noextension (database))
  126.         strcat (database, ".DBF");
  127.  
  128.     for (u = database, w = NULL; *u; u++) {
  129.         if (*u == '\\') w = u;
  130.         }
  131.  
  132.     /* w now points to the last backslash in database */
  133.     /* path[0] = 0;  Heh, heh... clear the path... */
  134.     memset (path, 0, PATHLEN);
  135.     if (w != NULL) strncpy (path, database, w - database);
  136.  
  137.     okay = 0;
  138.  
  139.     if (findfirst (database, &fff, 0) == 0) do {
  140.         strcpy (target, path);
  141.         if (w != NULL) strcat (target, "\\");
  142.         strcat (target, fff.ff_name);
  143.         if ((f = fopen (target, "rb")) != NULL) {
  144.              ff (f, target);
  145.             fclose (f);
  146.             }
  147.         else {
  148.             fprintf (stderr, "Could not open file %s for input.\n", target);
  149.             fprintf (stderr, "(DOS error code is %d.)\n", _doserrno);
  150.             okay = _doserrno;
  151.             break;
  152.             }
  153.         } while (findnext (&fff) == 0);
  154.     else {
  155.         fprintf (stderr, "Could not find a match for file name %s.\n", database);
  156.         okay = -1;
  157.         }
  158.     return okay;
  159.     }
  160.  
  161. int ff (FILE *f, char *filename)
  162.  
  163. {
  164.  
  165. unsigned int headersize;
  166. unsigned int fieldcount;
  167. short int i;
  168. char buffer[33];
  169. int offset = 1;
  170. int end;
  171. unsigned char id_byte;
  172.  
  173.     fseek (f, 0L, SEEK_SET);
  174.     fread (&id_byte, 1, sizeof (char), f);
  175.     if (id_byte != 3) {
  176.         fprintf (stderr, "%s is not a dBASE file!\n", filename);
  177.         return 0;
  178.         }
  179.  
  180.     fseek (f, 8L, SEEK_SET);
  181.     fread (&headersize, 1, sizeof (headersize), f);
  182.     fieldcount = headersize / 32 - 1;
  183.  
  184.     if (datadict) {
  185.         printf ("*%s\n", filename);
  186.         }
  187.     else {
  188.         if (fieldcount == 1)
  189.             printf ("1 field in database %s:\n", filename);
  190.         else
  191.             printf ("%d fields in database %s:\n", fieldcount, filename);
  192.         printf ("  Field   Name         Type    Len    Dec Offset   End\n");
  193.         }
  194.  
  195.     for (i = 1; i <= fieldcount; i++) {
  196.         fseek (f, (long) 32 * i, SEEK_SET);
  197.         fread (buffer, 32, sizeof (char), f);
  198.  
  199.         if (datadict) {
  200.             printf ("%10.10s %c %03d %03d\n",
  201.              buffer, buffer[11], buffer[16], buffer[17]);
  202.             }
  203.  
  204.         else {
  205.             printf ("  %3d     %10.10s      %c    %3d",
  206.                 i, buffer, buffer[11], buffer[16]);
  207.  
  208.             if (buffer[11] == 'N')
  209.                 printf ("     %2d", buffer[17]);
  210.             else
  211.                 printf ("       ");
  212.             }
  213.  
  214.         end = offset + buffer[16] - 1;
  215.         if (!datadict)
  216.             printf ("   %3d    %3d\n", offset, end);
  217.         offset = end + 1;
  218.         }
  219.     return 0;
  220.     }
  221.  
  222.  
  223. int noextension (char *filename)
  224.  
  225. {
  226.  
  227. char *ptr;
  228. register int period = 0;
  229.  
  230. /*
  231.     If the last period is after the last slash, an
  232.     extension exists.  Otherwise no extension exists.
  233. */
  234.  
  235.     for (ptr = filename; *ptr; ptr++) {
  236.         if (*ptr == '.') period = 1;
  237.         if (*ptr == '\\') period = 0;
  238.         }
  239.     return !period;
  240.     }
  241.