rc_t CC ReportRedirect ( KWrtHandler* handler, const char* filename, bool* to_file, bool finalize ) { rc_t rc = 0; if (!finalize) { if (handler) { handler->writer = KOutWriterGet(); handler->data = KOutDataGet(); } if (filename) { KDirectory* dir = NULL; SFile* data = calloc(1, sizeof *data); if (data == NULL) { return RC(rcFS, rcMemory, rcAllocating, rcMemory, rcExhausted); } data->magic = MAGIC; rc = KDirectoryNativeDir(&dir); if (rc == 0) { rc = KDirectoryCreateFile (dir, &data->f, false, 0664, kcmInit, "%s", filename); } if (rc == 0) { rc = KOutHandlerSet(fileWriter, data); } RELEASE(KDirectory, dir); } if (rc != 0 || filename == NULL) { KOutHandlerSetStdErr(); if (to_file) { *to_file = false; } } else if (to_file) { *to_file = true; } } else { void* data = KOutDataGet(); SFile* self = (SFile*)data; if (self) { if (self->magic == MAGIC) { RELEASE(KFile, self->f);; } memset(self, 0, sizeof *self); free(self); } if (handler) { KOutHandlerSet(handler->writer, handler->data); } } return rc; }
void release_out_redir( out_redir * self ) { KFileRelease( self->kfile ); if( self->org_writer != NULL ) { KOutHandlerSet( self->org_writer, self->org_data ); } self->org_writer = NULL; }
rc_t make_stdout_redir ( stdout_redir ** writer, const char * filename, size_t bufsize ) { KDirectory *dir; rc_t rc = 0; stdout_redir * wr = NULL; if ( writer == NULL ) { *writer = NULL; rc = RC ( rcApp, rcNoTarg, rcAllocating, rcSelf, rcNull ); } else { wr = calloc( 1, sizeof *wr ); if ( wr == NULL ) rc = RC ( rcApp, rcNoTarg, rcAllocating, rcMemory, rcExhausted ); } if ( rc == 0 ) { rc = KDirectoryNativeDir( &dir ); if ( rc == 0 ) { KFile *of; rc = KDirectoryCreateFile ( dir, &of, false, 0664, kcmInit | kcmCreate, "%s", filename ); if ( rc == 0 ) { KFile* buf; rc = KBufFileMakeWrite( &buf, of, false, bufsize ); if ( rc == 0 ) { wr->kfile = buf; wr->org_writer = KOutWriterGet(); wr->org_data = KOutDataGet(); rc = KOutHandlerSet( stdout_redir_callback, wr ); if ( rc != 0 ) LOGERR( klogInt, rc, "KOutHandlerSet() failed" ); } KFileRelease( of ); } KDirectoryRelease( dir ); } } if ( rc == 0 ) *writer = wr; else { if ( wr != NULL ) free( wr ); } return rc; }
void release_stdout_redirection( stdout_redir * writer ) { if ( writer != NULL ) { KFileRelease( writer->kfile ); if( writer->org_writer != NULL ) { KOutHandlerSet( writer->org_writer, writer->org_data ); } writer->org_writer = NULL; free( writer ); } }
static rc_t run(bool quiet) { rc_t rc = 0; char password[PWD_SZ] = ""; bool empty = false; KWrtHandler handler; handler.writer = KOutWriterGet(); handler.data = KOutDataGet(); KOutHandlerSetStdErr(); KLogLibHandlerSet(NULL, NULL); OUTMSG(("Changing password\n")); #if 1 if (rc == 0) { int i = 0; for (i = 0; i < 3 && rc == 0; ++i) { rc = get_pass("New password:"******"cannot read the password"); if (rc != 0) { break; } if (password[0] == '\0') { OUTMSG(("BAD PASSWORD: too short\n")); } else { break; } } if (rc == 0 && password[0] == '\0') { rc = RC(rcExe, rcString, rcReading, rcString, rcIncorrect); empty = true; /* LOGERR(klogErr, rc, "failed to set password"); */ } } if (rc == 0) { char repassword[PWD_SZ] = ""; rc = get_pass("Retype new password:"******"cannot read the password"); if (rc == 0) { if (strcmp(password, repassword)) { OUTMSG(("Sorry, passwords do not match.\n")); rc = RC(rcExe, rcString, rcReading, rcString, rcInconsistent); /* LOGERR(klogErr, rc, "failed to set password"); */ } } } #else strcpy(password, "1n2"); #endif if (rc == 0) { rc = SetPwd(password, quiet); } KOutHandlerSet(handler.writer, handler.data); if (empty) { rc = 0; } return rc; }
rc_t CC KMain ( int argc, char *argv [] ) { rc_t rc = KOutHandlerSet ( write_to_FILE, stdout ); if ( rc != 0 ) { PLOGERR( klogErr, ( klogErr, rc, "KOutHandlerSet() failed in $(func)", "func=%s", __func__ ) ); } else { Args * args; rc = ArgsMakeAndHandle ( &args, argc, argv, 1, ToolOptions, sizeof ToolOptions / sizeof ToolOptions [ 0 ] ); if ( rc != 0 ) { PLOGERR( klogErr, ( klogErr, rc, "ArgsMakeAndHandle() failed in $(func)", "func=%s", __func__ ) ); } else { tool_options options; rc = init_tool_options( &options ); if ( rc == 0 ) { rc = get_tool_options( args, &options ); if ( rc == 0 ) { bool cache_paths_needed = false; switch( options.main_function ) { case tf_report : ; case tf_unlock : ; case tf_clear : cache_paths_needed = true; break; case tf_rreport : ; case tf_enable : ; case tf_disable : ; case tf_unknown : ; break; } if ( options.path_count == 0 && cache_paths_needed ) rc = get_cache_path_from_repo_mgr( &options ); if ( rc == 0 ) { if ( options.path_count == 0 && cache_paths_needed ) { rc = explain_no_cache_found (); } else rc = perform( &options, args ); } } clear_tool_options( &options ); } } { rc_t rc2 = ArgsWhack ( args ); if ( rc2 != 0 ) { PLOGERR( klogErr, ( klogErr, rc2, "ArgsWhack() failed in $(func)", "func=%s", __func__ ) ); } } } return rc; }
rc_t init_out_redir( out_redir * self, out_redir_mode_t mode, const char * filename, size_t bufsize ) { rc_t rc; KFile *output_file; if ( filename != NULL ) { KDirectory *dir; rc = KDirectoryNativeDir( &dir ); if ( rc != 0 ) LOGERR( klogInt, rc, "KDirectoryNativeDir() failed" ); else { rc = KDirectoryCreateFile ( dir, &output_file, false, 0664, kcmInit, "%s", filename ); KDirectoryRelease( dir ); } } else rc = KFileMakeStdOut ( &output_file ); if ( rc == 0 ) { KFile *temp_file; /* wrap the output-file in compression, if requested */ switch ( mode ) { case orm_gzip : rc = KFileMakeGzipForWrite( &temp_file, output_file ); break; case orm_bzip2 : rc = KFileMakeBzip2ForWrite( &temp_file, output_file ); break; case orm_uncompressed : break; } if ( rc == 0 ) { if ( mode != orm_uncompressed ) { KFileRelease( output_file ); output_file = temp_file; } /* wrap the output/compressed-file in buffering, if requested */ if ( bufsize != 0 ) { rc = KBufFileMakeWrite( &temp_file, output_file, false, bufsize ); if ( rc == 0 ) { KFileRelease( output_file ); output_file = temp_file; } } if ( rc == 0 ) { self->kfile = output_file; self->org_writer = KOutWriterGet(); self->org_data = KOutDataGet(); self->pos = 0; rc = KOutHandlerSet( out_redir_callback, self ); if ( rc != 0 ) LOGERR( klogInt, rc, "KOutHandlerSet() failed" ); } } } return rc; }