Hi,

This has been asked and answered a few days ago:
    http://lists.harbour-project.org/pipermail/harbour/2010-March/033534.html

Anyway it belongs to user's list.

Brgds,
Viktor

On 2010 Mar 13, at 14:02, CarozoDeQuilmes wrote:

> Hi smu, many thanks for your help but it don't respond my question.  I will 
> make a pure Harbour sample and I will send my question again.
> 
> Thanks and regards
> CdQ
> 
> On Wed, Mar 10, 2010 at 5:07 PM, smu johnson <smujohn...@gmail.com> wrote:
> Take a look at this example that is working
> 
>     #include "Fileio.ch
> 
>     IF (nHandle := FCREATE(VERSION_FILE, FC_NORMAL)) == -1
>       ? VERSION_FILE + " cannot be created:", FERROR()
>       BREAK
>     ELSE
>       cString := "// " + cBm_now + CRLF
>       cString += "func BM_Versio()" + CRLF
>       cString += "  return " + DOUBLE_QUOTE + cBm_now + DOUBLE_QUOTE + CRLF
> 
>       FWRITE(nHandle, cString)
>       FCLOSE(nHandle)
>       ? "Wrote to: " + VERSION_FILE
>     ENDIF
> 
> 
> 
> On Wed, Mar 10, 2010 at 12:03 PM, CarozoDeQuilmes <carozodequil...@gmail.com> 
> wrote:
> Hi, in an old project, I have a little C ansi program (found in Internet or 
> in the MiniGUI, I don't remmeber) that execute and executable file and 
> redirect the standard output to file.
> 
> The C program run fine and trap Standard Output from another C program that 
> made:
> 
> fprintf( stdout , 'hello' );
> 
> but I can't redirect (or trap) the "?" (or QOUT) command from a Harbour 
> program.
> 
> Is this true or I'm wrong ???
> 
> Thanks in advance and sorry by my bad english
> 
> CdQ
> 
> Program C:
> 
> /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
> /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
> /*
> 
>    Redir 2.0 Copyright (C) 1995-1998 DJ Delorie (d...@delorie.com)
>    Modified 1999 by Mumit Khan <k...@xraylith.wisc.edu>
> 
>    Redir is free software; you can redistribute it and/or modify it
>    under the terms of the GNU General Public License as published by
>    the Free Software Foundation; either version 2, or (at your option)
>    any later version.
> 
>    Redir is distributed in the hope that it will be useful, but
>    WITHOUT ANY WARRANTY; without even the implied warranty of
>    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>    General Public License for more details.
> 
> */
> 
> #include <ctype.h>
> #include <errno.h>
> #include <fcntl.h>
> #include <process.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include "US_Log.h"
> 
> /* Here's the deal.  We need to pass the command-line arguments to the
>    child program *exactly* as we got them.  This means we cannot allow
>    any wildcard expansion, we need to retain any quote characters, and
>    we need to disable response files processing.  That's why we must
>    link with CRT_noglob.o!  */
> 
> void xmalloc_failed(size_t size)
> {
>   fprintf(stderr, "Out of memory allocating %lu bytes\n", (unsigned long) 
> size);
>   exit(1);
> }
> 
> void *xmalloc (size_t size)
> {
>   void *newmem;
> 
>   if (size == 0)
>     size = 1;
>   newmem = malloc (size);
>   if (!newmem)
>     xmalloc_failed (size);
> 
>   return (newmem);
> }
> 
> void *xrealloc(void *oldmem, size_t size)
> {
>   void *newmem;
> 
>   if (size == 0)
>     size = 1;
>   if (!oldmem)
>     newmem = malloc (size);
>   else
>     newmem = realloc (oldmem, size);
>   if (!newmem)
>     xmalloc_failed (size);
> 
>   return (newmem);
> }
> 
> int display_exit_code=0;
> int std_err_fid;
> FILE *std_err;
> int rv;
> 
> static void
> usage(void)
> {
>   /*               
> ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
>  */
>   fprintf(stderr, "Redir 2.0 Copyright (C) 1995 - 1998 DJ Delorie 
> (d...@delorie.com)\n");
>   fprintf(stderr, "Modified 1999 for Mingw32 by Mumit Khan 
> <k...@xraylith.wisc.edu>\n");
>   fprintf(stderr, "Distribute freely.  There is NO WARRANTY.\n");
>   fprintf(stderr, "This program is protected by the GNU General Public 
> License.\n\n");
>   fprintf(stderr, "Usage: redir [-i file] [-o file] [-oa file] [-e file] [-ea 
> file]\n");
>   fprintf(stderr, "                [-eo] [-oe] [-x] command [args . . 
> .]\n\n");
>   fprintf(stderr, "  -i file   redirect stdandard input from file\n");
>   fprintf(stderr, "  -o file   redirect standard output to file\n");
>   fprintf(stderr, "  -oa file  append standard output to file\n");
>   fprintf(stderr, "  -e file   redirect standard error to file\n");
>   fprintf(stderr, "  -ea file  append standard error to file\n");
>   fprintf(stderr, "  -eo       redirect standard error to standard output\n");
>   fprintf(stderr, "  -oe       redirect standard output to standard error\n");
>   fprintf(stderr, "  -x        print exit code\n");
>   fprintf(stderr, "  command   the program you want to run, with 
> arguments\n\n");
>   fprintf(stderr, "Options are processed in the order they are 
> encountered.\n\n");
>   exit(1);
> }
> 
> static void
> fatal(const char *msg, const char *fn)
> {
>   fprintf(std_err, msg, fn);
>   fprintf(std_err, "\nThe error was: %s\n", strerror(errno));
>   exit(1);
> }
> 
> static void
> unquote(const char *src, char *dst)
> {
>   int quote=0;
> 
>   while ((quote || !isspace(*src)) && *src)
>   {
>     if (quote && *src == quote)
>     {
>       quote = 0;
>       src++;
>     }
>     else if (!quote && (*src == '\'' || *src == '"'))
>     {
>       quote = *src;
>       src++;
>     }
>     else if (*src == '\\' && strchr("'\"", src[1]) && src[1])
>     {
>       src++;
>       *dst++ = *src++;
>     }
>     else
>     {
>       *dst++ = *src++;
>     }
>   }
>   *dst = '\0';
> }
> 
> static char *
> unquoted_argv(int argc, char *argv[], char *reuse)
> {
>   char *new_arg;
> 
>   if (reuse)
>     new_arg = (char *)xrealloc(reuse, strlen(argv[argc]) + 1);
>   else
>     new_arg = (char *)xmalloc(strlen(argv[argc]) + 1);
>   unquote(argv[argc], new_arg);
>   return new_arg;
> }
> 
> static int
> run_program(int argc, char *argv[])
> {
>   return spawnvp (P_WAIT, argv[1], argv+1);
> }
> 
> int
> main(int argc, char **argv)
> {
>   char *arg1 = NULL, *arg2 = NULL;
> 
>   if (argc < 2)
>     usage();
> 
>   std_err_fid = dup(1);
>   std_err = fdopen(std_err_fid, "w");
> 
>   /* US_log( "US_Redir by CdQ" );  */
> 
>   /* We requested that the startup code retains any quote characters
>      in the argv[] elements.  So we need to unquote those that we
>      process as we go.  */
>   while (argc > 1 && (arg1 = unquoted_argv(1, argv, arg2))[0] == '-')
>   {
>     int temp;
>     if (strcmp(arg1, "-i")==0 && argc > 2)
>     {
>       if ((temp = open(arg2 = unquoted_argv(2, argv, arg1),
>                        O_RDONLY, 0666)) < 0
>           || dup2(temp, 0) == -1)
>         fatal("redir: attempt to redirect stdin from %s failed", arg2);
>       close(temp);
>       argc--;
>       argv++;
>     }
>     else if (strcmp(arg1, "-o")==0 && argc > 2)
>     {
>       if ((temp = open(arg2 = unquoted_argv(2, argv, arg1),
>                        O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0
>           || dup2(temp, 1) == -1)
>         fatal("redir: attempt to redirect stdout to %s failed", arg2);
>       close(temp);
>       argc--;
>       argv++;
>     }
>     else if (strcmp(arg1, "-oa")==0 && argc > 2)
>     {
>       if ((temp = open(arg2 = unquoted_argv(2, argv, arg1),
>                        O_WRONLY|O_APPEND|O_CREAT, 0666)) < 0
>           || dup2(temp, 1) == -1)
>         fatal("redir: attempt to append stdout to %s failed", arg2);
>       close(temp);
>       argc--;
>       argv++;
>     }
>     else if (strcmp(arg1, "-e")==0 && argc > 2)
>     {
>       if ((temp = open(arg2 = unquoted_argv(2, argv, arg1),
>                        O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0
>           || dup2(temp, 2) == -1)
>         fatal("redir: attempt to redirect stderr to %s failed", arg2);
>       close(temp);
>       argc--;
>       argv++;
>     }
>     else if (strcmp(arg1, "-ea")==0 && argc > 2)
>     {
>       if ((temp = open(arg2 = unquoted_argv(2, argv, arg1),
>                        O_WRONLY|O_APPEND|O_CREAT, 0666)) < 0
>           || dup2(temp, 2) == -1)
>         fatal("redir: attempt to append stderr to %s failed", arg2);
>       close(temp);
>       argc--;
>       argv++;
>     }
>     else if (strcmp(arg1, "-eo")==0)
>     {
>       if (dup2(1,2) == -1)
>         fatal("redir: attempt to redirect stderr to stdout failed", 0);
>     }
>     else if (strcmp(arg1, "-oe")==0)
>     {
>       if (dup2(2,1) == -1)
>         fatal("redir: attempt to redirect stdout to stderr failed", 0);
>     }
>     else if (strcmp(arg1, "-x")==0)
>     {
>       display_exit_code = 1;
>     }
>     else
>       usage();
>     argc--;
>     argv++;
>   }
> 
>   if (argc <= 1)
>   {
>     errno = EINVAL;
>     fatal("Missing program name; aborting", "");
>   }
> 
>   rv = run_program(argc, argv);
> 
>   if (rv < 0)
>     fatal("Error attempting to run program %s", argv[1]);
> 
>   if (display_exit_code)
>   {
>     fprintf(std_err, "Exit code: %d\n", rv);
>   }
> 
>   return rv;
> }
> 
> 
> ==============================================================
> END PROGRAM
> 
> _______________________________________________
> Harbour mailing list (attachment size limit: 40KB)
> Harbour@harbour-project.org
> http://lists.harbour-project.org/mailman/listinfo/harbour
> 
> 
> 
> 
> -- 
> smu johnson <smujohn...@gmail.com>
> 
> 
> _______________________________________________
> Harbour mailing list (attachment size limit: 40KB)
> Harbour@harbour-project.org
> http://lists.harbour-project.org/mailman/listinfo/harbour
> 
> 
> _______________________________________________
> Harbour mailing list (attachment size limit: 40KB)
> Harbour@harbour-project.org
> http://lists.harbour-project.org/mailman/listinfo/harbour

_______________________________________________
Harbour mailing list (attachment size limit: 40KB)
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to