Beispiel #1
0
int main(int argc, char** argv)
{
   double principle=0;
   double amount;
   int duration=0;
   double interest_rate=0;
   int capitalize=1;
   if ( argc < 6 ) {
      helptext(argv[0]);
      exit(-1);
   }
   int i=1;
   while (i < argc ) {
      if ( strcmp(argv[i], "-a")==0 ) {
         i++;
         principle=atof(argv[i]);
      }
      if ( strcmp(argv[i], "-i")==0 ) {
         i++;
         interest_rate=atof(argv[i]);
      }
      if ( strcmp(argv[i], "-d")==0 ) {
         i++;
         duration=atoi(argv[i]);
      }
      if ( strcmp(argv[i], "-c")==0 ) {
         i++;
         capitalize=atoi(argv[i]);
      }
      i++;
   }
   if ( principle == 0 || interest_rate == 0 || duration == 0 ) {
      helptext(argv[0]);
      exit(-1);
   }
   printf("Principle=£%.2f\nInterest=%5.2f%%\nDuration=%d years\n", principle, interest_rate, duration);
   if ( capitalize > 1 ) printf("Capitialized %d times a year\n", capitalize);
   amount=principle * ( pow( 1 + ( (interest_rate/100) / capitalize ), (capitalize * duration) ) );
   printf("After %d years your investment will be worth £%.2f\n", duration, amount);
   exit(0);
}
Beispiel #2
0
/*
 * Read args - -t/-u necessary, -p xxxx, if not default port 10001
 * -c destination 
 */
