Newsgroups: comp.os.linux.misc, comp.os.linux.advocacy
Von: Tim Smith <reply_in_gr...@mouse-potato.com>
Datum: Sun, 25 Jan 2004 02:13:33 GMT
Lokal: So 25 Jan. 2004 03:13
Betreff: 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. :-) -- Sie müssen sich anmelden, bevor Sie Nachrichten veröffentlichen können.
Bevor Sie eine Nachricht posten können, müssen Sie zunächst dieser Gruppe beitreten.
Bitte aktualisieren Sie vor dem Posten in den Abonnementeinstellungen Ihren Spitznamen.
Sie haben nicht die erforderliche Berechtigung zum Posten.
| ||||||||||||||