FreeBASIC  0.91.0
sys_cmd.c
Go to the documentation of this file.
1 /* command$ */
2 
3 #include "fb.h"
4 
6 {
7  FBSTRING *dst;
8  ssize_t i, len;
9 
10  /* return all arguments? */
11  if( arg < 0 )
12  {
13  /* no args? */
14  if( __fb_ctx.argc <= 1 )
15  return &__fb_ctx.null_desc;
16 
17  /* concatenate all args but 0 */
18  len = 0;
19  for( i = 1; i < __fb_ctx.argc; i++ )
20  len += strlen( __fb_ctx.argv[i] );
21 
22  dst = fb_hStrAllocTemp( NULL, len + __fb_ctx.argc-2 );
23  if( dst == NULL )
24  return &__fb_ctx.null_desc;
25 
26  dst->data[0] = '\0';
27  for( i = 1; i < __fb_ctx.argc; i++ )
28  {
29  strcat( dst->data, __fb_ctx.argv[i] );
30  if( i != __fb_ctx.argc-1 )
31  strcat( dst->data, " " );
32  }
33 
34  return dst;
35  }
36 
37  /* return just one argument */
38  if( arg >= __fb_ctx.argc )
39  return &__fb_ctx.null_desc;
40 
41  len = strlen( __fb_ctx.argv[arg] );
42  dst = fb_hStrAllocTemp( NULL, len );
43  if( dst == NULL )
44  return &__fb_ctx.null_desc;
45 
46  strcpy( dst->data, __fb_ctx.argv[arg] );
47 
48 #ifdef HOST_DOS
49  if( arg == 0 )
50  {
51  /* make drive letter uppercase */
52  if( dst->data[1] == ':' )
53  dst->data[0] = toupper( dst->data[0] );
54 
55  /* DOS gives us argv[0] with '/' path separators -
56  * change them to the more DOS-like '\'. */
57  for( i = 0; i < len; ++i )
58  {
59  if( dst->data[i] == '/' )
60  dst->data[i] = '\\';
61  }
62  }
63 #endif
64 
65  return dst;
66 }