home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / COMPILER / TOUCH / TOUCH.C < prev    next >
Text File  |  1995-05-29  |  20KB  |  322 lines

  1. /*┌──────────────────────────────────────────────────────────────────────────┐*/
  2. /*│                                                                          │*/
  3. /*│ PROGRAM NAME: TOUCH                                                      │*/
  4. /*│ -------------                                                            │*/
  5. /*│  An OS/2 program that changes the date and time of selected files to the │*/
  6. /*│  date and time at which the command is run.                              │*/
  7. /*│                                                                          │*/
  8. /*│ COPYRIGHT:                                                               │*/
  9. /*│ ----------                                                               │*/
  10. /*│  Copyright (C) International Business Machines Corp., 1991,1992,1993.    │*/
  11. /*│                                                                          │*/
  12. /*│ DISCLAIMER OF WARRANTIES:                                                │*/
  13. /*│ -------------------------                                                │*/
  14. /*│  The following [enclosed] code is sample code created by IBM Corporation.│*/
  15. /*│  This sample code is not part of any standard IBM product and is provided│*/
  16. /*│  to you solely for the purpose of assisting you in the development of    │*/
  17. /*│  your applications.  The code is provided "AS IS", without warranty of   │*/
  18. /*│  any kind.  IBM shall not be liable for any damages arising out of your  │*/
  19. /*│  use of the sample code, even if they have been advised of the           │*/
  20. /*│  possibility of such damages.                                            │*/
  21. /*│                                                                          │*/
  22. /*│ REVISION LEVEL: 1.0                                                      │*/
  23. /*│ ---------------                                                          │*/
  24. /*│                                                                          │*/
  25. /*│ WHAT THIS PROGRAM DOES:                                                  │*/
  26. /*│ -----------------------                                                  │*/
  27. /*│  This program uses the parameters passed to determine which files are    │*/
  28. /*│  to have their date and time updated to the current date and time.       │*/
  29. /*│                                                                          │*/
  30. /*│ WHAT THIS PROGRAM DEMONSTRATES:                                          │*/
  31. /*│ -------------------------------                                          │*/
  32. /*│  This program demonstrates how to query the date and time, how to find   │*/
  33. /*│  files based on a file mask, how to open those files, how to update the  │*/
  34. /*│  date and time of those files, and how to close the files when done.     │*/
  35. /*│                                                                          │*/
  36. /*│ WHAT YOU NEED TO COMPILE THIS PROGRAM:                                   │*/
  37. /*│ --------------------------------------                                   │*/
  38. /*│                                                                          │*/
  39. /*│  REQUIRED FILES:                                                         │*/
  40. /*│  ---------------                                                         │*/
  41. /*│                                                                          │*/
  42. /*│    TOUCH    C     - touch source file                                    │*/
  43. /*│    TOUCH    MAK   - touch make file                                      │*/
  44. /*│                                                                          │*/
  45. /*│  REQUIRED LIBRARIES:                                                     │*/
  46. /*│  -------------------                                                     │*/
  47. /*│                                                                          │*/
  48. /*│    OS2386.LIB     - OS/2 API library                                     │*/
  49. /*│                                                                          │*/
  50. /*│  REQUIRED PROGRAMS:                                                      │*/
  51. /*│  ------------------                                                      │*/
  52. /*│                                                                          │*/
  53. /*│    IBM C Set++ Compiler (icc.exe)                                        │*/
  54. /*│    IBM Linker (link386.exe)                                              │*/
  55. /*│                                                                          │*/
  56. /*│ EXPECTED INPUT:                                                          │*/
  57. /*│ ---------------                                                          │*/
  58. /*│                                                                          │*/
  59. /*│    One or more file masks indicating the files that are to have their    │*/
  60. /*│    date and time updated.                                                │*/
  61. /*│                                                                          │*/
  62. /*│ EXPECTED OUTPUT:                                                         │*/
  63. /*│ ----------------                                                         │*/
  64. /*│                                                                          │*/
  65. /*│    A message for each file indicated the successful update of the date   │*/
  66. /*│    and time.                                                             │*/
  67. /*│                                                                          │*/
  68. /*└──────────────────────────────────────────────────────────────────────────┘*/
  69.  
  70. /*┌──────────────────────────────────────────────────────────────────────────┐*/
  71. /*│ TOUCH.C                                                                  │*/
  72. /*│                                                                          │*/
  73. /*│ Touch main source file                                                   │*/
  74. /*└──────────────────────────────────────────────────────────────────────────┘*/
  75.  
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78.  
  79. #define INCL_DOSFILEMGR
  80. #define INCL_DOSDATETIME
  81. #define INCL_DOSERRORS
  82. #define INCL_NOCOMMON
  83. #include <os2.h>
  84.  
  85. main(int argc, char *argv[], char *envp[])
  86.    /***************************************************************************/{
  87.    /* variables needed for OS/2 DosXxxx() calls                               */
  88.    /***************************************************************************/
  89.    HFILE        FileHandle;      /* for DosOpen/Close, DosQuery/SetFileInfo   */
  90.    ULONG        ActionTaken;     /* for DosOpen                               */
  91.    USHORT       rc;              /* for all DosXxxx calls                     */
  92.    PBYTE        FileInfoBuf;     /* for DosQuery/SetFileInfo                  */
  93.    USHORT       FileInfoBufSize; /* for DosQuery/SetFileInfo                  */
  94.    DATETIME     DateTime;        /* for DosGetDateTime                        */
  95.    FILESTATUS   FileStatus;      /* for DosQuery/SetFileInfo                  */
  96.    FILEFINDBUF3 FileFindBuf;     /* for DosFindFirst/Next                     */
  97.    HDIR         hdir;            /* for DosFindFirst/Next                     */
  98.    USHORT       attrib;          /* for DosFindFirst/Next                     */
  99.    ULONG        count;           /* for DosFindFirst/Next                     */
  100.  
  101.    /***************************************************************************/
  102.    /* internal variables                                                      */
  103.    /***************************************************************************/
  104.    int          i;               /* index into argv array for file mask       */
  105.    BOOL         error  = FALSE;  /* error flag                                */
  106.    int          errors = 0;      /* error count used as return code           */
  107.  
  108.    /***************************************************************************/
  109.    /* Check if any parameters were given, if not, display some help infomation*/
  110.    /***************************************************************************/
  111.    if (argc == 1)
  112.       {
  113.       fprintf(stderr, "%s:  Syntax is '%s arg1 [arg2...argn]'.\n", argv[0], argv[0]);
  114.       fprintf(stderr, "%s:  where argX are filenames or patterns.\n", argv[0]);
  115.       exit(1);
  116.       }
  117.  
  118.    /***************************************************************************/
  119.    /* Get the current date and time and use it for all files 'touch'ed.       */
  120.    /***************************************************************************/
  121.    if (rc = DosGetDateTime(&DateTime))
  122.       {
  123.       fprintf(stderr, "%s:  DosGetDateTime() failed with OS/2 rc = %d.\n", argv[0], rc);
  124.       exit(rc);
  125.       }
  126.  
  127.    /***************************************************************************/
  128.    /* Initialize variables for DosQueryFileInfo()                             */
  129.    /***************************************************************************/
  130.    FileInfoBuf     = (char *) &FileStatus;
  131.    FileInfoBufSize = sizeof(FileStatus);
  132.  
  133.    /***************************************************************************/
  134.    /* initialize variables for DosFindFirst()                                 */
  135.    /***************************************************************************/
  136.    hdir   = HDIR_CREATE;
  137.    attrib = FILE_NORMAL | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_ARCHIVED;
  138.    count  = 1;
  139.  
  140.    /***************************************************************************/
  141.    /* for each arguments provided:                                            */
  142.    /*   - find each file that matches the argument                            */
  143.    /*   - update the file's date and time to the time obtained earlier        */
  144.    /***************************************************************************/
  145.    for (i = 1; i < argc; i++)
  146.       {
  147.       /************************************************************************/
  148.       /* find the first file that matches the file pattern (argument)         */
  149.       /************************************************************************/
  150.       rc = DosFindFirst(argv[i],
  151.                         &hdir,
  152.                         attrib,
  153.                         &FileFindBuf,
  154.                         sizeof(FileFindBuf),
  155.                         &count,
  156.                         FIL_STANDARD);
  157.  
  158.       if (rc)
  159.          {
  160.          /*********************************************************************/
  161.          /* Indicate to user that no files matched the pattern supplied       */
  162.          /*********************************************************************/
  163.          fprintf(stderr, "%s:  Failed to find any files matching '%s' (OS/2 rc = %d).\n", argv[0], argv[i], rc);
  164.          errors += 1;
  165.          }
  166.       else
  167.          {
  168.          /*********************************************************************/
  169.          /* touch the file, find the next one and repeat                      */
  170.          /*********************************************************************/
  171.          while (!rc)
  172.             /******************************************************************/{
  173.             /* Open the file found so that the current date/time structure    */
  174.             /* can be used to set the new date/time                           */
  175.             /******************************************************************/
  176.             if (rc = DosOpen(FileFindBuf.achName, &FileHandle, &ActionTaken, 0L,
  177.                                          0x0000, 0x0001, 0x2011, 0L))
  178.                /***************************************************************/{
  179.                /* Indicate to user that the open failed                       */
  180.                /***************************************************************/
  181.                fprintf(stderr, "%s:  DosOpen() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  182.                error = TRUE;
  183.                }
  184.             else
  185.                {
  186.                /***************************************************************/
  187.                /* Get the information associated with the current file        */
  188.                /***************************************************************/
  189.                if (rc = DosQueryFileInfo(FileHandle, 1, FileInfoBuf, FileInfoBufSize))
  190.                   /************************************************************/{
  191.                   /* Indicate to user that the query failed                   */
  192.                   /************************************************************/
  193.                   fprintf(stderr, "%s:  DosQueryFileInfo() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  194.                   error = TRUE;
  195.                   }
  196.                else
  197.                   {
  198.                   #if DEBUG
  199.                   /************************************************************/
  200.                   /* Print the old date of the file if debugging              */
  201.                   /************************************************************/
  202.                   printf("DEBUG:  Old date is: %d/%02d/%02d\n",
  203.                                     FileStatus.fdateLastWrite.year + 1980,
  204.                                     FileStatus.fdateLastWrite.month,
  205.                                     FileStatus.fdateLastWrite.day);
  206.                   printf("DEBUG:  Old time is: %d:%02d:%02d\n",
  207.                                     FileStatus.ftimeLastWrite.hours,
  208.                                     FileStatus.ftimeLastWrite.minutes,
  209.                                     FileStatus.ftimeLastWrite.twosecs * 2);
  210.                   #endif
  211.  
  212.                   /************************************************************/
  213.                   /* change the FileStatus structure to contain the date/time */
  214.                   /* obtained earlier (making adjustments for differences)    */
  215.                   /************************************************************/
  216.                   FileStatus.fdateLastWrite.day     = DateTime.day;
  217.                   FileStatus.fdateLastWrite.month   = DateTime.month;
  218.                   FileStatus.fdateLastWrite.year    = DateTime.year - 1980;
  219.  
  220.                   FileStatus.ftimeLastWrite.twosecs = DateTime.seconds / 2;
  221.                   FileStatus.ftimeLastWrite.minutes = DateTime.minutes;
  222.                   FileStatus.ftimeLastWrite.hours   = DateTime.hours;
  223.  
  224.                   #if DEBUG
  225.                   /************************************************************/
  226.                   /* Print the new date of the file if debugging              */
  227.                   /************************************************************/
  228.                   printf("DEBUG:  New date is: %d/%02d/%02d\n",
  229.                                     FileStatus.fdateLastWrite.year + 1980,
  230.                                     FileStatus.fdateLastWrite.month,
  231.                                     FileStatus.fdateLastWrite.day);
  232.                   printf("DEBUG:  New time is: %d:%02d:%02d\n",
  233.                                     FileStatus.ftimeLastWrite.hours,
  234.                                     FileStatus.ftimeLastWrite.minutes,
  235.                                     FileStatus.ftimeLastWrite.twosecs * 2);
  236.                   #endif
  237.  
  238.                   /************************************************************/
  239.                   /* Set the information for the current file with the        */
  240.                   /* structure that was just modified                         */
  241.                   /************************************************************/
  242.                   if (rc = DosSetFileInfo(FileHandle, 1, FileInfoBuf, FileInfoBufSize))
  243.                      {
  244.                      /*********************************************************/
  245.                      /* Indicate to user that the update failed               */
  246.                      /*********************************************************/
  247.                      fprintf(stderr, "%s:  DosSetFileInfo() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  248.                      error = TRUE;
  249.                      }
  250.                   }
  251.  
  252.                /***************************************************************/
  253.                /* Close the current file so that the new information is       */
  254.                /* written                                                     */
  255.                /***************************************************************/
  256.                if (rc = DosClose(FileHandle))
  257.                   /************************************************************/{
  258.                   /* Indicate to user that an error occured closing the file  */
  259.                   /************************************************************/
  260.                   fprintf(stderr, "%s:  DosClose() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  261.                   error = TRUE;
  262.                   }
  263.                }
  264.  
  265.             /******************************************************************/
  266.             /* Check to see if an error occurred                              */
  267.             /******************************************************************/
  268.             if (error)
  269.                {
  270.                /***************************************************************/
  271.                /* If it has, reset the flag and continue on to the next file  */
  272.                /* since a message should already have been printed            */
  273.                /***************************************************************/
  274.                error = FALSE;
  275.                errors += 1;
  276.                }
  277.             else
  278.                {
  279.                /***************************************************************/
  280.                /* Else, print a message indicating the successful update of   */
  281.                /* the date/time on the file                                   */
  282.                /***************************************************************/
  283.                printf("%s:  %s has been successfully 'touch'ed.\n", argv[0], FileFindBuf.achName);
  284.                }
  285.  
  286.             /******************************************************************/
  287.             /* Find the next file matching the same pattern                   */
  288.             /******************************************************************/
  289.             count  = 1;
  290.             rc = DosFindNext(hdir, &FileFindBuf, sizeof(FileFindBuf), &count);
  291.             }
  292.  
  293.          /*********************************************************************/
  294.          /* Indicate to user that an error occured finding the next file if   */
  295.          /* a return code other than expected is returned                     */
  296.          /*********************************************************************/
  297.          if (rc != ERROR_NO_MORE_FILES)
  298.             {
  299.             fprintf(stderr, "%s:  DosFindNext() failed on %d with OS/2 rc = %d.\n", argv[0], argv[i], rc);
  300.             }
  301.          }
  302.  
  303.       count  = 1;
  304.       }
  305.  
  306.    /***************************************************************************/
  307.    /* Clean up the DosFindFirst/Next handle that was allocated by the system  */
  308.    /* and print a message if there was an error                               */
  309.    /***************************************************************************/
  310.    rc = DosFindClose(hdir);
  311.    if (rc != 0 && rc != ERROR_INVALID_HANDLE)
  312.       {
  313.       fprintf(stderr, "%s:  DosFindClose() failed with OS/2 rc = %d.\n", argv[0], rc);
  314.       }
  315.  
  316.    /***************************************************************************/
  317.    /* Indicate to user that everything is done                                */
  318.    /***************************************************************************/
  319.    printf("%s:  All done!\n", argv[0]);
  320.    return errors;
  321.    }
  322.