File: doscmd.c

package info (click to toggle)
regina 2.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,332 kB
  • ctags: 4,775
  • sloc: ansic: 38,518; sh: 2,552; lex: 1,878; yacc: 1,028; makefile: 771
file content (588 lines) | stat: -rw-r--r-- 21,080 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#ifndef lint
static char *RCSid = "$Id: doscmd.c,v 1.7 2001/05/13 06:13:59 mark Exp $";
#endif

/*
 *  The Regina Rexx Interpreter
 *  Copyright (C) 1992-1994  Anders Christensen <anders@pvv.unit.no>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#if defined(MAC) || defined(DOS) || defined(WIN32) || defined(OS2) || defined(_AMIGA) /* MH 10-06-96 */

#include "rexx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(DOS)                                        /* MH 10-06-96 */
# include <dos.h>
#endif                                                  /* MH 10-06-96 */

#include <errno.h>

#if defined(HAVE_ASSERT_H)
# include <assert.h>
#endif

#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif

#if defined(WIN32)
# if defined(__BORLANDC__)
#  include <time.h>
#  include <process.h>
# endif
# ifdef _MSC_VER
#  if _MSC_VER >= 1100
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
#   pragma warning(disable: 4115 4201 4214)
#  endif
# endif
# include <windows.h>
# ifdef _MSC_VER
#  if _MSC_VER >= 1100
#   pragma warning(default: 4115 4201 4214)
#  endif
# endif
#endif

#if defined(MAC) || defined(__WATCOMC__) || defined(_MSC_VER) || defined(__SASC) || defined(__MINGW32__) || defined(__BORLANDC__)
# include "utsname.h"                                   /* MH 10-06-96 */
# define MAXPATHLEN  _MAX_PATH                          /* MH 10-06-96 */
#else                                                   /* MH 10-06-96 */
# if defined(WIN32) && defined(__IBMC__)                /* LM 26-02-99 */
#  include "utsname.h"
#  define MAXPATHLEN (8192)
#  include <io.h>
# else
#  include <sys/param.h>                                 /* MH 10-06-96 */
#  include <sys/utsname.h>                               /* MH 10-06-96 */
# endif
#endif

#if defined(MAC) || defined (__EMX__) || defined(__WATCOMC__) || defined(_MSC_VER) || defined(DJGPP) || defined(__CYGWIN32__) || defined(__BORLANDC__) || defined(__MINGW32__)
# define HAVE_BROKEN_TMPNAM
# define PATH_DELIMS ":\\/"
# if defined(__EMX__) || defined(__CYGWIN32__)
#  define ISTR_SLASH "/"  /* This is not a must, \\ works, too */
#  define I_SLASH '/'  /* This is not a must, \\ works, too */
# elif defined(MAC)
#  define ISTR_SLASH ":"
#  define I_SLASH ':'
# else
#  define ISTR_SLASH "\\" /* This is not a must, / works at least for MSC, too */
#  define I_SLASH '\\'    /* This is not a must, / works at least for MSC, too */
                          /* FGC: system depending: DOS, OS/2, Win32 work! */
# endif
#if !defined(MAC)
#  ifndef HAVE_UNISTD_H
#   include <io.h> /* access() */
#  endif
#  include <process.h> /* FGC, 5.1.00 for getpid at least under MSC/W32 */
# endif
# include <time.h> /* FGC, 5.1.00 for clock at least under MSC/W32 */
#endif

#if !defined(WIN32)
# define mysystem( tsd, cmd) system( cmd )
#endif

#define BUF_SIZE 512

#define RC_OUT_OF_MEMORY (-1)
#define RC_EOF           (-2)


