home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / mkfiles32 / waitfor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.7 KB  |  104 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <sys/stat.h>
  22.  
  23. void usage( char *pMsg ){
  24.     if( pMsg ){
  25.         fprintf( stderr, "waitfor: %s\n\n", pMsg );
  26.     }
  27.     fprintf( stderr, 
  28.             "waitfor - wait for a file to exist\n"
  29.             "\n"
  30.             "usage: waitfor [-a] <file> [<file>..] \n"
  31.             "\n"
  32.             "       Waits for file <file> to exist and returns 0, if more than one file is\n"
  33.             "       specified, waitfor waits for the first one found.\n"
  34.             "\n"
  35.             "Parameters:"
  36.             "\n"
  37.             "   -a  Waits for all files to be created \n"
  38.             );
  39.     exit(-1);
  40. }
  41.  
  42. #define FILEMAX 300
  43.  
  44. void waitfor( int iFileCount, char**ppFiles, int bAll ){
  45.     int bDone = 0;
  46.     
  47.     while( !bDone ){
  48.         struct stat statbuf;
  49.                
  50.         int i = 0; 
  51.         while( i < iFileCount ){
  52.             if( stat( ppFiles[i], &statbuf ) == 0 ){
  53.                 if( !bAll ){
  54.                     bDone = 1;
  55.                 }
  56.             }
  57.             else {
  58.                 goto SLEEP;
  59.             }
  60.             i++;
  61.         }
  62.         bDone = 1;
  63.         
  64. SLEEP:
  65.         if( !bDone ){
  66.             _sleep( 100 * 1 );
  67.         }
  68.     }
  69.  
  70. }
  71.  
  72.  
  73. int main( int argc, char** argv ){
  74.     if( argc == 0 ){
  75.         usage(0);
  76.     }
  77.     int bAll = 0;
  78.  
  79.     int i = 1;
  80.     char* ppFiles[FILEMAX];
  81.     int iFileCount = 0;
  82.  
  83.     while( i < argc ){
  84.         if( argv[i][0] == '-' ){
  85.             switch( argv[i][1] ){
  86.             case 'a':
  87.                 bAll = 1;
  88.             defaut:
  89.                 usage("Unknown option");
  90.             }
  91.         }
  92.         else {
  93.             ppFiles[iFileCount] = argv[i];
  94.             iFileCount++;
  95.         }
  96.         if( iFileCount >= FILEMAX || iFileCount == 0 ){
  97.             usage("Too many files");
  98.         }
  99.         i++;
  100.     }
  101.     waitfor( iFileCount, ppFiles, bAll );
  102.     return 0;
  103. }
  104.