home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / util2src / ftest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  18.5 KB  |  527 lines

  1. /*============================================================================*
  2.  * main() module: ftest.c - Test various file functions.  OS/2 2.0 and C Set/2
  3.  *
  4.  * (C)Copyright IBM Corporation, 1991, 1992                      Brian E. Yoder
  5.  *
  6.  * 04/05/91 - Created.
  7.  * 04/17/91 - Initial version.
  8.  * 04/29/91 - Ported to OS/2.
  9.  * 05/06/91 - Updated per new slrewind() interface in speclist.c file.
  10.  * 05/09/91 - Updated per new slmake() interface in speclist.c file.
  11.  * 05/16/91 - Added check for maximum system path length.
  12.  * 04/13/92 - Added tests for current drive and filesystem type, and ishpfs().
  13.  *            The structure and info for DosQFSAttach() is in bsedos.h.
  14.  * 07/23/92 - Ported to OS/2 2.0 and removed query for max. pathname.
  15.  * 07/24/92 - Added getwidth() function from ls.c, for testing.
  16.  * 07/30/92 - Added IsDir() to test stat() under 2.0 and with long HPFS names.
  17.  *            The C Set/2 migration library version of stat() works!
  18.  *            (The IBM C/2 1.1 library version of stat() didn't.)
  19.  * 09/19/92 - Print the bpathlen field of SLPATH, and added test for makepath().
  20.  * 04/20/93 - Fix an apparent bug in the D option processing whereby the ptr   .
  21.  *            to the remote name was 2 bytes past where it should have been.
  22.  *            The original code worked for 16-bit OS/2 programs, but not
  23.  *            for 32-bit programs. Strange--needs to be looked into sometime.
  24.  *============================================================================*/
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31.  
  32. #include "util.h"
  33.  
  34. /*----------------------------------------------------------------------------*
  35.  * Static data
  36.  *----------------------------------------------------------------------------*/
  37.  
  38. static  int   uniq = TRUE;
  39.  
  40. static  char  buff[2048];
  41.  
  42. static  PATH_NAME *pn;
  43. static  char  path[1024];
  44. static  char  name[1024];
  45.  
  46. static  ushort  maxpath;            /* Maximum system path length */
  47.  
  48. /*============================================================================*
  49.  * Define internal functions to allow forward access
  50.  *============================================================================*/
  51.  
  52. static void       syntax       ( void );
  53. static uint       getwidth     ( void );
  54. static int        IsDir        ( char * );
  55.  
  56. /*============================================================================*
  57.  * Main Program Entry Point
  58.  *============================================================================*/
  59.  
  60. main(argc, argv)
  61.  
  62. int argc;                           /* arg count */
  63. char *argv[];                       /* arg pointers */
  64.  
  65. {
  66.   int     rc;                       /* Return code store area */
  67.   char   *str;                      /* Pointer to string */
  68.   char    id;                       /* Test id */
  69.   int     dwidth;                   /* Width of display */
  70.  
  71.   ushort  mflags;                   /* For slrewind() */
  72.   SLPATH *speclist;                 /* Pointer to linked list of path specs */
  73.  
  74.   FINFO  *fdata;                    /* Pointers returned by slmatch() */
  75.   SLPATH *pdata;
  76.   SLNAME *ndata;
  77.  
  78.   ulong   lval;
  79.   char   *endcvt;
  80.  
  81.   ulong   drivenum;                 /* Drive number */
  82.   ulong   drivemap;                 /* Drive map */
  83.   char    drivestr[3];              /* String: "D:" */
  84.   char   *fsname = "";              /* Pointer to filesystem name */
  85.  
  86.   char    sbuff[128];               /* Character array buffer */
  87.   ULONG   bufferlen = sizeof(sbuff);
  88.   FSQBUFFER2 *buffer;               /* For DosQFSAttach() */
  89.   USHORT *ubuff;                    /* Pointer to USHORT array */
  90.  
  91.  /*----------------------------------------------------------------------------*
  92.   * Check arguments
  93.   *----------------------------------------------------------------------------*/
  94.  
  95.   argc--;                 /* Ignore 1st argument (program name) */
  96.   argv++;
  97.  
  98.   if (argc < 1)           /* Be sure there are at least two arguments */
  99.      syntax();
  100.  
  101.   id = *(argv[0]);        /* Store test identifier */
  102.  
  103.   argc--;                 /* Bump args past test identifier */
  104.   argv++;
  105.  
  106.  /*----------------------------------------------------------------------------*
  107.   * In case we call slrewind, set up mflags:
  108.   *----------------------------------------------------------------------------*/
  109.  
  110.   mflags = 0;
  111.   mflags = mflags | SL_NORMAL |
  112.                     SL_SYSTEM |
  113.                     SL_HIDDEN |
  114.                     SL_DIR    |
  115.                     SL_DOTDIR |
  116.                     SL_VOLID;
  117.  
  118.  /*----------------------------------------------------------------------------*
  119.   * Run tests, depending upon test id:
  120.   *----------------------------------------------------------------------------*/
  121.  
  122.   switch (id)
  123.   {
  124.     /*---------------------------------------------------------*
  125.      * Test decompose()
  126.      *---------------------------------------------------------*/
  127.      case 'd':
  128.         if (argc < 1)
  129.         {
  130.            printf("ftest d [d:][path]name\n");
  131.            return(2);
  132.         }
  133.         printf("Testing decompose()...\n");
  134.         printf("String is '%s'...\n", argv[0]);
  135.         pn = decompose(argv[0]);
  136.  
  137.         memcpy(path, pn->path,       /* Store path portion and terminate it */
  138.                      pn->pathlen);
  139.         *(path+pn->pathlen) = '\0';
  140.  
  141.         memcpy(name, pn->name,       /* Store name portion and terminate it */
  142.                      pn->namelen);
  143.         *(name+pn->namelen) = '\0';
  144.  
  145.         printf("[d:path] = '%s'\n", path);
  146.         printf("name     = '%s'\n", name);
  147.         break;
  148.  
  149.     /*---------------------------------------------------------*
  150.      * Test isdir()
  151.      *---------------------------------------------------------*/
  152.      case 'i':
  153.         if (argc < 1)
  154.         {
  155.            printf("ftest i pathname\n");
  156.            return(2);
  157.         }
  158.         printf("Testing isdir()...\n");
  159.         rc = isdir(argv[0]);
  160.         if (rc == TRUE)
  161.            printf("    String '%s' is a directory.\n", argv[0]);
  162.         else
  163.            printf("--- String '%s' is not a directory.\n", argv[0]);
  164.         break;
  165.  
  166.     /*---------------------------------------------------------*
  167.      * Test for directory using stat().
  168.      *---------------------------------------------------------*/
  169.      case 'I':
  170.         if (argc < 1)
  171.         {
  172.            printf("ftest I pathname\n");
  173.            return(2);
  174.         }
  175.         printf("Testing stat()...\n");
  176.         rc = IsDir(argv[0]);
  177.         if (rc == TRUE)
  178.            printf("    String '%s' is a directory.\n", argv[0]);
  179.         else
  180.            printf("--- String '%s' is not a directory.\n", argv[0]);
  181.         break;
  182.  
  183.     /*---------------------------------------------------------*
  184.      * Check current drive and its filesystem
  185.      *---------------------------------------------------------*/
  186.      case 'D':
  187.         printf("Querying current drive's filesystem...\n");
  188.  
  189.         rc = DosQueryCurrentDisk(&drivenum, &drivemap);
  190.  
  191.         drivestr[0] = 'A' + drivenum - 1;
  192.         drivestr[1] = ':';
  193.         drivestr[2] = '\0';
  194.  
  195.         printf("Current drive: %s\n", drivestr);
  196.  
  197.         rc = DosQFSAttach(drivestr,        /* Query FS name for this drive */
  198.                       0L,                  /* Ordinal is ignored */
  199.                       FSAIL_QUERYNAME,     /* Info level: query name */
  200.                       (FSQBUFFER2 *)sbuff, /* Pointer to info buffer */
  201.                       &bufferlen);         /* Size of buffer */
  202.  
  203.         printf("DosQFSAttach() returned %d\n", rc);
  204.  
  205.         if (rc == 0)                       /* If ok: */
  206.         {
  207.            ubuff = (USHORT *)sbuff;             /* Point ubuff to start of buffer */
  208.            buffer = (FSQBUFFER2 *)sbuff;        /* Point buffer to FS info hdr */
  209.            switch (buffer->iType)               /* Check iType of filesystem: */
  210.            {
  211.               case FSAT_LOCALDRV:
  212.                  fsname = &sbuff[ 7 + ubuff[2] ];    /* FAT or HPFS */
  213.                  break;
  214.  
  215.               case FSAT_REMOTEDRV:
  216.                  fsname = &sbuff[ 7 + ubuff[2] ];    /* usually LAN */
  217.  
  218.                  str = &sbuff[ 7 + ubuff[2] ];
  219.                  str += strlen(str);
  220.                  str += 1;
  221. //(4/20/93)--->  str += 2;
  222.                  printf("Remote name:   %s\n", str);
  223.  
  224.                  break;
  225.  
  226.               default:
  227.                  fsname = "*unknown*";
  228.                  break;
  229.            }
  230.  
  231.            printf("Filesystem:    %s\n", fsname);
  232.         }
  233.  
  234.         break;
  235.  
  236.     /*---------------------------------------------------------*
  237.      * Test ishpfs()
  238.      *---------------------------------------------------------*/
  239.      case 'h':
  240.         if (argc < 1)
  241.         {
  242.            printf("ftest h pathname\n");
  243.            return(2);
  244.         }
  245.         printf("Testing ishpfs()...\n");
  246.         rc = ishpfs(argv[0]);
  247.         if (rc == TRUE)
  248.            printf("    Filesystem on %s is HPFS.\n", argv[0]);
  249.         else
  250.            printf("    Filesystem on %s is *not* HPFS.\n", argv[0]);
  251.         break;
  252.  
  253.     /*---------------------------------------------------------*
  254.      * Test pathcat()
  255.      *---------------------------------------------------------*/
  256.      case 'p':
  257.         if (argc < 2)
  258.         {
  259.            printf("ftest p [d:][path] [name]\n");
  260.            return(2);
  261.         }
  262.         printf("Testing pathcat()...\n");
  263.         if (argc != 2)
  264.         {
  265.            printf("--- Two strings needed for pathcat().\n");
  266.            return(0);
  267.         }
  268.         strcpy(buff, argv[0]);
  269.         pathcat(buff, argv[1]);
  270.         printf("Result: '%s'\n", buff);
  271.         break;
  272.  
  273.     /*---------------------------------------------------------*
  274.      * Test slmake()
  275.      *---------------------------------------------------------*/
  276.      case 's':
  277.         if (argc < 1)
  278.         {
  279.            printf("ftest s fspec ...\n");
  280.            return(2);
  281.         }
  282.         printf("Testing slmake()...\n");
  283.         rc = slmake(&speclist, uniq, TRUE, argc, argv);
  284.         switch (rc)
  285.         {
  286.            case 0:
  287.               break;
  288.  
  289.            case REGX_MEMORY:
  290.               fprintf(stderr, "Out of memory while processing '%s'.\n",
  291.                  slerrspec());
  292.               return(1);
  293.               break;
  294.  
  295.            default:
  296.               fprintf(stderr, "Invalid filespec: '%s'.\n",
  297.                  slerrspec());
  298.               return(1);
  299.               break;
  300.         }
  301.         sldump(speclist);            /* Dump the specification list */
  302.         break;
  303.  
  304.     /*---------------------------------------------------------*
  305.      * Test slmatch() (need to call slmake() first)
  306.      *---------------------------------------------------------*/
  307.      case 'm':
  308.         if (argc < 1)
  309.         {
  310.            printf("ftest m fspec ...\n");
  311.            return(2);
  312.         }
  313.         printf("Testing slmatch()...\n");
  314.         rc = slmake(&speclist, uniq, TRUE, argc, argv);
  315.         switch (rc)
  316.         {
  317.            case 0:
  318.               break;
  319.  
  320.            case REGX_MEMORY:
  321.               fprintf(stderr, "Out of memory while processing '%s'.\n",
  322.                  slerrspec());
  323.               return(1);
  324.               break;
  325.  
  326.            default:
  327.               fprintf(stderr, "Invalid filespec: '%s'.\n",
  328.                  slerrspec());
  329.               return(1);
  330.               break;
  331.         }
  332.         printf("'Pathname'  'Filename'  'Filename specification':\n");
  333.         slrewind(speclist, mflags,   /* Initialize slmatch() */
  334.                            TRUE);
  335.         for (;;)                     /* Loop to find all matching files: */
  336.         {
  337.            fdata = slmatch(&pdata,
  338.                            &ndata);
  339.            if (fdata == NULL)
  340.               break;
  341.            printf("   '%s' (%u) '%s' '%s'%s\n",
  342.               pdata->path,
  343.               pdata->bpathlen,
  344.               fdata->achName,               /* For OS/2 */
  345.               ndata->name,
  346.               pdata->inserted ? " (inserted)" : "");
  347.         }
  348.         break;
  349.  
  350.     /*---------------------------------------------------------*
  351.      * Test SetFileMode().  File mode bits are:
  352.      *
  353.      * FILE_NORMAL    =  0x0000  =  0000 0000 (binary)
  354.      * FILE_READONLY  =  0x0001  =  0000 0001
  355.      * FILE_HIDDEN    =  0x0002  =  0000 0010
  356.      * FILE_SYSTEM    =  0x0004  =  0000 0100
  357.      * FILE_DIRECTORY =  0x0010  =  0001 0000
  358.      * FILE_ARCHIVED  =  0x0020  =  0010 0000
  359.      *---------------------------------------------------------*/
  360.      case 'f':
  361.         if (argc < 2)
  362.         {
  363.            printf("ftest f mode filename\n");
  364.            return(2);
  365.         }
  366.  
  367.         lval = strtoul(argv[0],      /* Convert to ulong, using */
  368.                        &endcvt,      /* 1st char of string to */
  369.                        0);           /* determine the base */
  370.  
  371.         if (*endcvt != '\0')
  372.         {
  373.            printf("'%s' is not a valid number.\n", argv[0]);
  374.            return(2);
  375.         }
  376.  
  377.         printf("Testing SetFileMode()...\n");
  378.         printf("Mode: 0x%08x   File: %s\n", lval, argv[1]);
  379.  
  380.         rc = SetFileMode(argv[1], (uint)lval );
  381.         printf("Return value: %d\n", rc);
  382.         break;
  383.  
  384.     /*---------------------------------------------------------*
  385.      * Check display width
  386.      *---------------------------------------------------------*/
  387.      case 'w':
  388.         dwidth = getwidth();
  389.         printf("The current display window has %d columns.\n", dwidth);
  390.         break;
  391.  
  392.     /*---------------------------------------------------------*
  393.      * Test makepath()
  394.      *---------------------------------------------------------*/
  395.      case 'P':
  396.         printf("Testing makepath()...\n");
  397.         rc = makepath(argv[0]);
  398.         printf("%s\n", rc==0 ? "Ok" : "failed");
  399.         break;
  400.  
  401.     /*---------------------------------------------------------*
  402.      * We don't know about any other test id
  403.      *---------------------------------------------------------*/
  404.      default:
  405.         printf("--- Test id '%c' is not supported.\n", id);
  406.         break;
  407.   }
  408.  
  409.   return(0);                        /* Done with main(): Return */
  410.  
  411. } /* end of main() */
  412.  
  413. /*============================================================================*
  414.  * syntax() - Display command syntax and exit to operating system!
  415.  *============================================================================*/
  416. static void syntax()
  417. {
  418.   fprintf(stderr, "Usage:  ftest id string [string ...]\n");
  419.   fprintf(stderr, "\n");
  420.   fprintf(stderr, "Valid test ids:\n");
  421.   fprintf(stderr, "    d - Test decompose().\n");
  422.   fprintf(stderr, "    h - Test ishpfs().\n");
  423.   fprintf(stderr, "    i - Test isdir().\n");
  424.   fprintf(stderr, "    I - Check for directory using stat().\n");
  425.   fprintf(stderr, "    p - Test pathcat().\n");
  426.   fprintf(stderr, "    s - Test slmake().\n");
  427.   fprintf(stderr, "    m - Test slmatch().\n");
  428.   fprintf(stderr, "    D - Query current drive and its filesystem.\n");
  429.   fprintf(stderr, "    f - Test SetFileMode().\n");
  430.   fprintf(stderr, "    w - Print the display's width.\n");
  431.   fprintf(stderr, "    P - Test makepath().\n");
  432.   fprintf(stderr, "\n");
  433.   exit(1);
  434. }
  435.  
  436. /*============================================================================*
  437.  * getwidth() - Gets width of display.
  438.  *
  439.  * REMARKS:
  440.  *   This subroutine gets the width of the current display.  If this width
  441.  *   is wider than MAXWIDTH, it returns MAXWIDTH.
  442.  *============================================================================*/
  443.  
  444. static uint getwidth()
  445. {
  446.   int  rc;                          /* Return code storage */
  447.   VIOMODEINFO vmi;                  /* Return info from vio */
  448.   uint dwidth;                      /* Width of display */
  449.  
  450.   rc = VioGetMode(&vmi, 0);         /* Get video mode */
  451.  
  452.   if (rc != 0)                      /* If error: */
  453.      dwidth = 80;                      /* Assume 80 cols */
  454.   else                              /* Else no error: */
  455.   {                                    /* Extract info from buffer */
  456.      dwidth = vmi.col;
  457.   }
  458.  
  459.   return(dwidth);                   /* Return the width */
  460. }
  461.  
  462. /*============================================================================*
  463.  * IsDir() - Does a string represent a valid directory name?
  464.  *
  465.  * REMARKS:
  466.  *   This subroutine checks to see if a string represents a valid directory
  467.  *   name.
  468.  *
  469.  *   A null string represents the current directory and is a valid directory.
  470.  *
  471.  *   The stat() function call for the IBM C/2 1.1 library doesn't support
  472.  *   long filenames.  Let's see what it does using the C Set/2 migration
  473.  *   library.
  474.  *
  475.  * RETURNS:
  476.  *   FALSE, if it 'dirstr' is not a valid directory.
  477.  *   TRUE, if it is.
  478.  *============================================================================*/
  479. int IsDir(
  480.  
  481. char *dirstr )                      /* Pointer to a directory name string */
  482.  
  483. {
  484.   int rc;                           /* Return code store area */
  485.   struct stat statb;                /* stat() structure information buffer */
  486.   int dlen;                         /* Length of dirstr string */
  487.   char *enddirstr;                  /* Pointer to end of dirstr specification */
  488.  
  489.  /*---------------------------------------------------------------------------*
  490.   * Get length of dirstr and a pointer to the last character
  491.   *---------------------------------------------------------------------------*/
  492.  
  493.   dlen = strlen(dirstr);            /* Get length */
  494.   enddirstr = dirstr + dlen - 1;    /* Store pointer to the end */
  495.  
  496.   if (dlen == 0)                    /* If string is zero length: */
  497.      return(TRUE);                       /* It's the current directory */
  498.  
  499.  /*---------------------------------------------------------------------------*
  500.   * Take care of checks for special cases
  501.   *---------------------------------------------------------------------------*/
  502.  
  503.   if (strcmp(dirstr, ".") == 0)     /* If "." it's a valid dir spec */
  504.      return(TRUE);
  505.  
  506.   if (strcmp(dirstr, "..") == 0)    /* If ".." it's a valid dir spec */
  507.      return(TRUE);
  508.  
  509.   if ((dlen == 2) &&                /* If "d:" drive spec: it's valid */
  510.       (*enddirstr == ':'))
  511.      return(TRUE);
  512.  
  513.  /*---------------------------------------------------------------------------*
  514.   * Be sure that the dirstr path exists and is a directory
  515.   *---------------------------------------------------------------------------*/
  516.  
  517.   rc = stat(dirstr, &statb);
  518.  
  519.   if (rc != 0)
  520.      return(FALSE);
  521.  
  522.   if ((statb.st_mode & S_IFDIR) == 0)
  523.      return(FALSE);
  524.  
  525.   return(TRUE);                     /* We passed all of the tests */
  526. }
  527.