main() { int uid, i; FILE *fd; char *myname, fname[128]; uid = getuid(); myname = (char *) getlogin(); if(myname == NULL) myname = getpwuid(uid)->pw_name; sprintf(fname, "%s/%s.key", maildir, myname); comminit(); setup(getpass("Gimme key: ")); mkb(); mkx(); #ifdef debug omout(b); omout(x); #endif mka(); i = creat(fname, 0644); if(i<0) { perror(fname); exit(1); } close(i); fd = fopen(fname, "w"); for(i=0; i<42; i++) nout(a[i], fd); exit(0); }
int main() { #ifdef B32 break_optimization(new Deriver<B, 0>); #endif #ifdef B64 break_optimization(new Deriver<B, 0>); break_optimization(new Deriver<B, 1>); #endif #ifdef BM break_optimization(new Deriver<B, 0>); break_optimization(new Deriver<B, 1>); break_optimization(new Deriver<B, 2>); #endif A *a = mkb(); break_optimization(a); // CFI: 1 // NCFI: 1 fprintf(stderr, "1\n"); ((B *)a)->f(); // UB here // CFI-NOT: 2 // NCFI: 2 fprintf(stderr, "2\n"); }
main() { int i; char *p; uid = getuid(); myname = getlogin(); if(myname == NULL) myname = getpwuid(uid)->pw_name; comminit(); mbuf = itom(0); files(); setup(getpass("Key: ")); mkb(); mkx(); #ifndef debug invert(x, b, x); #else invert(x, b, z); mult(x, z, z); mdiv(z, b, q, z); omout(z); invert(x, b, x); #endif for(i=0; i<fcnt; i++) { sprintf(line, "%s%s.%d", maildir, myname, fnum[i]); if(stat(line, &stbuf)<0) { perror(line); continue; } if(stbuf.st_size == 0) { printf("zero length mail file\n"); unlink(line); continue; } if((mf = fopen(line, "r"))==NULL) { perror(line); continue; } decipher(mf, stdout); cmnd: printf("? "); fgets(buf, sizeof(buf), stdin); if(feof(stdin)) exit(0); switch(buf[0]) { case 'q': exit(0); case 'n': case 'd': case '\n': unlink(line); fclose(mf); break; case '!': system(buf+1); printf("!\n"); goto cmnd; case 's': case 'w': rewind(mf); if(buf[1] == '\n' || buf[1] == '\0') strcpy(buf, "s mbox\n"); for(p=buf; !isspace(*p); p++); for(; isspace(*p); p++); p[strlen(p)-1] = 0; kf = fopen(p, "a"); if(kf == NULL) { perror(p); break; } decipher(mf, kf); fclose(mf); fclose(kf); unlink(line); break; } } exit(0); }
main() { int i; char *p; uid = getuid(); myname = (char *)getlogin(); if(myname == NULL) myname = getpwuid(uid)->pw_name; comminit(); mbuf = itom(0); files(); setup(getpass("Key: ")); mkb(); mkx(); #ifndef debug invert(x, b, x); #else invert(x, b, z); mult(x, z, z); mdiv(z, b, q, z); omout(z); invert(x, b, x); #endif for(i=0; i<fcnt; i++) { sprintf(line, "%s%s.%d", maildir, myname, fnum[i]); if(stat(line, &stbuf)<0) { perror(line); continue; } if(stbuf.st_size == 0) { printf("zero length mail file\n"); unlink(line); continue; } if((mf = fopen(line, "r"))==NULL) { perror(line); continue; } decipher(mf, stdout); cmnd: printf("? "); fgets(buf, sizeof(buf), stdin); if(feof(stdin)) exit(0); switch(buf[0]) { case 'q': exit(0); case 'n': case 'd': case '\n': fclose(mf); unlink(line); break; case '!': system(buf+1); printf("!\n"); goto cmnd; case 's': case 'w': rewind(mf); if(buf[1] == '\n' || buf[1] == '\0') strcpy(buf, "s mbox\n"); for(p = buf+1; isspace(*p); p++); p[strlen(p)-1] = 0; kf = fopen(p, "a"); if(kf == NULL) { perror(p); goto cmnd; } decipher(mf, kf); fclose(mf); fclose(kf); unlink(line); break; default: printf("Commands are:\n"); printf("q quit, leaving unread messages\n"); printf("n delete current message and goto next\n"); printf("d same as above\n"); printf("\\n same as above\n"); printf("! execute shell command\n"); printf("s save message in the named file or mbox\n"); printf("w same as above\n"); printf("? prints this list\n"); goto cmnd; } } exit(0); }
void mkbxy(int p,float x,float y,float xx,float yy,float v){ xx-=x; yy-=y; float xy=sqrt(xx*xx+yy*yy); if(xy)mkb(p,x,y,xx*v/xy,yy*v/xy); }
void mkbd(int p,float x,float y,float v,float d){ mkb(p,x,y,cos(d)*v,sin(d)*v); }
///<param name = "*fname"> The name of the function </param> ///<param name = "*arg"> The argument it is given </param> static INPparseNode *mkfnode(char *fname, INPparseNode *arg) { int i; INPparseNode *p = NULL; char buf[128], *name = NULL, *s = NULL; IFvalue temp; temp.nValue = 0;// this line is required by microsoft. Generates a C4700 error if this is not included /* Make sure the case is ok. */ (void)strcpy(buf, fname); for (s = buf; *s; s++) { if (isupper(*s)) { *s = tolower(*s); } } p = (INPparseNode *)MALLOC(sizeof(INPparseNode)); if (!strcmp(buf, "v")) { name = MALLOC(128); if (arg->type == PT_PLACEHOLDER) { strcpy(name, arg->funcname); } else if (arg->type == PT_CONSTANT) { (void)sprintf(name, "%d", (int)arg->constant); } else if (arg->type != PT_COMMA) { fprintf(stderr, "Error: badly formed node voltage\n"); return (NULL); } if (arg->type == PT_COMMA) { /* Change v(a,b) into v(a) - v(b) */ p = mkb(PT_MINUS, mkfnode(fname, arg->left), mkfnode(fname, arg->right)); } else { /* printf("getting a node called '%s'\n", name); */ INPtermInsert(circuit, &name, tables, &(temp.nValue)); /* This block is reallocating the value variable. Although defined as a pointer, it is actually being used as a dynamic array*/ for (i = 0; i < numvalues; i++) { if ((types[i] == IF_NODE) && (values[i].nValue == temp.nValue)) break; } if (i == numvalues) { if (numvalues) { /*This is a test*/ values = (IFvalue *)REALLOC((char *)values, (numvalues + 1) * sizeof(IFvalue)); /* values is becoming an array where each element is IFvalue. Dynamic Array Allocation */ types = (int *)REALLOC((char *)types, (numvalues + 1) * sizeof(int)); } else { /* If numvalues is 0, then the program will allocate values with 1 cell The program will only execute this block once */ values = (IFvalue *)MALLOC(sizeof(IFvalue)); /* values is becoming an array where each element is IFvalue */ types = (int *)MALLOC(sizeof(int)); } values[i] = temp; types[i] = IF_NODE; numvalues++; } p->valueIndex = i; p->type = PT_VAR; } } else if (!strcmp(buf, "i")) { name = MALLOC(128); if (arg->type == PT_PLACEHOLDER) strcpy(name, arg->funcname); else if (arg->type == PT_CONSTANT) (void) sprintf(name, "%d", (int)arg->constant); else { fprintf(stderr, "Error: badly formed branch current\n"); return (NULL); } /* printf("getting a device called '%s'\n", name); */ INPinsert(&name, tables); for (i = 0; i < numvalues; i++) { if ((types[i] == IF_INSTANCE) && (values[i].uValue == temp.uValue)) break; } if (i == numvalues) { if (numvalues) { values = (IFvalue *)REALLOC((char *)values, (numvalues + 1) * sizeof(IFvalue)); types = (int *)REALLOC((char *)types, (numvalues + 1) * sizeof(int)); } else { values = (IFvalue *)MALLOC(sizeof(IFvalue)); types = (int *)MALLOC(sizeof(int)); } values[i].uValue = (IFuid)name; types[i] = IF_INSTANCE; numvalues++; } p->valueIndex = i; p->type = PT_VAR; } else { for (i = 0; i < NUM_FUNCS; i++) { if (!strcmp(funcs[i].name, buf)) break; } if (i == NUM_FUNCS) { fprintf(stderr, "Error: no such function '%s'\n", buf); return (NULL); } p->type = PT_FUNCTION; p->left = arg; p->funcname = funcs[i].name; p->funcnum = funcs[i].number; p->function = funcs[i].funcptr; } return (p); }
///<param name = "*p"> Tree to differentiate </param> ///<param name = "varnum"> Number of variable to differentiate WRT </param> static INPparseNode *PTdifferentiate(INPparseNode *p, int varnum) { INPparseNode *arg1 = NULL, *arg2 = NULL, *newp = NULL; /* printf("differentiating: "); printTree(p); printf(" wrt var %d\n", varnum);*/ switch (p->type) { case PT_CONSTANT: newp = mkcon((double)0); break; case PT_VAR: /* Is this the variable we're differentiating wrt? */ if (p->valueIndex == varnum) newp = mkcon((double)1); else newp = mkcon((double)0); break; case PT_PLUS: case PT_MINUS: arg1 = PTdifferentiate(p->left, varnum); arg2 = PTdifferentiate(p->right, varnum); newp = mkb(p->type, arg1, arg2); break; case PT_TIMES: /* d(a * b) = d(a) * b + d(b) * a */ arg1 = PTdifferentiate(p->left, varnum); arg2 = PTdifferentiate(p->right, varnum); newp = mkb(PT_PLUS, mkb(PT_TIMES, arg1, p->right), mkb(PT_TIMES, p->left, arg2)); break; case PT_DIVIDE: /* d(a / b) = (d(a) * b - d(b) * a) / b^2 */ arg1 = PTdifferentiate(p->left, varnum); arg2 = PTdifferentiate(p->right, varnum); newp = mkb(PT_DIVIDE, mkb(PT_MINUS, mkb(PT_TIMES, arg1, p->right), mkb(PT_TIMES, p->left, arg2)), mkb(PT_POWER, p->right, mkcon((double)2))); break; case PT_POWER: /* Two cases... If the power is a constant then we're cool. * Otherwise we have to be tricky. */ if (p->right->type == PT_CONSTANT) { arg1 = PTdifferentiate(p->left, varnum); newp = mkb(PT_TIMES, mkb(PT_TIMES, mkcon(p->right->constant), mkb(PT_POWER, p->left, mkcon(p->right->constant - 1))), arg1); } else { /* This is complicated. f(x) ^ g(x) -> * exp(y(x) * ln(f(x)) ... */ arg1 = PTdifferentiate(p->left, varnum); arg2 = PTdifferentiate(p->right, varnum); newp = mkb(PT_TIMES, mkf(PTF_EXP, mkb(PT_TIMES, p->right, mkf(PTF_LN, p->left))), mkb(PT_PLUS, mkb(PT_TIMES, p->right, mkb(PT_DIVIDE, arg1, p->left)), mkb(PT_TIMES, arg2, mkf(PTF_LN, arg1)))); } break; case PT_FUNCTION: /* Many cases. Set arg1 to the derivative of the function, * and arg2 to the derivative of the argument. */ switch (p->funcnum) { case PTF_ABS: /* sgn(u) */ arg1 = mkf(PTF_SGN, p->left, 0); break; case PTF_SGN: arg1 = mkcon((double) 0.0); break; case PTF_ACOS: /* - 1 / sqrt(1 - u^2) */ arg1 = mkb(PT_DIVIDE, mkcon((double)-1), mkf(PTF_SQRT, mkb(PT_MINUS, mkcon((double)1), mkb(PT_POWER, p->left, mkcon((double)2))))); break; case PTF_ACOSH: /* 1 / sqrt(u^2 - 1) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkf(PTF_SQRT, mkb(PT_MINUS, mkb(PT_POWER, p->left, mkcon((double)2)), mkcon((double)1)))); break; case PTF_ASIN: /* 1 / sqrt(1 - u^2) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkf(PTF_SQRT, mkb(PT_MINUS, mkcon((double)1), mkb(PT_POWER, p->left, mkcon((double)2))))); break; case PTF_ASINH: /* 1 / sqrt(u^2 + 1) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkf(PTF_SQRT, mkb(PT_PLUS, mkb(PT_POWER, p->left, mkcon((double)2)), mkcon((double)1)))); break; case PTF_ATAN: /* 1 / (1 + u^2) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkb(PT_PLUS, mkb(PT_POWER, p->left, mkcon((double)2)), mkcon((double)1))); break; case PTF_ATANH: /* 1 / (1 - u^2) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkb(PT_MINUS, mkcon((double)1), mkb(PT_POWER, p->left, mkcon((double)2)))); break; case PTF_COS: /* - sin(u) */ arg1 = mkf(PTF_UMINUS, mkf(PTF_SIN, p->left)); break; case PTF_COSH: /* sinh(u) */ arg1 = mkf(PTF_SINH, p->left); break; case PTF_EXP: /* exp(u) */ arg1 = mkf(PTF_EXP, p->left, 0); break; case PTF_LN: /* 1 / u */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), p->left); break; case PTF_LOG: /* log(e) / u */ arg1 = mkb(PT_DIVIDE, mkcon((double)M_LOG10E), p->left); break; case PTF_SIN: /* cos(u) */ arg1 = mkf(PTF_COS, p->left); break; case PTF_SINH: /* cosh(u) */ arg1 = mkf(PTF_COSH, p->left); break; case PTF_SQRT: /* 1 / (2 * sqrt(u)) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkb(PT_TIMES, mkcon((double)2), mkf(PTF_SQRT, p->left))); break; case PTF_TAN: /* 1 / (cos(u) ^ 2) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkb(PT_POWER, mkf(PTF_COS, p->left), mkcon((double)2))); break; case PTF_TANH: /* 1 / (cosh(u) ^ 2) */ arg1 = mkb(PT_DIVIDE, mkcon((double)1), mkb(PT_POWER, mkf(PTF_COSH, p->left), mkcon((double)2))); break; case PTF_USTEP: arg1 = mkcon((double) 0.0); break; case PTF_URAMP: arg1 = mkf(PTF_USTEP, p->left); break; case PTF_UMINUS: /* - 1 ; like a constant (was 0 !) */ arg1 = mkcon((double)-1.0); break; default: fprintf(stderr, "Internal Error: bad function # %d\n", p->funcnum); newp = NULL; break; } arg2 = PTdifferentiate(p->left, varnum); newp = mkb(PT_TIMES, arg1, arg2); break; default: fprintf(stderr, "Internal error: bad node type %d\n", p->type); newp = NULL; break; } /* printf("result is: "); printTree(newp); printf("\n"); */ return (newp); }
static INPparseNode *mkfnode(char *fname, INPparseNode * arg) { int i; INPparseNode *p; char buf[128], *name, *s; IFvalue temp; /* Make sure the case is ok. */ (void) strcpy(buf, fname); for (s = buf; *s; s++) if (isupper(*s)) *s = tolower(*s); p = (INPparseNode *) MALLOC(sizeof(INPparseNode)); if (!strcmp(buf, "v")) { name = MALLOC(128); if (arg->type == PT_PLACEHOLDER) { strcpy(name, arg->funcname); } else if (arg->type == PT_CONSTANT) { (void) sprintf(name, "%d", (int) arg->constant); } else if (arg->type != PT_COMMA) { fprintf(stderr, "Error: badly formed node voltage\n"); return (NULL); } if (arg->type == PT_COMMA) { /* Change v(a,b) into v(a) - v(b) */ p = mkb(PT_MINUS, mkfnode(fname, arg->left), mkfnode(fname, arg->right)); } else { /* printf("getting a node called '%s'\n", name); */ INPtermInsert(circuit, &name, tables, &(temp.nValue)); for (i = 0; i < numvalues; i++) if ((types[i] == IF_NODE) && (values[i].nValue == temp.nValue)) break; if (i == numvalues) { if (numvalues) { values = (IFvalue *) REALLOC((char *) values, (numvalues + 1) * sizeof(IFvalue)); types = (int *) REALLOC((char *) types, (numvalues + 1) * sizeof(int)); } else { values = (IFvalue *) MALLOC(sizeof(IFvalue)); types = (int *) MALLOC(sizeof(int)); } values[i] = temp; types[i] = IF_NODE; numvalues++; } p->valueIndex = i; p->type = PT_VAR; } } else if (!strcmp(buf, "i")) { name = MALLOC(128); if (arg->type == PT_PLACEHOLDER) strcpy(name, arg->funcname); else if (arg->type == PT_CONSTANT) (void) sprintf(name, "%d", (int) arg->constant); else { fprintf(stderr, "Error: badly formed branch current\n"); return (NULL); } /* printf("getting a device called '%s'\n", name); */ INPinsert(&name, tables); for (i = 0; i < numvalues; i++) if ((types[i] == IF_INSTANCE) && (values[i].uValue == temp.uValue)) break; if (i == numvalues) { if (numvalues) { values = (IFvalue *) REALLOC((char *) values, (numvalues + 1) * sizeof(IFvalue)); types = (int *) REALLOC((char *) types, (numvalues + 1) * sizeof(int)); } else { values = (IFvalue *) MALLOC(sizeof(IFvalue)); types = (int *) MALLOC(sizeof(int)); } values[i].uValue = (IFuid) name; types[i] = IF_INSTANCE; numvalues++; } p->valueIndex = i; p->type = PT_VAR; } else { for (i = 0; i < NUM_FUNCS; i++) if (!strcmp(funcs[i].name, buf)) break; if (i == NUM_FUNCS) { fprintf(stderr, "Error: no such function '%s'\n", buf); return (NULL); } p->type = PT_FUNCTION; p->left = arg; p->funcname = funcs[i].name; p->funcnum = funcs[i].number; p->function = funcs[i].funcptr; } return (p); }