home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / wkframe / touchk / touch.c__ / TOUCH.C
Encoding:
Text File  |  1992-03-16  |  19.5 KB  |  321 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.         │*/
  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/2 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.  
  78. #define INCL_DOSFILEMGR
  79. #define INCL_DOSDATETIME
  80. #define INCL_DOSERRORS
  81. #define INCL_NOCOMMON
  82. #include <os2.h>
  83.  
  84. main(int argc, char *argv[], char *envp[])
  85.    /***************************************************************************/{
  86.    /* variables needed for OS/2 DosXxxx() calls                               */
  87.    /***************************************************************************/
  88.    HFILE        FileHandle;      /* for DosOpen/Close, DosQuery/SetFileInfo   */
  89.    ULONG        ActionTaken;     /* for DosOpen                               */
  90.    USHORT       rc;              /* for all DosXxxx calls                     */
  91.    PBYTE        FileInfoBuf;     /* for DosQuery/SetFileInfo                  */
  92.    USHORT       FileInfoBufSize; /* for DosQuery/SetFileInfo                  */
  93.    DATETIME     DateTime;        /* for DosGetDateTime                        */
  94.    FILESTATUS   FileStatus;      /* for DosQuery/SetFileInfo                  */
  95.    FILEFINDBUF3 FileFindBuf;     /* for DosFindFirst/Next                     */
  96.    HDIR         hdir;            /* for DosFindFirst/Next                     */
  97.    USHORT       attrib;          /* for DosFindFirst/Next                     */
  98.    ULONG        count;           /* for DosFindFirst/Next                     */
  99.  
  100.    /***************************************************************************/
  101.    /* internal variables                                                      */
  102.    /***************************************************************************/
  103.    int          i;               /* index into argv array for file mask       */
  104.    BOOL         error  = FALSE;  /* error flag                                */
  105.    int          errors = 0;      /* error count used as return code           */
  106.  
  107.    /***************************************************************************/
  108.    /* Check if any parameters were given, if not, display some help infomation*/
  109.    /***************************************************************************/
  110.    if (argc == 1)
  111.       {
  112.       fprintf(stderr, "%s:  Syntax is '%s arg1 [arg2...argn]'.\n", argv[0], argv[0]);
  113.       fprintf(stderr, "%s:  where argX are filenames or patterns.\n", argv[0]);
  114.       exit(1);
  115.       }
  116.  
  117.    /***************************************************************************/
  118.    /* Get the current date and time and use it for all files 'touch'ed.       */
  119.    /***************************************************************************/
  120.    if (rc = DosGetDateTime(&DateTime))
  121.       {
  122.       fprintf(stderr, "%s:  DosGetDateTime() failed with OS/2 rc = %d.\n", argv[0], rc);
  123.       exit(rc);
  124.       }
  125.  
  126.    /***************************************************************************/
  127.    /* Initialize variables for DosQueryFileInfo()                             */
  128.    /***************************************************************************/
  129.    FileInfoBuf     = (char *) &FileStatus;
  130.    FileInfoBufSize = sizeof(FileStatus);
  131.  
  132.    /***************************************************************************/
  133.    /* initialize variables for DosFindFirst()                                 */
  134.    /***************************************************************************/
  135.    hdir   = HDIR_CREATE;
  136.    attrib = FILE_NORMAL | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_ARCHIVED;
  137.    count  = 1;
  138.  
  139.    /***************************************************************************/
  140.    /* for each arguments provided:                                            */
  141.    /*   - find each file that matches the argument                            */
  142.    /*   - update the file's date and time to the time obtained earlier        */
  143.    /***************************************************************************/
  144.    for (i = 1; i < argc; i++)
  145.       {
  146.       /************************************************************************/
  147.       /* find the first file that matches the file pattern (argument)         */
  148.       /************************************************************************/
  149.       rc = DosFindFirst(argv[i],
  150.                         &hdir,
  151.                         attrib,
  152.                         &FileFindBuf,
  153.                         sizeof(FileFindBuf),
  154.                         &count,
  155.                         FIL_STANDARD);
  156.  
  157.       if (rc)
  158.          {
  159.          /*********************************************************************/
  160.          /* Indicate to user that no files matched the pattern supplied       */
  161.          /*********************************************************************/
  162.          fprintf(stderr, "%s:  Failed to find any files matching '%s' (OS/2 rc = %d).\n", argv[0], argv[i], rc);
  163.          errors += 1;
  164.          }
  165.       else
  166.          {
  167.          /*********************************************************************/
  168.          /* touch the file, find the next one and repeat                      */
  169.          /*********************************************************************/
  170.          while (!rc)
  171.             /******************************************************************/{
  172.             /* Open the file found so that the current date/time structure    */
  173.             /* can be used to set the new date/time                           */
  174.             /******************************************************************/
  175.             if (rc = DosOpen(FileFindBuf.achName, &FileHandle, &ActionTaken, 0L,
  176.                                          0x0000, 0x0001, 0x2011, 0L))
  177.                /***************************************************************/{
  178.                /* Indicate to user that the open failed                       */
  179.                /***************************************************************/
  180.                fprintf(stderr, "%s:  DosOpen() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  181.                error = TRUE;
  182.                }
  183.             else
  184.                {
  185.                /***************************************************************/
  186.                /* Get the information associated with the current file        */
  187.                /***************************************************************/
  188.                if (rc = DosQueryFileInfo(FileHandle, 1, FileInfoBuf, FileInfoBufSize))
  189.                   /************************************************************/{
  190.                   /* Indicate to user that the query failed                   */
  191.                   /************************************************************/
  192.                   fprintf(stderr, "%s:  DosQueryFileInfo() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  193.                   error = TRUE;
  194.                   }
  195.                else
  196.                   {
  197.                   #if DEBUG
  198.                   /************************************************************/
  199.                   /* Print the old date of the file if debugging              */
  200.                   /************************************************************/
  201.                   printf("DEBUG:  Old date is: %d/%02d/%02d\n",
  202.                                     FileStatus.fdateLastWrite.year + 1980,
  203.                                     FileStatus.fdateLastWrite.month,
  204.                                     FileStatus.fdateLastWrite.day);
  205.                   printf("DEBUG:  Old time is: %d:%02d:%02d\n",
  206.                                     FileStatus.ftimeLastWrite.hours,
  207.                                     FileStatus.ftimeLastWrite.minutes,
  208.                                     FileStatus.ftimeLastWrite.twosecs * 2);
  209.                   #endif
  210.  
  211.                   /************************************************************/
  212.                   /* change the FileStatus structure to contain the date/time */
  213.                   /* obtained earlier (making adjustments for differences)    */
  214.                   /************************************************************/
  215.                   FileStatus.fdateLastWrite.day     = DateTime.day;
  216.                   FileStatus.fdateLastWrite.month   = DateTime.month;
  217.                   FileStatus.fdateLastWrite.year    = DateTime.year - 1980;
  218.  
  219.                   FileStatus.ftimeLastWrite.twosecs = DateTime.seconds / 2;
  220.                   FileStatus.ftimeLastWrite.minutes = DateTime.minutes;
  221.                   FileStatus.ftimeLastWrite.hours   = DateTime.hours;
  222.  
  223.                   #if DEBUG
  224.                   /************************************************************/
  225.                   /* Print the new date of the file if debugging              */
  226.                   /************************************************************/
  227.                   printf("DEBUG:  New date is: %d/%02d/%02d\n",
  228.                                     FileStatus.fdateLastWrite.year + 1980,
  229.                                     FileStatus.fdateLastWrite.month,
  230.                                     FileStatus.fdateLastWrite.day);
  231.                   printf("DEBUG:  New time is: %d:%02d:%02d\n",
  232.                                     FileStatus.ftimeLastWrite.hours,
  233.                                     FileStatus.ftimeLastWrite.minutes,
  234.                                     FileStatus.ftimeLastWrite.twosecs * 2);
  235.                   #endif
  236.  
  237.                   /************************************************************/
  238.                   /* Set the information for the current file with the        */
  239.                   /* structure that was just modified                         */
  240.                   /************************************************************/
  241.                   if (rc = DosSetFileInfo(FileHandle, 1, FileInfoBuf, FileInfoBufSize))
  242.                      {
  243.                      /*********************************************************/
  244.                      /* Indicate to user that the update failed               */
  245.                      /*********************************************************/
  246.                      fprintf(stderr, "%s:  DosSetFileInfo() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  247.                      error = TRUE;
  248.                      }
  249.                   }
  250.  
  251.                /***************************************************************/
  252.                /* Close the current file so that the new information is       */
  253.                /* written                                                     */
  254.                /***************************************************************/
  255.                if (rc = DosClose(FileHandle))
  256.                   /************************************************************/{
  257.                   /* Indicate to user that an error occured closing the file  */
  258.                   /************************************************************/
  259.                   fprintf(stderr, "%s:  DosClose() failed on %s with OS/2 rc = %d.\n", argv[0], FileFindBuf.achName, rc);
  260.                   error = TRUE;
  261.                   }
  262.                }
  263.  
  264.             /******************************************************************/
  265.             /* Check to see if an error occurred                              */
  266.             /******************************************************************/
  267.             if (error)
  268.                {
  269.                /***************************************************************/
  270.                /* If it has, reset the flag and continue on to the next file  */
  271.                /* since a message should already have been printed            */
  272.                /***************************************************************/
  273.                error = FALSE;
  274.                errors += 1;
  275.                }
  276.             else
  277.                {
  278.                /***************************************************************/
  279.                /* Else, print a message indicating the successful update of   */
  280.                /* the date/time on the file                                   */
  281.                /***************************************************************/
  282.                printf("%s:  %s has been successfully 'touch'ed.\n", argv[0], FileFindBuf.achName);
  283.                }
  284.  
  285.             /******************************************************************/
  286.             /* Find the next file matching the same pattern                   */
  287.             /******************************************************************/
  288.             count  = 1;
  289.             rc = DosFindNext(hdir, &FileFindBuf, sizeof(FileFindBuf), &count);
  290.             }
  291.  
  292.          /*********************************************************************/
  293.          /* Indicate to user that an error occured finding the next file if   */
  294.          /* a return code other than expected is returned                     */
  295.          /*********************************************************************/
  296.          if (rc != ERROR_NO_MORE_FILES)
  297.             {
  298.             fprintf(stderr, "%s:  DosFindNext() failed on %d with OS/2 rc = %d.\n", argv[0], argv[i], rc);
  299.             }
  300.          }
  301.  
  302.       count  = 1;
  303.       }
  304.  
  305.    /***************************************************************************/
  306.    /* Clean up the DosFindFirst/Next handle that was allocated by the system  */
  307.    /* and print a message if there was an error                               */
  308.    /***************************************************************************/
  309.    rc = DosFindClose(hdir);
  310.    if (rc != 0 && rc != ERROR_INVALID_HANDLE)
  311.       {
  312.       fprintf(stderr, "%s:  DosFindClose() failed with OS/2 rc = %d.\n", argv[0], rc);
  313.       }
  314.  
  315.    /***************************************************************************/
  316.    /* Indicate to user that everything is done                                */
  317.    /***************************************************************************/
  318.    printf("%s:  All done!\n", argv[0]);
  319.    return errors;
  320.    }
  321.