Linux Customized Commands

A

Afifov

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=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]*/
}



}
 
C

Christopher Benson-Manica

Afifov said:
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=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.
 
S

SM Ryan

# 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.
 
S

SM Ryan

# > 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.
 
K

Keith Thompson

SM Ryan said:
# 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.
 
S

SM Ryan

# 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.
 
J

Jack Klein

# 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.
 
A

Afifov

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top