Implementing Chmod command using c

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shastri

    #1

    Implementing Chmod command using c

    Hi all,

    I was trying to implement chmod command(UNIX) in c. While passing
    arguments to the main function like :

    -r for reading the file
    -w for writing the file


    How can I compare the second argument i.e argv[1] with something like
    -r?
    While running the program if I enter -r it should read the file.
    So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
    integer. so Please suggest how to do this??


    Thanks
    Shastri

  • Peter Nilsson

    #2
    Re: Implementing Chmod command using c

    Shastri wrote:[color=blue]
    >
    > ... While passing arguments to the main function like :
    >
    > -r for reading the file
    > -w for writing the file
    >
    >
    > How can I compare the second argument i.e argv[1] with something like
    > -r?[/color]

    if (strcmp(argv[1], "-r") == 0)
    ...

    --
    Peter

    Comment

    • Ian Collins

      #3
      Re: Implementing Chmod command using c

      Shastri wrote:[color=blue]
      > Hi all,
      >
      > I was trying to implement chmod command(UNIX) in c. While passing
      > arguments to the main function like :
      >
      > -r for reading the file
      > -w for writing the file
      >
      >
      > How can I compare the second argument i.e argv[1] with something like
      > -r?
      > While running the program if I enter -r it should read the file.
      > So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
      > integer. so Please suggest how to do this??
      >
      >[/color]
      Check for '-', then test the following character.

      --
      Ian Collins.

      Comment

      • sathyashrayan

        #4
        Re: Implementing Chmod command using c


        "Shastri" <sas429s@gmail. com> wrote in message
        news:1139187143 .434946.125840@ z14g2000cwz.goo glegroups.com.. .[color=blue]
        > Hi all,
        >
        > I was trying to implement chmod command(UNIX) in c. While passing
        > arguments to the main function like :
        >
        > -r for reading the file
        > -w for writing the file
        >
        >
        > How can I compare the second argument i.e argv[1] with something like
        > -r?
        > While running the program if I enter -r it should read the file.
        > So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
        > integer. so Please suggest how to do this??
        >
        >
        > Thanks
        > Shastri
        >
        >[/color]


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

        In a main() like the above you can use multi dimentation array
        such as ,argv[rows][coloums] along with counter argc inside a if statement.
        Simply use loop iterations for rows and coloums to march through the array.
        Hope that helps.


        Comment

        • Barry Schwarz

          #5
          Re: Implementing Chmod command using c

          On 5 Feb 2006 16:52:23 -0800, "Shastri" <sas429s@gmail. com> wrote:
          [color=blue]
          >Hi all,
          >
          >I was trying to implement chmod command(UNIX) in c. While passing
          >arguments to the main function like :
          >
          >-r for reading the file
          >-w for writing the file
          >
          >
          >How can I compare the second argument i.e argv[1] with something like
          >-r?
          >While running the program if I enter -r it should read the file.
          >So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
          >integer. so Please suggest how to do this??[/color]

          You are looking for the strcmp function.

          BTW, '-r' is a no-no. The ' delimiter is for a character constant
          containing a single character. You want "-r" which defines a string
          literal.[color=blue]
          >
          >
          >Thanks
          >Shastri[/color]
          Remove del for email

          Comment

          • Mark B

            #6
            Re: Implementing Chmod command using c


            "Barry Schwarz" <schwarzb@doezl .net> wrote in message
            news:ucufu15lp9 cckkt67grup5ti6 vck3vohoq@4ax.c om...[color=blue]
            > On 5 Feb 2006 16:52:23 -0800, "Shastri" <sas429s@gmail. com> wrote:
            >[color=green]
            >>Hi all,
            >>
            >>I was trying to implement chmod command(UNIX) in c. While passing
            >>arguments to the main function like :
            >>
            >>-r for reading the file
            >>-w for writing the file
            >>
            >>
            >>How can I compare the second argument i.e argv[1] with something like
            >>-r?
            >>While running the program if I enter -r it should read the file.
            >>So if(argv[1]=='-r') can't be done as we cant compare a pointer to an
            >>integer. so Please suggest how to do this??[/color]
            >
            > You are looking for the strcmp function.
            >
            > BTW, '-r' is a no-no. The ' delimiter is for a character constant
            > containing a single character. You want "-r" which defines a string
            > literal.[/color]

            Yeah, I don't think that will work too well either as written...
            How about: if(strcmp(argv[1], "-r") == 0)
            or better yet, use getopt() if it's available on your platform.

            Mark


            Comment

            Working...