Re: Review of FreeBSD 5.4
From: The Ghost In The Machine (ewill_at_sirius.athghost7038suus.net)
Date: 07/21/05
- Next message: michaelrmgreen_at_yahoo.co.uk: "wireless setup"
- Previous message: Steven G. Kargl: "Re: FBSD 4.11/5.4 install problems"
- In reply to: Roy Culley: "Re: Review of FreeBSD 5.4"
- Next in thread: Steve O'Hara-Smith: "Re: Review of FreeBSD 5.4"
- Reply: Steve O'Hara-Smith: "Re: Review of FreeBSD 5.4"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 21 Jul 2005 19:00:04 GMT
In comp.os.linux.advocacy, Roy Culley
<rgc@nodomain.none>
wrote
on Wed, 20 Jul 2005 01:15:39 +0200
<r5e0r2-bhc.ln1@dog.did.it>:
> begin risky.vbs
> <864qaqlcz7.fsf@yahoo.com>,
> Mike Cox <mikecoxlinux@yahoo.com> writes:
>>
>> Oops, I forgot to address this very important point in my previous
>> response to you. Of course being a BSD guy, you can't imagine
>> someone not test compiling a release before releasing it. What I'm
>> about to tell you will make you really look down on Linux. Linus
>> Torvalds often does NOT compile a kernel when it is released.
*sound of scissors being sharpened*
>
> Do you ever even compile your code far less test it before releasing
> it Cox? Seems not:
>
> || Newsgroups: comp.os.linux.misc, comp.os.linux.advocacy
> || From: mikecoxli...@yahoo.com (Mike Cox) - Find messages by this author
> || Date: 24 Jan 2004 17:49:42 -0800
> || Local: Sun,Jan 25 2004 2:49 am
> || Subject: Mike releases the "home" command under the GPL
> || /*
> || ******** home command ********
> || The 'home'command takes you to your home directory if
> || you've been wandering far, and don't want to type so
> || much. Just type "home" at the shell, and you're immediatly
> || transported to /home/yourusername/
> ||
> || (c) 2004 Mike Cox. Released under the GNU GPL License.
> || email: mikecoxli...@yahoo.com
> || web: www.geocities.com/mikecoxlinux /
> ||
> || compile instructions:
> || g++ -o home home.cpp
> ||
> || install instructions:
> || move it to the /bin directory.
[1] (suggestion) /usr/local/bin or /local/bin would be a more logical place.
[2] (error) Operation impossible; subprocess cannot change
current directory of current process. 'man chdir' for
details. Continuing anyway...
> ||
> || ..Or create a Makefile as follows:
> || home: home.cpp
> || g++ -o home home.cpp
[3] (error) Missing tab.
> || install:
> || mv home /bin/
[4] (warning) Consider using install.
> || clean:
[5] (error) Badshifted makefile target.
> || rm home
> || */
> ||
> || #include <iostream>
[6] (error) Missing #includes for popen et al, strcpy, strcat, getlogin.
> ||
> || int main()
[7] (warning) Missing parameters.
> || {
> || char* name= new char[strlen(getlogin()) + 1];
[8] (warning) C code within C++; consider replacing
with STL constructs such as std::string name = getlogin().
> || strcpy(name,getlogin());
[9] (inefficiency) getlogin() called twice.
> || if(strcmp(name, "root") == 0)
> || {
> || std::cout<<"going to home directory...\n";
[10] (possible inefficiency) Duplicate code.
> || FILE* pipe;
> || pipe = popen("cd /root/", "w");
> || pclose(pipe);
[11] (error) Meaningless operation.
[12] (error) "root" homedirectory not correctly computed;
suggest using getpwuid().
> || } else{
> || char* base = "/home/";
> || strcat(base, name);
[13] (error) User homedirectory not correctly computed; suggest using
getpwuid().
[14] (inefficiency) Duplicate code/codefork unnecessary.
> || FILE* bong;
> || std::cout<<"going to home directory...\n";
> || bong = popen(base,"w");
[15] fork()/popen()/system() on directory; misconstructed command;
code inconsistency with previous line.
[16] Meaningless operation.
> || pclose(bong);
> || }
> || delete [] name;
> || return 0;
> || }
So let's rewrite this -- ahem -- command properly. Not that it
would do all that much, though this one *will* work more
or less as specified.
It might even be useful... :-)
/**
* Command to execute another command in the user's home directory.
*
* Author: The Ghost In The Machine
* (c) 2005, The Ghost In The Machine
* Licensed under the GPL v2. For details see
* http://www.gnu.org/licenses .
*
* Usage: homeexec command arg arg ...
*
* Compilation instructions: g++ -o homeexec homeexec.c
*
*/
#include <iostream>
#include <unistd.h>
#include <pwd.h>
int main(int argc, char **argv)
{
uid_t uid = getuid();
struct passwd *upasswd;
/* Get the user's home directory, among other things. */
upasswd = getpwuid(uid);
if(upasswd == NULL) { /* [1] */
perror("getpwuid");
return 1;
}
/* Change the process working directory to the user's
* home directory.
*/
if(chdir(upasswd->pw_dir) < 0) {
perror(upasswd->pw_dir);
return 1;
}
/* This is quick and dirty; just execute the command
* specified on the user's home line. argv[0] is me,
* so it's discarded. The rest is passed verbatim.
*/
if(execvp(argv[1], argv+1) < 0) {
perror(argv[1]);
return 1;
}
/* unreachable */
return 0; /* so g++ doesn't complain */
}
Note that I use getuid() instead of geteuid(), to avoid
setuid security issues. The [1] indicates an interesting
conundrum in C/C++. C requires NULL. C++ can accept NULL
or 0.
It's also considerably simpler than Mike's effort. :-)
Of course it's bare-bones.
-- #191, ewill3@earthlink.net It's still legal to go .sigless.
- Next message: michaelrmgreen_at_yahoo.co.uk: "wireless setup"
- Previous message: Steven G. Kargl: "Re: FBSD 4.11/5.4 install problems"
- In reply to: Roy Culley: "Re: Review of FreeBSD 5.4"
- Next in thread: Steve O'Hara-Smith: "Re: Review of FreeBSD 5.4"
- Reply: Steve O'Hara-Smith: "Re: Review of FreeBSD 5.4"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|