int main (int argc, char **argv) { int command_number = 1; bool print_tree = false; char const *profile_name = 0; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "p:t")) { case 'p': profile_name = optarg; break; case 't': print_tree = true; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); int profiling = -1; if (profile_name) { profiling = prepare_profiling (profile_name); if (profiling < 0) error (1, errno, "%s: cannot open", profile_name); } command_t last_command = NULL; command_t command; while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %dn", command_number++); print_command (command); } else { last_command = command; execute_command (command, profiling); } } return print_tree || !last_command ? 0 : command_status (last_command); }
int main (int argc, char **argv) { int command_number = 1; bool print_tree = false; char const *profile_name = 0; program_name = argv[0]; for (;;) switch (getopt (argc, argv, "p:t")) { case 'p': profile_name = optarg; break; case 't': print_tree = true; break; default: usage (); break; case -1: goto options_exhausted; } options_exhausted:; // There must be exactly one file argument. if (optind != argc - 1) usage (); script_name = argv[optind]; FILE *script_stream = fopen (script_name, "r"); if (! script_stream) error (1, errno, "%s: cannot open", script_name); command_stream_t command_stream = make_command_stream (get_next_byte, script_stream); int profiling = -1; if (profile_name) { profiling = prepare_profiling (profile_name); if (profiling < 0) error (1, errno, "%s: cannot open", profile_name); } command_t last_command = NULL; command_t command; //timer starts struct timespec absolute; double start_time, prev_user_CPU, prev_sys_CPU; get_timer(&start_time, &prev_user_CPU, &prev_sys_CPU); while ((command = read_command_stream (command_stream))) { if (print_tree) { printf ("# %d\n", command_number++); print_command (command); } else { last_command = command; execute_command (command, profiling); } } //timer stops clock_gettime(CLOCK_REALTIME, &absolute); double absolute_time = absolute.tv_sec + (double)absolute.tv_nsec/1000000000.0; double end_time, current_user_CPU, current_sys_CPU; get_timer(&end_time, ¤t_user_CPU, ¤t_sys_CPU); double real_time = end_time - start_time; double userCPU = current_user_CPU-prev_user_CPU; double sysCPU = current_sys_CPU-prev_sys_CPU; pid_t pid = getpid(); char buffer[1024]; sprintf(buffer, "%f %f %.3f %.3f %d\n", absolute_time, real_time, userCPU, sysCPU, (int)pid); write(profiling, buffer, strlen(buffer)); return print_tree || !last_command ? 0 : command_status (last_command); }