Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ownership and User name....

Status
Not open for further replies.

jstreich

Programmer
Apr 20, 2002
1,067
US
I have a program in the works, and I need to compare the user name as a string to a string entered into the computer. I've come up with a fairly sloppy way, and want to know if there is a function in the standard libraries, or a more elegant way?
 
Here is a way to do it

char username[20];
cin >> username; //getting the username
if ( !strcmpi("jstreich", command))
{
\\ do some code if the username is jstreich
}

so the strcmpi is the way to do it
I'm not sure what library you have to include, I thinkit's string.h or iostream.h
 
Use the following program...

#include <iostream.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char username[20];
cin >> username; //getting the username
if ( ! strcmp(username, &quot;jstreich&quot;))
{
\\ do some code if the username is jstreich
}
return EXIT_SUCCESS;
}
 
Errr... not, a username entered into the program, the Unix user. Say for instance there is a Unix user joe_blow ... And later you read in a series of strings, and you want to find out if his Unix logon name is the third or not...
What I was thinking of was this:
Code:
string users[4] == {jill,gorege,joe_blow,john}
system(&quot;who am i > user.temp&quot;);
ifstream fin;
fin.open(&quot;user.temp&quot;);
string me;
fin >> me;
fin.close();
if(me == users[2])
{
  //do stuff
}
else
{
  /do stuff different
}

But it seems clumbsy to require a call to en external program. Is there a standard (or not so standard) way of getting the current Unix user running the program?
Sorry, I was vauge before...
 
getlogin() used to get the current user name.

Use strcmp() function to compare two strings.

For more information, see the man pages of those two functions.
 
Why is everyone so big on the old C calls like strcmp()?
the overloaded == operator is much easier to type.... And it is just a wraped call to strcmp. Anywho, unistd, whoda' thunk. I suppose I should have guessed that what the function would be called, and knowen that it has a man page. I'm babling.

I did try man-ing a number of other words before my orignial post. In anycase, thanks.
 
Use this program .....

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>

int main()
{
string users[4] =
{&quot;jill&quot;,&quot;gorege&quot;,&quot;joe_blow&quot;,&quot;john&quot;};
string me;

// One way of finding the user name
// system(&quot;whoami > user.temp&quot;);
// ifstream fin;
// fin.open(&quot;user.temp&quot;);
// fin >> me;
// fin.close();

// Another way of finding the current user name
me = getlogin();
if(me == users[2])
{
// do stuff
}
else
{
// do stuff different
}
return EXIT_SUCCESS;
}

Your code was exactly correct ... The only mistake was, you have used &quot;who am i&quot; utility which will return more info about the current user. Instead you can use &quot;whoami&quot; utility which will return the name of the current user alone.

Sorry I am a C programmer ... that is why i have asked you to use &quot;strcmp()&quot; function.


 
You probably have solved this by now, but the following all get the user name I believe on solaris

getlogin // this is a posix function too
cuserid

anyway, copy this into a string as you suggest then compare it to the entered value. You probably don't need the following code but anyway this works on solaris and should work on everything:

string Act = getlogin();
string In;
cin >> In;

if (Act == In)
{
cout << &quot;Match&quot; << endl;
}
else
{
cout << &quot;NOPE&quot; << endl;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top