/** *Set the first name * *Input: * info is a pointer to person's info * name contains the first name *Return: * 0 to indicate failure * 1 to indicate success */ int setFirstName(PersonInfo *info, char *name) { if (name == NULL ) // Check for empty string { printf("No first name detected !"); return 0; } else { int i = 0; while( name[i] != '\0' ) // Count for the total number of characters in the string until end of the string i++ ; if ( i > 20 ) // Check if the name has more than 20 characters, it will return 0 { printf("Insufficient space to allocate your lengthy first name.I'm sorry !"); return 0; } int j = 0 ; for (j = 0 ; name[j] != '\0' ; ) //Loop throughout the string { if ( isalpha(name[j]) != 0 ) // If the current character is an alphabet, isalpha will return a value not equal to 0 j++; // and increment the counter to check for next character else if ( isblank(name[j]) != 0 ) // Check for space in first name , isblank return not zero when true { if ( j == 0 ) // To check for the starting character of the name cannot be blank or space { printf("You can't have your first name starting with a blank"); return 0; } else j++; } else if ( isdigit(name[j]) != 0 ) //Check for presence of digits, isdigit will also return non zero value if true { printf("You have number/numbers in your first name @_@ ???"); return 0; } else //If all the above condition aren't true, it will be characters liek @,_|\ and more.... { printf("Invalid character found in your first name!"); return 0; } } printf("Hello %s ",name); // Just to print out the first name return 1; // Pass all the test and return 1 for success } }
/* * this must be thread safe . no global, no static. * return value next symbol : success * S_NULL : end * S_ERROR : error * forereading is not necessary , so do not do it. */ static int get_sym(struct c_desc *cd) { char *p; int len; int val; /* skip white space */ while(*cd->cur_p == ' ') cd->cur_p++; if(*cd->cur_p == '$') { /* $Foobar */ p = cd->cur_p + 1; while(isalpha(*p)) p++; len = p - cd->cur_p - 1; if(cd->id_len < len + 1) { cd->id = (char *)xrealloc(cd->id,len + 1); cd->id_len = len + 1; } memcpy(cd->id,cd->cur_p + 1,len); cd->id[len] = '\0'; cd->cur_p = p; cd->op = S_ID; } else if(isdigit(*cd->cur_p)) { val = 0; while(isdigit(*cd->cur_p)) { /* always decimal */ val *= 10; val += *cd->cur_p - '0'; cd->cur_p++; } cd->op = S_NUM; cd->val = val; } else if(*cd->cur_p == '<') { cd->cur_p++; if(*cd->cur_p == '=') { cd->cur_p++; cd->op = S_LESSEQ; } else { cd->op = S_LESS; } } else if(*cd->cur_p == '>') { cd->cur_p++; if(*cd->cur_p == '=') { cd->cur_p++; cd->op = S_MOREEQ; } else { cd->op = S_MORE; } } else if(*cd->cur_p == '&') { cd->cur_p++; if(*cd->cur_p == '&') { cd->cur_p++; cd->op = S_AND; } else { cd->op = S_ERROR; /* & operator is not supported */ } } else if(*cd->cur_p == '|') { cd->cur_p++; if(*cd->cur_p == '|') { cd->cur_p++; cd->op = S_OR; } else { cd->op = S_ERROR; /* | operator is not supported */ } } else if(*cd->cur_p == '=') { cd->cur_p++; if(*cd->cur_p == '=') { cd->cur_p++; cd->op = S_EQ; } else { cd->op = S_SUBSTI; } } else if(*cd->cur_p == '(') { cd->cur_p++; cd->op = S_LPAREN; } else if(*cd->cur_p == ')') { cd->cur_p++; cd->op = S_RPAREN; } else if(*cd->cur_p == ',' || *cd->cur_p == ';') { /*end of condition ',' */ cd->op = S_NULL; /*END*/ } else { display(MSDL_ERR,"asmrule: syntax error\n"); cd->op = S_ERROR; } return cd->op; }
static int32_t gettoken(struct snmp_toolinfo *snmptoolctx) { int c; struct enum_type *t; if (saved_token != -1) { c = saved_token; saved_token = -1; return (c); } again: /* * Skip any whitespace before the next token. */ while ((c = tgetc()) != EOF) { if (c == '\n') input->lno++; if (!isspace(c)) break; } if (c == EOF) return (TOK_EOF); if (!isascii(c)) { warnx("unexpected character %#2x", (u_int) c); return (TOK_ERR); } /* * Skip comments. */ if (c == '#') { while ((c = tgetc()) != EOF) { if (c == '\n') { input->lno++; goto again; } } warnx("unexpected EOF in comment"); return (TOK_ERR); } /* * Single character tokens. */ if (strchr("():|", c) != NULL) return (c); if (c == '"' || c == '<') { int32_t end = c; size_t n = 0; val = 1; if (c == '<') { val = 0; end = '>'; } while ((c = tgetc()) != EOF) { if (c == end) break; if (n == sizeof(nexttok) - 1) { nexttok[n++] = '\0'; warnx("filename too long '%s...'", nexttok); return (TOK_ERR); } nexttok[n++] = c; } nexttok[n++] = '\0'; return (TOK_FILENAME); } /* * Sort out numbers. */ if (isdigit(c)) { size_t n = 0; nexttok[n++] = c; while ((c = tgetc()) != EOF) { if (!isdigit(c)) { if (tungetc(c) < 0) return (TOK_ERR); break; } if (n == sizeof(nexttok) - 1) { nexttok[n++] = '\0'; warnx("number too long '%s...'", nexttok); return (TOK_ERR); } nexttok[n++] = c; } nexttok[n++] = '\0'; sscanf(nexttok, "%lu", &val); return (TOK_NUM); } /* * So that has to be a string. */ if (isalpha(c) || c == '_' || c == '-') { size_t n = 0; nexttok[n++] = c; while ((c = tgetc()) != EOF) { if (!isalnum(c) && c != '_' && c != '-') { if (tungetc (c) < 0) return (TOK_ERR); break; } if (n == sizeof(nexttok) - 1) { nexttok[n++] = '\0'; warnx("string too long '%s...'", nexttok); return (TOK_ERR); } nexttok[n++] = c; } nexttok[n++] = '\0'; /* * Keywords. */ for (c = 0; keywords[c].str != NULL; c++) if (strcmp(keywords[c].str, nexttok) == 0) { val = keywords[c].val; return (keywords[c].tok); } if ((t = snmp_enumtc_lookup(snmptoolctx, nexttok)) != NULL) { val = t->syntax; return (TOK_DEFTYPE); } return (TOK_STR); } if (isprint(c)) warnx("%u: unexpected character '%c'", input->lno, c); else warnx("%u: unexpected character 0x%02x", input->lno, (u_int) c); return (TOK_ERR); }
static char *get_protocol_scheme(char const **s, unsigned int *err_out) { *err_out = UPARSE_ERROR; if (NULL == *s) { fprintf(stderr,"input s is null\n"); return NULL; } // Local copy we can advance. char const *c = *s; // Choose a sensible limit for a scheme. size_t const max_scheme_len = 16; char scheme[max_scheme_len+1]; memset(&scheme[0], 0, sizeof(scheme)); size_t j = 0; bool seen_prefix = false; // Copy alpha characters preceeding the ':' while (*c) { if (SCHEME_DELIM_PREFIX == c[0]) { seen_prefix = true; // Advance past SCHEME_DELIM_PREFIX, c should be pointing at a SCHEME_SLASH now. c++; break; } else if (!isalpha(*c)) { fprintf(stderr,"'%c' is invalid\n",*c); return NULL; } else if (max_scheme_len == j) { fprintf(stderr,"scheme exceeds max scheme len %lu\n",max_scheme_len); return NULL; } else { scheme[j] = c[0]; } c++; j++; } if (!seen_prefix) { fprintf(stderr,"no scheme delimiter prefix '%c' found\n",SCHEME_DELIM_PREFIX); return NULL; } // No scheme was found. if (0 == j) { fprintf(stderr,"no scheme was found\n"); return NULL; } // Look for the slashes after SCHEME_DELIM_PREFIX. bool seen_slash = false; while (*c) { if (SCHEME_SLASH == c[0]) { seen_slash = true; c++; } else { break; } } if (!seen_slash) { fprintf(stderr,"no scheme slash '%c' found\n",SCHEME_SLASH); return NULL; } // Advance pointer past all of the scheme chars and delims. *s = c; scheme[j] = '\0'; *err_out = NO_UPARSE_ERROR; return strdup(scheme); }
token_t akl_lex(struct akl_io_device *dev) { int ch; /* We should take care of the '+', '++', and etc. style functions. Moreover the positive and negative numbers must also work: '(++ +5)' should be valid. */ char op = 0; if (!buffer) init_buffer(); assert(dev); while ((ch = akl_io_getc(dev))) { /* We should avoid the interpretation of the Unix shebang */ if (dev->iod_char_count == 1 && ch == '#') { while ((ch = akl_io_getc(dev)) && ch != '\n') ; } if (ch == EOF) { return tEOF; } else if (ch == '\n') { dev->iod_line_count++; dev->iod_char_count = 0; } else if (ch == '+' || ch == '-') { if (op != 0) { if (op == '+') strcpy(buffer, "++"); else strcpy(buffer, "--"); op = 0; return tATOM; } op = ch; } else if (isdigit(ch)) { akl_io_ungetc(ch, dev); copy_number(dev, op); op = 0; return tNUMBER; } else if (ch == ' ' || ch == '\n') { dev->iod_column = dev->iod_char_count+1; if (op != 0) { if (op == '+') strcpy(buffer, "+"); else strcpy(buffer, "-"); op = 0; return tATOM; } else { continue; } } else if (ch == '"') { ch = akl_io_getc(dev); if (ch == '"') return tNIL; akl_io_ungetc(ch, dev); copy_string(dev); return tSTRING; } else if (ch == '(') { dev->iod_column = dev->iod_char_count+1; ch = akl_io_getc(dev); if (ch == ')') return tNIL; akl_io_ungetc(ch, dev); return tLBRACE; } else if (ch == ')') { return tRBRACE; } else if (ch == '\'' || ch == ':') { return tQUOTE; } else if (ch == ';') { while ((ch = akl_io_getc(dev)) != '\n') { if (akl_io_eof(dev)) return tEOF; } } else if (isalpha(ch) || ispunct(ch)) { akl_io_ungetc(ch, dev); copy_atom(dev); if ((strcasecmp(buffer, "NIL")) == 0) return tNIL; else if ((strcasecmp(buffer, "T")) == 0) return tTRUE; else return tATOM; } else { continue; } } return tEOF; }
static void opt(struct tbl_node *tbl, int ln, const char *p, int *pos) { int i, sv; char buf[KEY_MAXNAME]; /* * Parse individual options from the stream as surrounded by * this goto. Each pass through the routine parses out a single * option and registers it. Option arguments are processed in * the arg() function. */ again: /* * EBNF describing this section: * * options ::= option_list [:space:]* [;][\n] * option_list ::= option option_tail * option_tail ::= [:space:]+ option_list | * ::= epsilon * option ::= [:alpha:]+ args * args ::= [:space:]* [(] [:alpha:]+ [)] */ while (isspace((unsigned char)p[*pos])) (*pos)++; /* Safe exit point. */ if (';' == p[*pos]) return; /* Copy up to first non-alpha character. */ for (sv = *pos, i = 0; i < KEY_MAXNAME; i++, (*pos)++) { buf[i] = (char)tolower((unsigned char)p[*pos]); if ( ! isalpha((unsigned char)buf[i])) break; } /* Exit if buffer is empty (or overrun). */ if (KEY_MAXNAME == i || 0 == i) { mandoc_msg(MANDOCERR_TBL, tbl->parse, ln, *pos, NULL); return; } buf[i] = '\0'; while (isspace((unsigned char)p[*pos])) (*pos)++; /* * Look through all of the available keys to find one that * matches the input. FIXME: hashtable this. */ for (i = 0; i < KEY_MAXKEYS; i++) { if (strcmp(buf, keys[i].name)) continue; /* * Note: this is more difficult to recover from, as we * can be anywhere in the option sequence and it's * harder to jump to the next. Meanwhile, just bail out * of the sequence altogether. */ if (keys[i].key) tbl->opts.opts |= keys[i].key; else if ( ! arg(tbl, ln, p, pos, keys[i].ident)) return; break; } /* * Allow us to recover from bad options by continuing to another * parse sequence. */ if (KEY_MAXKEYS == i) mandoc_msg(MANDOCERR_TBLOPT, tbl->parse, ln, sv, NULL); goto again; /* NOTREACHED */ }
char *TKGetNextToken( TokenizerT * tk ) { // Begin TKGetNextToken /** * Check that the string is not empty. */ if(tk->str == '\0') { // Begin if-statement return '\0'; } // End if-statement /* * calloc(tk->strSize-tk->curChar, sizeof(char)); * This will assign for the first token the amount of space * required to contain the entire string in the case that the first token * the whole input is one token. The next time the function is used it will * allocate the size of the string minus the amount of characters already * surpassed. */ char *arr = calloc(tk->strSize-tk->curChar, sizeof(char)); int i = 0; // Current index in arr[i] while ((char)tk->str[tk->curChar] != '\0') { // Begin while-loop /** * This if-statement will check if tk->str[tk->curChar] is * a whitespace character. If a whitespace character is found * the current character which contains the whitespace * will be incremented over (e.g. tk->curChar++) so that the * next time the method is called it will start after the * whitespace character. */ if((char)tk->str[tk->curChar] == ' ') { // Begin if-statement tk->curChar++; if((char)tk->str[tk->curChar] == '\0') { arr[i] = ' '; break; } } // End if-statement else if(isalpha(tk->str[tk->curChar])) { // Begin if-statement arr[i] = tk->str[tk->curChar]; // Store current str[index] in arr[i] i++; tk->curChar++; /** * Continue tokenizing word while next character is an alphanumeric * character. */ while(isalnum(tk->str[tk->curChar]) && (char)tk->str[tk->curChar] != '\0') { // Begin while loop arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; } // tk->tokenDesc[tk->manyTokens] = "word"; break; } else if(isdigit(tk->str[tk->curChar])) { arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; while(isdigit(tk->str[tk->curChar]) && (char)tk->str[tk->curChar] != '\0') { arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; } if((char)tk->str[tk->curChar] == 'L') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "long decimal number"; tk->curChar++; break; } else if((char)tk->str[tk->curChar] == '.') { tk->tokenDesc[tk->manyTokens] = "float"; arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; while(isdigit(tk->str[tk->curChar])) { arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; if((char)tk->str[tk->curChar] == 'e') { arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; if((char)tk->str[tk->curChar] == '-' || (char)tk->str[tk->curChar] == '+') { arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; if(!isdigit(tk->str[tk->curChar])) { tk->tokenDesc[tk->manyTokens] = "improper float"; break; } while(isdigit(tk->str[tk->curChar])) { arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; } tk->tokenDesc[tk->manyTokens] = "scientific float"; break; } else { tk->tokenDesc[tk->manyTokens] = "improper float"; break; } } } break; } tk->tokenDesc[tk->manyTokens] = "decimal number"; tk->curChar++; break; } else if((char)tk->str[tk->curChar] == '[') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "left brace"; tk->curChar++; break; } else if((char)tk->str[tk->curChar] == ']') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "right brace"; tk->curChar++; break; } else if((char)tk->str[tk->curChar] == '(') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "left parentheses"; tk->curChar++; break; } else if((char)tk->str[tk->curChar] == ')') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "right parentheses"; tk->curChar++; break; } else if((char)tk->str[tk->curChar] == '+') { arr[i] = tk->str[tk->curChar]; i++; tk->tokenDesc[tk->manyTokens] = "plus"; tk->curChar++; if((char)tk->str[tk->curChar] == '=') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "plusequals"; tk->curChar++; } else if((char)tk->str[tk->curChar] == '+') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "increment"; tk->curChar++; } break; } else if((char)tk->str[tk->curChar] == '-') { arr[i] = tk->str[tk->curChar]; i++; tk->tokenDesc[tk->manyTokens] = "minus"; tk->curChar++; if((char)tk->str[tk->curChar] == '=') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "minusequals"; tk->curChar++; } else if((char)tk->str[tk->curChar] == '-') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "decrement"; tk->curChar++; } break; } else if((char)tk->str[tk->curChar] == '\%') { arr[i] = tk->str[tk->curChar]; i++; tk->curChar++; if((char)tk->str[tk->curChar] == 'd') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "signed decimal integer conversion character"; } else if((char)tk->str[tk->curChar] == 'u') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "unsigned decimal integer conversion character (used in printf only)"; } else if((char)tk->str[tk->curChar] == 'x') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "unsigned hexidecimal integer conversion character"; } else if((char)tk->str[tk->curChar] == 'h') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "unsigned short integer conversion character (used in scanf only)"; } else if((char)tk->str[tk->curChar] == 'o') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "unsigned octal integer conversion character"; } else if((char)tk->str[tk->curChar] == 'c') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "single character conversion character"; } else if((char)tk->str[tk->curChar] == 's') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "null terminated string conversion character"; } else if((char)tk->str[tk->curChar] == 'f') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "fixed point notation for float or double conversion character"; } else if((char)tk->str[tk->curChar] == 'e') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "scientific notation for float or double conversion character (printf only)"; } else if((char)tk->str[tk->curChar] == 'g') { arr[i] = tk->str[tk->curChar]; tk->tokenDesc[tk->manyTokens] = "use %e or %f, whichever is shorter conversion character (printf only)"; } tk->curChar++; break; } } // End while-loop return arr; } // End TKGetNextToken
int isalnum( int c ) { return isalpha( c ) | isdigit( c ); }
static int32_t valid_typing (char c) { /* Valid typing include letters, numbers, space, and backspace/delete. */ return (isalpha (c) || isdigit (c) || ' ' == c || 8 == c || 127 == c); }
static PRBool IsRootDirectory(char *fn, size_t buflen) { char *p; PRBool slashAdded = PR_FALSE; PRBool rv = PR_FALSE; if (_PR_IS_SLASH(fn[0]) && fn[1] == '\0') { return PR_TRUE; } if (isalpha(fn[0]) && fn[1] == ':' && _PR_IS_SLASH(fn[2]) && fn[3] == '\0') { rv = GetDriveType(fn) > 1 ? PR_TRUE : PR_FALSE; return rv; } /* The UNC root directory */ if (_PR_IS_SLASH(fn[0]) && _PR_IS_SLASH(fn[1])) { /* The 'server' part should have at least one character. */ p = &fn[2]; if (*p == '\0' || _PR_IS_SLASH(*p)) { return PR_FALSE; } /* look for the next slash */ do { p++; } while (*p != '\0' && !_PR_IS_SLASH(*p)); if (*p == '\0') { return PR_FALSE; } /* The 'share' part should have at least one character. */ p++; if (*p == '\0' || _PR_IS_SLASH(*p)) { return PR_FALSE; } /* look for the final slash */ do { p++; } while (*p != '\0' && !_PR_IS_SLASH(*p)); if (_PR_IS_SLASH(*p) && p[1] != '\0') { return PR_FALSE; } if (*p == '\0') { /* * GetDriveType() doesn't work correctly if the * path is of the form \\server\share, so we add * a final slash temporarily. */ if ((p + 1) < (fn + buflen)) { *p++ = '\\'; *p = '\0'; slashAdded = PR_TRUE; } else { return PR_FALSE; /* name too long */ } } rv = GetDriveType(fn) > 1 ? PR_TRUE : PR_FALSE; /* restore the 'fn' buffer */ if (slashAdded) { *--p = '\0'; } } return rv; }