Newsgroups: comp.os.linux.misc, comp.os.linux.advocacy
From: Tim Smith <reply_in_gr...@mouse-potato.com>
Date: Sun, 25 Jan 2004 02:13:33 GMT
Local: Sun, Jan 25 2004 3:13 am
Subject: Re: Mike releases the "home" command under the GPL
In article <3d6111f1.0401241749.22dc5...@posting.google.com>, Mike Cox wrote: Why call getlogin twice? > #include <iostream> > int main() char * login_name = getlogin(); or better yet, use std::string: std::string name(getlogin()); > if(strcmp(name, "root") == 0) String constants like "/home/" aren't guaranteed to be in writable memory. > { > std::cout<<"going to home directory...\n"; > FILE* pipe; > pipe = popen("cd /root/", "w"); > pclose(pipe); > } else{ > char* base = "/home/"; > strcat(base, name); If it is, you've just trashed whatever followed it. You want this: char * base = new char[name_len + 7]; But wait...what is the point of the "name" variable? char * base = new char[name_len + 7]; Or, doing this right and using std::string: std::string base = "/home/" + std::string(getlogin()); > FILE* bong; Oops. You left out the "cd". Change "/home/" in the earlier stuff to > std::cout<<"going to home directory...\n"; > bong = popen(base,"w"); "cd /home/". > pclose(bong); After you build this and it doesn't work, and you finally figure out why, you > } > delete [] name; 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. :-) -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||