Exemple #1
0
/* finishing... */
VOID fini_stride(INT32 code, VOID* v){

	int i;

	UINT64 cum;

	if(interval_size == -1){
		output_file_stride.open(mkfilename("stride_full_int"), ios::out|ios::trunc);
	}
	else{
		output_file_stride.open(mkfilename("stride_phases_int"), ios::out|ios::app);
	}
	output_file_stride << numReadInstrsAnalyzed;
	/* local read distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += localReadDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) || (i == 262144) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144)
			break;
	}
	/* global read distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += globalReadDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) || (i == 262144) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144)
			break;
	}
	output_file_stride << " " << numWriteInstrsAnalyzed;
	/* local write distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += localWriteDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) || (i == 262144) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144)
			break;
	}
	/* global write distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += globalWriteDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144){
			output_file_stride << " " << cum << endl;
			break;
		}
	}
	output_file_stride << "number of instructions: " << total_ins_count_for_hpc_alignment << endl;
	output_file_stride.close();
}
Exemple #2
0
/* finishing... */
VOID fini_reg(INT32 code, VOID* v){

	if(interval_size == -1){
		output_file_reg.open(mkfilename("reg_full_int"), ios::out|ios::trunc);
		output_file_reg << total_ins_count;
	}
	else{
		output_file_reg.open(mkfilename("reg_phases_int"), ios::out|ios::app);
		output_file_reg << interval_ins_count;
	}

	int i;
	UINT64 totNumOps = 0;
	UINT64 num;
	/* total number of operands */
	for(i = 1; i < MAX_NUM_OPER; i++){
		totNumOps += opCounts[i]*i;
	}
	output_file_reg << " " << totNumOps;

	// ** average degree of use **
	num = 0;
	for(i = 0; i < MAX_REG_USE; i++){
		num += regUseDistr[i];
	}
	output_file_reg << " " << num;
	num = 0;
	for(i = 0; i < MAX_REG_USE; i++){
		num += i * regUseDistr[i];
	}
	output_file_reg << " " << num;

	// ** register dependency distributions **
	num = 0;
	for(i = 0; i < MAX_COMM_DIST; i++){
		num += regAgeDistr[i];
	}
	output_file_reg << " " << num;
	num = 0;
	for(i = 0; i < MAX_COMM_DIST; i++){
		num += regAgeDistr[i];
		if( (i == 1) || (i == 2) || (i == 4) || (i == 8) || (i == 16) || (i == 32) || (i == 64)){
			output_file_reg << " " << num;
		}
	}
	output_file_reg << endl;
	output_file_reg << "number of instructions: " << total_ins_count_for_hpc_alignment << endl;
	output_file_reg.close();
}
Exemple #3
0
/* initializing */
void init_reg(){

	int i;

	/* initializing total instruction counts is done in mica.cpp */

	/* allocate memory */
	opCounts = (UINT64*) checked_malloc(MAX_NUM_OPER * sizeof(UINT64));
	regRef = (BOOL*) checked_malloc(MAX_NUM_REGS * sizeof(BOOL));
	PCTable = (INT64*) checked_malloc(MAX_NUM_REGS * sizeof(INT64));
	regUseCnt = (INT64*) checked_malloc(MAX_NUM_REGS * sizeof(INT64));
	regUseDistr = (INT64*) checked_malloc(MAX_REG_USE * sizeof(INT64));
	regAgeDistr = (INT64*) checked_malloc(MAX_COMM_DIST * sizeof(INT64));

	/* initialize */
	for(i = 0; i < MAX_NUM_OPER; i++){
		opCounts[i] = 0;
	}
	for(i = 0; i < MAX_NUM_REGS; i++){
		regRef[i] = false;
		PCTable[i] = 0;
		regUseCnt[i] = 0;
	}
	for(i = 0; i < MAX_REG_USE; i++){
		regUseDistr[i] = 0;
	}
	for(i = 0; i < MAX_COMM_DIST; i++){
		regAgeDistr[i] = 0;
	}

	if(interval_size != -1){
		output_file_reg.open(mkfilename("reg_phases_int"), ios::out|ios::trunc);
		output_file_reg.close();
	}
}
Exemple #4
0
unsigned long getSeq()
{
   char seqfile[FILENAME_MAX];
   FILE *stream;
   unsigned long seq = 0;

   mkfilename(seqfile, E_spooldir, SFILENAME);

   if ((stream = FOPEN(seqfile, "r+", IMAGE_MODE)) == nil(FILE))
   {
      printerr( seqfile );

      if ((stream = FOPEN(seqfile, "w", IMAGE_MODE)) == nil(FILE))
      {
         printerr( seqfile );
         panic();
      }
   }
   else {
      if ( fread( &seq, sizeof seq, 1, stream ) != 1 )
         printerr( seqfile );
   }

/*--------------------------------------------------------------------*/
/*     Generate a new seed for our sequence if we can't read one      */
/*--------------------------------------------------------------------*/

   if ( ! seq++ )
   {
      seq = ((unsigned long) getpid()) % 10000000;
                                    /* Start number small, semi-unique*/

      printmsg(0,"Resetting sequence number to %lu (0x%08lx)",
                  seq,
                  seq );
   }

/*--------------------------------------------------------------------*/
/*                       Update sequence number                       */
/*--------------------------------------------------------------------*/

   rewind( stream );

   if ((fwrite( &seq, sizeof seq, 1, stream ) != 1) || fclose(stream))
   {
      printerr( seqfile );
      panic();
   }

#ifdef UDEBUG
   printmsg(8, "getseq: seq#=%ld", seq);
#endif

   return seq;

} /* getseq */
Exemple #5
0
    void RollingFileAppender::doRotate()
    {
      closeFile();

      // ignore unlink- and rename-errors. In case of failure the
      // original file is reopened

      std::string newfilename = mkfilename(_maxbackupindex);
      ::unlink(newfilename.c_str());
      for (unsigned idx = _maxbackupindex; idx > 0; --idx)
      {
        std::string oldfilename = mkfilename(idx - 1);
        ::rename(oldfilename.c_str(), newfilename.c_str());
        newfilename = oldfilename;
      }

      ::rename(fname().c_str(), newfilename.c_str());

      openFile();
      _fsize = 0;
    }
Exemple #6
0
VOID stride_instr_interval_output(){
	int i;

	UINT64 cum;

	output_file_stride.open(mkfilename("stride_phases_int"), ios::out|ios::app);

	output_file_stride << numReadInstrsAnalyzed;
	/* local read distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += localReadDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) || (i == 262144) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144)
			break;
	}
	/* global read distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += globalReadDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) || (i == 262144) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144)
			break;
	}
	output_file_stride << " " << numWriteInstrsAnalyzed;
	/* local write distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += localWriteDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) || (i == 262144) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144)
			break;
	}
	/* global write distribution */
	cum = 0;
	for(i = 0; i < MAX_DISTR; i++){
		cum += globalWriteDistrib[i];
		if( (i == 0) || (i == 8) || (i == 64) || (i == 512) || (i == 4096) || (i == 32768) ){
			output_file_stride << " " << cum;
		}
		if(i == 262144){
			output_file_stride << " " << cum << endl;
			break;
		}
	}
	output_file_stride.close();
}
Exemple #7
0
  void LoggerImpl::doRotate()
  {
    outfile.clear();
    outfile.close();

    // ignore unlink- and rename-errors. In case of failure the
    // original file is reopened

    std::string newfilename = mkfilename(maxbackupindex);
    ::unlink(newfilename.c_str());
    for (unsigned idx = maxbackupindex; idx > 0; --idx)
    {
      std::string oldfilename = mkfilename(idx - 1);
      ::rename(oldfilename.c_str(), newfilename.c_str());
      newfilename = oldfilename;
    }

    ::rename(fname.c_str(), newfilename.c_str());

    outfile.open(fname.c_str(), std::ios::out | std::ios::app);
    counter.resetCount(outfile.tellp());
  }
Exemple #8
0
/* initializing */
void init_stride(){

	int i;

	/* initializing total instruction counts is done in mica.cpp */

	/* initial sizes */
	numRead = 1024;
	numWrite = 1024;

	/* allocate memory */
	instrRead = (ADDRINT*) checked_malloc(numRead * sizeof(ADDRINT));
	instrWrite = (ADDRINT*) checked_malloc(numWrite * sizeof(ADDRINT));

	/* initialize */
	readIndex = 1;
	writeIndex = 1;
	for (i = 0; i < (int)numRead; i++)
		instrRead[i] = 0;
	for (i = 0; i < (int)numWrite; i++)
		instrWrite[i] = 0;
	lastReadAddr = 0;
	lastWriteAddr = 0;
	for (i = 0; i < MAX_DISTR; i++) {
		localReadDistrib[i] = 0;
		localWriteDistrib[i] = 0;
		globalReadDistrib[i] = 0;
		globalWriteDistrib[i] = 0;
	}
	numInstrsAnalyzed = 0;
	numReadInstrsAnalyzed = 0;
	numWriteInstrsAnalyzed = 0;

	indices_memRead_size = 1024;
	indices_memRead = (ADDRINT*) checked_malloc(indices_memRead_size*sizeof(ADDRINT));
	for (i = 0; i < (int)indices_memRead_size; i++)
		indices_memRead[i] = 0;

	indices_memWrite_size = 1024;
	indices_memWrite = (ADDRINT*) checked_malloc(indices_memWrite_size*sizeof(ADDRINT));
	for (i = 0; i < (int)indices_memWrite_size; i++)
		indices_memWrite[i] = 0;

	if(interval_size != -1){
		output_file_stride.open(mkfilename("stride_phases_int"), ios::out|ios::trunc);
		output_file_stride.close();
	}
}
Exemple #9
0
VOID reg_instr_interval_output(){
	int i;

	output_file_reg.open(mkfilename("reg_phases_int"), ios::out|ios::app);

	UINT64 totNumOps = 0;
	UINT64 num;

	/* total number of operands */
	for(i = 1; i < MAX_NUM_OPER; i++){
		totNumOps += opCounts[i]*i;
	}
	output_file_reg << interval_size << " " << totNumOps;

	/* average degree of use */
	num = 0;
	for(i = 0; i < MAX_REG_USE; i++){
		num += regUseDistr[i];
	}
	output_file_reg << " " << num;
	num = 0;
	for(i = 0; i < MAX_REG_USE; i++){
		num += i * regUseDistr[i];
	}
	output_file_reg << " " << num;

	/* register dependency distributions */
	num = 0;
	for(i = 0; i < MAX_COMM_DIST; i++){
		num += regAgeDistr[i];
	}
	output_file_reg << " " << num;
	num = 0;
	for(i = 0; i < MAX_COMM_DIST; i++){
		num += regAgeDistr[i];
		if( (i == 1) || (i == 2) || (i == 4) || (i == 8) || (i == 16) || (i == 32) || (i == 64)){
			output_file_reg << " " << num;
		}
	}
	output_file_reg << endl;

	output_file_reg.close();
}
Exemple #10
0
char *mkmailbox(char *buf, const char *userid)
{
   KWBoolean append = (KWBoolean) (( E_mailext != NULL ) ? KWTrue : KWFalse);

   if (buf == NULL)           /* Do we need to allocate buffer?       */
   {
      buf = malloc( FILENAME_MAX );
      checkref(buf);
   } /* if */

   if (bflag[F_DIRECT])
   {
      sprintf(buf,"%s%c%.8s%c%s", E_maildir, SEPCHAR,
                               userid, SEPCHAR,"newmail");
   } /* if (bflag[F_DIRECT]) */
   else {
      char tuser[FILENAME_MAX];
      strcpy( tuser, userid );

      if (strchr(userid, '.') == NULL )
         tuser[8] = '\0';        /* Auto-truncate mbox name           */
      else
         append = KWFalse;

      mkfilename(buf, E_maildir, tuser);
   } /* else */

/*--------------------------------------------------------------------*/
/*              If we need a mailbox extension, add one               */
/*--------------------------------------------------------------------*/

   if ( append )
      strcat( strcat(buf,".") , E_mailext );

   return buf;

} /* mkmailbox */
Exemple #11
0
static void InitAlias( void )
{
   char buf[BUFSIZ];
   int subscript  = -1;
   size_t maxmimumAliases = 64;
   KWBoolean inAlias = KWFalse;
   FILE *stream;
   long here;

/*--------------------------------------------------------------------*/
/*            Build the file name and try to open the file            */
/*--------------------------------------------------------------------*/

   mkfilename( buf, E_confdir, "aliases" );
   SysAliases = newstr( buf );

   stream = FOPEN( SysAliases , "r", TEXT_MODE );

   if ( stream == NULL )
   {
      if (debuglevel > 1)
         printerr( SysAliases );

      return;

   } /* if */

/*--------------------------------------------------------------------*/
/*                 The file is open, allocate a table                 */
/*--------------------------------------------------------------------*/

   aliasTable = malloc( sizeof *aliasTable * maxmimumAliases );
   checkref(aliasTable);

   here = ftell( stream );       /* Remember location in file        */

/*--------------------------------------------------------------------*/
/*                    Begin loop to process names                     */
/*--------------------------------------------------------------------*/

   while (fgets( buf , BUFSIZ , stream ) != NULL )
   {
      char *s = buf;

/*--------------------------------------------------------------------*/
/*    Ignore comments, lines that begin with whitespace, and empty    */
/*    lines                                                           */
/*--------------------------------------------------------------------*/

      while( *s && ! isgraph( *s ))
         s++;

      if (*s == '#')
      {
            /* Comment, no operation  */
      }
      else if ( ! *s )           /* Empty line?                      */
      {                          /* Yes --> 'tis end of alias        */
         if ( inAlias )
         {
            inAlias = KWFalse;
            if (aliasTable[subscript].start == -1 )
               printmsg(0,"%s: Invalid alias %s, no data defined!",
                  SysAliases, aliasTable[subscript].alias );
            else
               aliasTable[subscript].end = here;
         }
      } /* if ( ! *s ) */
      else if (inAlias)
      {
         if (aliasTable[subscript].start == -1 )
            aliasTable[subscript].start = here + (s - buf);
      }
      else {                           /* Start of a new alias */

         char *colon = strchr( s, ':' );

         if (s != buf )
            printmsg(0,"%s: Error aliases must begin in column 1",
                  SysAliases );

         if (colon == NULL )
         {
            printmsg(0,"%s: No colon after alias %s",
                  SysAliases, s);
            continue;
         }

         if ( subscript+2 == (int) maxmimumAliases )
         {
            maxmimumAliases *= 2;
            aliasTable = realloc( aliasTable,
                                  maxmimumAliases * sizeof *aliasTable );
            checkref( aliasTable );
         }

         *colon = '\0';          /* Terminate the name               */
         aliasTable[++subscript].alias = newstr(strtok(s,WHITESPACE));
                                 /* Save name of alias               */
         s = strtok( colon+1, WHITESPACE);
                                 /* Find next token                  */

         if ((s == NULL) || (*s == '#'))  /* Any alias on same?      */
             aliasTable[subscript].start = -1;
                                 /* No--> Look for it later          */
         else
             aliasTable[subscript].start = here + ( s - buf );

          aliasTable[subscript].end  = -1;
          aliasTable[subscript].recurse = KWFalse;
          inAlias = KWTrue;

      } /* if */

      here = ftell( stream );       /* Remember start of next line   */

   } /* while */

/*--------------------------------------------------------------------*/
/*                   Clean up and return to caller                    */
/*--------------------------------------------------------------------*/

   if ( inAlias )
      aliasTable[subscript].end = LONG_MAX;

   aliases = (size_t) subscript + 1;

   if ( aliases == 0 )
      free( aliasTable );
   else {
      aliasTable = realloc( aliasTable, aliases * sizeof *aliasTable );
      checkref(aliasTable);
   }

   printmsg(2,"InitAlias: Loaded %d system aliases from %s",
               aliases,
               SysAliases);

   fclose( stream );

} /* InitAlias */
Exemple #12
0
//---------------------------------------------------------------------------
// mode & 1 = test only, don't create dirs
QString TSatScript::getcommand(QString script, QStringList *args, bool *error, int mode)
{
    QString rc, arg;
    QString out_file, frames_dir, frames_ext;
    QString baseband_dir, baseband_ext;
    int     i, index, len;

    *error = true;

    if(script.isEmpty())
        return "Error: No script file!";
#if 0
    QFileInfo fi(script);
    if(!fi.exists())
        return "Error: File not found: " + script;
#endif

    if(!args->count())
        return "Error: No arguments: " + script;

    *error = false;
    rc = script;

    assign_constants();

    for(i=0; i<args->count(); i++) {
        arg = args->at(i);

        if(arg.contains("~"))
            arg.replace("~", QDir::homePath());

        arg = parse_argument(arg, error, mode);
        arg = arg.trimmed();

        if(arg.isEmpty())
            continue;

        if(*error) {
            rc += " " + arg;
            rc += "\n\nError!";

            return rc;
        }

        index = getcommand_index(arg);
        if(index < 0) {
            rc += " " + arg;
            continue;
        }

        len = commands->at(index).length() + 1;

        // qDebug("\narg: %s, [%s:%d]\n", arg.toStdString().c_str(), __FILE__, __LINE__);

        switch(index) {
        case SS_CMD_OUT_FILE:
        {
            out_file = arg.remove(0, len);
            out_file = out_file.trimmed();
        }
        break;
        case SS_CMD_BASEBAND_DIR:
        {
            //qDebug("\nbaseband dir arg: %s, [%s:%d]", arg.toStdString().c_str(), __FILE__, __LINE__);

            baseband_dir = arg.remove(0, len);
            baseband_dir = baseband_dir.trimmed();

            //qDebug("baseband dir: %s, [%s:%d]", baseband_dir.toStdString().c_str(), __FILE__, __LINE__);

            // remove the trailing slash
            if(baseband_dir.endsWith('/') || baseband_dir.endsWith('\\'))
                baseband_dir.remove(baseband_dir.length() - 1, 1);

            //qDebug("baseband dir: %s, [%s:%d]\n", baseband_dir.toStdString().c_str(), __FILE__, __LINE__);
        }
        break;
        case SS_CMD_BASEBAND_EXT:
        {
            baseband_ext = arg.remove(0, len);
            baseband_ext = baseband_ext.trimmed();
        }
        break;
        case SS_CMD_FRAMES_DIR:
        {
            frames_dir = arg.remove(0, len);
            frames_dir = frames_dir.trimmed();

            // remove the trailing slash
            if(frames_dir.endsWith('/') || frames_dir.endsWith('\\'))
                frames_dir.remove(frames_dir.length() - 1, 1);
        }
        break;

        case SS_CMD_FRAMES_EXT:
        {
            frames_ext = arg.remove(0, len);
            frames_ext = frames_ext.trimmed();
        }
        break;

        default:
            continue;
        }
    }

    free_private();

#if 0
    // TODO: add no-outfile option
    if(out_file.isEmpty()) {
        *error = true;
        rc += "\n\nError: No out-file";

        return rc;
    }
#endif

    if(!frames_dir.isEmpty() && !frames_ext.isEmpty())
        _frames_filename = mkfilename(frames_dir, out_file, frames_ext);

    if(!baseband_dir.isEmpty() && !baseband_ext.isEmpty())
        _baseband_filename = mkfilename(baseband_dir, out_file, baseband_ext);

    if(!_frames_filename.isEmpty())
        rc += " --frames-file " + _frames_filename;

    if(!_baseband_filename.isEmpty())
        rc += " --baseband-file " + _baseband_filename;

#if 0
    qDebug("frames file: %s:%d", _frames_filename.toStdString().c_str(), __LINE__);
    qDebug("baseband file: %s:%d", _baseband_filename.toStdString().c_str(), __LINE__);
#endif

    return rc;
}
Exemple #13
0
void openlog(const char *log)
{
   char *newLogName;
   FILE *stream = NULL;
   int saveDebuglevel = debuglevel;

/*--------------------------------------------------------------------*/
/*                             Housekeeping                           */
/*--------------------------------------------------------------------*/

   if (E_logdir == NULL)         /* We DID call configure, didn't we? */
      panic();                   /* Ooopps --> I guess not.           */

    MKDIR(E_logdir);             /* Make sure directory exists!       */

/*--------------------------------------------------------------------*/
/*       If we already had a log file, spin it off and copy it        */
/*--------------------------------------------------------------------*/

   if (permanentLogName != NULL)
      copylog();

/*--------------------------------------------------------------------*/
/*                Create the final log name for later                 */
/*--------------------------------------------------------------------*/

   if ((log != NULL) || (permanentLogName == NULL))
   {
      char fname[FILENAME_MAX];
      char *newName = (char*) ((log == NULL) ? compilen : log);
      char *period = strchr(newName, '.');

      mkfilename(fname, E_logdir, newName);

      if (period == NULL)
         strcat(fname, ".log");

      newLogName = newstr(fname);
   }
   else
      newLogName = permanentLogName;

/*--------------------------------------------------------------------*/
/*                   Create temporary log file name                   */
/*--------------------------------------------------------------------*/

   if (bflag[F_MULTITASK])
   {
      char fname[FILENAME_MAX];
      short retries = 15;

      while ((stream == NULL) && retries--)
      {
         mkdirfilename(fname, E_logdir, "log");
                                    /* Get a temp log file name       */

         denormalize(fname);
         stream = fopen(fname, "a+");

         if (stream == NULL)
            printerr(fname);

      } /* while */

      currentLogName = newstr(fname);
                                 /* Save name we log to for posterity */

   } /* if */
   else {
      currentLogName = newLogName;  /* Log directly to true log file  */
      stream  = FOPEN(currentLogName , "a",TEXT_MODE);
                              /* We append in case we are not in
                                 multitask mode and we do not want
                                 to clobber the real log!             */
   } /* else */

   if (stream == NULL)
   {
      printmsg(0,"Cannot open any log file!");
      panic();
   }

   full_log_file_name = currentLogName;
                                    /* Tell printmsg() what our log
                                       file name is                   */
   logfile  = stream;               /* And of the the stream itself   */

/*--------------------------------------------------------------------*/
/*               Request the copy function be run later               */
/*--------------------------------------------------------------------*/

   if (permanentLogName == NULL)
      atexit(copylog);

/*--------------------------------------------------------------------*/
/*    Tag the new log file with the current time and program date.    */
/*--------------------------------------------------------------------*/

   debuglevel = 0;                  /* Insure we time stamp          */

   printmsg(-1,"%s: %s %s (%s %s)",
            compilen, compilep, compilev, compiled, compilet);
   debuglevel = saveDebuglevel;

   if (ferror(logfile))
   {
      printerr(currentLogName);
      panic();
   }

/*--------------------------------------------------------------------*/
/*       Save log file name for latter reference and as a flag        */
/*       that we were called                                          */
/*--------------------------------------------------------------------*/

   permanentLogName = newLogName;

} /* openlog */
Exemple #14
0
FTSENT *
fts_read(FTS *fts)
{
    struct dirent *dirent;
    int rc;
    char *name;
    char buf[1024];

    if(fts->ftsent.fts_path) {
        free(fts->ftsent.fts_path);
        fts->ftsent.fts_path = NULL;
    }

    if(fts->dname) {
        free(fts->dname);
        fts->dname = NULL;
    }

 again:
    dirent = readdir(fts->dir[fts->depth]);
    if(dirent == NULL) {
        char *newcwd = NULL;
        closedir(fts->dir[fts->depth]);
        fts->dir[fts->depth] = NULL;
        fts->depth--;
        if(fts->depth >= 0) {
            fts->dname = basename_a(fts->cwd);
            if(fts->dname == NULL)
                goto error;
            newcwd = dirname_a(fts->cwd);
            if(newcwd == NULL)
                goto error;
        }
        if(fts->cwd) free(fts->cwd);
        fts->cwd = NULL;
        if(fts->depth < 0)
            return NULL;
        rc = change_to_dir(fts->dir[fts->depth]);
        if(rc < 0) {
            free(newcwd);
            goto error;
        }
        fts->cwd = newcwd;
        name = fts->dname;
        fts->ftsent.fts_info = FTS_DP;
        goto done;
    }

    name = dirent->d_name;

 again2:
    rc = stat(name, &fts->stat);
    if(rc < 0) {
        fts->ftsent.fts_info = FTS_NS;
        goto error2;
    }

    if(S_ISDIR(fts->stat.st_mode)) {
        char *newcwd;
        DIR *dir;

        if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
            goto again;

        if(fts->depth >= FTS_MAX_DEPTH) {
            errno = ENFILE;
            goto error;
        }
        dir = opendir(dirent->d_name);
        if(dir == NULL) {
            if(errno == EACCES) {
                fts->ftsent.fts_info = FTS_DNR;
                goto error2;
            } else
                goto error;
        }
        newcwd = mkfilename(fts->cwd, dirent->d_name);
        rc = change_to_dir(dir);
        if(rc < 0) {
            free(newcwd);
            goto error;
        }
        free(fts->cwd);
        fts->cwd = newcwd;
        fts->ftsent.fts_info = FTS_D;
        fts->depth++;
        fts->dir[fts->depth] = dir;
        goto done;
    } else if(S_ISREG(fts->stat.st_mode)) {
        fts->ftsent.fts_info = FTS_F;
        goto done;
#ifdef S_ISLNK
    } else if(S_ISLNK(fts->stat.st_mode)) {
        int rc;
        rc = readlink(name, buf, 1024);
        if(rc < 0)
            goto error;
        if(rc >= 1023) {
            errno = ENAMETOOLONG;
            goto error;
        }
        buf[rc] = '\0';
        name = buf;
        if(access(buf, F_OK) >= 0)
            goto again2;
        fts->ftsent.fts_info = FTS_SLNONE;
        goto done;
#endif
    } else {
        fts->ftsent.fts_info = FTS_DEFAULT;
        goto done;
    }
    
 done:
    if(fts->cwd == NULL)
        fts->cwd = getcwd_a();
    if(fts->cwd == NULL) goto error;
    fts->ftsent.fts_path = mkfilename(fts->cwd, name);
    if(fts->ftsent.fts_path == NULL) goto error;
    fts->ftsent.fts_accpath = name;
    fts->ftsent.fts_statp = &fts->stat;
    return &fts->ftsent;

 error:
    fts->ftsent.fts_info = FTS_ERR;
 error2:
    fts->ftsent.fts_errno = errno;
    return &fts->ftsent;
}
Exemple #15
0
KWBoolean
init_sys( void )
{

    FILE       *sysFileStream = NULL;
    char        sysFileName[ FILENAME_MAX ];
    char        line[ LINESIZ ];
    char       *t;
    char        buf[ INITSIZ ];
    KWBoolean   wantMore = KWTrue;
    KWBoolean   success  = KWTrue;

    mkfilename(sysFileName, E_confdir, "SYS");

    /*--------------------------------------------------------------------*/
    /*               Generate a new SYS file if we need to                */
    /*--------------------------------------------------------------------*/

    if ( stater( sysFileName, 0 ) == -1 )
        bootStrap( sysFileName );

    sysFileStream = fopen(sysFileName, "rb");

    if ( sysFileStream == NULL )
    {
        printerr(sysFileName);
        panic();
    }

    printmsg(3, "init_sys: reading system file %s", sysFileName);

    memset(buf, 0, INITSIZ);

    while (fgets(line, LINESIZ, sysFileStream) != NULL)
    {

        /*--------------------------------------------------------------------*/
        /*       Trim trailing spaces and cr/lf.  (Prevents empty lines in    */
        /*       the log file).                                               */
        /*--------------------------------------------------------------------*/

        t = line + strlen(line) - 1;

        while ((t >= line) && isspace(*t))
            *t-- = '\0';

        /*--------------------------------------------------------------------*/
        /*                      Also trim leading spaces                      */
        /*--------------------------------------------------------------------*/

        t = line;

        while (t && isspace(*t))
            t++;

        printmsg(6, "init_sys: read line length %u, \"%s\"",
                 strlen(t),
                 t);

        /*--------------------------------------------------------------------*/
        /*       Process any buffered data if this line is empty (which       */
        /*       terminates the entry)                                        */
        /*--------------------------------------------------------------------*/

        if ( *buf && (! strlen(t) || ! wantMore ))
            /* Previous entry complete?      */
        {
            if (! process_sys( buf ))  /* Yes --> end of entry, process */
                success = KWFalse;

            *buf = '\0';               /* Also, reset buffer to empty   */
        }

        /*--------------------------------------------------------------------*/
        /*           Buffer the new data if this is not a comment line        */
        /*--------------------------------------------------------------------*/

        if (*t != '#')                /* Comment line?                 */
        {   /* No --> Add it to our buffer   */

            if ( t[ strlen(t) - 1 ] == '\\' )
                wantMore = KWTrue;
            else
                wantMore = KWFalse;
            strcat(buf, t);

        }  /* else if (*t != '#') */

    } /* while (fgets(line, LINESIZ, sysFileStream) != NULL) */

    /*--------------------------------------------------------------------*/
    /*                Process the final system entry, if any              */
    /*--------------------------------------------------------------------*/

    if (( *buf ) && ! process_sys( buf ) )
        success = KWFalse;

    fclose( sysFileStream );

    /*--------------------------------------------------------------------*/
    /*              Create a cache buffer for use by check_sys            */
    /*--------------------------------------------------------------------*/

    if ( cacheLength )
    {
        cache = (char *) malloc( cacheLength );
        checkref( cache );
    }

    /*--------------------------------------------------------------------*/
    /*       Report if we had any problems processing the SYS file to     */
    /*       our caller                                                   */
    /*--------------------------------------------------------------------*/

    return success;

} /* init_sys */