Linux Customized Commands

Discussion in 'C Programming' started by Afifov, Aug 9, 2004.

  1. Afifov

    Afifov Guest

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



    }
    Afifov, Aug 9, 2004
    #1
    1. Advertising

  2. Afifov <> 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=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.
    Christopher Benson-Manica, Aug 9, 2004
    #2
    1. Advertising

  3. Afifov

    SM Ryan Guest

    # 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?
    SM Ryan, Aug 9, 2004
    #3
  4. Afifov

    SM Ryan Guest

    # > 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.
    SM Ryan, Aug 9, 2004
    #4
  5. SM Ryan <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 9, 2004
    #5
  6. Afifov

    SM Ryan Guest

    Keith Thompson <> 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!
    SM Ryan, Aug 10, 2004
    #6
  7. Afifov

    Jack Klein Guest

    On Tue, 10 Aug 2004 02:10:29 -0000, SM Ryan
    <> wrote in
    comp.lang.c:

    > Keith Thompson <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Aug 10, 2004
    #7
  8. Afifov

    Afifov Guest

    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.
    Afifov, Aug 11, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,952
    Thomas Stegen
    Jun 28, 2003
  2. Tim Stanka
    Replies:
    1
    Views:
    785
    Jeff Epler
    Aug 2, 2004
  3. Adriaan Renting

    Filtering terminal commands on linux

    Adriaan Renting, Jul 29, 2005, in forum: Python
    Replies:
    1
    Views:
    378
    Lonnie Princehouse
    Jul 29, 2005
  4. Pat
    Replies:
    3
    Views:
    760
    Ian Wilson
    Oct 13, 2006
  5. nkb
    Replies:
    2
    Views:
    93
    I did not
    Oct 28, 2004
Loading...

Share This Page