view cygwin/cygrun/cygrun.c @ 648:898e6d98f933

Translated comments into English.
author Oleksandr Gavenko <gavenkoa@gmail.com>
date Wed, 19 Dec 2018 01:06:46 +0200
parents 073940496d59
children
line wrap: on
line source
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
# include <process.h>
#else
# include <unistd.h>
#endif

#define MAX_STR_LEN  500

int main(int argc, char **argv)
{
    char cmd[MAX_STR_LEN] = "";
    char **cmdarg = (char **) malloc((argc+1) * sizeof(char *));
    char *start, *end;

    /* Break infinite loading itself if appropriate shell script absent. */
    if (getenv("CYGRUN")) {
        fprintf(stderr, "cygrun: recursive call was detected, fix your PATH!");
        exit(1);
    }

    /* To prevent argument damage (like stripping curly braces) check if 'noglob' is set. */
    char *cygwin_env = getenv("CYGWIN");
    if (cygwin_env == NULL) {
        fprintf(stderr, "cygrun: CYGWIN env var isn't set, we expect 'noglob' there.");
        exit(EXIT_FAILURE);
    }
    if (strstr(cygwin_env, "noglob") == NULL) {
        fprintf(stderr, "cygrun: 'noglob' is expected in CYGWIN env var.");
        exit(EXIT_FAILURE);
    }

    start = strrchr(*argv, '/');
    if (start)
        start++;
    else
        start = *argv;
    end = strrchr(start, '.');
    if (end) {
        memcpy(cmd, start, end - start);
        cmd[end - start] = '\0';
    } else
        strcpy(cmd, start);
    for (int i = 0; i < argc; i++)
        cmdarg[i] = argv[i];
    cmdarg[argc] = NULL;

#ifdef DEBUG
    char log[MAX_STR_LEN] = "";
#ifdef __CYGWIN__
    sprintf(log, "/tmp/cygrun-%s.log", cmd);
#endif
#if defined(_MSC_VER) || defined( __WIN32__)
    sprintf(log, "%s/cygrun-%s.log", getenv("TMP"), cmd);
#endif
    FILE *f = fopen(log, "ab");
    /* FILE *f = stdout; */
    if (!f)
        return 1;
    fprintf(f, "====== %s ======\n", cmd);
    for (int i = 0; i < argc; i++)
        fprintf(f, "%s\n", argv[i]);
    fclose(f);
#endif

    putenv("CYGRUN=1");
    return execvp(cmd, cmdarg);
}