Exemplo n.º 1
0
void load_conffile(void)
{
	FILE *f;
	char *confdir = NULL;
	char *head;

	if (loaded)
		return;
	if (conffile == NULL) {
		conffile = DefaultConfFile;
		confdir = DefaultConfDir;
	}

	if (strcmp(conffile, "partitions")==0) {
		char *list = dl_strdup("DEV");
		dl_init(list);
		dl_add(list, dl_strdup("partitions"));
		devline(list);
		free_line(list);
	} else if (strcmp(conffile, "none") != 0) {
		f = fopen(conffile, "r");
		/* Debian chose to relocate mdadm.conf into /etc/mdadm/.
		 * To allow Debian users to compile from clean source and still
		 * have a working mdadm, we read /etc/mdadm/mdadm.conf
		 * if /etc/mdadm.conf doesn't exist
		 */
		if (f == NULL &&
		    conffile == DefaultConfFile) {
			f = fopen(DefaultAltConfFile, "r");
			if (f) {
				conffile = DefaultAltConfFile;
				confdir = DefaultAltConfDir;
			}
		}
		if (f) {
			conf_file_or_dir(f);
			fclose(f);
		}
		if (confdir) {
			f = fopen(confdir, "r");
			if (f) {
				conf_file_or_dir(f);
				fclose(f);
			}
		}
	}
	/* If there was no AUTO line, process an empty line
	 * now so that the MDADM_CONF_AUTO env var gets processed.
	 */
	head = dl_strdup("AUTO");
	dl_init(head);
	autoline(head);
	free_line(head);

	loaded = 1;
}
Exemplo n.º 2
0
void
dlmod_load_module(struct dlmod *dlm)
{
    char            sym_init[64];
    char           *p, tmp_path[255];
    int             (*dl_init) (void);
    char           *st;

    DEBUGMSGTL(("dlmod", "dlmod_load_module %s: %s\n", dlm->name,
                dlm->path));

    if (!dlm || !dlm->path || !dlm->name ||
        (dlm->status != DLMOD_UNLOADED && dlm->status != DLMOD_ERROR))
        return;

    if (dlm->path[0] == '/') {
#ifdef RTLD_NOW
        dlm->handle = dlopen(dlm->path, RTLD_NOW);
#else
        dlm->handle = dlopen(dlm->path, RTLD_LAZY);
#endif
        if (dlm->handle == NULL) {
            snprintf(dlm->error, sizeof(dlm->error),
                     "dlopen failed: %s", dlerror());
            dlm->status = DLMOD_ERROR;
            return;
        }
    } else {
        for (p = strtok_r(dlmod_path, ":", &st); p; p = strtok_r(NULL, ":", &st)) {
            snprintf(tmp_path, sizeof(tmp_path), "%s/%s.so", p, dlm->path);
            DEBUGMSGTL(("dlmod", "p: %s tmp_path: %s\n", p, tmp_path));
#ifdef RTLD_NOW
            dlm->handle = dlopen(tmp_path, RTLD_NOW);
#else
            dlm->handle = dlopen(tmp_path, RTLD_LAZY);
#endif
            if (dlm->handle == NULL) {
                snprintf(dlm->error, sizeof(dlm->error),
                         "dlopen failed: %s", dlerror());
                dlm->status = DLMOD_ERROR;
            }
        }
        strncpy(dlm->path, tmp_path, sizeof(dlm->path));
        if (dlm->status == DLMOD_ERROR)
            return;
    }
    snprintf(sym_init, sizeof(sym_init), "init_%s", dlm->name);
    dl_init = dlsym(dlm->handle, sym_init);
    if (dl_init == NULL) {
        dlclose(dlm->handle);
        snprintf(dlm->error, sizeof(dlm->error),
                 "dlsym failed: can't find \'%s\'", sym_init);
        dlm->status = DLMOD_ERROR;
        return;
    }

    dl_init();
    dlm->error[0] = '\0';
    dlm->status = DLMOD_LOADED;
}
Exemplo n.º 3
0
BOOL WINAPI DllMain( HINSTANCE dll, DWORD reason, LPVOID reserved ) {
    if ( reason == DLL_PROCESS_ATTACH ) { dl_init(); }
    else if ( reason == DLL_PROCESS_DETACH ) { dl_fini(); }
    else if ( reason == DLL_THREAD_ATTACH ) { pt_init(); }
    else if ( reason == DLL_THREAD_DETACH ) { pt_fini(); }
    else { }                    /* ignore */ 
    return 1;
}
Exemplo n.º 4
0
task main()
{
    dl_init("dl_log.txt", false);

    dl_insert_int(23);
    dl_append_int(24);
    dl_close();

}
Exemplo n.º 5
0
Arquivo: lib.c Projeto: Saber817/j1939
extern void j1939_init(void)
{
    debug_init();
    fifo_init();

    al_init();
    dl_init();
    pl_init();

    return;
}
Exemplo n.º 6
0
// Insert a value (val) into the tail of the given list (list).
bool dl_insert(list_t *list1,char *word)
{
	//printf("Hit here");
    //printf("word read in: %s\n",word);
		
  list_t *curr = front;
	if(front != NULL && incerementCount(word) == TRUE )
			return TRUE;
	
  list_t *list;
	list = (list_t *)malloc(sizeof(list_t));
	
	if(list != NULL)
	{
		dl_init(list);
		list->count = 1;
		list->word = (char *)malloc(strlen(word)+1);
    
		
		strcpy(list->word,word);
	}
	else
	{
		printf("Error");
		return FALSE;
	}
		
	if(curr == NULL)
	{

		front = list;
		rear = list;
		front->prev=rear;
		rear->next=front;
		//printf("INSERT CALL word: %s\n",list->word);
		return TRUE;
	}
	else if(curr != NULL)
	{
	
		rear->next = list;
		list->prev = rear;
		rear = list;
		rear->next = front;
		front->prev = rear;
	
    //printf("INSERT CALL word: %s\n",list->word);
		return TRUE;
	}
	return FALSE;


}
Exemplo n.º 7
0
static int 
_csngen_init_callbacks (CSNGen *gen)
{
	/* create a lock to control access to the callback list */
	gen->callbacks.lock = slapi_new_rwlock();	
	if (gen->callbacks.lock == NULL)
	{
		return CSN_NSPR_ERROR;
	}

    gen->callbacks.list = dl_new ();
	dl_init (gen->callbacks.list, 0);

    return CSN_SUCCESS;
}
Exemplo n.º 8
0
void *
pg_dlopen(char *filename)
{
	static int	dl_initialized = 0;
	void	   *handle;

	/*
	 * initializes the dynamic loader with the executable's pathname. (only
	 * needs to do this the first time pg_dlopen is called.)
	 */
	if (!dl_initialized)
	{
		if (!dl_init(my_exec_path))
			return NULL;

		/*
		 * if there are undefined symbols, we want dl to search from the
		 * following libraries also.
		 */
		dl_setLibraries("/usr/lib/libm_G0.a:/usr/lib/libc_G0.a");
		dl_initialized = 1;
	}

	/*
	 * open the file. We do the symbol resolution right away so that we will
	 * know if there are undefined symbols. (This is in fact the same
	 * semantics as "ld -A". ie. you cannot have undefined symbols.
	 */
	if ((handle = dl_open(filename, DL_NOW)) == NULL)
	{
		int			count;
		char	  **list = dl_undefinedSymbols(&count);

		/* list the undefined symbols, if any */
		if (count)
		{
			while (*list)
			{
				elog(WARNING, "\"%s\" is undefined", *list);
				list++;
			}
		}
	}

	return (void *) handle;
}
Exemplo n.º 9
0
char *conf_line(FILE *file)
{
	char *w;
	char *list;

	w = conf_word(file, 1);
	if (w == NULL) return NULL;

	list = dl_strdup(w);
	free(w);
	dl_init(list);

	while ((w = conf_word(file,0))){
		char *w2 = dl_strdup(w);
		free(w);
		dl_add(list, w2);
	}
/*    printf("got a line\n");*/
	return list;
}
Exemplo n.º 10
0
void *execute_read(void *ptr){
	readargs *args = (readargs*)ptr;	
	list_t *temp_list = NULL;
	temp_list = dl_init();
	read_file_word_by_word(args->fp, temp_list, 
		args->actual_offset, args->child_read_size, args->size);
	int node_count = dl_count(temp_list);
	int wordcount = 0;
	char *temp_word = NULL;
	pthread_mutex_lock(&mutex);
	int i = 0;
	for(i = 0; i < node_count; i++){
		temp_word = dl_get_back(temp_list);
		wordcount = dl_get_wordcount(temp_list, temp_word);
		dl_insert_merge(args->list, temp_word, wordcount);
		dl_pop_back(temp_list);
	}
	pthread_mutex_unlock(&mutex);
	dl_clear_destroy(temp_list);
	return (NULL);
}
Exemplo n.º 11
0
int main(int argc, char *argv[]) {
    char *input = argv[1];
    char *verify_number = argv[2];
    int number = atoi(argv[2]);
    char *output = argv[3];
    // validate inputs. Store the result
    // of the file check for later use
    list_t *wordlist = NULL;
    check(argc == 4, "Invalid number of arguments:\n%s", usages);
    int result = is_file_or_dir(input);
    check(result != 0,
          "File/Dir '%s' does not exist or is not readable.\n%s",
          input,  usages);
    check(is_file_or_dir(output) == 0,
          "File '%s' already exists.\n%s",
          output, usages);
    if(number == 0 && strlen(verify_number) == 1) {
        check(isdigit(verify_number[0]) != 0 && number >=0,
              "%s is not a valid number.\n%s",
              verify_number, usages);
    }
    wordlist = dl_init();
    // begin branching logic to gather file names
    // for processing
    if(result == 2) { // if result == 2, it's a directory
        get_words_from_files(wordlist, input);
        print_words_to_file_and_screen(wordlist, number, output);
    } else if(result == 1) { // if result == 2, it's a file
        read_file_word_by_word(input, wordlist);
        print_words_to_file_and_screen(wordlist, number, output);
    }

    dl_clear_destroy(wordlist);
    return 0;
error:
    if(wordlist != NULL) {
        dl_clear_destroy(wordlist);
    }
    return 1;
}
Exemplo n.º 12
0
bool get_words_from_files(list_t *wordlist, char *input) {
    list_t *filelist = NULL;
    filelist = dl_init();
    check(get_all_files(input, filelist) == 1,
          "Could not compile full list.");
    int file_count = dl_count(filelist);
    int i = 0;
    char *filename = NULL;
    for(i = 0; i < file_count; i++) {
        filename = dl_get_back(filelist);
        check(filename != NULL,
              "filename was NULL.");
        read_file_word_by_word(filename, wordlist);
        check(dl_pop_back(filelist) == 1,
              "Did not pop back properly.");
    }
    dl_clear_destroy(filelist);
    return 1;
error:
    if(filelist != NULL) {
        dl_clear_destroy(filelist);
    }
    return 0;
}
Exemplo n.º 13
0
// Insert a value (val) into the front of the given list (list), which becomes the first node o
bool dl_insert_front(list_t *list1, char *word)
{
	list_t *curr = front;
    list_t *list;
	list = (list_t *)malloc(sizeof(list_t));
	if(list != NULL)
	{
	dl_init(list);
		list->count = 1;
		list->word = word;	
	}
	else
	{
	//printf("ERROR");
	return FALSE;
	}
	if(curr == NULL)
	{
		front = list;
		rear = list;
		front->prev=rear;
		rear->next=front;
		return TRUE;
	}
	else
	{
		list->next = front;
		front->prev = list;
		front = list;
		front->prev = rear;
		rear->next = front;
		return TRUE;
	}
	return FALSE;
	//printf(" Insert %d. node created\n",val);
}
Exemplo n.º 14
0
int main(){
	DblList* dlist;
	dlist = (DblList *) malloc(sizeof(DblList));
	dlist->head = (NodeType *) malloc(sizeof(NodeType*));
	dlist->tail = (NodeType *) malloc(sizeof(NodeType*));
	dl_init(dlist);

	int x = 0;//just made a random variable to have an infinite while loop
	while (x == 0){
		int y;//this represents the user choice, used effectively like a switch case

		printf("888       888        888                                      \n");
		printf("888   o   888        888                                      \n");
		printf("888  d8b  888        888                                      \n");
		printf("888 d888b 888 .d88b. 888 .d8888b .d88b. 88888b.d88b.  .d88b.  \n");
		printf("888d88888b888d8P  Y8b888d88P    d88  88b888  888  88bd8P  Y8b \n");
		printf("88888P Y8888888888888888888     888  888888  888  88888888888 \n");
		printf("8888P   Y8888Y8b.    888Y88b    Y88..88P888  888  888Y8b.     \n");
		printf("888P     Y888  Y8888 888  Y8888P  Y88P  888  888  888  Y8888  \n");
		printf("\n");
		printf("1. Add a movie\n");
		printf("2. Remove a movie\n");
		printf("3. Print movies\n");
		printf("4. Exit the program\n");
		scanf("%d", &y);
		/************************************************************************
		*GOES INTO ADD MENU
		************************************************************************/
		while(y == 1){//WHILE ADDING
			Movie* movie = (Movie *) malloc(sizeof(Movie));
			movie->year = 0;
			int g;
			int yy;
			char *title;
			
			printf("Enter title to add\n");
			scanf("%15s", title); //15s so i dont get a buffer overflow
			strcpy(movie->title, title);

			printf("Choose a genre\n");
			printf("1. COMEDY\n");
			printf("2. DRAMA\n");
			printf("3. THRILLER\n");
			printf("4. ACTION\n");
			scanf("%d", &g);

			if (g == 1){movie->genre = COMEDY;}
				else if (g == 2){movie->genre = DRAMA;}
				else if (g == 3){movie->genre = THRILLER;}
				else if (g == 4){movie->genre = ACTION;}
				else{
					printf("Invalid choice\n");
					free(movie);
					continue;
				}
			dl_add(dlist, movie);
			printf("Enter 0 to add more or 1 to get out\n");
			scanf("%d", &yy);
			if(yy == 1){
				y = 0;
			}

		}
		/************************************************************************
		*GOES INTO DELETE MENU
		************************************************************************/
		while(y == 2){//WHILE DELETING
			int yy;
			char title[16];
			Movie* movie = (Movie *) malloc(sizeof(Movie));

			printf("Enter title to remove\n");
			scanf("%15s", &title); 
			strcpy(movie->title, title);

			dl_del(dlist, movie);
			free(movie);

			printf("Enter 0 to remove more or 1 to get out\n");
			scanf("%d", &yy);
			if(yy == 1){
				y = 0;
			}
		}
		/************************************************************************
		*GOES INTO PRINT MENU
		************************************************************************/
		while(y == 3){//WHILE PRINTING
			int yy;
			int g;

			printf("1. Print Forward\n");
			printf("2. Print backward\n");
			printf("3. Print by genre\n");
			printf("4. EXIT\n");
			scanf("%d", &yy);

			if( yy == 1){dl_printFwd(dlist);

			}else if (yy == 2){dl_printBack(dlist);

			}else if (yy == 3){
				printf("Please choose a GENRE to print\n");
				printf("1. COMEDY\n");
				printf("2. DRAMA\n");
				printf("3. THRILLER\n");
				printf("4. ACTION\n");
				scanf("%d", &g);
				if (g == 1){dl_printByGenre(dlist, COMEDY);}
				else if (g == 2){dl_printByGenre(dlist, DRAMA);}
				else if (g == 3){dl_printByGenre(dlist, THRILLER);}
				else if (g == 4){dl_printByGenre(dlist, ACTION);}
				else{printf("Incorrect choice");}

			}else{
				y = 0;
				printf("Exiting...");
			}
		}
		while(y == 4){//USER SELECTED EXIT
			dl_cleanup(dlist);
			printf("Goodbye! \n");
			x = 1; //exit the outermost while loop thereby returning and exiting the main
			break;

		}

	}
	return 0;
}
Exemplo n.º 15
0
int main(int argc, char *argv[])
{
  struct dlist *node, list, *trav;
  struct list list2;
  cat_time_t ct;

  int arr1[] = { 10, 15, 5, 7, 12, 2, 7 },
      arr2[] = { 9, 1, 6, 10, 6 }, 
      s1 = sizeof(arr1) / sizeof(arr1[0]),
      s2 = sizeof(arr2) / sizeof(arr1[0]), i, j = 0;

  printf("Initial: ");
  for ( i = 0 ; i < s1 ; ++i )
    printf("%u/%u ", ++j, arr1[i]);
  printf("  |  ");
  for ( i = 0 ; i < s2 ; ++i )
    printf("%u/%u ", ++j, arr2[i]);
  printf("\n");

  j=0;
  dl_init(&list, tm_zero);
  for ( i = 0 ; i < s1 ; ++i )
  {
    ++j;
    node = cdl_new(tm_lset(arr1[i], 0), int2ptr(j));
    dl_ins(&list, node);
  }

  dl_first(&list, &ct);
  printf("The first is at %u\n", (uint)tm_sec(ct));
  node = dl_deq(&list);
  printf("The first was %u at %u\n\n", (uint)ptr2uint(cdl_data(node)),
         (uint)tm_sec(node->ttl));
  cdl_free(node);

  printf("Nodes from advance 10:  ");
  l_init(&list2);
  dl_adv(&list, tm_lset(10, 0), &list2);

  while ( ! l_isempty(&list2) )
  {
    trav = container(l_head(&list2), struct dlist, entry);
    printf("%u/%u ", (uint)ptr2uint(cdl_data(trav)), (uint)tm_sec(trav->ttl));
    l_rem(&trav->entry);
    cdl_free(trav);
  }
  printf("\n\n");

  for ( i = 0 ; i < s2 ; ++i ) 
  {
    ++j;
    node = cdl_new(tm_lset(arr2[i], 0), int2ptr(j));
    dl_ins(&list, node);
  }

  printf("After inserting arr2 array is :\n\t");
  while ( node = dl_deq(&list) )
  {
    printf("%u/", (uint)ptr2uint(cdl_data(node)));
    printf("%u ", (uint)tm_sec(node->ttl));
    cdl_free(node);
  }
  printf("\n");

  return 0;
}
Exemplo n.º 16
0
void load_conffile(char *conffile)
{
	FILE *f;
	char *line;

	if (loaded) return;
	if (conffile == NULL)
		conffile = DefaultConfFile;

	if (strcmp(conffile, "none") == 0) {
		loaded = 1;
		return;
	}
	if (strcmp(conffile, "partitions")==0) {
		char *list = dl_strdup("DEV");
		dl_init(list);
		dl_add(list, dl_strdup("partitions"));
		devline(list);
		free_line(list);
		loaded = 1;
		return;
	}
	f = fopen(conffile, "r");
	/* Debian chose to relocate mdadm.conf into /etc/mdadm/.
	 * To allow Debian users to compile from clean source and still
	 * have a working mdadm, we read /etc/mdadm/mdadm.conf
	 * if /etc/mdadm.conf doesn't exist
	 */
	if (f == NULL &&
	    conffile == DefaultConfFile) {
		f = fopen(DefaultAltConfFile, "r");
		if (f)
			conffile = DefaultAltConfFile;
	}
	if (f == NULL)
		return;

	loaded = 1;
	while ((line=conf_line(f))) {
		switch(match_keyword(line)) {
		case 0: /* DEVICE */
			devline(line);
			break;
		case 1: /* ARRAY */
			arrayline(line);
			break;
		case 2: /* MAIL */
			mailline(line);
			break;
		case 3: /* PROGRAM */
			programline(line);
			break;
		default:
			fprintf(stderr, Name ": Unknown keyword %s\n", line);
		}
		free_line(line);
	}
    
	fclose(f);

/*    printf("got file\n"); */
}
Exemplo n.º 17
0
void load_conffile(void)
{
	FILE *f;
	char *line;

	if (loaded) return;
	if (conffile == NULL)
		conffile = DefaultConfFile;

	if (strcmp(conffile, "none") == 0) {
		loaded = 1;
		return;
	}
	if (strcmp(conffile, "partitions")==0) {
		char *list = dl_strdup("DEV");
		dl_init(list);
		dl_add(list, dl_strdup("partitions"));
		devline(list);
		free_line(list);
		loaded = 1;
		return;
	}
	f = fopen(conffile, "r");
	/* Debian chose to relocate mdadm.conf into /etc/mdadm/.
	 * To allow Debian users to compile from clean source and still
	 * have a working mdadm, we read /etc/mdadm/mdadm.conf
	 * if /etc/mdadm.conf doesn't exist
	 */
	if (f == NULL &&
	    conffile == DefaultConfFile) {
		f = fopen(DefaultAltConfFile, "r");
		if (f)
			conffile = DefaultAltConfFile;
	}
	if (f == NULL)
		return;

	loaded = 1;
	while ((line=conf_line(f))) {
		switch(match_keyword(line)) {
		case Devices:
			devline(line);
			break;
		case Array:
			arrayline(line);
			break;
		case Mailaddr:
			mailline(line);
			break;
		case Mailfrom:
			mailfromline(line);
			break;
		case Program:
			programline(line);
			break;
		case CreateDev:
			createline(line);
			break;
		case Homehost:
			homehostline(line);
			break;
		default:
			fprintf(stderr, Name ": Unknown keyword %s\n", line);
		}
		free_line(line);
	}
    
	fclose(f);

/*    printf("got file\n"); */
}
Exemplo n.º 18
0
/*
 * NAME:	showfiles()
 * DESCRIPTION:	display a set of files
 */
