Пример #1
0
/* entry point of the program */
int main(int argc, char *argv[]) {
	struct Individual *individuals = NULL;
    struct Family *families = NULL;

	/* two ints to hold the sizes of the two sets of parallel arrays */
    int indi_count = 0;
    int fam_count = 0;

	/* and a FILE */
    FILE *fp = NULL;

    /* there should only be 2 arguments */
    if(argc != 2) {
        printf("Usage: family.out <ged_filename>\n");
        return 1;
    }

	fp = count_records(argv[1], &indi_count, &fam_count);
    initialize(indi_count, fam_count, &individuals, &families);

	read_file(fp, individuals, families);


	/* sort individuals and families qsort functions */
    qsort(individuals, indi_count, sizeof(struct Individual), individual_cmp);
	qsort(families, fam_count, sizeof(struct Family), family_cmp);

    find_children(indi_count, fam_count, individuals, families);
    deallocate(indi_count, fam_count, individuals, families);

	return 0;
}
Пример #2
0
/* set up the inital statistics for rd.
   current will point to start of currently displayed list. */
void recdata_initialise_stats (struct rec_data * rd) {
	if (rd->current == NULL) {
		rd->index = 0;
		rd->total = 0;
	} else {
		rd->index = 1;
		rd->total = count_records (rd->current);
	}
}
Пример #3
0
int main(int argc, char **argv)
{
  Individual *individuals;
  Family *families;
  int individual_count, family_count;
  FILE *fp;
  
  if(argc != 2)
  {
    printf("Usage: family.out <ged_filename>\n");
    return 1;
  } // if the number of arguments is wrong
  
  fp = count_records(argv[1], &individual_count, &family_count);
  initialize(&individuals, &families, individual_count, family_count);
  read_file(individuals, families, fp);
  qsort(individuals, 4, sizeof(Individual), individual_cmp);
  qsort(families, 4, sizeof(Family), family_cmp);
  find_children(individuals, families, individual_count, family_count);
  deallocate(individuals, families, individual_count, family_count);
  return 0;
} // main()
/*
 * A function that performs a series of writes to a
 * Berkeley DB database. The information written
 * to the database is largely nonsensical, but the
 * mechanism of transactional commit/abort and
 * deadlock detection is illustrated here.
 */
