// Number.cmm CEnvi program to find the first number in a program's output #define DEFAULT_INDEX 1 main(ArgCount,ArgStrings) { Index = DEFAULT_INDEX if ( ArgCount != 1 && ( ArgCount != 2 || 0 == (Index = atoi(ArgStrings[1])) ) ) Instructions() else { HowManyFound = 0; // this will keep a list of how many numbers found so far while ( (input = gets()) ) { // get the next number in this line while ( (input = strpbrk(input,"-0123456789")) ) { if ( '-' == input[0] && !isdigit(input[1]) ) { // this was a negative sign, but no numbers follow input++ } else { // convert these digits to a number NumberList[HowManyFound] = atol(input) if ( ++HowManyFound == Index ) return(NUMBER = NumberList[HowManyFound-1]) // skip past all the digits text input += 1 + strspn(input+1,"0123456789") } } } if ( Index < 0 ) { // if enough numbers have been found, then find this offset from the end if ( -Index <= HowManyFound ) { return(NUMBER = NumberList[HowManyFound+Index]) } } } // if did not return earlier, then number was not found undefine(NUMBER) return(0) } Instructions() { printf("\a\n") printf("Find#.cmm - Read input for numbers. Set the NUMBER environment variable to the\n") printf(" number found and return ERRORLEVEL to number found. 0 if not found.\n") printf("\n") printf("USAGE: CEnvi Find#.cmm [Index]\n") printf("\n") printf("Where Index tells which found number to return, where 1 is the first number\n") printf("found, 2 is the seconds number, and so on; -1 is the last number found, -2 is\n") printf("the second to last number, and so on. If Index is not supplied then the\n") printf("default is 1. If the number is not found then NUMBER is cleared.\n") printf("\n") printf("Example: To find how many bytes are used the current directory:\n") printf(" dir | cenvi Find#.cmm -2\n") printf("\n") }