Author Topic: Need help with gcc copy routine  (Read 846 times)

R.M. Klippstein

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 205
    • View Profile
Need help with gcc copy routine
« on: November 12, 2016, 10:46:48 pm »
The following is a copy routine that can be used in gcc to copy a file or directory to another location while running a compile. I have this routine embedded in a .cpp file (alloc.cpp – this is just for testing to make sure the routine works) the routine will be used later in the compile that takes about 1 hour to build. I've defined “out” and now need to plug in the values for src_path =argv[1]; and dst_path = argv[2]; I've been unable to come up with the correct values for argv[1&2] and get the error indicated below. For testing purposes I'm using F:\4.0.37\testcopy for  the destination path  and F:\4.0.37\include\testfile.txt for the file to be copied.
Here is the routine in alloc.cpp:

*   Header Files                                                               *
*******************************************************************************/
#define RTMEM_NO_WRAP_TO_EF_APIS

#define exit

#include <iprt/mem.h>
#include "internal/iprt.h"

#include <iprt/assert.h>
#include <iprt/string.h>


#include <stdio.h>

int main(int argn, char * argv[]) {

    int src_fd, dst_fd, n, err;
    unsigned char buffer[4096];
    char * src_path, dst_path;

    // Assume that the program takes two arguments the source path followed
    // by the destination path.

    if (argn != 3) {
        printf("Wrong argument count.\n");
        exit(1);
    }

    src_path = argv[1];
    dst_path = /testcopy;

    src_fd = open(src_path, O_RDONLY);
    dst_fd = open(dst_path, O_CREAT | O_WRONLY);

    while (1) {
        err = read(src_fd, buffer, 4096);
        if (err == -1) {
            printf("Error reading file.\n");
            exit(1);
        }
        n = err;

        if (n == 0) break;

        err = write(dst_fd, buffer, n);
        if (err == -1) {
            printf("Error writing to file.\n");
            exit(1);
        }
    }

    close(src_fd);
    close(dst_fd);
}


