view cygwin/cygrun/cygrun.c @ 623:285855784c5a

Make possible to start Cygwin Emacs-w32 if it is not yet started.
author Oleksandr Gavenko <gavenkoa@gmail.com>
date Mon, 23 Jan 2017 02:30:10 +0200
parents 17b49d08a1a6
children
line wrap: on
line source
#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, "Recursive calls was detected. Fix your PATH!");
        exit(1);
    }

    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
#ifdef _MSC_VER
    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);
}