void *
writer_thread(void *args)
{
    static char *key_strings[] = {
	"key 1", "key 2", "key 3", "key 4", "key 5",
	"key 6", "key 7", "key 8", "key 9", "key 10"
    };
    DB *dbp;
    DB_ENV *envp;
    DBT key, value;
    DB_TXN *txn;
    int i, j, payload, ret, thread_num;
    int retry_count, max_retries = 20;   /* Max retry on a deadlock */

    dbp = (DB *)args;
    envp = dbp->get_env(dbp);

    /* Get the thread number */
    (void)mutex_lock(&thread_num_lock);
    global_thread_num++;
    thread_num = global_thread_num;
    (void)mutex_unlock(&thread_num_lock);

    /* Initialize the random number generator */
    srand(thread_num);

    /* Write 50 times and then quit */
    for (i = 0; i < 50; i++) {
	retry_count = 0; /* Used for deadlock retries */

	/*
	 * Some think it is bad form to loop with a goto statement, but
	 * we do it anyway because it is the simplest and clearest way
	 * to achieve our abort/retry operation.
	 */
retry:
	/* Begin our transaction. We group multiple writes in
	 * this thread under a single transaction so as to
	 * (1) show that you can atomically perform multiple writes
	 * at a time, and (2) to increase the chances of a
	 * deadlock occurring so that we can observe our
	 * deadlock detection at work.
	 *
	 * Normally we would want to avoid the potential for deadlocks,
	 * so for this workload the correct thing would be to perform our
	 * puts with autocommit. But that would excessively simplify our
	 * example, so we do the "wrong" thing here instead.
	 */
	ret = envp->txn_begin(envp, NULL, &txn, 0);
	if (ret != 0) {
	    envp->err(envp, ret, "txn_begin failed");
	    return ((void *)EXIT_FAILURE);
	}
	for (j = 0; j < 10; j++) {
	    /* Set up our key and values DBTs */
	    memset(&key, 0, sizeof(DBT));
	    key.data = key_strings[j];
	    key.size = (u_int32_t)strlen(key_strings[j]) + 1;

	    memset(&value, 0, sizeof(DBT));
	    payload = rand() + i;
	    value.data = &payload;
	    value.size = sizeof(int);

	    /* Perform the database put. */
	    switch (ret = dbp->put(dbp, txn, &key, &value, 0)) {
		case 0:
		    break;

		/*
		 * Here's where we perform deadlock detection. If
		 * DB_LOCK_DEADLOCK is returned by the put operation,
		 * then this thread has been chosen to break a deadlock.
		 * It must abort its operation, and optionally retry the
		 * put.
		 */
		case DB_LOCK_DEADLOCK:
		    /*
		     * First thing that we MUST do is abort the
		     * transaction.
		     */
		    (void)txn->abort(txn);
		    /*
		     * Now we decide if we want to retry the operation.
		     * If we have retried less than max_retries,
		     * increment the retry count and goto retry.
		     */
		    if (retry_count < max_retries) {
			printf("Writer %i: Got DB_LOCK_DEADLOCK.\n",
			    thread_num);
			printf("Writer %i: Retrying write operation.\n",
			    thread_num);
			retry_count++;
			goto retry;
		    }
		    /*
		     * Otherwise, just give up.
		     */
		    printf("Writer %i: ", thread_num);
		    printf("Got DB_LOCK_DEADLOCK and out of retries.\n");
		    printf("Writer %i: Giving up.\n", thread_num);
		    return ((void *)EXIT_FAILURE);
		/*
		 * If a generic error occurs, we simply abort the
		 * transaction and exit the thread completely.
		 */
		default:
		    envp->err(envp, ret, "db put failed");
		    ret = txn->abort(txn);
		    if (ret != 0)
			envp->err(envp, ret, "txn abort failed");
		    return ((void *)EXIT_FAILURE);
	     } /** End case statement **/

	}   /** End for loop **/

	/*
	 * print the number of records found in the database.
	 * See count_records() for usage information.
	 */
	printf("Thread %i. Record count: %i\n", thread_num,
	    count_records(dbp, txn));

	/*
	 * If all goes well, we can commit the transaction and
	 * exit the thread.
	 */
	ret = txn->commit(txn, 0);
	if (ret != 0) {
	    envp->err(envp, ret, "txn commit failed");
	    return ((void *)EXIT_FAILURE);
	}
    }
    return ((void *)EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
  char                     opt;
  char                     user[16];
  char                     dir[256];
  char                     string1[256];
  char                     string2[256];
  char                     new_user[16];
  char                     new_tty[16];
  char                     new_host[256];
  char                     ll_h[256];
  char                     ll_i[256];
  char                     ll_t[256];
  long                     new_login = 0;
  long                     new_logout = 0;
  int                      replace = 0;
  int                      add = 0;
  int                      record = (-1);
  int                      total1 = 0;
  int                      total2 = 0;
  int                      debug = 0;
  int                      user_check = 0;
  int                      dir_check = 0;
  int                      new_check = 0;
  int                      open_check1 = 0;
#ifdef SUN
  int                      open_check2 = 0;
#endif
  int                      flag = 0;
  bzero(user, sizeof(user));
  bzero(dir, sizeof(dir));
  bzero(string1, sizeof(string1));
  bzero(string2, sizeof(string2));
  bzero(new_user, sizeof(new_user));
  bzero(new_tty, sizeof(new_tty));
  bzero(new_host, sizeof(new_host));
  bzero(ll_h, sizeof(ll_h));
  bzero(ll_i, sizeof(ll_i));
  bzero(ll_t, sizeof(ll_t));
#ifdef SUN
  strcpy(dir, "/var/adm/");
#endif
#ifndef SUN
  strcpy(dir, "/var/log/");
#endif
  while((opt = getopt(argc, argv, "u:n:D:a:b:U:T:H:I:O:RAd")) != -1)
  {
    switch (opt)
    {
      case 'u':
      {
	strcpy(user, optarg);
	user_check++;
	break;
      }
      case 'n':
      {
	record = atoi(optarg);
	break;
      }
      case 'D':
      {
	bzero(dir, sizeof(dir));
	strcpy(dir, optarg);
	dir_check++;
	break;
      }
      case 'a':
      {
	strcpy(string1, optarg);
	flag++;
	break;
      }
      case 'b':
      {
	strcpy(string2, optarg);
	flag++;
	break;
      }
      case 'U':
      {
	strcpy(new_user, optarg);
	new_check++;
	break;
      }
      case 'T':
      {
	strcpy(new_tty, optarg);
	new_check++;
	break;
      }
      case 'H':
      {
	strcpy(new_host, optarg);
	new_check++;
	break;
      }
      case 'I':
      {
	new_login = atol(optarg);
	new_check++;
	break;
      }
      case 'O':
      {
	new_logout = atol(optarg);
	new_check++;
	break;
      }
      case 'R':
      {
	replace++;
	break;
      }
      case 'A':
      {
	add++;
	break;
      }
      case 'd':
      {
	debug++;
	break;
      }
    }
  }
  if((user_check == 0 && add == 0 && dir_check == 0 && flag == 0) || (replace == 1 && add == 1) || (add == 1 && new_check != 5) || (replace == 1 && user_check == 0) || (replace == 1 && new_check == 0)
     || (replace == 1 && record == 0) || (dir_check == 1 && flag == 0))
  {
    usage(argv[0]);
    exit(0);
  }
  printf("\n******************************\n");
  printf("* MIG Logcleaner v2.0 by no1 *\n");
  printf("******************************\n\n");
  if(record == (-1))
  {
    record = 1;
  }
  if(user[0] != 0)
    total1 = count_records(user, 1, debug);
  if(total1 == (-1))
  {
    if(debug == 1)
      fprintf(stderr, "Error opening %s file to count records\n", WTMP);
    open_check1++;
  }
  if(open_check1 != 1 && replace == 0 && add == 0 && user_check != 0 && (record <= total1))
  {
    utmp_clean(user, record, total1, debug);
  }
#ifdef SUN
  if(user[0] != 0)
    total2 = count_records(user, 2, debug);
  if(total2 == (-1))
  {
    if(debug == 1)
      fprintf(stderr, "Error opening %s file to count records\n", WTMPX);
    open_check2++;
  }
  if(open_check2 != 1 && replace == 0 && add == 0 && user_check != 0 && (record <= total2))
  {
    utmpx_clean(user, record, total2, debug);
  }
#endif
  if(replace == 1 && (record <= total1)
#ifdef SUN
     && (record <= total2)
#endif
    )
  {
    if(l == 1)
    {
      strcpy(ll_h, lastlog_hostname);
      strcpy(ll_i, lastlog_time);
      strcpy(ll_t, lastlog_tty);
    }
    replase(user, record, total1, total2, new_user, new_host, new_login, new_logout, debug);
  }
  if(add == 1)
  {
    if(user[0] != 0 && (record > total1)
#ifdef SUN
       && (record > total2)
#endif
      )
    {
      usage(argv[0]);
      exit(0);
    }
    addd(user, record, total1, total2, new_user, new_tty, new_host, new_login, new_logout, debug);
  }
  if((record == 1 || record == 0) && add == 0)
  {
    if(l == 1)
    {
      strcpy(ll_h, lastlog_hostname);
      strcpy(ll_i, lastlog_time);
      strcpy(ll_t, lastlog_tty);
    }
    lastlog_clean(user, debug, ll_h, ll_t, atol(ll_i), record);
  }
  if(flag != 0)
  {
    txt_clean(dir, string1, string2, debug);
  }
  printf("\n");
  return (0);
}