Exemple #1
0
/* expects range [0,999] */
void getMagStr(uint32_t n, char ** const str, uint32_t * const count, uint32_t * const size) {
  uint32_t x,first=1;
  if(n>=100) {
    strapp(str,ones[n/100-1],count,size);
    strapp(str,hund,count,size);
    first=0;
    n%=100;
  }
  if(n==0)
    return;
  if(n<20) {
    if(!first)
      strapp(str,and,count,size);
    strapp(str, (n<10) ? ones[n-1] : teens[n-10] ,count,size);
    first=0;
  }
  else {
    if(!first)
      strapp(str,and,count,size);
    strapp(str,tens[n/10-1],count,size);
    x = n%10;
    if(!x)
      return;
    strapp(str,hyphen,count,size);
    strapp(str,ones[--x],count,size);
  }
}
Exemple #2
0
void citemhandler::init() {
    citemhandler::OFFSETX=200;
    citemhandler::OFFSETY=200;
    //citemhandler::layerlist= enew carray<carray<citem>*> ();
        for(int i=0; i<EF_LAYERS;i++){
            char* buf=NULL;
            char num[16]="";
            itoa(i,num,16);
            strapp(buf,"itemlist");
            strapp(buf, num );
            citemhandler::itemlist[i]= enew carray<citem> ();
            //citemhandler::layerlist->add( &citemhandler::itemlist[i], buf);
            efree(buf);
        }
}
Exemple #3
0
char* toBritishStr(uint64_t n) {
  uint32_t i,first,size=10,count=0;
  char     *out;
  out = (char*) malloc(size * sizeof(char));
  for(i=mags-1,first=1; i<mags; --i) {
    if(n>=mag[i]) {
      if(!first)
        strapp(&out,and,&count,&size);
      first=0;
      getMagStr(n/mag[i],&out,&count,&size);
      strapp(&out,denom[i],&count,&size);
      n%=mag[i];
    }
  }
  if(count>=size)
    expand_str(&out,&size);
  out[count++] = '\0';
  return out;
}
Exemple #4
0
void pollOnline(char * ret){
	int j;
	for(j = 0; j<=fdmax; j++){
		if (FD_ISSET(j, &master) && j!=listener && ol_list[j]) {
			//printf("Polled %d %s\n", j, user_list[j]);
			strapp(ret, " [", user_list[j], "]", NULL);
		}
	}
	strcat(ret, "\n");
}
Exemple #5
0
static int cb_iptables_rule_common(const struct filterent *ent, struct fg_misc *misc, sa_family_t family, const char *iptables)
{
    char *rulechain = NULL, *revchain = NULL, *natchain = NULL;
    char *ruletarget = NULL, *revtarget = NULL, *nattarget = NULL;
    char *natrule = NULL, *rule = NULL, *rule_r = NULL;
    char *forchain = NULL, *forrevchain = NULL;
    char *fortarget = NULL, *forrevtarget = NULL;
    char *subchain = NULL, *subtarget = NULL;
    int neednat = 0, needret = 0;
    int islocal = (ent->rtype != ROUTEDONLY);
    int isforward = (ent->rtype != LOCALONLY);
    long *feat = (long*)misc->misc;
    enum filtertype target = ent->target;
    int orules = 0;

    /* nat rule? */
    if((target == MASQ) || (target == REDIRECT)) {
        neednat = 1;
        if(family == AF_INET6) {
            fprintf(stderr, "can't NAT with IPv6\n");
            return -1;
        }
        if((target == MASQ) && (ent->direction == INPUT)) {
            fprintf(stderr, "can't masquerade on input\n");
            return -1;
        } else if((target == REDIRECT) && (ent->direction == OUTPUT)) {
            fprintf(stderr, "can't redirect on output\n");
            return -1;
        }
    }

    /* sub-stuff? */
    if(target == F_SUBGROUP) {
        subtarget = strapp(strdup(ent->subgroup), "-");
        needret = 1;
    } else subtarget = strdup("");

    if(ent->groupname) subchain = strapp(strdup(ent->groupname), "-");
    else subchain = strdup("");

    switch(ent->direction) {
    case INPUT:
        natchain = strdup("PREROUTING");
        rulechain = strdup("INPUT");
        revchain = strdup("OUTPUT");
        forchain = strdup("FORWARD");
        forrevchain = strdup("FORW_OUT");
        if(ent->iface && strcmp(ent->iface, "*")) {
            if(NEG(DIRECTION)) {
                APPS(natrule, "!");
                APPS(rule, "!");
                APPS(rule_r, "!");
            }
            APPSS2(natrule, "-i", ent->iface);
            APPSS2(rule, "-i", ent->iface);
            APPSS2(rule_r, "-o", ent->iface);
        }
        break;
    case OUTPUT:
        natchain = strdup("POSTROUTING");
        rulechain = strdup("OUTPUT");
        revchain = strdup("INPUT");
        forchain = strdup("FORW_OUT");
        forrevchain = strdup("FORWARD");
        if(ent->iface && strcmp(ent->iface, "*")) {
            if(NEG(DIRECTION)) {
                APPS(natrule, "!");
                APPS(rule, "!");
                APPS(rule_r, "!");
            }
            APPSS2(natrule, "-o", ent->iface);
            APPSS2(rule, "-o", ent->iface);
            APPSS2(rule_r, "-i", ent->iface);
        }
        break;
    default:
        fprintf(stderr, "unknown direction\n");
        abort();
    }

    /* state and reverse rules here */
    /* FIXME: state established on reverse for every rule, not just
     * specifically udp and tcp */
    if(ent->proto.name) {
        int needstate = 0;

        APPSS2(natrule, "-p", ent->proto.name);
        APPSS2(rule, "-p", ent->proto.name);
        APPSS2(rule_r, "-p", ent->proto.name);
        switch(ent->proto.num) {
        case IPPROTO_TCP:
            needret++;
            needstate++;
            *feat |= A_TCP;
            APPS(rule_r, "! --syn");
            break;
        case IPPROTO_UDP:
            needret++;
            needstate++;
            *feat |= A_UDP;
            break;
        }
        if(needstate) {
            APPS(rule, "-m state --state NEW,ESTABLISHED");
            APPS(rule_r, "-m state --state ESTABLISHED");
        }
    }

    if(ent->srcaddr.addrstr) {
        NEGA(natrule, SOURCE);
        NEGA(rule, SOURCE);
        NEGA(rule_r, SOURCE);
        APPIP2("-s", natrule, &ent->srcaddr);
        APPIP2("-s", rule, &ent->srcaddr);
        APPIP2("-d", rule_r, &ent->srcaddr);
    }
    if(ent->dstaddr.addrstr) {
        NEGA(natrule, DEST);
        NEGA(rule, DEST);
        NEGA(rule_r, DEST);
        APPIP2("-d", natrule, &ent->dstaddr);
        APPIP2("-d", rule, &ent->dstaddr);
        APPIP2("-s", rule_r, &ent->dstaddr);
    }

    switch(ent->proto.num) {
    case 0:
        break;
    case IPPROTO_UDP:
    case IPPROTO_TCP:
        if(ent->u.ports.src.minstr) {
            NEGA(natrule, SPORT);
            NEGA(rule, SPORT);
            NEGA(rule_r, SPORT);
            APPPORT2("--sport", natrule, &ent->u.ports.src);
            APPPORT2("--sport", rule, &ent->u.ports.src);
            APPPORT2("--dport", rule_r, &ent->u.ports.src);
        }
        if(ent->u.ports.dst.minstr) {
            NEGA(natrule, DPORT);
            NEGA(rule, DPORT);
            NEGA(rule_r, DPORT);
            APPPORT2("--dport", natrule, &ent->u.ports.dst);
            APPPORT2("--dport", rule, &ent->u.ports.dst);
            APPPORT2("--sport", rule_r, &ent->u.ports.dst);
        }
        break;
    case IPPROTO_ICMP:
        if(ent->u.icmp) {
            NEGA(natrule, ICMPTYPE);
            APPSS2(natrule, "--icmp-type", ent->u.icmp);
            NEGA(rule, ICMPTYPE);
            APPSS2(rule, "--icmp-type", ent->u.icmp);
        }
        break;
    default:
        ;
    }

    APPS(natrule, "-j");
    APPS(rule, "-j");
    APPS(rule_r, "-j");

    /* The "rule+1" in the printfs below are an ugly hack to
     * prevent a double-space in the output rule */

    /* Yuck, separate rules for logging packets.  Be still my
     * beating lunch.
     *
     * Logging and target rules have to be the last bits
     * before output, or this doesn't work.  This will also
     * fail if any mangling has been done above.
     */
    if(ESET(ent, LOG)) {
        char *lc, *la, *ls;
        if(ent->logmsg) {
            lc = strdup(" --log-prefix=");
            la = ent->logmsg;
            ls = strdup("\" \"");
        } else
            lc = la = ls = strdup("");
        if(islocal) orules++,oprintf("%s -A %s %s LOG%s%s%s\n", iptables, rulechain, rule+1, lc, la, ls);
        if(isforward) orules++,oprintf("%s -A %s %s LOG%s%s%s\n", iptables, forchain, rule+1, lc, la, ls);
    }

    /* Do this twice, once for NAT, once for filter */
    if(neednat) {
        switch(target) {
        case MASQ:
            nattarget = strdup("MASQUERADE");
            break;
        case REDIRECT:
            nattarget = strdup("REDIRECT");
            break;
        default:
            abort();
        }
    }

    switch(target) {
    case MASQ:
    case REDIRECT:
    case T_ACCEPT:
        ruletarget = revtarget =
                         fortarget = forrevtarget = strdup("ACCEPT");
        switch(ent->direction) {
        case INPUT:
            fortarget = strdup("FORW_OUT");
            break;
        case OUTPUT:
            forrevtarget = strdup("FORW_OUT");
            break;
        default:
            abort();
        }
        break;
    case DROP:
        ruletarget = fortarget = strdup("DROP");
        needret = 0;
        break;
    case T_REJECT:
        ruletarget = fortarget = strdup("REJECT");
        needret = 0;
        *feat |= T_REJECT;
        break;
    case F_SUBGROUP:
        switch(ent->direction) {
        case INPUT:
            ruletarget = strdup("INPUT");
            revtarget = strdup("OUTPUT");
            fortarget = strdup("FORWARD");
            forrevtarget = strdup("FORW_OUT");
            break;
        case OUTPUT:
            ruletarget = strdup("OUTPUT");
            revtarget = strdup("INPUT");
            fortarget = strdup("FORW_OUT");
            forrevtarget = strdup("FORWARD");
            break;
        default:
            abort();
        }
        break;
    default:
        abort();
    }

    if((misc->flags & FF_LSTATE) && (target != T_REJECT)) needret = 0;

    if(ent->oneway) needret = 0;

    if(neednat) orules++,oprintf("%s -t nat -A %s%s %s %s%s\n", iptables, subchain, natchain, natrule+1, subtarget, nattarget);
    if(islocal) orules++,oprintf("%s -A %s%s %s %s%s\n", iptables, subchain, rulechain, rule+1, subtarget, ruletarget);
    if(needret) orules++,oprintf("%s -I %s%s %s %s%s\n", iptables, subchain, revchain, rule_r+1, subtarget, revtarget);
    if(isforward) {
        orules++,oprintf("%s -A %s%s %s %s%s\n", iptables, subchain, forchain, rule+1, subtarget, fortarget);
        if(needret) orules++,oprintf("%s -I %s%s %s %s%s\n", iptables, subchain, forrevchain, rule_r+1, subtarget, forrevtarget);
    }

    free(natrule);
    free(rule);
    free(rule_r);
    free(subchain);
    free(subtarget);
    return orules;
}
Exemple #6
0
int main(int argc, char* argv[])
{
	program_name = argv[0];

	if (argc < 3) {
		print_help();
		exit(EXIT_FAILURE);
	}
	
	char binfile[50];
	base = argv[1];
	strapp(binfile, ".nc");

	int total_ids = atoi(argv[2]);

	FILE *fp;
	if (argc > 2) {
		infile = argv[3];
		fp = fopen(infile, "r");
		if (!fp) 
			error(1, errno, "%s", infile);
	} else {
		infile = "stdin";
		fp = stdin;
	}

	/* Get the first line with the SNP names */
	char *line = NULL;
	size_t n = 0;
	int read = getlin(&line, &n, fp);
	if (read == -1)
		error(1, errno, "%s", infile);
   	int nsnps = getsnps(line);
	free(line);
	line = NULL;
	printf("%d markers\n", nsnps);

	printf("Converting intensity values to binary\n");
	int ncid, stat, sampid, varid;
	size_t nids = 0, perkey;
	size_t count[3] = { 1, 0, 0 };
	size_t start[3] = { 0, 0, 0 };
	char *id;	
	float *vals;

	while (getlin(&line, &n, fp) != -1) {
		if (nids == 0) {
			/* Count number of vals in first line to alloc indv_dat */
			read = getstrings(line, NULL, DELIM) - 1;
			if (read < 1)
				error(2, 0, "No values found");
			if (read % nsnps != 0)
				error(2, 0, "Not same number of values for each marker");
			perkey = read / nsnps;
			printf("%zu values per marker\n", perkey);
			vals = malloc(sizeof(float) * read);
			if (vals == NULL)
				error(1, errno, "dat.vals");


			/* Create netcdf file and write snp names */
 			ncid = create_nc(binfile, total_ids, nsnps, perkey);
			stat = nc_inq_varid(ncid, "sample", &sampid);
			check_err(stat,__LINE__,__FILE__);
			stat = nc_inq_varid(ncid, "intensity", &varid);
			check_err(stat,__LINE__,__FILE__);
			writesnps(ncid, nsnps);

			count[1] = nsnps;
			count[2] = perkey;
		}
				
		/* Read valuess from line */
		id = linevals(line, vals);

		/* Write sample id */
		stat = nc_put_var1_string(ncid, sampid, &nids, (const char **) &id);
		check_err(stat,__LINE__,__FILE__);

		/* Write intensity values */
		start[0] = nids;
		stat = nc_put_vara_float(ncid, varid, start, count, vals);
    	check_err(stat,__LINE__,__FILE__);

		free(line);
		line = NULL;
		nids++;
		printf("Read %zu\r", nids);
		fflush(stdout);
	}
			
	fclose(fp);
    stat = nc_close(ncid);
    check_err(stat,__LINE__,__FILE__);
	free(vals);
	free(line);

	printf("Wrote %zu samples to [ %s ]\n", nids, binfile);
	exit(EXIT_SUCCESS);
}
Exemple #7
0
int main(int argc, char ** argv)
{

	int i;
	int n = 0;
	int unordered = 0;

	/*
		Command-line arguments.
	*/
	int _f = 0;
	int _q = 0;

	int valid_argc = 1;

	char valid_options[][3] = {"-f","-q"};
	int * valid_options_var[] = {&_f, &_q};

	//Check if specified options are valid
	for(i=1; i<argc; i++)
	{
		if(argv[i][0]=='-')
		{
			if( !checkArg(argv[i], sizeof(valid_options) / sizeof(valid_options[0]), valid_options) )
			{
				printf("quotes: Invalid option %s.\n",argv[i]);
				return 0;
			}
		}
	}

	for(i=0; i<sizeof(valid_options)/sizeof(valid_options[0]); i++)
		setarg(valid_options[i], valid_options_var[i], argc, argv, &valid_argc);

	if(argc != valid_argc)
	{
		help_message
	}


	/*
	if(argc == 3)
	{
		if(!strcmp(argv[1], "-f"))
		{
			_f = 1;
		}
		else if (!strcmp(argv[1], "-q"))
		{
			_q = 1;
		}
		else
		{
			printf("quotes: Unrecognized option '%s'\n",argv[1]);
			help_message
		}
	}*/


	FILE * fp; 

	if(_f)
		fp = fopen(argv[_f+1],"r");
	else
		fp = fopen("/home/jddantes/Dropbox/Git/Scripts/quotes","r");
	


	if(!fp)
	{
		printf("Error. Could not open file.\n");
		return 0;
	}

	char ** quotes_list = 0;

	int num_quotes = 0;
	int quote_id = 0;
	while(fscanf(fp,"%d",&quote_id)!=EOF)
	{

		if(quote_id -1 != num_quotes )
		{
			unordered = num_quotes;

			//printf("%s: Quote %d out of order. Previous quote is numbered %d.\n",argv[0],num_quotes);
			//return 0;
		}

		num_quotes++;
		quotes_list = (char **)realloc(quotes_list, sizeof(char *) * num_quotes);
		quotes_list[num_quotes-1] = (char *)malloc(sizeof(char) * quote_max_size + 1);
		quotes_list[num_quotes-1][0] = 0;

		char c;

		/* 
			Scan until quote_start
		*/

		int quote_start_matched = 0;

		while(fscanf(fp,"%c",&c)==1)
		{
			if(c==quote_start[quote_start_matched])
			{	
				quote_start_matched++;
				if(quote_start_matched == quote_start_size)
					break;
			}
			else
			{
				quote_start_matched = 0;
			}
		}

		/*
			Scan quote_start END
		*/

		/*
			Scan and print (or store into quotes_list) until quote_end
		*/

		int quote_end_matched = 0;
		char buffer[quote_end_size+1];
		buffer[0] = 0;

		char char_buffer[2]="";

		while(fscanf(fp,"%c",&c)==1)
		{

			if(c==quote_end[quote_end_matched])
			{
				buffer[quote_end_matched] = c;
				buffer[quote_end_matched + 1] = 0;

				quote_end_matched++;

				if(quote_end_matched == quote_end_size)
				{
					buffer[0] = 0;
					break;
				}
			}
			else
			{
				quote_end_matched = 0;

				if(strlen(buffer))
				{
					strapp(quotes_list[num_quotes-1],buffer);
					buffer[0] = 0;
				}
				
				
				char_buffer[0]=c;
				char_buffer[1]=0;

				strapp(quotes_list[num_quotes-1],char_buffer);
				
			}
		}

		/*
			Scan and print until quote_end END
		*/
	}

	fclose(fp);

	//Print quotes_list
	/*
	printf("Printing quotes list...\n");
	for(i=0; i<num_quotes; i++)
	{
		printf("Quote %d: %s\n",i+1, quotes_list[i]);
	}
	printf("End of list.\n");
	*/

	if(_q) //Quote id specified
	{
		if(unordered)
		{
			printf("quotes: warning: The entry after quote %d is numbered incorrectly.\n",unordered);
		}

		int q;
		sscanf(argv[_q+1],"%d",&q);

		if(!q)
		{
			printf("Indices start at either 1 or -1. If the value exceeds the number of quotes, the modulo value will be used.\n");
			return 0;
		}

		if(abs(q) >  num_quotes)
			q = q % num_quotes;

		if(q>0)
			n = q-1;
		else
			n = num_quotes + q;

		
	}
	else //Produce random quote.
	{
		srand(time(NULL));
		n = rand() % num_quotes;
	}

	printf("%s\n",quotes_list[n]);

	for(i=0; i<num_quotes; i++)
		free(quotes_list[i]);
	free(quotes_list);

	return 0;
}