Web Images Videos Maps News Groups Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Mike releases the "home" command under the GPL

View parsed - Show only message text

Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!newsread1.news.atl.earthlink.net.POSTED!00d81f24!not-for-mail
Newsgroups: comp.os.linux.misc,comp.os.linux.advocacy
From: Tim Smith <reply_in_gr...@mouse-potato.com>
Subject: Re: Mike releases the "home" command under the GPL
References: <3d6111f1.0401241749.22dc5bbd@posting.google.com>
Organization: Institute of Lawsonomy, Department of Suction and Pressure
User-Agent: slrn/0.9.7.4 (OS/2 Warp 5 for ENIAC)
Lines: 67
Message-ID: <hDFQb.23622$i4.21389@newsread1.news.atl.earthlink.net>
Date: Sun, 25 Jan 2004 02:13:33 GMT
NNTP-Posting-Host: 65.40.220.119
X-Complaints-To: abuse@earthlink.net
X-Trace: newsread1.news.atl.earthlink.net 1074996813 65.40.220.119 (Sat, 24 Jan 2004 18:13:33 PST)
NNTP-Posting-Date: Sat, 24 Jan 2004 18:13:33 PST

In article <3d6111f1.0401241749.22dc5...@posting.google.com>, Mike Cox wrote:
> #include <iostream>
> 
> int main()
> {
> 	char* name= new char[strlen(getlogin()) + 1];
> 	strcpy(name,getlogin());

Why call getlogin twice?

    char * login_name = getlogin();
    int name_len = strlen(login_name);
    char * name = new char[name_len+1];
    strcpy(name,login_name);

or better yet, use std::string:

    std::string name(getlogin());

> 	if(strcmp(name, "root") == 0)
> 	{
> 		std::cout<<"going to home directory...\n";
> 		FILE* pipe;
> 	       	pipe =	popen("cd /root/", "w");	
> 		pclose(pipe);
> 	} else{
> 		char* base = "/home/";
> 		strcat(base, name);

String constants like "/home/" aren't guaranteed to be in writable memory.
If it is, you've just trashed whatever followed it.  You want this:

                char * base = new char[name_len + 7];
                strcpy( base, "/home/" );
                strcat( base, name );

But wait...what is the point of the "name" variable?

                char * base = new char[name_len + 7];
                strcpy( base, "/home/" );
                strcat( base, login_name );

Or, doing this right and using std::string:

                std::string base = "/home/" + std::string(getlogin());

> 		FILE* bong;
> 		std::cout<<"going to home directory...\n";
> 		bong = popen(base,"w");

Oops.  You left out the "cd".  Change "/home/" in the earlier stuff to
"cd /home/".

> 		pclose(bong);
> 	}
> 	
> 	delete [] name;
> 	return 0;
> }

After you build this and it doesn't work, and you finally figure out why, you
will feel really dumb.  However, don't feel bad.  Dennis Ritchie made the
same mistake when he implemented the "cd" command way back on the earliest
Unix that had a shell and a filesystem with directories. :-)

-- 
--Tim Smith

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google