By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

Linux Customized Commands

P: n/a
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]*/
}

}


Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Afifov <af****@yahoo.com> 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.
Nov 14 '05 #2

P: n/a
# 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?
Nov 14 '05 #3

P: n/a
# > 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.
Nov 14 '05 #4

P: n/a
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> 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) ks***@mib.org <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.
Nov 14 '05 #5

P: n/a
Keith Thompson <ks***@mib.org> 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!
Nov 14 '05 #6

P: n/a
On Tue, 10 Aug 2004 02:10:29 -0000, SM Ryan
<wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
comp.lang.c:
Keith Thompson <ks***@mib.org> 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
Nov 14 '05 #7

P: n/a
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.

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.