Example #1
0
int main(int argc,char *argv[]) 
{
    char outputName[FILENAME_MAX_LENGTH];
    FILE *output = stdout;
    if (argc == 2) {
        strncpy(outputName,argv[1],FILENAME_MAX_LENGTH);
        outputName[FILENAME_MAX_LENGTH - 1] = '\0';
        output = fopen(outputName,"a");
        if (output == NULL) {
            fprintf(stderr, "could not create output file: %s\n", outputName);
            output = stdout;
        }
    }
    int count = concatenate_files(output);
    fprintf(stderr, "\nFiles processed: %d\n", count);
    return 0;
}
Example #2
0
static void
handle_packet(
        const unsigned char *dir_path,
        const struct nwrun_in_packet *packet,
        const unsigned char *result_path,
        struct nwrun_out_packet *result)
{
  unsigned char dst_program_path[EJ_PATH_MAX];
  unsigned char src_program_path[EJ_PATH_MAX];
  unsigned char dst_input_path[EJ_PATH_MAX];
  unsigned char src_input_path[EJ_PATH_MAX];
  unsigned char run_output_path[EJ_PATH_MAX];
  unsigned char full_output_path[EJ_PATH_MAX];
  unsigned char run_error_path[EJ_PATH_MAX];
  //unsigned char log_file_path[EJ_PATH_MAX];
  unsigned char error_file_path[EJ_PATH_MAX];
  unsigned char result_file_path[EJ_PATH_MAX];

  ssize_t error_file_size;
  ssize_t output_file_size;

  FILE *f = 0;
  int cur_status = RUN_OK;

  /* copy the executable */
  snprintf(dst_program_path, sizeof(dst_program_path), "%s/%s",
           global->work_dir, packet->program_name);
  snprintf(src_program_path, sizeof(src_program_path), "%s/%s",
           dir_path, packet->program_name);
  if (fast_copy_file(src_program_path, dst_program_path) < 0) {
    snprintf(result->comment, sizeof(result->comment),
             "copy failed: %s -> %s", src_program_path, dst_program_path);
    goto cleanup;
  }

  /* copy the input file */
  snprintf(dst_input_path, sizeof(dst_input_path), "%s/%s",
           global->work_dir, packet->input_file_name);
  snprintf(src_input_path, sizeof(src_input_path), "%s/%s",
           dir_path, packet->test_file_name);
  if (packet->enable_unix2dos > 0) {
    if (generic_copy_file(CONVERT, "", src_input_path, "",
                          CONVERT, "", dst_input_path, "") < 0) {
      snprintf(result->comment, sizeof(result->comment),
               "unix2dos copy failed: %s -> %s", src_input_path, dst_input_path);
      goto cleanup;
    }
  } else {
    if (fast_copy_file(src_input_path, dst_input_path) < 0) {
      snprintf(result->comment, sizeof(result->comment),
               "copy failed: %s -> %s", src_input_path, dst_input_path);
      goto cleanup;
    }
  }

  if (packet->combined_stdin > 0) {
    snprintf(dst_input_path, sizeof(dst_input_path), "%s/%s.stdin",
             global->work_dir, packet->input_file_name);
    if (packet->enable_unix2dos > 0) {
      if (generic_copy_file(CONVERT, "", src_input_path, "",
                            CONVERT, "", dst_input_path, "") < 0) {
        snprintf(result->comment, sizeof(result->comment),
                 "unix2dos copy failed: %s -> %s", src_input_path,
                 dst_input_path);
        goto cleanup;
      }
    } else {
      if (fast_copy_file(src_input_path, dst_input_path) < 0) {
        snprintf(result->comment, sizeof(result->comment),
                 "copy failed: %s -> %s", src_input_path, dst_input_path);
        goto cleanup;
      }
    }
  }