static
int showfiles(darray *files, int flags, int options, int width)
{
  dlist list;
  int i, sz, result = 0;
  queueent *ents;
  dstring str;
  char **strs;
  void (*show)(int, queueent *, char **, int, int, int);

  if (dl_init(&list) == -1)
    {
      fprintf(stderr, "%s: not enough memory\n", argv0);
      return -1;
    }

  sz   = darr_size(files);
  ents = darr_array(files);

  dstr_init(&str);

  for (i = 0; i < sz; ++i)
    {
      if (outpath(&str, &ents[i], flags) == -1 ||
	  dl_append(&list, dstr_string(&str)) == -1)
	{
	  result = -1;
	  break;
	}
    }

  dstr_free(&str);

  strs = dl_array(&list);

  switch (options & F_MASK)
    {
    case F_LONG:
      show = show_long;
      break;

    case F_ONE:
      show = show_one;
      break;

    case F_MANY:
      show = show_many;
      break;

    case F_HORIZ:
      show = show_horiz;
      break;

    case F_COMMAS:
      show = show_commas;
      break;

    default:
      abort();
    }

  show(sz, ents, strs, flags, options, width);

  dl_free(&list);

  return result;
}
Exemplo n.º 19
0
int main(int argc, char *argv[]){
	int pid = 0;
	int i = 0;
	int child = 0;
	int status;
	
	char *first_param = "2";
	int num_children = atoi(first_param);
		
	int children[num_children];

	struct stat st;
	char *file_name = "./dllist.c";
	int size = 0;
	stat(file_name, &st);
	size = st.st_size;
	printf("file_name: %s, file_size: %d\n", file_name, size);
	FILE *fp = fopen(file_name, "r");
	int child_read_size = size / num_children;	
	int child_segment_size = 0;
	for(i = 0; i < NUM_ELEMS(children); i++){
		child_segment_size += child_read_size;
		pid = fork();
		if(pid != 0){
			children[i] = pid;
			printf("child_pid: %d\n", pid);
			waitpid(children[i], &status, 0);
		} else {
			child = 1;
			break;
		}
	}
	if(child == 0){
		printf("parent process, pid: %d\n", getpid());
	} else {
		printf("child process, pid: %d, ppid: %d\n", pid, getppid());
		printf("\tchild_start: %d, child_end: %d\n", 
			child_segment_size - child_read_size,
			child_segment_size);
		int actual_offset = child_segment_size - child_read_size;
		int offset = fseek(fp, actual_offset, SEEK_SET);
		printf("\toffset: %d\n", offset);
		int ch = 0;
		int byte_count = 0;
		char string_buffer[512];
		if(actual_offset != 0){
			int cur_character;
			do{
				cur_character = fgetc(fp);
				if(isspace(cur_character) == 0){
					offset = fseek(fp, -2, SEEK_CUR);
				} else {
					offset = fseek(fp, -1, SEEK_CUR);
				}
			} while(isspace(cur_character) == 0
				&& (cur_character != EOF
				&& offset == 0));
			printf("\t\tftell(fp): %lu\n", ftell(fp));
		}
		int word_count = 0;
		list_t *list = NULL;
		list = dl_init();
		do {		
			ch = fscanf(fp, "%s", string_buffer);
			if(actual_offset != 0 && byte_count == 0){
				printf("\t\tfirst_word: %s\n", string_buffer);
			}
			byte_count = ftell(fp) - (actual_offset);
			if(string_buffer[0] == '\0'){
				printf("\t\twhitespace_at_bytecount: %d\n", byte_count);
			}
			if(byte_count < child_read_size
				|| (actual_offset + byte_count) == size){
				if(ch != EOF){
					string_to_lower(string_buffer);
					if(validate_word(string_buffer) != 0){
						char *temp_word = string_buffer;
						check(dl_insert(list, temp_word) == 1,
							"Did not insert '%s' properly.",
							temp_word);
						word_count++;
					}
				} else {
					printf("\t\tlast_word_file: %s\n", string_buffer);
				}
			} else {
				printf("\t\tlast_word: %s\n", string_buffer);	
			}
		} while( ch != EOF && byte_count < child_read_size);
		dl_clear_destroy(list);	
		printf("\tbyte_count: %d, word_count: %d\n", byte_count, word_count);
		fclose(fp);
		exit(0);
	}
	rewind(fp);
	int temp;
	char temp_buffer[512];
	int true_wordcount = 0;
	do{
		temp = fscanf(fp, "%s", temp_buffer); 
		if(temp != EOF){
			true_wordcount++;
		}
	} while(temp != EOF);
	fclose(fp);
	printf("true_wordcount: %d\n", true_wordcount);
	return 0;
error:
	return 1;
}
Exemplo n.º 20
0
int main(int argc, char *argv[]){
	char *input = argv[1];
	char *verify_procnumber = argv[2];
	char *verify_printnumber = argv[3];
	char *output = argv[4];
	list_t *list = NULL;
	check(argc == 5, "Invalid number of arguments: %d\n%s", argc, usages);
	int input_check_result = is_file_or_dir(input);
	check(input_check_result != 0 && input_check_result != 2, 
		"File/Dir '%s' does not exist or is not a file.\n%s",
		input,  usages);
	check(is_file_or_dir(output) == 0,
		"File '%s' already exists.\n%s", 
		output, usages);
	check(verify_input_is_valid_digit(verify_procnumber) == 1,
		"PROCNUMBER is not a valid number.");
	check(verify_input_is_valid_digit(verify_printnumber) == 1,
		"PRINTNUMBER is not a valid number.");
	// begin branching logic to gather file names
	// for processing
	stat_obj size_test;
	stat(input, &size_test);
	int proc_number = atoi(verify_procnumber);
	int print_number = atoi(verify_printnumber);
	int size = size_test.st_size;
	list = dl_init();
	pthread_mutex_init(&mutex, NULL);
	pthread_cond_init(&m_cond, NULL);

	FILE *fp = NULL;
	int child_read_size = 0;
	int child_segment_size = 0;
	if(proc_number == 0){
		child_read_size = size;
	} else {
		child_read_size = size / proc_number;
	}
	int actual_offset = 0;
	if(input_check_result == 1 && proc_number > 0){
		readargs *all_readargs[proc_number];
		pthread_t thread_ids[proc_number];
		int i = 0;
		for(i = 0; i < proc_number; i++){
			all_readargs[i] = calloc(1, sizeof(readargs));
			all_readargs[i]->fp = fopen(input, "rb");
			all_readargs[i]->list = list;
			all_readargs[i]->thread_num = i;
			child_segment_size += child_read_size;
			if((size - child_segment_size) < child_read_size){
				int excess_bytes = size - child_segment_size;
				child_read_size += excess_bytes;
			}
			all_readargs[i]->actual_offset = 
				child_segment_size - child_read_size;
			all_readargs[i]->child_read_size = child_read_size;
			all_readargs[i]->size = size;
		}				
		for(i = 0; i < proc_number; i++){
			pthread_create(&thread_ids[i], NULL, execute_read, 
				(void*)all_readargs[i]);
		}
		for(i = 0; i < proc_number; i++){
			pthread_join(thread_ids[i], NULL);
		}
		for(i = 0; i < proc_number; i++){
			fclose(all_readargs[i]->fp);
		}
		check(print_words_to_file_and_screen(list, print_number,
			 output) == 1, 
			"print to '%s' failed", output);
		dl_clear_destroy(list);	
		for(i = 0; i < proc_number; i++){
			free(all_readargs[i]);
		}
	} else if (input_check_result == 1) {
		fp = fopen(input, "r");
		check(read_file_word_by_word(fp, list, 
			actual_offset, child_read_size, size) == 1,
			"Could not read file for single parent thread.");
		check(print_words_to_file_and_screen(list, print_number, output) == 1,
			"printing to '%s' failed", output);
		dl_clear_destroy(list);
	}
	return 0;
error:
	if(list != NULL){
		dl_clear_destroy(list);
	}
	return 1;
}
Exemplo n.º 21
0
int
main(int argc, char **argv, char **env)
{
    int exitstatus;
    int i;
    char **fakeargv;
    GV* tmpgv;
    SV* tmpsv;
    int options_count;

    PERL_SYS_INIT3(&argc,&argv,&env);

    if (!PL_do_undump) {
	my_perl = perl_alloc();
	if (!my_perl)
	    exit(1);
	perl_construct( my_perl );
	PL_perl_destruct_level = 0;
    }
#ifdef USE_ITHREADS
    for( i = 0; i < 117; ++i ) {
        av_push( PL_regex_padav, newSViv(0) );
    }
    PL_regex_pad = AvARRAY( PL_regex_padav );
#endif
#ifdef CSH
    if (!PL_cshlen) 
      PL_cshlen = strlen(PL_cshname);
#endif

#ifdef ALLOW_PERL_OPTIONS
#define EXTRA_OPTIONS 3
#else
#define EXTRA_OPTIONS 4
#endif /* ALLOW_PERL_OPTIONS */
    New(666, fakeargv, argc + EXTRA_OPTIONS + 1, char *);

    fakeargv[0] = argv[0];
    fakeargv[1] = "-e";
    fakeargv[2] = "";
    options_count = 3;
    if( 0 ) {
        fakeargv[options_count] = "-T";
        ++options_count;
    }
#ifndef ALLOW_PERL_OPTIONS
    fakeargv[options_count] = "--";
    ++options_count;
#endif /* ALLOW_PERL_OPTIONS */
    for (i = 1; i < argc; i++)
	fakeargv[i + options_count - 1] = argv[i];
    fakeargv[argc + options_count - 1] = 0;

    exitstatus = perl_parse(my_perl, xs_init, argc + options_count - 1,
			    fakeargv, NULL);

    if (exitstatus)
	exit( exitstatus );

    TAINT;
    if ((tmpgv = gv_fetchpv("0",TRUE, SVt_PV))) {/* hello.p */
        tmpsv = GvSV(tmpgv);
        sv_setpv(tmpsv, argv[0]);
        SvSETMAGIC(tmpsv);
    }
    if ((tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))) {/* $^X */
        tmpsv = GvSV(tmpgv);
#ifdef WIN32
        sv_setpv(tmpsv,"perl.exe");
#else
        sv_setpv(tmpsv,"perl");
#endif
        SvSETMAGIC(tmpsv);
    }

    TAINT_NOT;

    /* PL_main_cv = PL_compcv; */
    PL_compcv = 0;

    exitstatus = perl_init();
    if (exitstatus)
	exit( exitstatus );
    dl_init(aTHX);

    exitstatus = perl_run( my_perl );

    perl_destruct( my_perl );
    perl_free( my_perl );

    PERL_SYS_TERM();

    exit( exitstatus );
}
Exemplo n.º 22
0
int main(int argc, char **argv)
{
	int ch, longindex, nr;
	int is_daemon = 1, is_debug = 1;
	pid_t pid;
	struct pollfd *poll_array;

	while ((ch = getopt_long(argc, argv, "fd:vh", long_options, &longindex)) >= 0) {
		switch (ch) {
		case 'f':
			is_daemon = 0;
			break;
		case 'd':
			is_debug = atoi(optarg);
			break;
		case 'v':
			exit(0);
			break;
		case 'h':
			usage(0);
			break;
		default:
			usage(1);
			break;
		}
	}

	init(is_daemon, is_debug);

	if (is_daemon) {
		pid = fork();
		if (pid < 0)
			exit(-1);
		else if (pid)
			exit(0);

		chdir("/");

		close(0);
		open("/dev/null", O_RDWR);
		dup2(0, 1);
		dup2(0, 2);
		setsid();
	}

	nl_fd = nl_open();
	if (nl_fd < 0)
		exit(nl_fd);

	ipc_fd = ipc_open();
	if (ipc_fd < 0)
		exit(ipc_fd);

	dl_init();

	nr = MAX_DL_HANDLES;
	poll_array = poll_init(nr);

	dl_config_load();

	event_loop(nr, poll_array);

	return 0;
}