コード例 #1
0
ファイル: connect_fail.c プロジェクト: smalinin/FreeTDS
int
main(int argc, char **argv)
{
	CS_CONTEXT *ctx;
	CS_CONNECTION *conn;
	int ret = 1;

	read_login_info();
	
	if (cs_ctx_alloc(CS_VERSION_100, &ctx) != CS_SUCCEED) {
		fprintf(stderr, "Context Alloc failed!\n");
		return ret;
	}
	if (ct_init(ctx, CS_VERSION_100) != CS_SUCCEED) {
		fprintf(stderr, "Library Init failed!\n");
		return ret;
	}
	if (ct_con_alloc(ctx, &conn) != CS_SUCCEED) {
		fprintf(stderr, "Connect Alloc failed!\n");
		return ret;
	}
	if (ct_con_props(conn, CS_SET, CS_USERNAME, (CS_VOID*) "sa", CS_NULLTERM, NULL) != CS_SUCCEED) {
		fprintf(stderr, "ct_con_props() SET USERNAME failed!\n");
		return ret;
	}
	if (ct_con_props(conn, CS_SET, CS_PASSWORD, (CS_VOID*) "invalid", CS_NULLTERM, NULL) != CS_SUCCEED) {
		fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n");
		return ret;
	}
	if (ct_connect(conn, SERVER, CS_NULLTERM) != CS_FAIL) {
		fprintf(stderr, "Connection succeeded??\n");
		return ret;
	}

	if (ct_cancel(conn, NULL, CS_CANCEL_ALL) != CS_SUCCEED) {
		fprintf(stderr, "ct_cancel() failed!\n");
		return ret;
	}
	if (ct_close(conn, CS_UNUSED) != CS_SUCCEED) {
		fprintf(stderr, "ct_close() failed!\n");
		return ret;
	}
	if (ct_con_drop(conn) != CS_SUCCEED) {
		fprintf(stderr, "ct_con_drop() failed!\n");
		return ret;
	}
	if (ct_exit(ctx, CS_UNUSED) != CS_SUCCEED) {
		fprintf(stderr, "ct_exit() failed!\n");
		return ret;
	}
	if (cs_ctx_drop(ctx) != CS_SUCCEED) {
		fprintf(stderr, "cs_ctx_drop() failed!\n");
		return ret;
	}

	fprintf(stdout, "Test succeeded\n");
	return 0;
}
コード例 #2
0
ファイル: test_imglist.cpp プロジェクト: LjdCN/ct
static void
_test_imglist(const char *vname) {
    struct ct_t **pcts;
    CvRect *objects;
    int n = 0;
    int i;
    const int gray_mode = 0;

    ImgQueue queue;
    IplImage *gray = 0;

    queue.open(vname);
    gray = queue.nextImg(gray_mode);
  
    n = _get_regions(gray, &objects);

    if (n < 1) {
	cvReleaseImage(&gray);
	return ;
    }

    pcts = (struct ct_t **)malloc(n * sizeof(struct ct_t **));
    for (i = 0; i < n; ++i) {
	pcts[i] = ct_new();
	ct_init(pcts[i], gray, &objects[i]);
    }

    cvNamedWindow("tracking", 1);
    while ( gray = queue.nextImg(gray_mode) ) {

#define _draw(f, r)						\
	cvRectangle(f, cvPoint((r).x, (r).y),			\
		    cvPoint((r).x+(r).width, (r).y+(r).height), \
		    cvScalar(255,255,255,0), 2,8,0)
	
	for (i = 0; i < n; ++i) {
	    ct_update(pcts[i], gray, &objects[i]);
	    _draw(gray, objects[i]);
	}
	
	cvShowImage("tracking", gray);
	if (27 == cvWaitKey(1)) break;
    }
#undef _draw
    
    cvDestroyWindow("tracking");
    
    for (i = 0; i < n; ++i) {
	ct_free( &pcts[i] );
    }
    free( pcts );
    free( objects );
}
コード例 #3
0
ファイル: main.c プロジェクト: poifra/ift2245_tp2
int
main (int argc, char *argv[argc + 1])
{

  initThreadRunning ();

  int32_t *theMotherOfAllRequests = malloc (sizeof (int32_t) * 5);
  if (theMotherOfAllRequests == NULL)
    {
      printf ("memory is kill\n");
      exit (-1);
    }

  theMotherOfAllRequests[0] = BEGIN;
  theMotherOfAllRequests[1] = num_resources;
  theMotherOfAllRequests[2] = num_clients;
  theMotherOfAllRequests[3] = -1;
  theMotherOfAllRequests[4] = -1;

  int begin_fd = connectServer ();
  if (send_request (0, 0, begin_fd, theMotherOfAllRequests) == -1)
    {
      printf ("The server said screw you\n");
      exit (-1);
    }

  shutdown (begin_fd, 0);
  close (begin_fd);

  client_thread client_threads[NUM_CLIENTS];

  for (unsigned int i = 0; i < num_clients; i++)
    {
      ct_init (&(client_threads[i]));
    }

  ct_wait_server ();

  // Affiche le journal.
  st_print_results (stdout, true);
  FILE *fp = fopen ("client_log", "w");
  if (fp == NULL)
    {
      fprintf (stderr, "Could not print log");
      return EXIT_FAILURE;
    }
  st_print_results (fp, false);
  fclose (fp);

  return EXIT_SUCCESS;
}
コード例 #4
0
CS_RETCODE SybConnection::init_()
{
    CS_RETCODE retcode;
    CS_INT netio_type = CS_SYNC_IO;

    // Get a context handle to use.
    if ((retcode = cs_ctx_alloc(EX_CTLIB_VERSION, &sContext_)) != CS_SUCCEED) {
        SysLogger::error("init_: cs_ctx_alloc() failed");
        return retcode;
    }

    // Initialize Open Client.
    if ((retcode = ct_init(sContext_, EX_CTLIB_VERSION)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_init() failed");
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    // Install client message handlers.
    if ((retcode = ct_callback(sContext_, NULL, CS_SET, CS_CLIENTMSG_CB,
                               (CS_VOID *)clientmsg_cb)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_callback(CS_CLIENTMSG_CB) failed");
        ct_exit(sContext_, CS_FORCE_EXIT);
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    // Install server message handlers.
    if ((retcode = ct_callback(sContext_, NULL, CS_SET, CS_SERVERMSG_CB,
                               (CS_VOID *)servermsg_cb)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_callback(CS_SERVERMSG_CB) failed");
        ct_exit(sContext_, CS_FORCE_EXIT);
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    // Set the input/output type to synchronous.
    if ((retcode = ct_config(sContext_, CS_SET, CS_NETIO, &netio_type,
                             CS_UNUSED, NULL)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_config() failed");
        ct_exit(sContext_, CS_FORCE_EXIT);
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    return CS_SUCCEED;
}
コード例 #5
0
ファイル: ct_c.c プロジェクト: Chris00/ocaml-freetds
/*** Allocation ***/
CAMLprim value mltds_cs_ctx_create(value unit)
{
    CAMLparam1(unit);
    CS_CONTEXT* context;
    CAMLlocal1(result);

    retval_inspect( "cs_ctx_alloc", cs_ctx_alloc(CS_VERSION_100, &context) );
    retval_inspect( "ct_init", ct_init(context, CS_VERSION_100) );

    result = alloc_custom(&context_operations, sizeof(CS_CONTEXT*), 0, 1);
    context_ptr(result) = context;

    CAMLreturn(result);
}
コード例 #6
0
ファイル: sort.cpp プロジェクト: rurban/checkedthreads
int main(int argc, char** argv)
{
    if(argc>1) {
        N = atoi(argv[1]);
    }
    ct_init(0);
#if 0
    ct_task tasks[] = {
        {a,0},
        {b,0},
        {0,0}
    };
    ct_invoke(tasks);
    ctx_invoke(
        [] { printf("A\n"); },
        [] { printf("B\n"); }
    );
#endif
    int* nums=0;
    const int nsorts = 3;
    const char* descr[nsorts] = {
        "quicksort",
        "mergesort",
        "TBB  sort",
    };

    for(int t=0; t<nsorts; ++t) {
        nums = new int[N];
        int i;
        for(i=0; i<N; ++i) {
            nums[i] = i;
        }
        std::random_shuffle(nums, nums+N);

        unsigned long long usec_start = curr_usec();
        switch(t) {
            case 0: quicksort(nums, nums+N); break;
            case 1: mergeSort(nums, new int[N], N); break;
            case 2: tbb::parallel_sort(nums, nums+N); break;
            default: break;
        };
        unsigned long long usec_finish = curr_usec();
        printf("%s: %f seconds\n", descr[t], (usec_finish - usec_start)/1000000.);

        print_and_check_results(nums);
    }
    
    ct_fini();
}
コード例 #7
0
ファイル: ct_util.c プロジェクト: carriercomm/cyphertite
/* Do a complete ct operation from start to finish */
int
ct_do_operation(struct ct_config *conf,  ct_op_cb *start,
    ct_op_complete_cb *complete, void *args, int flags)
{
	struct ct_global_state	*state;
	int		 	 ret;

	ct_prompt_for_login_password(conf);

	if ((ret = ct_init(&state, conf, flags, NULL)) != 0)
		return (ret);

	ct_add_operation(state, start, complete, args);
	ret = ct_event_dispatch(state->event_state);
	ct_cleanup(state);

	return (ret);
}
コード例 #8
0
ファイル: ZipSS.cpp プロジェクト: geoffsmith82/delphizip
int ZipOp::ZipStreamStream(void)
{
    fwindow_size = 0L;

    if (fGEncrypt)
    {
        fkey = fGPassword;

        if (!fkey || !*(fkey))
        {
            // use global
            if (GetUserPW() != DZ_ERR_GOOD)
                return -1;  // error

            fkey = fuser_key;
        }
    }

    fZipInfile = new ZStream(this, _T("0:<INSTREAM>"), fSS->fSSInput);

    AutoStream inz(&fZipInfile);
    fimax = fSS->Size;
    fcrc = crc32(0L, NULL, 0);
    fisize = 0;
    CB->SetArg1(1);
    CB->UserCB(zacCount);

    // Pass total filesize.
    CB->SetFileSize(fimax);
    CB->UserCB(zacSize);
    ulg f_crc = 0;
    __int64 fsz = 0;
    bool haveCRC = false;

    if (fkey)
    {
        if (!fNoPrecalc)
        {
            if (Verbose < 0)
                Notify(ITRACE,  _T("about to call Precalculate CRC"));

            // +++++ get CRC before we start
			CB->UserXItem(fimax, 13, _T("*PreCalculate"));
            __int64 pos1 = 0;

            if (!fZipInfile->IsFile)
                pos1 = fZipInfile->SetPosition(0, FILE_CURRENT); // get start posn

			f_crc = crc32(0L, NULL, 0);
            unsigned long byts;

            while (true)
            {
                unsigned ToRead = sizeof(fwindow);

                if (fimax > 0 && (fsz + ToRead) > fimax)
                {
                    ToRead = (unsigned)(fimax - fsz);

                    if (!ToRead)
                        break;
                }

                if (!fZipInfile->Read(fwindow, ToRead, &byts) || !byts)
                    break;

				fsz += byts;
				f_crc = crc32(f_crc, (const uch*)fwindow, (int)byts);
				CB->UserXProgress(byts, 13);

                if (Abort_Flag)
					Fatal(DZ_ERM_ABORT, 0);
            }

            fSS->CRC = f_crc;

            haveCRC = true;
            // reposition

            if (fZipInfile->SetPosition(pos1, FILE_BEGIN) != pos1)
            {
                if (Verbose)
					Notify(IVERBOSE, _T("Could not reposition %s [%s]"),
						fZipInfile->fname.c_str(), SysMsg().c_str());

				return  DZError(DZ_ERM_ERROR_SEEK);
			}

            if (fimax > fsz)
                fimax = fsz;
        }

        // ----- get CRC before we start
        // Since we do not yet know the crc here, we pretend that the crc is the
        //   modification time:
//    if (!haveCRC)
//      fSS->CRC = z->tim << 16;
        if (Verbose < 0)
            Notify(ITRACE,  _T("using supplied CRC %lu"), fSS->CRC);
    }

    // connect to output
    fZipOutfile = new ZStream(this, _T("0:<OUTSTREAM>"), fSS->fSSOutput);

    AutoStream outz(&fZipOutfile);

    CB->UserItem(fimax, _T("<INSTREAM>"));

	if (fkey)
		crypthead(fkey, fSS->CRC);  // write

    // Write stored or deflated file to zip file
    fSS->Method &= 0xFF;

    if (fSS->Method != DEFLATE)
        fSS->Method = 0;

    if (flevel < 1)
        fSS->Method = 0;

	int mthd = (int)fSS->Method;

    if (mthd == DEFLATE)
    {
        if (Verbose < 0)
            Notify(ITRACE,  _T("about to call Deflate"));

        bi_init();
        ush att = BINARY;
        ush flg = FLAG_ENCRYPT_BIT;

        // will be changed in deflate()
        ct_init(&att, &mthd);
        lm_init(flevel, &flg);

        // PERFORM THE DEFLATE
        fSS->Size = deflate();

        if (Abort_Flag)
			Fatal(DZ_ERM_ABORT, 0);
    }
    else
    {
        int k;

        if (Verbose)
            Notify(IVERBOSE, _T("Storing %s "), fZipInfile->fname.c_str());

        while ((k = read_buf(fwindow, sizeof(fwindow))) > 0
                && k != EOF)
        {
            if (Abort_Flag)
				Fatal(DZ_ERM_ABORT, 0);

			if (!zfwrite(fwindow,  (extent)k))
				return DZ_ERM_TEMP_FAILED;
        }

    }
    /*  Finished Item */
    CB->UserItem(-1, _T("<INSTREAM>")); // mark end of item
    CB->UserCB(zacEndOfBatch); // done with stream compression

    if (haveCRC)
    {
        if (f_crc != fcrc)
            Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File CRC changed while zipping: %s"),
                   fZipInfile->fname.c_str());

        if (fisize != fsz)
            Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File size changed while zipping: %s"),
                   fZipInfile->fname.c_str());
    }

    fSS->Size = fisize;

    fSS->CRC = fcrc;
    fSS->Method = (DWORD)mthd | (fkey ? 0xff00 : 0);
    return DZ_ERR_GOOD;
}
コード例 #9
0
ファイル: freetds.c プロジェクト: ebryn/freetds4ruby
static VALUE connection_Initialize(VALUE self, VALUE connection_hash) {
	TDS_Connection* conn;
	
	char *servername = NULL;
	char *username = NULL;
	char *password = NULL;
	char *confile = NULL;
	char *charset = NULL;
	VALUE temp;
	VALUE errors;
	CS_RETCODE ret;
	
	Data_Get_Struct(self, TDS_Connection, conn);
	cs_ctx_alloc(CS_VERSION_100, &conn->context);
	ct_init(conn->context, CS_VERSION_100);
	ct_con_alloc(conn->context, &conn->connection);
	
//	conn->context->msg_handler = connection_handle_message;
//	conn->context->err_handler = connection_handle_message;
	
	/* now let's get the connection parameters */
	temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("username")));
	username = value_to_cstr(temp);
	
	temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("password")));
	password = value_to_cstr(temp);

	temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("servername")));
	servername = value_to_cstr(temp);

	temp = rb_hash_aref(connection_hash, ID2SYM(rb_intern("charset")));
	charset = value_to_cstr(temp);

	if(charset==NULL) {
		charset = strdup("ISO-8859-1");
	}
	
	/* validate parameters */
	if (!servername) {
		rb_raise(rb_eArgError, "You must specify a servername");
		return Qnil;
	}
	
	if (!username) {
		rb_raise(rb_eArgError, "No username specified");
		return Qnil;
	}

	if (!password) {
		password = strdup("");
	}	
	
//	printf("*** servername='%s', username='******' password='******'\n", servername, username, password);
	
	ct_con_props(conn->connection, CS_SET, CS_USERNAME, username, CS_NULLTERM, NULL);
	ct_con_props(conn->connection, CS_SET, CS_PASSWORD, password, CS_NULLTERM, NULL);

	/* Try to open a connection */
   	ret = ct_connect(conn->connection, servername, CS_NULLTERM);
	
	/* free up all the memory */
	if (username) {
		free(username);
		username = NULL;
	}
	if (password) {
		free(password);
		password = NULL;
	}
	if (servername) {
		free(servername);
	}
	if(ret!=CS_SUCCEED) {
		rb_raise(rb_eIOError, "Connection failed");
		return Qnil;
	}

	rb_iv_set(self, "@messages", rb_ary_new());
	errors = rb_ary_new();
	rb_iv_set(self, "@errors", errors);
	
	return Qnil;
}
コード例 #10
0
int ct_open_board (ct_board_t *b, int num, port_t port, int irq, int dma)
{
	ct_chan_t *c;
	const unsigned char *fw;
	const cr_dat_tst_t *ft;
	long flen;

	if (num >= NBRD || ! ct_probe_board (port, irq, dma))
		return 0;

	/* init callback pointers */
	for (c=b->chan; c<b->chan+NCHAN; ++c) {
		c->call_on_tx = 0;
		c->call_on_rx = 0;
		c->call_on_msig = 0;
		c->call_on_scc = 0;
		c->call_on_err = 0;
	}

	/* init DDK channel variables */
	for (c=b->chan; c<b->chan+NCHAN; ++c) {
		c->sccrx_empty = c->scctx_empty = 1;
		c->sccrx_b = c->sccrx_e = 0;
		c->scctx_b = c->scctx_e = 0;
		c->e1_first_int = 1;
	}

	/* init board structure */
	ct_init (b, num, port, irq, dma, ctau_fw_data, 
		ctau_fw_len, ctau_fw_tvec, ctau2_fw_data);

	/* determine which firmware should be loaded */
	fw = ctau_fw_data;
	flen = ctau_fw_len;
	ft = ctau_fw_tvec;
	switch (b->type) {
	case B_TAU2:
	case B_TAU2_G703:
	case B_TAU2_E1:
	case B_TAU2_E1D:
		fw = ctau2_fw_data;
		flen = 0;
		ft = 0;
		break;
#ifndef CT_DDK_NO_G703
	case B_TAU_G703:
		fw = ctaug703_fw_data;
		flen = ctaug703_fw_len;
		ft = ctaug703_fw_tvec;
		break;
#endif
#ifndef CT_DDK_NO_E1
	case B_TAU_E1:
		fw = ctaue1_fw_data;
		flen = ctaue1_fw_len;
		ft = ctaue1_fw_tvec;
		break;
#endif
	}
	/* Load firmware and set up board */
	return ct_setup_board (b, fw, flen, ft);
}
コード例 #11
0
ファイル: cs_config.c プロジェクト: Distrotech/freetds
int
main(int argc, char **argv)
{
	int verbose = 1;
	CS_CONTEXT *ctx;

	CS_CHAR string_in[16], string_out[16];
	CS_INT  int_in,        int_out;
	CS_INT ret_len;

	if (verbose) {
		fprintf(stdout, "Trying cs_config with CS_USERDATA\n\n");
	}

	if (cs_ctx_alloc(CS_VERSION_100, &ctx) != CS_SUCCEED) {
		fprintf(stderr, "cs_ctx_alloc() for first context failed\n");
	}
	if (ct_init(ctx, CS_VERSION_100) != CS_SUCCEED) {
		fprintf(stderr, "ct_init() for first context failed\n");
	}

	fprintf(stdout, "Testing CS_SET/GET USERDATA with char array\n");

	strcpy(string_in,"FreeTDS");

	if (cs_config(ctx, CS_SET, CS_USERDATA, (CS_VOID *)string_in,  CS_NULLTERM, NULL)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() set failed\n");
		return 1;
	}
	if (cs_config(ctx, CS_GET, CS_USERDATA, (CS_VOID *)string_out, 16, &ret_len)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() get failed\n");
		return 1;
	}

	if (strcmp(string_in, string_out)) {
		fprintf(stdout, "returned value >%s< not as stored >%s<\n", (char *)string_out, (char *)string_in);
		return 1;
	}
    if (ret_len != (strlen(string_in) + 1)) {
		fprintf(stdout, "returned length >%d< not as expected >%u<\n", ret_len, (unsigned int) (strlen(string_in) + 1));
		return 1;
	}

	fprintf(stdout, "Testing CS_SET/GET USERDATA with char array\n");

	strcpy(string_in,"FreeTDS");

	if (cs_config(ctx, CS_SET, CS_USERDATA, (CS_VOID *)string_in,  CS_NULLTERM, NULL)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() set failed\n");
		return 1;
	}

	strcpy(string_out,"XXXXXXXXXXXXXXX");

	if (cs_config(ctx, CS_GET, CS_USERDATA, (CS_VOID *)string_out, 4, &ret_len)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() get failed\n");
		return 1;
	}

	if (strcmp(string_out, "FreeXXXXXXXXXXX")) {
		fprintf(stdout, "returned value >%s< not as expected >%s<\n", (char *)string_out, "FreeXXXXXXXXXXX");
		return 1;
	}
    if (ret_len != (strlen(string_in) + 1)) {
		fprintf(stdout, "returned length >%d< not as expected >%u<\n", ret_len, (unsigned int) (strlen(string_in) + 1));
		return 1;
	}

	fprintf(stdout, "Testing CS_SET/GET USERDATA with int\n");

	int_in = 255;

	if (cs_config(ctx, CS_SET, CS_USERDATA, (CS_VOID *)&int_in,  sizeof(int), NULL)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() set failed\n");
		return 1;
	}
	if (cs_config(ctx, CS_GET, CS_USERDATA, (CS_VOID *)&int_out, sizeof(int), &ret_len)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() get failed\n");
		return 1;
	}

	if (int_in != int_out) {
		fprintf(stdout, "returned value >%d< not as stored >%d<\n", int_out, int_in);
		return 1;
	}
	if (ret_len != (sizeof(int))) {
		fprintf(stdout, "returned length >%d< not as expected >%u<\n", ret_len, (unsigned int) sizeof(int));
		return 1;
	}

	cs_ctx_drop(ctx);

	return 0;
}
コード例 #12
0
ファイル: common.c プロジェクト: dparnell/freetds
CS_RETCODE
try_ctlogin(CS_CONTEXT ** ctx, CS_CONNECTION ** conn, CS_COMMAND ** cmd, int verbose)
{
CS_RETCODE ret;
char query[30];
TDSCONTEXT *tds_ctx;

	/* read login information from PWD file */
	ret = read_login_info();
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "read_login_info() failed!\n");
		}
		return ret;
	}
	ret = cs_ctx_alloc(CS_VERSION_100, ctx);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Context Alloc failed!\n");
		}
		return ret;
	}

	/* Force default date format, some tests rely on it */
	tds_ctx = (TDSCONTEXT *) (*ctx)->tds_ctx;
	if (tds_ctx && tds_ctx->locale && tds_ctx->locale->date_fmt) {
		free(tds_ctx->locale->date_fmt);
		tds_ctx->locale->date_fmt = tds_strdup("%b %d %Y %I:%M%p");
	}

	ret = ct_init(*ctx, CS_VERSION_100);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Library Init failed!\n");
		}
		return ret;
	}
	ret = ct_con_alloc(*ctx, conn);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Connect Alloc failed!\n");
		}
		return ret;
	}
	ret = ct_con_props(*conn, CS_SET, CS_USERNAME, USER, CS_NULLTERM, NULL);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "ct_con_props() SET USERNAME failed!\n");
		}
		return ret;
	}
	ret = ct_con_props(*conn, CS_SET, CS_PASSWORD, PASSWORD, CS_NULLTERM, NULL);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n");
		}
		return ret;
	}
	ret = ct_connect(*conn, SERVER, CS_NULLTERM);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Connection failed!\n");
		}
		return ret;
	}
	ret = ct_cmd_alloc(*conn, cmd);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Command Alloc failed!\n");
		}
		return ret;
	}

	strcpy(query, "use ");
	strncat(query, DATABASE, 20);

	ret = run_command(*cmd, query);
	if (ret != CS_SUCCEED)
		return ret;

	return CS_SUCCEED;
}
コード例 #13
0
ファイル: common.c プロジェクト: smalinin/FreeTDS
CS_RETCODE
continue_logging_in(CS_CONTEXT ** ctx, CS_CONNECTION ** conn, CS_COMMAND ** cmd, int verbose)
{
	CS_RETCODE ret;
	char query[30];
#ifdef TDS_STATIC_CAST
	TDSCONTEXT *tds_ctx;
#endif

	ret = cs_ctx_alloc(CS_VERSION_100, ctx);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Context Alloc failed!\n");
		}
		return ret;
	}

