File: shell.c

package info (click to toggle)
staden 2.0.0%2Bb11-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 21,556 kB
  • sloc: ansic: 240,603; tcl: 65,360; cpp: 12,854; makefile: 11,201; sh: 2,952; fortran: 2,033; perl: 63; awk: 46
file content (99 lines) | stat: -rw-r--r-- 1,872 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "misc.h"
#include <stdio.h>

void shell_call(char *command, char *output, int len)
{
    FILE *pipe;
    char *a;

    output[0]='\0';
    /* some systems don't prototype this */
    pipe = (FILE *)popen(command,"r");
    if (!fgets(output,len,pipe))
	*output = 0;
    pclose(pipe);

    /* clobber last new line */
    for (a=output;*a && *a != '\n'; a++);
    *a = '\0';
}

/* Getopt for windows */
#ifdef _MSC_VER

/*
 * An implementation of the getopt() function to be used on platforms which
 * do not have a system version (eg. MS Windows)
 */
#include <stdio.h>
#include <string.h>

char *optarg = NULL;
int optind=1;
int opterr=1;

int getopt(int argc, char * const argv[], const char *optstring) {
    static char *arg = NULL;
    char *optcode;
    int opt;

    /* Initialisation */
    if (!arg) {
	arg = "";
	optind = 1;
    }

    /* End of this argument (or starting up), move on to next. */
    if (!*arg) {
	if (optind >= argc) {
	    arg = NULL;
	    return -1;
	}

	arg = argv[optind++];

	/* Arguments end when we see a non '-' sign */
	if (*arg != '-') {
	    optind--;
	    arg = NULL;
	    return -1;
	}
	arg++;
    }

    /* Get the option. '-' by itself as an option also ends the parsing */
    if (!(opt = *arg++)) {
	optind--;
	arg = NULL;
	return -1;
    }

    /* Find the index in the optstring */
    if (NULL == (optcode = strchr(optstring, opt))) {
	if (opterr)
	    fprintf(stderr, "%s: illegal option -- %c\n", argv[0], opt);
	return '?';
    }

    /* If next char is ':', set optarg to point to the argument */
    if (*++optcode == ':') {
	if (*arg) {
	    optarg = arg;
	    arg = "";
	} else {
	    if (optind < argc) {
		optarg = argv[optind++];
	    } else {
		if (opterr)
		    fprintf(stderr, "%s: option requires an argument -- %c\n",
			    argv[0], opt);
		optarg = "?";
	    }
	}
    }

    return opt;
}

#endif