Re: I'm a C++ programmer, and Relf's X.CPP is good.
From: Mike Cox (mikecoxlinux_at_yahoo.com)
Date: 09/25/04
- Next message: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Previous message: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Maybe in reply to: Mike Cox: "I'm a C++ programmer, and Relf's X.CPP is good."
- Next in thread: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Reply: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Reply: Kaz Kylheku: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 24 Sep 2004 17:06:53 -0700
Stefaan A Eeckels wrote:
> On Fri, 24 Sep 2004 13:46:37 -0700
> Mike Cox <mikecoxlinux@yahoo.com> wrote:
>
>> I've developed my hm command (pronounced "home")*, I've modified a Linux
>> kernel mouse driver* to support Microsoft Wireless Optical mice with
>> tilt wheel technology. I use emacs and extend it with LISP. I know my
>> stuff. And with this authority, I have to say that I see nothing wrong
>> with Jeff Relf's X.CPP.
>
> <snip drivel>
>
>> 1. Do a groups.google search for the code to both my mouse driver
>> modification and my "hm" command.
>
> I don't know why you wrote 'hm', as the 'cd' command without
> parameters will take you back to your home dir :-).
>
> But as you insist in positioning yourself as an expert, I had a
> look at your so-called C++ code, and, to be quite candid, it stinks,
> and it doesn't compile either.
>
> This is the thing I located:
>
> | /*
> | (c) 2004 Mike Cox. Released under the GNU GPL License.
> | email: mikecoxlinux@yahoo.com
> | web: www.geocities.com/mikecoxlinux/
> |
> | ****** Home Version 2 ******
> | The "hm" (shortened from home) command takes you to
> | your home directory if you've been wandering far and
> | don't want to type so much.
> |
> | Just type "hm" at the shell, and you're immediatly
> | transported to /home/yourusername/
>
>
>
You fool. The version I was at was version 3.0. The thread title is called
""hm" command posted without formattin errors." Type that in google
groups. But you don't have too since I'm going to post the code here.
Here is the full version:
/*
AUTHOR: Mike Cox.
EMAIL: mikecoxlinux@yahoo.com
WEB: www.geocities.com/mikecoxlinux/
COPYRIGHT: (C) 2004 Mike Cox. All Rights Reserved.
VERSION: Version 3.0
******** THE "hm" [pronounced "home"] COMMAND ********
LICENSE: "hm" is released under the GNU GPL License.
WARRANTY: This program is provided AS IS. There is NO
warranty for this software program. The Author
assumes NO responsiblity for the usablity of this
software. Use at your own risk.
FEATURES: 0: Takes the user to their home directory.
1: Allows user to store directories in 26 buffers.
2: Allows user to change to directories in those buffers.
3: Allows user to list contents of the current directory.
4: Allows user to list contents of directories in the 26 buffers.
5: Allows user to specify directory for hm to list the contents of.
EXAMPLES:
0: linux:/usr/local/# hm
1: linux:/usr/local/doc/# hm -s a
-or-
linux:/home/user# hm -s a /usr/local/share/doc/
2: linux:/home/user# hm -r a
3: linux:/home/user# hm -l
4: linux:/home/user# hm -l a
5: linux:/home/user# hm -l /usr/local/share/doc/
COMPILE: "hm" can be compiled using this command:
g++ -o hm hm.cpp
INSTALL: Move the "hm" program to:
/usr/local/bin/
*/
#include <iostream>
#include <pwd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
using std::vector;
void recall_path(int argc, char** argv);
void set_path(int argc, char** argv);
void write_path(char* hm_path, char hm_buf);
void list_contents(int argc, char** argv);
void go_home();
int main(int argc, char** argv)
{
if(argc > 1){
if(strcmp(argv[1], "-r") == 0){
recall_path(argc, argv);
}else if(strcmp(argv[1], "-s") == 0){ set_path(argc, argv);
}else if(strcmp(argv[1], "-l") == 0){ list_contents(argc, argv);
}else{
std::cout<<"Error: "<<argv[1];
std::cout<<" is not a supported option.\n";
}
}
//Going home.
else{
go_home();
} return 0;
}
void recall_path(int argc, char** argv)
{
/* Get the user .hm file. */
uid_t hm_usr;
struct passwd* hm_ps;
hm_usr = getuid();
hm_ps = getpwuid(hm_usr);
if(!hm_ps){
std::cout<<"Error: Set Path Failed.\n ";
exit(1);
}
char* hm_name = new char[strlen(hm_ps->pw_dir) + 5];
strcpy(hm_name, hm_ps->pw_dir);
strcat(hm_name,"/.hm");
/* Read file and find buffer. Execute corrosponding path*/
FILE* o;
o = fopen(hm_name,"r");
//please help. I'm stuck on how to do this. Windows has a better API.
// fseek();
fclose(o);
delete [] hm_name;
}
void set_path(int argc, char** argv)
{
/*
Makes sure the user has specified an alpha buffer.
Then writes the current directory or a user specified one to the buffer.
*/
if(argc < 3){
std::cout<<"Error: Need to specify a buffer. Lower case [a-z] are valid.
\n";
exit(1);
}
if(islower(argv[2][0])){
if(argc == 3){
write_path(get_current_dir_name(),argv[2][0]);
}else if(argv[3][0] == '/'){ write_path(argv[3], argv[2][0]);
}else{ std::cout<<"Error: Not a valid directory name.\n";
}
}else{
for(int i = 0; i < argc; ++i){
std::cout<<argv[i];
std::cout<<" ";
}
std::cout<<"Error: Need to specify a buffer. Lower case [a-z] are valid.
\n";
}
}
void write_path(char* hm_path, char hm_buf )
{
/*
The current POSIX way of getting the user's name and home directory.
cuserid() is deprecated. See glibc documentation at: http://www.gnu.org
*/
uid_t hm_usr;
struct passwd* hm_ps;
hm_usr = getuid();
hm_ps = getpwuid(hm_usr);
if(!hm_ps){
std::cout<<"Error: Set Path Failed.\n ";
exit(1);
}
char* hm_name = new char[strlen(hm_ps->pw_dir) + 5];
strcpy(hm_name, hm_ps->pw_dir);
strcat(hm_name,"/.hm");
FILE* o;
o =fopen(hm_name, "a");
/* Turn hm_buf into a string */
char b[2];
b[0] = hm_buf;
b[1] = '\0';
/* Put the buffer and path into the file. */
//there is a bug in that it doesn't check to see if buffer
//exists already. Ideas on a fix would be helpful. 2 or more
//same buffers are possible. :-(.
fputs(b, o);
fputs(" ", o);
fputs(hm_path, o);
fputs("\n", o);
/* close file and delete memory */
fclose(o);
delete [] hm_name;
}
void list_contents(int argc, char** argv)
{
/*
For right now, it just lists the current dir contents.
Soon, you will be able to list contents in buffer directories.
*/
system("ls -la");
}
void go_home()
{
uid_t hm_usr;
struct passwd* hm_ps;
hm_usr = getuid();
hm_ps = getpwuid(hm_usr);
if(!hm_ps){
std::cout<<"Error: Go Home Failed. \n ";
exit(1);
}
chdir(hm_ps->pw_dir);
execl(hm_ps->pw_shell,hm_ps->pw_shell,(const char*) NULL);
}
- Next message: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Previous message: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Maybe in reply to: Mike Cox: "I'm a C++ programmer, and Relf's X.CPP is good."
- Next in thread: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Reply: Stefaan A Eeckels: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Reply: Kaz Kylheku: "Re: I'm a C++ programmer, and Relf's X.CPP is good."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|