#ifdef TDS_STATIC_CAST
	/* Force default date format, some tests rely on it */
	tds_ctx = (TDSCONTEXT *) (*ctx)->tds_ctx;
	if (tds_ctx && tds_ctx->locale && tds_ctx->locale->date_fmt) {
		free(tds_ctx->locale->date_fmt);
		tds_ctx->locale->date_fmt = strdup("%b %d %Y %I:%M%p");
	}
#endif

	ret = ct_init(*ctx, CS_VERSION_100);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Library Init failed!\n");
		}
		return ret;
	}
	if ((ret = ct_callback(*ctx, NULL, CS_SET, CS_CLIENTMSG_CB, 
			       (CS_VOID*) clientmsg_cb)) != CS_SUCCEED) {
		fprintf(stderr, "ct_callback() failed\n");
		return ret;
	}
	if ((ret = ct_callback(*ctx, NULL, CS_SET, CS_SERVERMSG_CB, servermsg_cb)) != CS_SUCCEED) {
		fprintf(stderr, "ct_callback() failed\n");
		return ret;
	}
	ret = ct_con_alloc(*ctx, conn);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Connect Alloc failed!\n");
		}
		return ret;
	}
	ret = ct_con_props(*conn, CS_SET, CS_USERNAME, USER, CS_NULLTERM, NULL);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "ct_con_props() SET USERNAME failed!\n");
		}
		return ret;
	}
	ret = ct_con_props(*conn, CS_SET, CS_PASSWORD, PASSWORD, CS_NULLTERM, NULL);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n");
		}
		return ret;
	}
	
	printf("connecting as %s to %s.%s\n", USER, SERVER, DATABASE);
	
	ret = ct_connect(*conn, SERVER, CS_NULLTERM);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Connection failed!\n");
		}
		return ret;
	}
	ret = ct_cmd_alloc(*conn, cmd);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Command Alloc failed!\n");
		}
		return ret;
	}

	strcpy(query, "use ");
	strncat(query, DATABASE, 20);

	ret = run_command(*cmd, query);
	if (ret != CS_SUCCEED)
		return ret;

	return CS_SUCCEED;
}
コード例 #14
0
ファイル: ct_main.c プロジェクト: finid/cyphertite
int
ct_main(int argc, char **argv)
{
	struct ct_extract_args		 cea;
	struct ct_archive_args		 caa;
	struct ct_ctfileop_args 	 cca;
	struct ct_ctfile_list_args	 ccla;
	struct ct_ctfile_delete_args	 ccda;
	struct ct_global_state		*state = NULL;
	struct ct_config		*conf;
	char				*ct_tdir = NULL;
	char				*ct_basisbackup = NULL;
	char				*ctfile = NULL;
	char				*ct_includefile = NULL;
	char				*ct_excludefile = NULL;
	char				*configfile = NULL, *config_file = NULL;
	char				*basisfile = NULL;
	char				*debugstring = NULL;
	char				**excludelist = NULL;
	char				**includelist = NULL;
	uint64_t			 debug_mask = 0;
	uint32_t			 cflags = CLOG_F_ENABLE | CLOG_F_STDERR;
	int				 ct_metadata = 0;
	int				 ct_match_mode = CT_MATCH_GLOB;
	int				 c;
	int				 ret = 0;
	int				 level0 = 0;
	int				 freeincludes = 0;
	int				 no_cross_mounts = 0;
	int				 strip_slash = 1;
	int				 follow_root_symlink = 0;
	int				 follow_symlinks = 0;
	int				 attr = 0;
	int				 verbose_ratios = 0;
	int				 ct_flags = 0;

	while ((c = getopt(argc, argv,
	    "AB:C:D:E:F:HI:PRVXacdef:hmprtvx0")) != -1) {
		switch (c) {
		case 'A':
			/* noop, deprecated */
			break;
		case 'B':
			basisfile = optarg;
			break;
		case 'C':
			ct_tdir = optarg;
			break;
		case 'D':
			if (debugstring != NULL)
				CFATALX("only one -D argument is valid");
			debugstring = optarg;
			break;
		case 'E':
			ct_excludefile = optarg;
			break;
		case 'F':
			configfile = optarg;
			break;
		case 'H':
			follow_root_symlink = 1;
			break;
		case 'I':
			ct_includefile = optarg;
			break;
		case 'P':
			strip_slash = 0;
			break;
		case 'R':
			verbose_ratios = 1;
			break;
		case 'V':
			show_version();
			exit(0);
			break;
		case 'X':
			no_cross_mounts = 1;
			break;
		case 'a':
			/* noop, deprecated */
			break;
		case 'c':
			if (ct_action)
				CFATALX("cannot mix operations, -c -e -t -x");
			ct_action = CT_A_ARCHIVE;
			break;
		case 'e':
			if (ct_action)
				CFATALX("cannot mix operations, -c -e -t -x");
			ct_action = CT_A_ERASE;
			break;
		case 'f': /* metadata file */
			ctfile = optarg;
			break;
		case 'h':
			follow_symlinks = 1;
			break;
		case 'm': /* metadata processing - XXX temporary? */
			ct_metadata = 1;
			break;
		case 'r':
			ct_match_mode = CT_MATCH_REGEX;
			break;
		case 'p':
			attr = 1;
			break;
		case 't':
			if (ct_action)
				CFATALX("cannot mix operations, -c -e -t -x");
			ct_action = CT_A_LIST;
			break;
		case 'v':
			ct_verbose++;
			break;
		case 'x':
			if (ct_action)
				CFATALX("cannot mix operations, -c -e -t -x");
			ct_action = CT_A_EXTRACT;
			break;
		case '0':
			level0 = 1;
			break;
		default:
			ct_usage();
			/* NOTREACHED */
		}
	}

	argc -= optind;
	argv += optind;

	if (debugstring) {
		cflags |= CLOG_F_DBGENABLE | CLOG_F_FILE | CLOG_F_FUNC |
		    CLOG_F_LINE | CLOG_F_DTIME;
		exude_enable(CT_LOG_EXUDE);
#if CT_ENABLE_THREADS
		exude_enable_threads();
#endif
		debug_mask |= ct_get_debugmask(debugstring);
	}

	/* please don't delete this line AGAIN! --mp */
	if (clog_set_flags(cflags))
		errx(1, "illegal clog flags");
	clog_set_mask(debug_mask);

	/* We can allocate these now that we've decided if we need exude */
	if (configfile)
		config_file = e_strdup(configfile);
	if (basisfile)
		ct_basisbackup = e_strdup(basisfile);

	if (ct_includefile != NULL) {
		int nentries;

		if ((ct_action == CT_A_LIST || ct_action == CT_A_EXTRACT) &&
		    argc != 0)
			CFATALX("-I is invalid when a pattern is "
			    "provided on the command line");
		includelist = ct_matchlist_fromfile(ct_includefile,
		    &nentries);
		if (nentries == -1)
			CFATAL("can't get includelist from %s",
			    ct_includefile);

		freeincludes = 1;
	} else if ((ct_action == CT_A_LIST || ct_action == CT_A_EXTRACT)) {
		includelist = argv;
	}
	if (ct_excludefile != NULL) {
		int	nentries;
		excludelist = ct_matchlist_fromfile(ct_excludefile, &nentries);
		if (nentries == -1)
			CFATAL("can't get excludelsit from %s", ct_excludefile);
	}


	if ((ret = ct_load_config(&conf, &config_file)) != 0) {
		CFATALX("%s", ct_strerror(ret));
	}

	if (!(ct_metadata && (ct_action == CT_A_LIST ||
	    ct_action == CT_A_ERASE))) {
		if (ctfile == NULL) {
			CWARNX("ctfile is required");
			ct_usage();
		}

		if (conf->ct_ctfile_mode == CT_MDMODE_REMOTE &&
		    ctfile_verify_name(ctfile))
			CFATALX("invalid ctfile: %s", ctfile);
	}

	/*
	 * !metadata extract with no args extracts everything.
	 * and all lists show everything if not filtered
	 */
	if (((ct_metadata == 0 && ct_action == CT_A_EXTRACT) ||
	    ct_action == CT_A_LIST) && argc == 0)
		ct_match_mode = CT_MATCH_EVERYTHING;

	if (level0)
		conf->ct_auto_incremental = 0; /* force incremental off */

	if (conf->ct_ctfile_mode == CT_MDMODE_REMOTE && ct_metadata == 0 &&
	    ct_basisbackup != NULL)
		CFATALX("incremental basis in remote mode");

	/* Don't bother starting a connection if just listing local files. */
	if (ct_action == CT_A_LIST &&
	    conf->ct_ctfile_mode == CT_MDMODE_LOCAL &&
	    ct_metadata == 0 ) {
		ret = ct_list(ctfile, includelist, excludelist,
		    ct_match_mode, NULL, strip_slash, ct_verbose);
		goto out;
	}

	ct_prompt_for_login_password(conf);

	if (ct_action == CT_A_EXTRACT ||
	    ct_action == CT_A_ARCHIVE || (ct_action == CT_A_LIST &&
	    conf->ct_ctfile_mode == CT_MDMODE_REMOTE && ct_metadata == 0) ||
	    ct_action == CT_A_ERASE)
		ct_flags |= CT_NEED_SECRETS;
	if (ct_action == CT_A_ARCHIVE)
		ct_flags |= CT_NEED_DB;


	if ((ret = ct_init(&state, conf, ct_flags, ct_info_sig)) != 0)
		CFATALX("failed to initialise cyphertite: %s",
		    ct_strerror(ret));

#if defined(CT_EXT_INIT)
	CT_EXT_INIT(state);
#endif

	if (conf->ct_crypto_passphrase != NULL &&
	    conf->ct_secrets_upload != 0) {
		ct_add_operation(state, ctfile_list_start,
		    ct_check_secrets_extract, conf->ct_crypto_secrets);
	}

	if (ct_action == CT_A_EXTRACT)
		ct_set_log_fns(state, &ct_verbose, ct_print_ctfile_info,
		    ct_print_file_start, ct_print_file_end,
		    ct_print_traverse_start, ct_print_traverse_end);
	else if (ct_action == CT_A_ARCHIVE)
		ct_set_log_fns(state, &ct_verbose, ct_print_ctfile_info,
		    ct_pr_fmt_file, ct_pr_fmt_file_end,
		    ct_print_traverse_start, ct_print_traverse_end);

	if (conf->ct_ctfile_mode == CT_MDMODE_REMOTE && ct_metadata == 0) {
		switch (ct_action) {
		case CT_A_EXTRACT:
		case CT_A_LIST:
			cea.cea_local_ctfile = NULL; /* to be found */
			cea.cea_filelist = includelist;
			cea.cea_excllist = excludelist;
			cea.cea_matchmode = ct_match_mode;
			cea.cea_ctfile_basedir = conf->ct_ctfile_cachedir;
			cea.cea_tdir = ct_tdir;
			cea.cea_strip_slash = strip_slash;
			cea.cea_attr = attr;
			cea.cea_follow_symlinks = follow_symlinks;
			cea.cea_log_state = &ct_verbose;
			cea.cea_log_chown_failed =
			    ct_print_extract_chown_failed;
			ctfile_find_for_operation(state, ctfile,
			    ((ct_action == CT_A_EXTRACT)  ?
			    ctfile_nextop_extract : ctfile_nextop_list),
			    &cea, 1, 0);
			break;
		case CT_A_ARCHIVE:
			ct_normalize_filelist(argv);
			caa.caa_filelist = argv;
			caa.caa_excllist = excludelist;
			caa.caa_matchmode = ct_match_mode;
			caa.caa_includelist = includelist;
			caa.caa_tdir = ct_tdir;
			caa.caa_tag = ctfile;
			caa.caa_ctfile_basedir = conf->ct_ctfile_cachedir;
			/* we want to encrypt as long as we have keys */
			caa.caa_no_cross_mounts = no_cross_mounts;
			caa.caa_strip_slash = strip_slash;
			caa.caa_follow_root_symlink = follow_root_symlink;
			caa.caa_follow_symlinks = follow_symlinks;
			caa.caa_max_incrementals = conf->ct_max_incrementals;
			if (conf->ct_auto_incremental)
				/*
				 * Need to work out basis filename and
				 * download it if necessary
				 */
				ctfile_find_for_operation(state, ctfile,
				    ctfile_nextop_archive, &caa, 0, 1);
			else   {
				/* No basis, just start the op */
				ctfile_nextop_archive(state, NULL, &caa);
			}
			break;
		default:
			CWARNX("invalid action");
			ct_usage();
			/* NOTREACHED */
			break;
		}
	} else if (ct_metadata != 0) {
		if (ct_action == CT_A_ARCHIVE || ct_action == CT_A_EXTRACT) {
			cca.cca_localname = ctfile;
			cca.cca_remotename = NULL;
			cca.cca_tdir = ct_tdir;
			cca.cca_cleartext = 0;
			cca.cca_ctfile = 1;
			/* only matters for archive */
			ct_add_operation(state,
			    ((ct_action == CT_A_ARCHIVE) ?
			    ctfile_archive : ctfile_extract),
			    ctfile_op_cleanup, &cca);
		} else if (ct_action == CT_A_ERASE) {
			if (ctfile != NULL)
				CFATALX("-f is not permitted with -me operation");
			if (argc == 0)
				CFATALX("no files specified");
			ccda.ccda_pattern = argv;
			ccda.ccda_matchmode = ct_match_mode;
			ccda.ccda_callback = ct_print_delete;
			ct_add_operation(state, ctfile_list_start,
			    ctfile_process_delete, &ccda);
		} else if (ct_action == CT_A_LIST) {
			ccla.ccla_search = includelist;
			ccla.ccla_exclude = excludelist;
			ccla.ccla_matchmode = ct_match_mode;
			ct_add_operation(state, ctfile_list_start,
			    ctfile_list_print, &ccla);
		} else {
			CWARNX("must specify action");
			ct_usage();
			/* NOTREACHED */
		}
	} else {
		/* list is handled above */
		if (ct_action == CT_A_ARCHIVE) {
			caa.caa_local_ctfile = ctfile;
			ct_normalize_filelist(argv);
			caa.caa_filelist = argv;
			caa.caa_excllist = excludelist;
			caa.caa_matchmode = ct_match_mode;
			caa.caa_includelist = includelist;
			caa.caa_tdir = ct_tdir;
			caa.caa_tag = ctfile;
			caa.caa_ctfile_basedir = NULL;
			/* we want to encrypt as long as we have keys */
			caa.caa_no_cross_mounts = no_cross_mounts;
			caa.caa_strip_slash = strip_slash;
			caa.caa_follow_root_symlink = follow_root_symlink;
			caa.caa_follow_symlinks = follow_symlinks;
			caa.caa_max_incrementals = 0; /* unlimited */
			caa.caa_basis = ct_basisbackup;

			ct_add_operation(state, ct_archive, NULL, &caa);
		} else if (ct_action == CT_A_EXTRACT) {
			cea.cea_local_ctfile = ctfile;
			cea.cea_filelist = includelist;
			cea.cea_excllist = excludelist;
			cea.cea_matchmode = ct_match_mode;
			cea.cea_ctfile_basedir = NULL;
			cea.cea_tdir = ct_tdir;
			cea.cea_strip_slash = strip_slash;
			cea.cea_attr = attr;
			cea.cea_follow_symlinks = follow_symlinks;
			cea.cea_log_state = &ct_verbose;
			cea.cea_log_chown_failed =
			    ct_print_extract_chown_failed;
			ct_add_operation(state, ct_extract, NULL, &cea);
		} else {
			CWARNX("must specify action");
			ct_usage();
			/* NOTREACHED */
		}
	}

	ct_wakeup_file(state->event_state);

	if ((ret = ct_run_eventloop(state)) != 0) {
		if (state->ct_errmsg[0] != '\0')
			CWARNX("%s: %s", state->ct_errmsg,
			    ct_strerror(ret));
		else
			CWARNX("%s", ct_strerror(ret));
		return (ret);
	}

	if (verbose_ratios)
		ct_dump_stats(state, stdout);
	ct_cleanup_login_cache();
	ct_cleanup(state);
out:
	if (includelist && freeincludes == 1)
		ct_matchlist_free(includelist);
	if (excludelist)
		ct_matchlist_free(excludelist);
	if (conf->ct_ctfile_mode == CT_MDMODE_REMOTE && ct_metadata == 0)
		ctfile_trim_cache(conf->ct_ctfile_cachedir,
		    conf->ct_ctfile_max_cachesize);

	ct_unload_config(config_file, conf);
#if CT_CHECK_MEMORY
	e_check_memory();
#endif
	exude_cleanup();

	return (ret);
}
コード例 #15
0
ファイル: cs_diag.c プロジェクト: Distrotech/freetds
/*
 * ct_send SQL |select name = @@servername|
 * ct_bind variable
 * ct_fetch and print results
 */