#if defined(WIN32)
/*********************************************************/
static int mysystem( const tsd_t *TSD, const char *command )
/*********************************************************/
{
   PROCESS_INFORMATION pinfo;
   STARTUPINFO         sinfo;
   DWORD               exitCode;
   DWORD               version;

   ZeroMemory(&sinfo, sizeof(sinfo));
   sinfo.cb = sizeof(sinfo);
#if !defined(HAVE_WIN32GUI)
   version = GetVersion();
   if (version < 0x80000000)
   {  /* WinNT system */
/*      printf("mysystem: WinNT\n"); */
      return system(command);
   }

   /* Win95, Win98 system */
/*   printf("mysystem: Win9x\n");*/

   /* test for redirections or pipes */
   if (command == NULL || strpbrk(command, "<>|"))
      return system(command);
/*   printf("no redirection\n");*/
#else
   sinfo.dwFlags=STARTF_USESHOWWINDOW;
   sinfo.wShowWindow=SW_HIDE;
#endif
   /* first try: assume it is a EXE-program like pkzip.exe */
   if (!CreateProcess(NULL,    /* pointer to name of executable module    */
             (char *) command, /* pointer to command line string          */
                      NULL,    /* pointer to process security attributes  */
                      NULL,    /* pointer to thread security attributes   */
                      TRUE,    /* handle inheritance flag                 */
                      0,       /* creation flags                          */
                      NULL,    /* pointer to new environment block        */
                      NULL,    /* pointer to current directory name       */
                      &sinfo,  /* pointer to STARTUPINFO                  */
                      &pinfo)) /* pointer to PROCESS_INFORMATION          */
   {  /* second try: assume a command like dir, copy, BAT-program which needs command.com */
      char *p, szTemp[4096];
      p = mygetenv(TSD, "COMSPEC", szTemp, 4096);
      if (!p)
         strcpy(szTemp, "COMMAND.COM");
      strcat(szTemp, " /C ");
      strcat(szTemp, command);
      assert(strlen(szTemp) < 4096);
/*      printf("mysystem: 2nd try: <%s>\n", szTemp);*/

      if (!CreateProcess(NULL,    /* pointer to name of executable module    */
                         szTemp,  /* pointer to command line string          */
                         NULL,    /* pointer to process security attributes  */
                         NULL,    /* pointer to thread security attributes   */
                         TRUE,    /* handle inheritance flag                 */
                         0,       /* creation flags                          */
                         NULL,    /* pointer to new environment block        */
                         NULL,    /* pointer to current directory name       */
                         &sinfo,  /* pointer to STARTUPINFO                  */
                         &pinfo)) /* pointer to PROCESS_INFORMATION          */
      {
/*       printf("mysystem: 2nd try: cannot launch child process\n"); */
      return -1;
      }
   }

   /* wait until child process exits */
   WaitForSingleObject(pinfo.hProcess, INFINITE);
   GetExitCodeProcess(pinfo.hProcess, &exitCode);
   CloseHandle(pinfo.hProcess);
   CloseHandle(pinfo.hThread);
/*   printf("mysystem: exitCode %i\n", exitCode);*/
   return exitCode;
}
/*********************************************************/
int my_win32_setenv( const char *name, const char *value )
/*********************************************************/
{
   return (SetEnvironmentVariable( name, value ) );
}
#endif

/***********************************************************************/
static char *get_a_line(FILE *fp,int *length,int *rcode)
/***********************************************************************/
{
   int ch=' ';
   int bufs = 1;
   register int i=0;
   char *string;
/*---------------------------------------------------------------------*/
/* Allocate the first block of memory.                                 */
/*---------------------------------------------------------------------*/
   if ((string = (char *)malloc(BUF_SIZE+1)) == NULL) /* realloc allowed! */
   {
      *rcode = RC_OUT_OF_MEMORY;
      return(NULL);
   }

   for(;;)
   {
/*---------------------------------------------------------------------*/
/* Read a character form the stream...                                 */
/*---------------------------------------------------------------------*/
      if ((ch = fgetc(fp)) == EOF)
      {
/*---------------------------------------------------------------------*/
/* If EOF is reached, check that it really is end of file.             */
/*---------------------------------------------------------------------*/
         if (feof(fp))
         {
            *length = i;
            *rcode = RC_EOF;
            return(string);
         }
      }
/*---------------------------------------------------------------------*/
/* If end of line is reached, nul terminate string and return.         */
/*---------------------------------------------------------------------*/
      if ((char)ch == '\n')
      {
         *(string+i) = '\0';
         break;
      }
/*---------------------------------------------------------------------*/
/* All other characters, copy to string.                               */
/*---------------------------------------------------------------------*/
      *(string+i++) = (char)ch;
/*---------------------------------------------------------------------*/
/* If we have got to the end of the allocated memory, realloc some more*/
/*---------------------------------------------------------------------*/
      if (i == BUF_SIZE*bufs)
      {
         if ((string = (char *)realloc(string,(BUF_SIZE*(++bufs))+1)) == NULL)
         {
            *rcode = RC_OUT_OF_MEMORY;
            return(NULL);
         }
      }
   }
/*---------------------------------------------------------------------*/
/* Return a line read form the temporary file.                         */
/*---------------------------------------------------------------------*/
   *length = i;
   *rcode = 0;
   return(string);
}

