Exemplo n.º 1
0
int main()
{
	FILE *f;
	int *a, i = 0;
	time_t t;

	f = fopen("data", "r");
	if (!f)
		PERROR_RET("Unable to open file data for reading");


	a = (int *) calloc(sizeof(int), MAX_NUM);

	while (!feof(f)) {
		fscanf(f, "%d", &a[i]);
		i++;
	}

	start_timestamp();
	insertsort(a, i - 1);
	t = stop_timestamp();
	printf("Sorting complete in %lu microseconds\n", t);
	printarray(a, i - 1);

	free(a);
	fclose(f);

	return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	FILE *f;
	int *a, i = 0, key;
	time_t t;
	bool found;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <key>\n", argv[0]);
		return 1;
	}

	f = fopen("data", "r");
	if (!f)
		PERROR_RET("Unable to open file data for reading");


	sscanf(argv[1], "%d", &key);

	a = (int *) calloc(sizeof(int), MAX_NUM);

	while (!feof(f)) {
		fscanf(f, "%d", &a[i]);
		i++;
	}

	start_timestamp();
	found = binarysearch(a, 0, i - 2, key);
	t = stop_timestamp();
	printf("Key %d was %s.\n", 
			key, found ? "found" : "not found");
	printf("Search complete in %lu microseconds\n", t);

	free(a);
	fclose(f);

	return 0;
}
Exemplo n.º 3
0
/*	e x e c _ S h e l l ( )
	If args[0] is NULL, spawn a shell, otherwise execute the specified
	command line.
	Return the exit status of the program, or -1 if wait() or fork()
	return an error.
*/
int
exec_Shell(char **args)
{
    register int child_pid;
    static char error_buf[32];
    void (*intr_sig)(), (*quit_sig)();
    if ( args[0] == NULL ) {
	char	*arg_sh = getenv( "SHELL" );
	/* $SHELL, if set, DFL_SHELL otherwise.			*/
	if (	arg_sh == NULL
		/* Work around for process group problem.		*/
		||	strcmp( arg_sh, TCSH ) == 0
		||	strcmp( arg_sh, CSH ) == 0
	    )
	    arg_sh = DFL_SHELL;
	args[0] = arg_sh;
	args[1] = NULL;
    }

    intr_sig = signal( SIGINT,  SIG_IGN );
    quit_sig = signal( SIGQUIT, SIG_IGN );
    switch ( child_pid = fork() ) {
	case -1 :
	    PERROR_RET();
	case  0 : /* Child process - execute.		*/
	{
	    int	tmp_fd;
	    if ((tmp_fd = open( "/dev/tty", O_WRONLY )) == -1) {
		PERROR_RET();
	    }
	    (void) close( 2 );
	    if ( fcntl( tmp_fd, F_DUPFD, 2 ) == -1 ) {
		PERROR_RET();
	    }
	    (void) execvp( args[0], args );
	    loc_Perror( args[0] );
	    bu_exit( errno, NULL );
	}
	default :
	{
	    register int	pid;
	    int		stat_loc;
	    while ((pid = wait( &stat_loc )) != -1 && pid != child_pid)
		;
	    prnt_Event( "\n" );
	    (void) signal( SIGINT,  intr_sig );
	    (void) signal( SIGQUIT, quit_sig );
	    if ( pid == -1 ) {
		/* No children. */
		loc_Perror( "wait" );
		return errno;
	    }
	    switch ( stat_loc & 0377 ) {
		case 0177 : /* Child stopped.		*/
		    bu_log(	"\"%s\" (%d) Child stopped.\n",
				__FILE__,
				__LINE__
			);
		    return	(stat_loc >> 8) & 0377;
		case 0 :    /* Child exited.		*/
		    return	(stat_loc >> 8) & 0377;
		default :   /* Child terminated.	*/
		    bu_log(	"\"%s\" (%d) Child terminated, signal %d, status=0x%x.\n",
				__FILE__,
				__LINE__,
				stat_loc&0177,
				stat_loc
			);
		    return	stat_loc&0377;
	    }
	}
    }
}