// OOP344 Week2 Environmental Variable Search and Display -- Peter Huang
// Program finds a user entered environmental variable (case insensitive). It displays
// the variable and the value/path of the data.
#include <iostream>
#include <cstring>
using namespace std;
// #define DEBUG
#define usrMAX strlen(argv[1])
#define arrMAX strlen(env[arr])
int main(int argc, char* argv[], char* env[])
{
int i;
int j;
int arr;
int pos;
int flag = 0;
char usrSearch[usrMAX+1];
strcpy(usrSearch,argv[1]); // copy search string to user search array
char searchEnv[usrMAX+1];
for(i = 0; env[i] && flag == 0; i++)
{
if(flag == 0) // loops and passes environmental variable string element, only to the length of user search array
{
for(j = 0; j < usrMAX; j++)
searchEnv[j] = env[i][j];
searchEnv[j] = '\0';
if(stricmp(usrSearch,searchEnv) == 0) // index the env array and pos when user search and environmental name match (case insensitive)
{
pos = j;
arr = i;
flag = 1;
}
}
}
if(flag == 1) // when match, finds which environmental index and the last pos (the actual data/path) and passes into a value array to output.
{
char searchVal[arrMAX];
for(i = 0; i < arrMAX; i++)
{
if(env[arr][pos+i] != '\0')
searchVal[i] = env[arr][pos+i];
}
searchVal[i] = '\0';
cout << searchEnv << searchVal << endl;
}
else
cout << "Not found" << endl;
#ifdef DEBUG // fardad's code for listing environmental variables
for(i=0;env[i] != 0;i++){
cout<<i<<": "<<env[i]<<endl;
}
#endif
return 0;
}
Sunday, January 20, 2013
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment