Need help with my copy programme

This is a discussion on Need help with my copy programme within the C Programming forums, part of the General Programming Boards category; The code is located here... http://rafb.net/paste/results/QfwPRg55.html Basically for an assignment at uni we have to create a small program that ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    Need help with my copy programme

    The code is located here...

    http://rafb.net/paste/results/QfwPRg55.html

    Basically for an assignment at uni we have to create a small program that takes the following syntax.

    copy source destination

    SO far my program copies files correctly as far as I can tell but the thing is if i'm copying a executable file, when ever I try and run the copied file always says..

    ./ben permission denied. I do ls -l and it isnt set as executable in the permission list. How do I set the file to execuable or give permission for it to be executed. I haven't been using c or unix for very long so be kind.

    TIA.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    chmod
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    3
    Everything has to be done from within the program.

    Isn't chmod just a unix command??

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Basically, you've copied the file, but you haven't copied the permissions.

    > Isn't chmod just a unix command??
    The chmod command is a wrapper around the chmod API call.

    Do something like
    man 2 chmod

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Or how are you writing the file? If you're using open() you can specify the permissions right there as the 3rd parameter.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i don't feel like looking it up right now....

    but the hackish way would be:
    create a executable bash script from within your program
    execute the script
    make the script do chmod for you
    erase the script
    voila!
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    Can I have some more help on using chmod please.

    Can I have some more help using chmod please. I'm having trouble with mode_t. I have only been programming in c for a couple of months and I am stil having problems with it.

    can someone please give me some sample code of how I would allow the destination file to be executed.

    Thanks in advance.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Post what you tried.

    - get value from stat
    - pass value to chmod
    would be my first guess without looking at the manuals.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Here's a copy program that I wrote at one point that I think worked:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(int argc, char **argv)
    {
      int fdin, fdout;
      struct stat st;
      char buf[BUFSIZ];
      int n_read;
    
      if(argc != 3)
      {
        puts("Usage: mycp <source> <destination>");
        exit(1);
      }
      if((fdin = open(argv[1], O_RDONLY)) == -1)
      {
        printf("open(): %s\n", strerror(errno));
        exit(1);
      }
      if(fstat(fdin, &st) == -1)
      {
        printf("stat(): %s\n", strerror(errno));
        exit(1);
      }
    
      umask(0);
      if((fdout = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, st.st_mode)) == -1)
      {
        printf("open(): %s\n", strerror(errno));
        exit(1);
      }
    
      while((n_read = read(fdin, buf, sizeof(buf))) > 0)
      {
        if(write(fdout, buf, n_read) == -1)
        {
          printf("write(): %s\n", strerror(errno));
          exit(1);
        }
      }
    
      close(fdin);
      close(fdout);
    
      if(n_read == -1)
      {
        printf("read(): %s\n", strerror(errno));
        exit(1);
      }
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  2. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  3. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  4. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 03:21 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21