Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Linux Customized Commands

Reply
Thread Tools

Linux Customized Commands

 
 
Afifov
Guest
Posts: n/a
 
      08-09-2004
Hello All, I have been working on some customized linux commands. I wrote
the command mycpd dir1 dir2 that is supposed to copy a directory to
another. I couldnt debug it in time. But here is wat it does. It reads one
directory recursively, cats the output to a file and then reads that file
(temporary) and call cp for each of its tokens to dir2.

If anyone needs it... check it out. The mycp method is working if u need
it. I can post it.

#include<stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

main(int argc, char *argv[]){

DIR *dir_p;
struct dirent *dir_entry_p;

dir_p= opendir(argv[1]);
FILE *f1 =fopen(argv[2],"w");

if( argv[1]!=NULL && argv[2]!= NULL){

// if(strcmp(argv[1],c1) ==0 && strcmp(argv[2],c2)==0)


while(NULL != (dir_entry_p=readdir(dir_p))){

fprintf(f1,"%s\n",dir_entry_p->d_name);


}
closedir(dir_p);
fclose(f1);
}
else
printf("Illegal Number of Arguments");

FILE *f2 = fopen(argv[2],"r");

char test_string[1000];
char *sub_string;
char c;
int i=0;
while(i<10){

c=getc(f2);
test_string[i]=c;
i++;
}

printf("%s\n",strtok(test_string," "));

while((sub_string=strtok(NULL," "))!=NULL){
printf("%s\n",sub_string);
/*copy each token to location execv("mycp sub_string argv[3]");
while(sub_string !=NULL){
execv("./mycp","sub_string","argv[3]");
i.e. Read the contents of file that displays the content of the directory,
and copy them to lovation argv[3]*/
}



}




 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      08-09-2004
Afifov <(E-Mail Removed)> spoke thus:

> Hello All, I have been working on some customized linux commands. I wrote
> the command mycpd dir1 dir2 that is supposed to copy a directory to
> another. I couldnt debug it in time. But here is wat it does. It reads one
> directory recursively, cats the output to a file and then reads that file
> (temporary) and call cp for each of its tokens to dir2.


This would have been better posted to comp.unix.programmer:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

> main(int argc, char *argv[]){


main() returns int; as of C99, the return type must be specified.

> FILE *f1 =fopen(argv[2],"w");


> if( argv[1]!=NULL && argv[2]!= NULL){


Huh? This is both illegal and unnecessary; far better to simply check
the value of argc, preferably before attempting to invoke fopen() on
argv[2].

> // if(strcmp(argv[1],c1) ==0 && strcmp(argv[2],c2)==0)
> while(NULL != (dir_entry_p=readdir(dir_p))){
> fprintf(f1,"%s\n",dir_entry_p->d_name);


And what if fopen() failed? Kablooie!

> }
> closedir(dir_p);
> fclose(f1);
> }
> else
> printf("Illegal Number of Arguments");


If it's illegal, why continue execution? exit( EXIT_FAILURE ) sounds
reasonable.

>
> FILE *f2 = fopen(argv[2],"r");


Are you sure you didn't mean to open argv[1] earlier?

> char test_string[1000];
> char *sub_string;
> char c;
> int i=0;
> while(i<10){
> c=getc(f2);


What if c == EOF? Your code will be none the wiser, and still more
broken.

> test_string[i]=c;
> i++;
> }


> printf("%s\n",strtok(test_string," "));


> while((sub_string=strtok(NULL," "))!=NULL){
> printf("%s\n",sub_string);
> /*copy each token to location execv("mycp sub_string argv[3]");
> while(sub_string !=NULL){
> execv("./mycp","sub_string","argv[3]");
> i.e. Read the contents of file that displays the content of the directory,
> and copy them to lovation argv[3]*/
> }


Among other problems that may exist here, you haven't ensured that
there *is* an argv[3], which is likely to lead to tragedy.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      08-09-2004
# while(NULL != (dir_entry_p=readdir(dir_p))){
#
# fprintf(f1,"%s\n",dir_entry_p->d_name);

I would suggest you echo this entry and verify what you are telling
the copier to copy. It may include things you are not expecting.

# while((sub_string=strtok(NULL," "))!=NULL){
# printf("%s\n",sub_string);
# /*copy each token to location execv("mycp sub_string argv[3]");
# while(sub_string !=NULL){
# execv("./mycp","sub_string","argv[3]");

A successful exec does not return. It replaces the current program
with the named program; if you want to call other program and continue
after with your current program, you can use the system() function
or unix specific calls involving fork() and waitpid() and other things.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
What kind of convenience store do you run here?
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      08-09-2004
# > FILE *f1 =fopen(argv[2],"w");

# > FILE *f2 = fopen(argv[2],"r");
#
# Are you sure you didn't mean to open argv[1] earlier?

You snipped the open of argv[1]: dir_p= opendir(argv[1]);

argv[2] is the name of the temporary file, openned write, written, closed,
and reopenned read.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
A bunch of savages in this town.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-09-2004
SM Ryan <(E-Mail Removed)> writes:
> # while(NULL != (dir_entry_p=readdir(dir_p))){
> #
> # fprintf(f1,"%s\n",dir_entry_p->d_name);
>
> I would suggest you echo this entry and verify what you are telling
> the copier to copy. It may include things you are not expecting.
>
> # while((sub_string=strtok(NULL," "))!=NULL){
> # printf("%s\n",sub_string);
> # /*copy each token to location execv("mycp sub_string argv[3]");
> # while(sub_string !=NULL){
> # execv("./mycp","sub_string","argv[3]");
>
> A successful exec does not return. It replaces the current program
> with the named program; if you want to call other program and continue
> after with your current program, you can use the system() function
> or unix specific calls involving fork() and waitpid() and other things.


We don't know whether a successful (or unsuccessful) call to exec() or
execv() returns or not. There is no such function in standard C.

To put it another way, this whole discussion belongs in another
newsgroup, most likely comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      08-10-2004
Keith Thompson <(E-Mail Removed)> wrote:

# To put it another way, this whole discussion belongs in another
# newsgroup, most likely comp.unix.programmer.

You need to renew your alt.religion.scientology cancelbot license.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I ASSURE YOU WE'RE OPEN!
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-10-2004
On Tue, 10 Aug 2004 02:10:29 -0000, SM Ryan
<(E-Mail Removed)> wrote in
comp.lang.c:

> Keith Thompson <(E-Mail Removed)> wrote:
>
> # To put it another way, this whole discussion belongs in another
> # newsgroup, most likely comp.unix.programmer.
>
> You need to renew your alt.religion.scientology cancelbot license.


You need to get a clue. We don't need time wasters posting off-topic
material here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Afifov
Guest
Posts: n/a
 
      08-11-2004
I tried EOF in both cases and it doesnt seem to stop. Could it be my linux
version? About arg3 , it is supposed to be implicit, as in a temporary
buffer.

Anyways,sorry for the post. But its C language anyways. will post at
unix.programmer next time.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is Netscape Leaving Firefox Behind on GNU/Linux? THE LINUX PROPAGANDA MACHINE CONTINUES. FIREFOX IGNORING LINUX............. traci.manicotti@gmail.com Computer Support 2 10-20-2007 02:12 PM
Dell produces customized Ubuntu Linux for customers Au79 Computer Support 15 09-16-2007 09:54 AM
Linux... yeah linux.. Linux Have a nice cup of pee NZ Computing 19 04-17-2006 10:16 AM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments