Re: Review of FreeBSD 5.4

From: The Ghost In The Machine (ewill_at_sirius.athghost7038suus.net)
Date: 07/21/05


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.


Relevant Pages

  • Problem with obj files in sysgen phase
    ... seems to be missing. ... BUILD: Compile and Link for x86. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Problem in calling c programs and compiling them in tcl/tk
    ... Are you using Tcl's glob command to do this? ... using exec gcc filename.c and later exec ./a.out filename.c. ... the execution of a command that might generate a Tcl error. ... i can call a c program and compile it? ...
    (comp.lang.tcl)
  • RE: Console Application Testing
    ... vbc.exe could be accessed in Visual Studio Command ... If you want to compile the project in command prompt with vbc.exe, ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Macro prompt
    ... I'm sorry, Pcman, I don't think I can help here. ... If you want to test the action of clicking a command button on a form, I suggest you test it by clicking the command button on the form, not by trying to run the Click event code from within the VBE window. ... Steve Schapel, Microsoft Access MVP ... options pop-up menu the the "Compile" section has already checked off Compile ...
    (microsoft.public.access.macros)
  • Re: Using MATLAB functions in C code
    ... interface to MATLAB engine.When I tried to ... option files needed.The command is as follows: ... MATLAB fft amd ifft functions.I just want to compile them like: ... int mlhs, mrhs; ...
    (comp.soft-sys.matlab)