//Set a decimal variable void set_var_decimal(Variable *varArry, int arrySpot){ String value = (String)malloc(ARRAYLENGHT * sizeof(char)); int flag; //Check if memory allocated properly. mem_check(value); do{ //User interaction printf("%s", "\n\nGive the variables value (Decimal):"); fgets(value, ARRAYLENGHT, stdin); //Check for valid lenght flag = false; if (value[strlen(value) - 1] == '\n' && strlen(value) > 1) value[strcspn(value, "\n")] = 0; //Drop the \n from the input else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); if (strlen(value) > 1) clear_stdin(); //Clear stdin from extra input } //Check if user entered a decimal if (!isdecimal(value) || flag) printf("%s", "\n\nThe value you gave is not a decimal!\n\n"); } while (!isdecimal(value)); //Copy the decimal to our array and free the buffer strcpy(varArry[arrySpot].value, value); free(value); }
static int read_digits(const char **s, int strict, char **b) { int us = 1; if (!isdecimal(**s)) return 0; while (isdecimal(**s) || **s == '_') { if (**s == '_') { if (strict) { if (us) return 0; } us = 1; } else { **b = **s; (*b)++; us = 0; } (*s)++; } if (us) do { (*s)--; } while (**s == '_'); return 1; }
/* * Read a number with a possible multiplier. * Returns -1 if the number format is illegal. */ static long getnum(const char *cp) { long value; if (!isdecimal(*cp)) return -1; value = 0; while (isdecimal(*cp)) value = value * 10 + *cp++ - '0'; switch (*cp++) { case 'k': value *= 1024; break; case 'b': value *= 512; break; case 'w': value *= 2; break; case '\0': return value; default: return -1; } if (*cp) return -1; return value; }
/*********************************************************************** # # Routine : GeneralCmds_DoKill # # Purpose : Kill command. # # Parameter: INPUT aPtr Application structure pointer # INPUT argc Argument count # INPUT argv Vector of pointers to arguments # # Returns : Nothing # #***********************************************************************/ void GeneralCmds_DoKill(GeneralCmds_TheApplicationTypePtrType aPtr, int argc, const char **argv) { const char *cp; int sig; int pid; sig = SIGTERM; if (argv[1][0] == '-') { cp = &argv[1][1]; if (strcmp(cp, "HUP") == 0) sig = SIGHUP; else if (strcmp(cp, "INT") == 0) sig = SIGINT; else if (strcmp(cp, "QUIT") == 0) sig = SIGQUIT; else if (strcmp(cp, "KILL") == 0) sig = SIGKILL; else { sig = 0; while (isdecimal(*cp)) sig = sig * 10 + *cp++ - '0'; if (*cp) { GeneralCmds_ShowUser(aPtr, "Unknown signal\n"); return; } } argc--; argv++; } while (argc-- > 1) { cp = *++argv; pid = 0; while (isdecimal(*cp)) pid = pid * 10 + *cp++ - '0'; if (*cp) { GeneralCmds_ShowUser(aPtr, "Non-numeric pid\n"); return; } if (kill(pid, sig) < 0) perror(*argv); } }
int main(int argc, char **argv) { char *cp; int uid; struct passwd *pwd; struct stat statbuf; if (argc < 3) goto usage; cp = argv[1]; if (isdecimal(*cp)) { uid = 0; while (isdecimal(*cp)) uid = uid * 10 + (*cp++ - '0'); if (*cp) { fprintf(stderr, "Bad uid value\n"); goto usage; } } else { pwd = getpwnam(cp); if (pwd == NULL) { fprintf(stderr, "Unknown user name\n"); goto usage; } uid = pwd->pw_uid; } argc--; argv++; while (argc-- > 1) { argv++; if ((stat(*argv, &statbuf) < 0) || (chown(*argv, uid, statbuf.st_gid) < 0)) perror(*argv); } exit(0); usage: fprintf(stderr, "usage: %s new_owner file1 [file2] ...\n", argv[0]); exit(1); }
static int read_comp(const char **s, int strict, VALUE *ret, char **b) { char *bb; int sign; VALUE num, num2; bb = *b; sign = read_sign(s, b); if (isimagunit(**s)) { (*s)++; num = INT2FIX((sign == '-') ? -1 : + 1); *ret = rb_complex_new2(ZERO, num); return 1; /* e.g. "i" */ } if (!read_rat_nos(s, strict, b)) { **b = '\0'; num = str2num(bb); *ret = rb_complex_new2(num, ZERO); return 0; /* e.g. "-" */ } **b = '\0'; num = str2num(bb); if (isimagunit(**s)) { (*s)++; *ret = rb_complex_new2(ZERO, num); return 1; /* e.g. "3i" */ } if (**s == '@') { int st; (*s)++; bb = *b; st = read_rat(s, strict, b); **b = '\0'; if (strlen(bb) < 1 || !isdecimal(*(bb + strlen(bb) - 1))) { *ret = rb_complex_new2(num, ZERO); return 0; /* e.g. "1@-" */ } num2 = str2num(bb); *ret = rb_complex_polar(num, num2); if (!st) return 0; /* e.g. "1@2." */ else return 1; /* e.g. "1@2" */ } if (issign(**s)) { bb = *b; sign = read_sign(s, b); if (isimagunit(**s)) num2 = INT2FIX((sign == '-') ? -1 : + 1); else { if (!read_rat_nos(s, strict, b)) { *ret = rb_complex_new2(num, ZERO); return 0; /* e.g. "1+xi" */ } **b = '\0'; num2 = str2num(bb); } if (!isimagunit(**s)) { *ret = rb_complex_new2(num, ZERO); return 0; /* e.g. "1+3x" */ } (*s)++; *ret = rb_complex_new2(num, num2); return 1; /* e.g. "1+2i" */ } /* !(@, - or +) */ { *ret = rb_complex_new2(num, ZERO); return 1; /* e.g. "3" */ } }
// compilation d'un prototype // [name] -> 0 int Compiler::parseproto() { int k; if (!parser->next(0)) { PRINTF(m)(LOG_COMPILER,"Compiler : integer or '=' expected (found EOF)\n"); return MTLERR_SN; } int nbarg=-1; if (!strcmp(parser->token,"=")) { if (k=creategraph(parser,PNTTOVAL(newpackage),0)) return k; int vp=STACKGET(m,0); if (vp!=NIL) { int* p=VALTOPNT(vp); if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_FUN)) { vp=TABGET(p,TYPEHEADER_LENGTH); if (vp!=NIL) { int* p=VALTOPNT(vp); if (TABGET(p,TYPEHEADER_CODE)==INTTOVAL(TYPENAME_TUPLE)) { nbarg=TABLEN(p)-TYPEHEADER_LENGTH; } } } } if (nbarg<0) { PRINTF(m)(LOG_COMPILER,"Compiler : function type expected\n"); return MTLERR_SN; } } else if (isdecimal(parser->token)) { nbarg=mtl_atoi(parser->token); if (k=createnodetype(TYPENAME_FUN)) return k; int i;for(i=0;i<nbarg;i++) if (k=createnodetype(TYPENAME_WEAK)) return k; if (k=createnodetuple(nbarg)) return k; TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH,STACKGET(m,0)); // attachement du noeud tuple au noeud fun STACKDROP(m); if (k=createnodetype(TYPENAME_WEAK)) return k; // noeud résultat TABSET(m,VALTOPNT(STACKGET(m,1)),TYPEHEADER_LENGTH+1,STACKGET(m,0)); // attachement du noeud resultat au noeud fun STACKDROP(m); } if (nbarg<0) { PRINTF(m)(LOG_COMPILER,"Compiler : integer or '=' expected (found '%s')\n",parser->token); return MTLERR_SN; } if (k=parser->parsekeyword(";;")) return k; // on crée le bloc fonction newref=MALLOCCLEAR(m,REF_LENGTH); if (!newref) return MTLERR_OM; TABSET(m,newref,REF_TYPE,STACKPULL(m)); TABSET(m,newref,REF_NAME,STACKPULL(m)); TABSET(m,newref,REF_CODE,INTTOVAL(nbarg)); TABSET(m,newref,REF_PACKAGE,INTTOVAL(ifuns++)); if (k=STACKPUSH(m,PNTTOVAL(newref))) return MTLERR_OM; // [newref] addreftopackage(newref,newpackage); STACKDROP(m); outputbuf->reinit(); outputbuf->printf("Compiler : proto %s : ",STRSTART(VALTOPNT(TABGET(newref,REF_NAME)))); echograph(outputbuf,VALTOPNT(TABGET(newref,REF_TYPE))); PRINTF(m)(LOG_COMPILER,"%s\n",outputbuf->getstart()); return 0; }
EPNODE * getE5(void) /* E5 -> (E1) */ /* VAR */ /* NUM */ /* $N */ /* FUNC(E1,..) */ /* ARG */ { int i; char *nam; EPNODE *ep1, *ep2; if (nextc == '(') { scan(); ep1 = getE1(); if (nextc != ')') syntax("')' expected"); scan(); return(ep1); } if (esupport&E_INCHAN && nextc == '$') { scan(); ep1 = newnode(); ep1->type = CHAN; ep1->v.chan = getinum(); return(ep1); } if (esupport&(E_VARIABLE|E_FUNCTION) && (isalpha(nextc) || nextc == CNTXMARK)) { nam = getname(); ep1 = NULL; if ((esupport&(E_VARIABLE|E_FUNCTION)) == (E_VARIABLE|E_FUNCTION) && curfunc != NULL) for (i = 1, ep2 = curfunc->v.kid->sibling; ep2 != NULL; i++, ep2 = ep2->sibling) if (!strcmp(ep2->v.name, nam)) { ep1 = newnode(); ep1->type = ARG; ep1->v.chan = i; break; } if (ep1 == NULL) { ep1 = newnode(); ep1->type = VAR; ep1->v.ln = varinsert(nam); } if (esupport&E_FUNCTION && nextc == '(') { ep2 = newnode(); ep2->type = FUNC; addekid(ep2, ep1); ep1 = ep2; do { scan(); addekid(ep1, getE1()); } while (nextc == ','); if (nextc != ')') syntax("')' expected"); scan(); } else if (!(esupport&E_VARIABLE)) syntax("'(' expected"); if (esupport&E_RCONST && isconstvar(ep1)) ep1 = rconst(ep1); return(ep1); } if (isdecimal(nextc)) { ep1 = newnode(); ep1->type = NUM; ep1->v.num = getnum(); return(ep1); } syntax("unexpected character"); return NULL; /* pro forma return */ }