  snprintf(run_output_path, sizeof(run_output_path), "%s/%s",
           global->work_dir, packet->output_file_name);
  if (packet->combined_stdout > 0) {
    snprintf(run_output_path, sizeof(run_output_path), "%s/%s.stdout",
             global->work_dir, packet->output_file_name);
  }

  snprintf(run_error_path, sizeof(run_error_path), "%s/%s",
           global->work_dir, packet->error_file_name);

  cur_status = run_program(packet, dst_program_path,
                           dst_input_path, run_output_path,
                           run_error_path, result);

  if (packet->combined_stdout > 0) {
    snprintf(full_output_path, sizeof(full_output_path), "%s/%s",
             global->work_dir, packet->output_file_name);
    concatenate_files(full_output_path, run_output_path);
    snprintf(run_output_path, sizeof(run_output_path), "%s", full_output_path);
  }

  info("Testing finished: CPU time = %d, real time = %d",
       result->cpu_time_millis, result->real_time_millis);

  /* copy the stderr output */
  result->error_file_existed = 0;
  if (packet->ignore_stderr <= 0) {
    error_file_size = generic_file_size("", run_error_path, "");
    if (error_file_size >= 0) {
      result->error_file_existed = 1;
      result->error_file_orig_size = error_file_size;
      if (error_file_size > packet->max_error_file_size) {
        result->error_file_truncated = 1;
        if (generic_truncate(run_error_path, packet->max_error_file_size) < 0) {
          snprintf(result->comment, sizeof(result->comment),
                   "truncate failed: %s", run_error_path);
          goto cleanup;
        }
        if (!(f = fopen(run_error_path, "a"))) {
          snprintf(result->comment, sizeof(result->comment),
                   "appending error file failed: %s", run_error_path);
          goto cleanup;
        }
        fprintf(f, "\n\nFile truncated!\n");
        fclose(f); f = 0;
        result->error_file_size = generic_file_size("", run_error_path, "");
      } else {
        result->error_file_truncated = 0;
        result->error_file_size = error_file_size;
      }

      if (packet->error_file_name[0]) {
        snprintf(error_file_path, sizeof(error_file_path), "%s/%s",
                 result_path, packet->error_file_name);
		info("Copy: %s -> %s", run_error_path, error_file_path);
        if (fast_copy_file(run_error_path, error_file_path) < 0) {
          snprintf(result->comment, sizeof(result->comment),
                   "copy failed: %s -> %s", run_error_path, error_file_path);
          goto cleanup;
        }
      }
    }
  }

  /* copy the program output */
  info("Copying program output");
  output_file_size = generic_file_size("", run_output_path, "");
  if (output_file_size < 0) {
    if (!result->comment[0]) {
      snprintf(result->comment, sizeof(result->comment),
               "no output file");
    }
    if (cur_status == RUN_OK) cur_status = RUN_PRESENTATION_ERR;
    result->status = cur_status;
    goto cleanup;
  }

  result->output_file_existed = 1;
  result->output_file_orig_size = output_file_size;
  if (output_file_size > packet->max_output_file_size) {
    result->output_file_too_big = 1;
    snprintf(result->comment, sizeof(result->comment),
             "output file is too big (%ld)", ((long) output_file_size));
    if (cur_status == RUN_OK) cur_status = RUN_PRESENTATION_ERR;
    result->status = cur_status;
    goto cleanup;
  }

  snprintf(result_file_path, sizeof(result_file_path), "%s/%s",
           result_path, packet->result_file_name);
  info("Copy: %s -> %s", run_output_path, result_file_path);
  if (fast_copy_file(run_output_path, result_file_path) < 0) {
    snprintf(result->comment, sizeof(result->comment),
             "copy failed: %s -> %s", run_output_path, result_file_path);
    result->status = RUN_CHECK_FAILED;
    goto cleanup;
  }

  result->status = cur_status;

 cleanup:
  if (f) fclose(f);
  remove_directory_recursively(global->work_dir, 1);
}