RTDECL(void *) RTMemDupTag(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW
{

And here is  the error output:

kBuild: Compiling filesplitter - F:/4.0.37/src/bldprogs/filesplitter.cpp
kBuild: Compiling RuntimeBldProg - F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.
cpp
F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.cpp: In function 'int main(int, cha
r**)':
F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.cpp:59:22: error: invalid conversio
n from 'char*' to 'char' [-fpermissive]
     dst_path = argv[2];
                      ^
F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.cpp:61:29: error: 'O_RDONLY' was no
t declared in this scope
     src_fd = open(src_path, O_RDONLY);


Thanks in advance for any help

klipp



Rick C. Hodgin

  • Full Member
  • ***
  • Thank You
  • -Given: 12
  • -Receive: 2
  • Posts: 130
    • View Profile
    • Liberty Software Foundation
Re: Need help with gcc copy routine
« Reply #1 on: November 14, 2016, 04:46:52 pm »
The following is a copy routine that can be used in gcc to copy a file or directory to another location while running a compile. I have this routine embedded in a .cpp file (alloc.cpp – this is just for testing to make sure the routine works) the routine will be used later in the compile that takes about 1 hour to build. I've defined “out” and now need to plug in the values for src_path =argv[1]; and dst_path = argv[2]; I've been unable to come up with the correct values for argv[1&2] and get the error indicated below. For testing purposes I'm using F:\4.0.37\testcopy for  the destination path  and F:\4.0.37\include\testfile.txt for the file to be copied.
Here is the routine in alloc.cpp:

*   Header Files                                                               *
*******************************************************************************/
#define RTMEM_NO_WRAP_TO_EF_APIS

#define exit

#include <iprt/mem.h>
#include "internal/iprt.h"

#include <iprt/assert.h>
#include <iprt/string.h>


#include <stdio.h>

int main(int argn, char * argv[]) {

    int src_fd, dst_fd, n, err;
    unsigned char buffer[4096];
    char * src_path, dst_path;

    // Assume that the program takes two arguments the source path followed
    // by the destination path.

    if (argn != 3) {
        printf("Wrong argument count.\n");
        exit(1);
    }

    src_path = argv[1];
    dst_path = /testcopy;

    src_fd = open(src_path, O_RDONLY);
    dst_fd = open(dst_path, O_CREAT | O_WRONLY);

    while (1) {
        err = read(src_fd, buffer, 4096);
        if (err == -1) {
            printf("Error reading file.\n");
            exit(1);
        }
        n = err;

        if (n == 0) break;

        err = write(dst_fd, buffer, n);
        if (err == -1) {
            printf("Error writing to file.\n");
            exit(1);
        }
    }

    close(src_fd);
    close(dst_fd);
}


RTDECL(void *) RTMemDupTag(const void *pvSrc, size_t cb, const char *pszTag) RT_NO_THROW
{

And here is  the error output:

kBuild: Compiling filesplitter - F:/4.0.37/src/bldprogs/filesplitter.cpp
kBuild: Compiling RuntimeBldProg - F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.
cpp
F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.cpp: In function 'int main(int, cha
r**)':
F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.cpp:59:22: error: invalid conversio
n from 'char*' to 'char' [-fpermissive]
     dst_path = argv[2];
                      ^
F:/4.0.37/src/VBox/Runtime/common/alloc/alloc.cpp:61:29: error: 'O_RDONLY' was no
t declared in this scope
     src_fd = open(src_path, O_RDONLY);


Thanks in advance for any help

klipp

This particular copy function is not very "intelligent."  You'll need to add some algorithms to examine the input to see if you're copying a file to a destination folder/directory, and if so then append in the correct information from the source and make sure the target folder/directory exists.

See if it will work with F:\4.0.37\include\testfile.txt and F:\4.0.37\testcopy\testfile.txt.  Make sure testcopy already exists.  If that works, the algorithm can be expanded or you can add logic to your launching script which handles that part of it for you.

Best regards,
Rick C. Hodgin

Andreas Kohl

  • Full Member
  • ***
  • Thank You
  • -Given: 4
  • -Receive: 6
  • Posts: 142
    • View Profile
    • warpserver.de
Re: Need help with gcc copy routine
« Reply #2 on: November 15, 2016, 12:07:29 am »
Awful C code

char * src_path, dst_path;
looks ugly
src_path = argv[1];
dst_path = /testcopy;
the compiler is right to refuse this wrong stuff

Try to replace with:
char *src_path=argv[1];
char *dst_path="your file name";

Caveat emptor - the last time I touched C code was in 1997

R.M. Klippstein

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 205
    • View Profile
Re: Need help with gcc copy routine
« Reply #3 on: November 15, 2016, 08:26:15 pm »
I agree, "Lousey code" I can't make it work, Have to try something else!
Thanks both of you for the help.

klipp

Andreas Kohl

  • Full Member
  • ***
  • Thank You
  • -Given: 4
  • -Receive: 6
  • Posts: 142
    • View Profile
    • warpserver.de
Re: Need help with gcc copy routine
« Reply #4 on: November 16, 2016, 12:05:48 am »
The compiler error is because in row "char * src_path, dst_path;" the variable dst_path is declared as char type and not as a char pointer. So you should put the asterisk also in front of dst_path ("char *src_path, *dst_path;"). It's funny that this lousy gcc compiler is not able to show the correct line of error - but that's an old story.

En alemán:
Der Übersetzerfehler erfolgt m.E. wegen der Zeile "char * src_path, dst_path;" da die Variable dst_path als Typ char deklariert wird und nicht als char-Zeiger. Wenn es unbedingt in einer Zeile sein muß, sollte auch vor dst_path ein Sternchen (*) gesetzt werden um den Kompilierer gnädig zu stimmen. Zusätzlich muß die Wertzuweisung später natürlich in Anführungszeichen ("//pfad//datei") erfolgen.

¡Buena suerte!