Git init
[external/mawk.git] / msdos / argvmks.c
1
2 /*  argvmks.c
3
4     for MKS Korn Shell
5
6     If you use this file, add -DHAVE_REARGV=1 to your
7     CFLAGS
8
9     Contributed by Jack Fitts (fittsj%wmv009@bcsaic.boeing.com)
10
11 */
12
13 /*
14 $Log: argvmks.c,v $
15  * Revision 1.2  1995/01/07  14:47:24  mike
16  * remove return 1 from void function
17  *
18  * Revision 1.1.1.1  1993/07/03  18:58:49  mike
19  * move source to cvs
20  *
21  * Revision 1.2  1992/12/17  02:48:01  mike
22  * 1.1.2d changes for DOS
23  *
24  * Revision 1.1  1992/12/05  22:38:41  mike
25  * Initial revision
26  *
27 */
28
29
30 /***********************************************************/
31 /*                                                         */
32 /* prototypes for reargv                                   */
33 /*                                                         */
34 /***********************************************************/
35
36 void *malloc(unsigned) ;
37 char * basename ( char * );
38 char *strcpy(char* , char*) ;
39
40
41 /***********************************************************/
42 /*                                                         */
43 /* reargv reset argc/argv from environment for MKS shell   */
44 /*                                                         */
45 /***********************************************************/
46
47
48 void reargv ( int *argcp, char *** argvp ) {
49
50     int i = 0;
51     int cnt ;
52     char ** v;
53     extern char **environ ;
54     register char **pe = environ;
55
56 /* MKS Command line args are in the first n lines of the environment */
57 /* each arg is preceded with a tilde (~)*/
58
59     while ( **(pe++) == '~' )
60         i++;
61
62 /* if no tilde found then not running under MKS */
63
64     if ( ! i )  return ;
65
66 /* malloc space for array of char pointers */
67
68     if ( ! ( v = ( char ** ) malloc (( i + 1 ) * sizeof ( char* ))) )
69         return ;
70
71 /* set argc to number of args in environ */
72
73     *argcp = cnt = i;
74
75 /* set char pointers to each command line arg */
76 /* jump over the tilde which is the first char in each string */
77
78     for ( i = 0; i < cnt ; i++ )
79         v[i] = environ[i]+1;
80
81     /*set last arg to null*/
82
83     v[cnt] = (char *) 0 ;
84     
85     /*strip leading directory stuff from argv[0] */
86
87     v[0] = basename(v[0]);
88
89     *argvp = v;
90 }
91
92
93 /***********************************************************/
94 /*                                                         */
95 /* basename                                                */
96 /*                                                         */
97 /***********************************************************/
98
99 static char * basename ( char * s ) {
100
101     register char * p ;
102     char *last ;
103     
104     /* find the last occurrence of ':' '\\' or '/' */
105     p = s ;  last = (char *) 0 ;
106     while ( *p ) {
107         if ( *p == ':' || *p == '\\' || *p == '/' ) last = p ;
108         p++ ;
109     }
110
111     return last ? last+1 : s ;
112 }