void readargs(int argc, char *argv[], char *proto, char *mode, unsigned int *address, unsigned short *port, char * default_port) {
  char c; //for getopt
  int protocount = 0, modecount = 0; //count how often a proto occurs, must be 1
  char *caddr = NULL; //character array for address
  char *cport = default_port; //character array for port
  unsigned int ip[4]; //helper that stores temp ip parts

  while( (c = getopt(argc, argv, "tuhsc:p:")) != -1) {
    switch(c) {
    case 't':
      *proto = 't';
      protocount++;
      break;
    case 'u':
      *proto = 'u';
      protocount++;
      break;
    case 's':
      *mode = 's';
      modecount++;
      break;
    case 'c':
      *mode = 'c';
      caddr = optarg;
      if ( sscanf(caddr, "%u.%u.%u.%u", 
		  &ip[0], 
		  &ip[1], 
		  &ip[2], 
		  &ip[3]) == EOF 
	   ) {
	fprintf(stderr, "Not a valid IP address\n");
      }
      *address = ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3];
      modecount++;
      break;
    case 'p':
      cport = optarg;
      break;
    case 'h':
      helptext();
      break;
    default:
      break;
    }
  }
  //convert port to int
  *port = (unsigned short) atoi(cport);
  if(*port < 10000) {
    fprintf(stderr, "Port %d not allowed. Needs to be >= 10000\n", *port);
    exit(1);
  }
  
  //make sure that either protocol is specified
  if(protocount != 1) {
    fprintf(stderr, "Please specify either TCP (-t), or UDP (-u)\n");
    exit(1);
  }

  //make sure that either server or client is specified
  if(modecount != 1) {
    fprintf(stderr, "Please specify either server (-s), or client (-c) mode\n");
    exit(1);
  }
}
Beispiel #3
0
int main(int argc, char **argv)
{
	// init vars
	have_xtest = False;
	numConfigs = 0;
	currConfig = 0;
	gui_selectConfig = 0;
	G15Version = 0;
	config_fd = 0;
	mled_state = G15_LED_M1;
	mkey_state = 0;
	recording = 0;
	// this is for keeping track of when to redraw
	gui_oldConfig = MAX_CONFIGS+1; // To make sure it will be redrawn at first
	was_recording = 1;

#ifdef USE_XTEST
    int xtest_major_version = 0;
    int xtest_minor_version = 0;
#endif
    struct sigaction new_action;
    int dummy=0,i=0;
    unsigned char user[256];
    struct passwd *username;
    char splashpath[1024];
    unsigned int dump = 0;
    unsigned int keysonly = 0;
    FILE *config;
    unsigned int convert = 0;

	memset(configDir,0,sizeof(configDir));
	strncpy(configDir,getenv("HOME"),1024);
	strncat(configDir,"/.g15macro/",1024-strlen(configDir));

    strncpy(configpath,getenv("HOME"),1024);

    memset(user,0,256);
    for(i=0;i<argc;i++){
        if (!strncmp(argv[i], "-u",2) || !strncmp(argv[i], "--user",6)) {
           if(argv[i+1]!=NULL){
             strncpy((char*)user,argv[i+1],128);
             i++;
           }
        }

        if (!strncmp(argv[i], "-d",2) || !strncmp(argv[i], "--dump",6)) {
          dump = 1;
        }

        if (!strncmp(argv[i], "-h",2) || !strncmp(argv[i], "--help",6)) {
          helptext();
          exit(0);
        }

        if (!strncmp(argv[i], "-k",2) || !strncmp(argv[i], "--keysonly",10)) {
          keysonly = 1;
        }

        if (!strncmp(argv[i], "-g",2) || !strncmp(argv[i], "--debug",7)) {
          printf("Debugging Enabled\n");
          debug = 1;
        }

        if (!strncmp(argv[i], "-v",2) || !strncmp(argv[i], "--version",9)) {
          printf("G15Macro version %s\n\n",PACKAGE_VERSION);
          exit(0);
        }

		if (!strncmp(argv[i], "-2",2) || !strncmp(argv[i], "--g15version2",13))
		{
			G15Version = 1; // See declaration for info
		}

    }

    if(strlen((char*)user)){
      username = getpwnam((char*)user);
        if (username==NULL) {
            username = getpwuid(geteuid());
            printf("BEWARE: running as effective uid %i\n",username->pw_uid);
        }
        else {
           if(0==setuid(username->pw_uid)) {
             setgid(username->pw_gid);
             strncpy(configpath,username->pw_dir,1024);
			 strncpy(configDir,username->pw_dir,1024);
             printf("running as user %s\n",username->pw_name);
           }
           else
             printf("Unable to run as user \"%s\" - you dont have permissions for that.\nRunning as \"%s\"\n",username->pw_name,getenv("USER"));
        }
		printf("BEWARE: this program will run files WITHOUT dropping any kind of privilegies.\n");
    }

    canvas = (g15canvas *) malloc (sizeof (g15canvas));

    if (canvas != NULL) {
        g15r_initCanvas(canvas);
    } else {
        printf("Unable to initialise the libg15render canvas\nExiting\n");
        return 1;
    }

    do {
      dpy = XOpenDisplay(getenv("DISPLAY"));
      if (!dpy) {
        printf("Unable to open display %s - retrying\n",getenv("DISPLAY"));
        sleep(2);
        }
    }while(!dpy);

    /* completely ignore errors and carry on */
    XSetErrorHandler(myx_error_handler);

	// Get keycodes for all keys
	strcpy(GKeyCodeCfg,configDir);
	strncat(GKeyCodeCfg,"GKeyCodes.cfg",1024-strlen(GKeyCodeCfg));
	printf("%s\n",GKeyCodeCfg);
	getKeyDefs(GKeyCodeCfg);


    configure_mmediakeys();
    change_keymap(0);
    XFlush(dpy);

    if(keysonly>0)
      goto close_and_exit;

    /* old binary config format */
    strncat(configpath,"/.g15macro",1024-strlen(configpath));
    strncat(configpath,"/g15macro-data",1024-strlen(configpath));
    config_fd = open(configpath,O_RDONLY|O_SYNC);

    mstates[0] = malloc(sizeof(mstates_t));
    mstates[1] = (mstates_t*)malloc(sizeof(mstates_t));
    mstates[2] = (mstates_t*)malloc(sizeof(mstates_t));

    if(config_fd>0) {
        printf("Converting old data\n");
        read(config_fd,mstates[0],sizeof(mstates_t));
        read(config_fd,mstates[1],sizeof(mstates_t));
        read(config_fd,mstates[2],sizeof(mstates_t));
        close(config_fd);
        strncpy(configpath,getenv("HOME"),1024);
        strncat(configpath,"/.g15macro",1024-strlen(configpath));
        char configbak[1024];
        strcpy(configbak,configpath);
        strncat(configpath,"/g15macro-data",1024-strlen(configpath));
        strncat(configbak,"/g15macro-data.old",1024-strlen(configpath));
        rename(configpath,configbak);
        convert = 1;
    }
	else
		cleanMstates(0); //0 = only NULL the pointers

    /* new format */
    strncpy(configpath,getenv("HOME"),1024);
    strncat(configpath,"/.g15macro",1024-strlen(configpath));
    mkdir(configpath,0777);
    strncat(configpath,"/g15macro.conf",1024-strlen(configpath));
    config=fopen(configpath,"a");
    fclose(config);

    do {
      if((g15screen_fd = new_g15_screen(G15_G15RBUF))<0){
        printf("Sorry, cant connect to the G15daemon - retrying\n");
        sleep(2);
      }
    }while(g15screen_fd<0);

	loadMultiConfig();
	printf("I've now got the following macro files:\n");
	for(i=0; i < MAX_CONFIGS;++i)
	{
		if(!configs[i])
			continue;
		printf("%i:%s%s\n",i,configDir,configs[i]->configfile);
	}

	if(!convert)
	{
		memset(configpath,0,sizeof(configpath));
		strcpy(configpath,configDir);
		strncat(configpath,configs[currConfig]->configfile,1024-strlen(configpath));
		restore_config(configpath);
	}


    if(dump){
        printf("G15Macro Dumping Codes...");
        dump_config(stderr);
        exit(0);
    }

    g15_send_cmd (g15screen_fd,G15DAEMON_KEY_HANDLER, dummy);
    usleep(1000);
    g15_send_cmd (g15screen_fd,G15DAEMON_MKEYLEDS,mled_state);
    usleep(1000);

    root_win = DefaultRootWindow(dpy);
    if (!root_win) {
        printf("Cant find root window\n");
        return 1;
    }


    have_xtest = False;
#ifdef HAVE_XTEST
#ifdef USE_XTEST
    have_xtest = XTestQueryExtension(dpy, &dummy, &dummy, &xtest_major_version, &xtest_minor_version);
    if(have_xtest == False || xtest_major_version < 2 || (xtest_major_version <= 2 && xtest_minor_version < 2))
    {
        printf("Warning: XTEST extension not supported by Xserver.  This is not fatal.\nReverting to XSendEvent for keypress emulation\n");
    }
#else //USE_XTEST
  printf("XTest disabled by configure option.  Using XSendEvent instead.\n");
#endif //USE_XTEST
#else //HAVE_XTEST
  printf("XTest disabled by configure: no devel package was found.  Using XSendEvent instead.\n");
#endif //HAVE_XTEST

	printf("XTest enabled. Using XTest.\n");


	new_action.sa_handler = g15macro_sighandler;
	new_action.sa_flags = 0;
	sigaction(SIGINT, &new_action, NULL);
	sigaction(SIGQUIT, &new_action, NULL);
	sigaction(SIGTERM, &new_action, NULL);
	sigaction(SIGPIPE, &new_action, NULL);
	sigaction(SIGHUP, &new_action, NULL);

	// So that forked processes that die can actually die instead of going defunct.
	struct sigaction act;
	memset(&act,0,sizeof(act));
	act.sa_handler = &cleanupChildren;
	sigaction(SIGCHLD,&act,NULL);


    snprintf((char*)splashpath,1024,"%s/%s",DATADIR,"g15macro/splash/g15macro.wbmp");
    g15r_loadWbmpSplash(canvas, splashpath);
    g15_send(g15screen_fd,(char *)canvas->buffer,G15_BUFFER_LEN);
	// Following piece of code is not documented (what i could find anyway)
	// But makes so that the user can never bring this screen to front.
	// TODO: Document it
//     #ifdef G15DAEMON_NEVER_SELECT
//         g15_send_cmd (g15screen_fd, G15DAEMON_NEVER_SELECT, dummy);
//     #endif

    usleep(1000);
    pthread_mutex_init(&x11mutex,NULL);
    pthread_mutex_init(&config_mutex,NULL);
	pthread_mutex_init(&gui_select,NULL);
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    int thread_policy=SCHED_FIFO;
    pthread_attr_setschedpolicy(&attr,thread_policy);
    pthread_attr_setstacksize(&attr,32*1024); /* set stack to 32k - dont need 8Mb !! */

    pthread_create(&Xkeys, &attr, xevent_thread, NULL);
    pthread_create(&Lkeys, &attr, Lkeys_thread, NULL);


	mainLoop();

	cleanup();

close_and_exit:
    /*    XCloseDisplay(dpy);  */
    return 0;
}
Beispiel #4
0
/*
 * Interactive Mode
 */
