Ejemplo n.º 1
0
int
save_database()
{
	FILE *out;
	int ret = 0;
	struct db_enumerator e = init_db_enumerator(ENUM_ALL);
	char *datafile_new = strconcat(datafile, ".new", NULL);
	char *datafile_old = strconcat(datafile, "~", NULL);

	if( (out = abook_fopen(datafile_new, "w")) == NULL ) {
		ret = -1;
		goto out;
	}

	if(!list_is_empty())
		/*
		 * Possibly should check if write_database failed.
		 * Currently it returns always zero.
		 */
		write_database(out, e);

	fclose(out);

	if(access(datafile, F_OK) == 0 &&
			(rename(datafile, datafile_old)) == -1)
		ret = -1;
	
	if((rename(datafile_new, datafile)) == -1)
		ret = -1;

out:
	free(datafile_new);
	free(datafile_old);
	return ret;
}
Ejemplo n.º 2
0
void nfosc_stop() {
    int i;
    if (!running) return;
    running = false;
    if( main_thread ) pthread_detach(main_thread);
    main_thread = NULL;
    
    lo_bundle osc_bundle = lo_bundle_new(LO_TT_IMMEDIATE);
    for (i=0;i<buffer_size;i++) {
        
        lo_message del_message = lo_message_new();
        lo_message_add_int32(del_message, tag_buffer[i].device_id);
        lo_message_add_int32(del_message, tag_buffer[i].symbol_id);
        lo_message_add_int32(del_message, tag_buffer[i].type_id);
        lo_message_add_string(del_message, tag_buffer[i].uid_str);
        lo_bundle_add_message(osc_bundle, "/nfosc/del", del_message);
        
        if (verbose) printf("del %d %d %d %s\n",tag_buffer[i].session_id,tag_buffer[i].symbol_id,tag_buffer[i].type_id,tag_buffer[i].uid_str);
    }
    
    lo_timetag frame_time;
    lo_timetag_now (&frame_time);
    
    for (int dev=0;dev<no_devices;dev++) {
        if (pnd[dev]==NULL) continue;
        lo_message frm_message = lo_message_new();
        lo_message_add_int32(frm_message, -1);
        lo_message_add_timetag(frm_message, frame_time);
        lo_message_add_int32(frm_message, 0);                       // sensor dim
        if (device_count>1) sprintf(source_string, "NFOSC:%d",dev);
        lo_message_add_string(frm_message, source_string);          // source name
        lo_bundle_add_message(osc_bundle, "/tuio2/frm", frm_message);
        
        lo_message sid_message = lo_message_new();
        lo_bundle_add_message(osc_bundle, "/tuio2/alv", sid_message);
    }
    
    int ret = lo_send_bundle(target, osc_bundle);
    if(ret == -1) {
        fprintf(stderr, "an OSC error occured: %s\n", lo_address_errstr(target));
        exit(1);
    }
    
    for (int dev=0;dev<no_devices;dev++) {
        if (pnd[dev]!=NULL) {
            printf("closing NFC reader #%d: %s\n",dev,nfc_device_get_name(pnd[dev]));
            nfc_close(pnd[dev]);
        }
    }
    nfc_exit(context);
    write_database();
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
    dict_t db;
    char *infile;

    if (argc < 2 || argc > 3) {
        fprintf(stderr, "%s usage: %s <dbfile> [outputfile]\n\n", argv[0], argv[0]);
        fprintf(stderr, "If [outputfile] is specified, dbfile is rewritten into outputfile after being\nparsed.\n\n");
        fprintf(stderr, "<dbfile> and/or [outputfile] may be given as '-' to use stdin and stdout,\nrespectively.\n");
        return 1;
    }

    tools_init();
    if (!strcmp(argv[1], "-")) {
        infile = "/dev/stdin";
    } else {
        infile = argv[1];
    }
    if (!(db = parse_database(infile))) return 2;
    fprintf(stdout, "Database read okay.\n");
    fflush(stdout);
    if (dict_foreach(db, check_record, 0)) return 3;
    if (!bad) {
        fprintf(stdout, "Database checked okay.\n");
        fflush(stdout);
    }

    if (argc == 3) {
        FILE *f;

        if (!strcmp(argv[2], "-")) {
            f = stdout;
        } else {
            if (!(f = fopen(argv[2], "w+"))) {
                fprintf(stderr, "fopen: %s\n", strerror(errno));
                return 4;
            }
        }

        write_database(f, db);
        fclose(f);
        fprintf(stdout, "Database written okay.\n");
        fflush(stdout);
    }

    return 0;
}
Ejemplo n.º 4
0
void sigint_handler(int signal_number)
{

  int status=0;
  system(CLEAR_SC);



  printf("  -->Waiting for last clients to be attended\n");

  while(wait(&status)!=-1); /*Waits until there is no more childs*/

  /* Save changes in the database to the disk */

  printf("  -->Terminating the server correctly\n");

  printf("  -->Closing the database\n");

  data_person * users = (data_person *) shmat(users_shared_memory,0,0);
  int * newid = (int *) shmat(newid_shared_memory,0,0);

  write_database(users,newid);

  shmdt(users);
  shmdt(newid);

  /*  Deallocate Shared Memory Segment */

  shmctl(users_shared_memory, IPC_RMID, 0);
  shmctl(newid_shared_memory, IPC_RMID, 0);

  /* Deallocate Semaphore */

  binary_semaphore_deallocate(semaphore);

  /* Release the lock. */
  lock.l_type = F_UNLCK;
  fcntl (database, F_SETLKW, &lock);

  close(database);

  printf("  -->Have a nice day\n");

  exit(status);
}