int
main(int argc, char **argv)
{
	int verbose = 1;
	CS_CONTEXT *ctx;
	CS_RETCODE ret;
	CS_DATAFMT srcfmt;
	CS_INT src = 32768;
	CS_DATAFMT dstfmt;
	CS_SMALLINT dst;
	CS_DATETIME dst_date;

	int i;

	CS_INT num_msgs;
	CS_CLIENTMSG client_message;

	fprintf(stdout, "%s: Testing context callbacks\n", __FILE__);

	srcfmt.datatype = CS_INT_TYPE;
	srcfmt.maxlength = sizeof(CS_INT);
	srcfmt.locale = NULL;

	dstfmt.datatype = CS_SMALLINT_TYPE;
	dstfmt.maxlength = sizeof(CS_SMALLINT);
	dstfmt.locale = NULL;

	if (verbose) {
		fprintf(stdout, "Trying clientmsg_cb with context\n");
	}
	if (cs_ctx_alloc(CS_VERSION_100, &ctx) != CS_SUCCEED) {
		fprintf(stderr, "cs_ctx_alloc() failed\n");
	}
	if (ct_init(ctx, CS_VERSION_100) != CS_SUCCEED) {
		fprintf(stderr, "ct_init() failed\n");
	}

	if (cs_diag(ctx, CS_INIT, CS_UNUSED, CS_UNUSED, NULL) != CS_SUCCEED) {
		fprintf(stderr, "cs_diag(CS_INIT) failed\n");
		return 1;
	}

	if (cs_convert(ctx, &srcfmt, &src, &dstfmt, &dst, NULL) == CS_SUCCEED) {
		fprintf(stderr, "cs_convert() succeeded when failure was expected\n");
		return 1;
	}

	dstfmt.datatype = CS_DATETIME_TYPE;
	dstfmt.maxlength = sizeof(CS_DATETIME);
	dstfmt.locale = NULL;

	if (cs_convert(ctx, &srcfmt, &src, &dstfmt, &dst_date, NULL) == CS_SUCCEED) {
		fprintf(stderr, "cs_convert() succeeded when failure was expected\n");
		return 1;
	}

	if (cs_diag(ctx, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
		fprintf(stderr, "cs_diag(CS_STATUS) failed\n");
		return 1;
	}

	for (i = 0; i < num_msgs; i++ ) {

		if (cs_diag(ctx, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message) != CS_SUCCEED) {
			fprintf(stderr, "cs_diag(CS_GET) failed\n");
			return 1;
		}
	
	    cslibmsg_cb(NULL, &client_message);
	
	}

	if ((ret = cs_diag(ctx, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message)) != CS_NOMSG) {
		fprintf(stderr, "cs_diag(CS_GET) did not fail with CS_NOMSG\n");
		return 1;
	}

	if (cs_diag(ctx, CS_CLEAR, CS_CLIENTMSG_TYPE, CS_UNUSED, NULL) != CS_SUCCEED) {
		fprintf(stderr, "cs_diag(CS_CLEAR) failed\n");
		return 1;
	}

	if (cs_diag(ctx, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
		fprintf(stderr, "cs_diag(CS_STATUS) failed\n");
		return 1;
	}
	if (num_msgs != 0) {
		fprintf(stderr, "cs_diag(CS_CLEAR) failed there are still %d messages on queue\n", num_msgs);
		return 1;
	}

	if (ct_exit(ctx, CS_UNUSED) != CS_SUCCEED) {
		fprintf(stderr, "ct_exit() failed\n");
	}
	if (cs_ctx_drop(ctx) != CS_SUCCEED) {
		fprintf(stderr, "cx_ctx_drop() failed\n");
	}

	return 0;
}