kmc_control_interactive(int fd_kmc, struct termios * oldtio)
{
	char 	z;
	int		zz;

	z = '\x00';
	printf("\t\n\n*************Now in interactive mode****************\n\n");
  
	zz = kmc_query_display_mode(fd_kmc);
 
	printf("\nIf you are having trouble in LIVE mode I\n");
	printf("would suggest pressing the ESC/STOP button\n");
 
	while ( z != 'Q'){
		printf("\nPlease enter a command (help: H) : ");
		scanf("%s", &z);
		switch (z) {

			case 'H':
				helptext("Interactive Mode");
				break;
  
			case 'q':
				morehelptext("additional commands");
				break;

			case 'v':
				fprintf(stderr, "kmc_control version 0.3.2\n");
				break;
         
			case 'R':
				kmc_press_record_ready_button(fd_kmc);
				break;

			case 'T':
				kmc_press_trigger_button(fd_kmc);
				break;

			case 'M':
				kmc_press_mode_button(fd_kmc);
				break;

			case 'P':
				kmc_press_play_button(fd_kmc);
				break;

			case 'S':
				kmc_press_stop_escape_button(fd_kmc,zz);
				break;

			case 'D':
				kmc_press_down_button(fd_kmc,zz);
				break;

			case 'E':
				kmc_press_menu_enter_button(fd_kmc,zz);
				break;

			case 'U':
				kmc_press_up_button(fd_kmc,zz);
				break;
    
			case 'm':
				kmc_query_display_mode(fd_kmc);
				break;

			case 'r':
				kmc_query_record_ready(fd_kmc);
				break;

			case 's':
				kmc_query_setup_mode(fd_kmc);
				break;

			case 'p':
				kmc_query_display_status(fd_kmc);
				break;

			case 't':
				kmc_query_trigger_mode(fd_kmc);
				break;

			case 'u':
				kmc_query_n_memory_boards(fd_kmc);
				break;

			case 'f':
				kmc_query_record_rate(fd_kmc);
				break;

			case 'd':
				kmc_query_display_size(fd_kmc);
				break;

			case 'e':
				kmc_query_shutter_speed(fd_kmc);
				break;

			case 'y':
				kmc_query_playback_rate(fd_kmc);
				break;
		}
	}
	close_serial_device(fd_kmc, &oldtio);
	exit(0);
}
Beispiel #5
0
int main(int argc, char *argv[]) {
	struct sigaction act;
	int argn = 1, ret;

	{
		const char *home = getenv("HOME");
		if (home) {
			char *conffile = xmalloc(strlen(home) + sizeof(CONFIG_FILE) + 2);
			strcpy(conffile, home);
			strcat(conffile, "/" CONFIG_FILE);
			xconfig_parse_file(evilwm_options, conffile);
			free(conffile);
		}
	}
	ret = xconfig_parse_cli(evilwm_options, argc, argv, &argn);
	if (ret == XCONFIG_MISSING_ARG) {
		fprintf(stderr, "%s: missing argument to `%s'\n", argv[0], argv[argn]);
		exit(1);
	} else if (ret == XCONFIG_BAD_OPTION) {
		if (0 == strcmp(argv[argn], "-h")
				|| 0 == strcmp(argv[argn], "--help")) {
			helptext();
			exit(0);
#ifdef STDIO
		} else if (0 == strcmp(argv[argn], "-V")
				|| 0 == strcmp(argv[argn], "--version")) {
			LOG_INFO("evilwm version " VERSION "\n");
			exit(0);
#endif
		} else {
			helptext();
			exit(1);
		}
	}

	if (opt_grabmask1) grabmask1 = parse_modifiers(opt_grabmask1);
	if (opt_grabmask2) grabmask2 = parse_modifiers(opt_grabmask2);
	if (opt_altmask) altmask = parse_modifiers(opt_altmask);

	wm_exit = 0;
	act.sa_handler = handle_signal;
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	sigaction(SIGTERM, &act, NULL);
	sigaction(SIGINT, &act, NULL);
	sigaction(SIGHUP, &act, NULL);

	setup_display();

	event_main_loop();

	/* Quit Nicely */
	while (clients_stacking_order)
		remove_client(clients_stacking_order->data);
	XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
	if (font) XFreeFont(dpy, font);
	{
		int i;
		for (i = 0; i < num_screens; i++) {
			ewmh_deinit_screen(&screens[i]);
			XFreeGC(dpy, screens[i].invert_gc);
			XInstallColormap(dpy, DefaultColormap(dpy, i));
		}
	}
	free(screens);
	XCloseDisplay(dpy);

	return 0;
}