/* create_tmpname create a temporary filename. It returns 1 on success and 0
 * if an error occurs. The argument should point to a buffer which is at least
 * REXX_PATH_MAX character long (incl. the term. 0). This buffer is filled on
 * success, only.
 * There are some limitations: All processes share the same name space.
 * (Well, maybe Regina only). If a new name is created this name will be
 * detected by another process as free, too. A new tmpname should be used
 * as fast as possible. Note, that the filename part of the tmpname is
 * limited to 8.3 since there might be problems with "higher" operating systems
 * when they access "lower" filesystems, e.g. OS/2 and FAT.
 */
int create_tmpname( const tsd_t *TSD, char *name )
{
   char *slash;
   char buf[REXX_PATH_MAX]; /* enough space for largest path name */
   unsigned i;
   unsigned long start,run;

   if ( mygetenv( TSD, "TMP", buf, sizeof(buf) ) == NULL)
   {
      if ( mygetenv( TSD, "TEMP", buf, sizeof(buf) ) == NULL)
      {
         if ( mygetenv( TSD, "TMPDIR", buf, sizeof(buf) ) == NULL)
         {
#ifdef UNIX
            strcpy(buf,"/tmp");
#else
            strcpy(buf,"C:");
#endif
         }
      }
   }

   if (strlen(buf) > REXX_PATH_MAX - 14 /* 8.3 + "\0" + ISLASH */)
      buf[REXX_PATH_MAX - 14] = '\0';

   if ( buf[strlen(buf)-1] != I_SLASH )
      slash = ISTR_SLASH;
   else
      slash = "";

   /* algorithm:
    * select a random number, e.g. a mixture of pid, tid and time.
    * increment this number by a fixed amount until we reach the starting
    * value again. Do a wrap around and an increment which is a unique prime.
    * The number 100000000 has the primes 2 and 5. We may use all primes
    * except 2 and 5; 9901 (which is prime) will give a good distribution.
    */
   start = TSD->thread_id;
   if (start == 0)
      start = 0xFFFFFFFFul;
   start *= (unsigned long) getpid();
   start *= (unsigned long) (clock() + 1);
   start %= 100000000ul;

   run = start;
   for (i = 0;i <= 10000;i++)            /* Not more than 10000 tries        */
   {
      /* form a name like "c:\temp\34503302._rx" or "/tmp/34503302._rx" */
      sprintf(name,"%s%s%08lu._rx", buf, slash, run );
      if (access(name,0) != 0)
         return(1);
      run += 9901;
      run %= 100000000ul;
      if (run == start)
         break;
   }
   return( 0 );
}

/***********************************************************************/
int dos_do_command( tsd_t *TSD, const streng *command, int io_flags, int dummy )
/***********************************************************************/
{
   int rc=0;
   streng *cmd=NULL ;
   int in=0, out=0, fout=0 ;
   streng *inredir=NULL,*outredir=NULL ;
   int length=0, rcode=0, flush=0 ;
   streng *result=NULL ;
   char *string=NULL;

   FILE *infp,*outfp;
   char infile[REXX_PATH_MAX];
   char outfile[REXX_PATH_MAX];

   in = io_flags & REDIR_INPUT ;
   out = io_flags & REDIR_OUTLIFO ;
   fout = io_flags & REDIR_OUTFIFO ;

   flush = (out!=0) + ((fout!=0)*2) ;

   if ((!in)&&(!out)&&(!fout))
   {
      ;       /* maybe this should not be allowed. ... */
   }
   dummy = dummy; /* keep compiler happy */
   cmd = NULL ;
/*---------------------------------------------------------------------*/
/* If redirecting stdin, create a temporary file and write the contents*/
/* of the stack to the file.                                           */
/*---------------------------------------------------------------------*/
   if (in)
   {
      if ( !create_tmpname( TSD, infile ) )
      {
         perror( "While getting temporary in-file name" ) ;
         return (-1);
      }
      if ( ( infp = fopen( infile, "w" ) ) == NULL )
      {
         perror( "While opening input file" ) ;
         return (-1);
      }
      while( !stack_empty( TSD ) )
      {
         result = popline( TSD, NULL, NULL, 0 ) ;
         fwrite( result->value, result->len, 1, infp ) ;
         fwrite( "\n", 1, 1, infp ) ;
         Free_stringTSD( result ) ;
      }
      if ( fclose( infp ) )
      {
         perror( "While closing input file" ) ;
         return (-1);
      }
      inredir = Str_makeTSD( strlen( infile ) + 4 );
      sprintf( inredir->value, "< %s ", str_trans( infile, '/', '\\' ) );
      inredir->len = strlen( inredir->value );
   }
/*---------------------------------------------------------------------*/
/* If redirecting stdout, create the name of a temporary file for the  */
/* output.                                                             */
/*---------------------------------------------------------------------*/
   if ((out)||(fout))
   {
      if ( !create_tmpname( TSD, outfile ) )
      {
         perror( "While getting temporary out-file name" ) ;
         return (-1);
      }
      outredir = Str_makeTSD( strlen( outfile ) + 3 );
      sprintf( outredir->value, "> %s", str_trans( outfile, '/', '\\' ) );
      outredir->len = strlen( outredir->value );
   }
/*---------------------------------------------------------------------*/
/* Build up the command to be passed to the system() command. The      */
/* command will contain the command to be executed, any arguments and  */
/* the redirection commands and file names.                            */
/*---------------------------------------------------------------------*/
   length = command->len + ((in) ? inredir->len : 0) + (((out)||(fout)) ? outredir->len : 0) + 2;
   cmd = Str_makeTSD(length);
   memcpy(cmd->value,command->value,command->len);
   cmd->len = command->len;
   if (in)
   {
      cmd = Str_catTSD(cmd,inredir);
      Free_stringTSD(inredir);
   }
   if (out||fout)
   {
      cmd = Str_catTSD(cmd,outredir);
      Free_stringTSD(outredir);
   }
/*---------------------------------------------------------------------*/
/* Execute the command.                                                */
/*---------------------------------------------------------------------*/
   string = str_of(TSD,cmd);
   rc = mysystem(TSD, string);
   FreeTSD(string);
   Free_stringTSD(cmd);
/*---------------------------------------------------------------------*/
/* If redirecting stdout, we now have to read the file and push each   */
/* line onto the stack.                                                */
/*---------------------------------------------------------------------*/
   if ((out)||(fout))
   {
      str_trans(outfile,'\\','/');
      if ((outfp = fopen(outfile,"r")) == NULL)
      {
         perror("While opening output file") ;
         return(-1);
      }

/*---------------------------------------------------------------------*/
/* If redirecting stdin, create a temporary file and write the contents*/
/* of the stack to the file.                                           */
/*---------------------------------------------------------------------*/
      for (;;)
      {
         string = get_a_line(outfp,&length,&rcode);
         if (rcode == RC_OUT_OF_MEMORY)
         {
            perror("While reading output file");
            return (-1);
         }
         if (rcode == RC_EOF && length == 0)
         {
            free(string);
            break;
         }
         result = Str_ncreTSD(string,length);
         tmp_stack(TSD,result,flush==2);
         free(string);
         if (rcode == RC_EOF)
            break;
      }
      if (flush)
         flush_stack(TSD, flush==2);

      if (fclose(outfp))
      {
         perror("While closing output file") ;
         return(-1);
      }
   }
/*---------------------------------------------------------------------*/
/* Delete the temporary file(s) and free up any memory.                */
/*---------------------------------------------------------------------*/
  if (in)
     unlink(str_trans(infile,'\\','/'));
  if ((out)||(fout))
     unlink(str_trans(outfile,'\\','/'));
/*---------------------------------------------------------------------*/
/* Return with, hopefully, return code from system() command.          */
/*---------------------------------------------------------------------*/
  return rc ;
}
# if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__SASC) || defined(__MINGW32__) || defined(__IBMC__) || defined(__BORLANDC__) || defined(MAC)
/********************************************************* MH 10-06-96 */
int uname(struct utsname *name)                         /* MH 10-06-96 */
/********************************************************* MH 10-06-96 */
{                                                       /* MH 10-06-96 */
#  if defined (WIN32)
 SYSTEM_INFO sysinfo;
 OSVERSIONINFO osinfo;
 char computername[MAX_COMPUTERNAME_LENGTH+1];
 char *pComputerName=computername;
 DWORD namelen=MAX_COMPUTERNAME_LENGTH+1;
#  endif
/*-------------------------------------------------------- MH 10-06-96 */
/* Set up values for utsname structure...                  MH 10-06-96 */
/*-------------------------------------------------------- MH 10-06-96 */
#  if defined(OS2)                                      /* MH 10-06-96 */
 strcpy(name->sysname,"OS2");                           /* MH 10-06-96 */
 sprintf(name->version,"%d",_osmajor);                  /* MH 10-06-96 */
 sprintf(name->release,"%d",_osminor);                  /* MH 10-06-96 */
 strcpy(name->nodename,"standalone");
 strcpy(name->machine,"i386");
#  elif defined(_AMIGA)
 strcpy(name->sysname,"AMIGA");
 sprintf(name->version,"%d",0);
 sprintf(name->release,"%d",0);
 strcpy(name->nodename,"standalone");
 strcpy(name->machine,"m68k");
#  elif defined(MAC)
 strcpy(name->sysname,"MAC");
 sprintf(name->version,"%d",0);
 sprintf(name->release,"%d",0);
 strcpy(name->nodename,"standalone");
 strcpy(name->machine,"m68k");
#  elif defined(WIN32)
 osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
 GetVersionEx(&osinfo);
 sprintf(name->version,"%d",osinfo.dwMajorVersion);
 sprintf(name->release,"%d",osinfo.dwMinorVersion);

 /* get specific OS version... */
 switch(osinfo.dwPlatformId)
 {
    case VER_PLATFORM_WIN32s:
       strcpy(name->sysname,"WIN32S");
       break;
    case VER_PLATFORM_WIN32_WINDOWS:
       if ( osinfo.dwMinorVersion >= 90 )
          strcpy(name->sysname,"WINME");
       else if ( osinfo.dwMinorVersion >= 10 )
          strcpy(name->sysname,"WIN98");
       else
          strcpy(name->sysname,"WIN95");
       break;
    case VER_PLATFORM_WIN32_NT:
       if ( osinfo.dwMajorVersion == 4 )
          strcpy(name->sysname,"WINNT");
       else if ( osinfo.dwMajorVersion == 5 )
          strcpy(name->sysname,"WIN2K");
       else
          strcpy(name->sysname,"UNKNOWN");
       break;
    default:
       strcpy(name->sysname,"UNKNOWN");
       break;
 }
 /* 
  * get name of computer if possible.
  */
 if ( GetComputerName(pComputerName,&namelen) )
    strcpy(name->nodename,pComputerName);
 else
    strcpy(name->nodename,"UNKNOWN");
 GetSystemInfo(&sysinfo);
 switch(sysinfo.dwProcessorType)
 {
    case PROCESSOR_INTEL_386:
       strcpy(name->machine,"i386");
       break;
    case PROCESSOR_INTEL_486:
       strcpy(name->machine,"i486");
       break;
    case PROCESSOR_INTEL_PENTIUM:
       strcpy(name->machine,"i586");
       break;
#if 0
    case PROCESSOR_INTEL_MIPS_R4000:
       strcpy(name->machine,"mipsR4000");
       break;
    case PROCESSOR_INTEL_ALPHA_21064:
       strcpy(name->machine,"alpha21064");
       break;
#endif

 }
#  endif /* WIN32 */

 return(0);
}
# endif
#endif