summaryrefslogtreecommitdiff
path: root/dmake/sysintf.c
blob: bed71916a1cd46ee4498656d6efbde03a755791c (plain)
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
/* RCS  $Id: sysintf.c,v 1.13 2008-03-05 18:30:58 kz Exp $
--
-- SYNOPSIS
--      System independent interface
--
-- DESCRIPTION
--  These are the routines constituting the system interface.
--  The system is taken to be essentially POSIX conformant.
--  The original code was extensively revised by T J Thompson at MKS,
--  and the library cacheing was added by Eric Gisin at MKS.  I then
--  revised the code yet again, to improve the lib cacheing, and to
--  make it more portable.
--
--  The following is a list of routines that are required by this file
--  in order to work.  These routines are provided as functions by the
--  standard C lib of the target system or as #defines in system/sysintf.h
--  or via appropriate C code in the system/ directory for the given
--  system.
--
--  The first group must be provided by a file in the system/ directory
--  the second group is ideally provided by the C lib.  However, there
--  are instances where the C lib implementation of the specified routine
--  does not exist, or is incorrect.  In these instances the routine
--  must be provided by the the user in the system/ directory of dmake.
--  (For example, the bsd/ dir contains code for putenv(), and tempnam())
--
--  DMAKE SPECIFIC:
--      seek_arch()
--      touch_arch()
--      void_lcache()
--      runargv()
--      DMSTAT()
--      Remove_prq()
--
--  C-LIB SPECIFIC:  (should be present in your C-lib)
--      utime()
--      time()
--      getenv()
--      putenv()
--      getcwd()
--      signal()
--      chdir()
--      tempnam()
--
-- AUTHOR
--      Dennis Vadura, dvadura@dmake.wticorp.com
--
-- WWW
--      http://dmake.wticorp.com/
--
-- COPYRIGHT
--      Copyright (c) 1996,1997 by WTI Corp.  All rights reserved.
--
--      This program is NOT free software; you can redistribute it and/or
--      modify it under the terms of the Software License Agreement Provided
--      in the file <distribution-root>/readme/license.txt.
--
-- LOG
--      Use cvs log to obtain detailed change logs.
*/

#include "extern.h"

/* The following definition controls the use of GetModuleFileName() */
#if defined(_MSC_VER) || defined(__MINGW32__)
#   define HAVE_GETMODULEFILENAMEFUNC 1

/* this is needed for the _ftime call below. Only needed here. */
#   include <sys/timeb.h>
#endif

/* for cygwin_conv_to_posix_path() in Prolog() and for cygdospath()*/
#if __CYGWIN__
#   include <sys/cygwin.h>
#endif

#include "sysintf.h"
#if HAVE_ERRNO_H
#  include <errno.h>
#else
   extern  int  errno;
#endif

/*
** Tries to stat the file name.  Returns 0 if the file
** does not exist.  Note that if lib is not null it tries to stat
** the name found inside lib.
**
** If member is NOT nil then look for the library object which defines the
** symbol given by name.  If found DmStrDup the name and return make the
** pointer pointed at by sym point at it.  Not handled for now!
*/
static time_t
really_dostat(name, buf)
char *name;
struct stat *buf;
{
   return( (   DMSTAT(name,buf)==-1
            || (STOBOOL(Augmake) && (buf->st_mode & S_IFDIR)))
       ? (time_t)0L
       : (time_t) buf->st_mtime
     );
}


PUBLIC time_t
Do_stat(name, lib, member, force)
char *name;
char *lib;
char **member;
int  force;
{
   struct stat buf;
   time_t seek_arch();

   if( member != NIL(char *) )
      Fatal("Library symbol names not supported");

   buf.st_mtime = (time_t)0L;
   if( lib != NIL(char) )
      return( seek_arch(Basename(name), lib) );
   else if( strlen(Basename(name)) > NameMax ) {
      Warning( "Filename [%s] longer than value of NAMEMAX [%d].\n\
      Assume unix time 0.\n", Basename(name), NameMax );
      return((time_t)0L);
   }
   else if( STOBOOL(UseDirCache) )
      return(CacheStat(name,force));
   else
      return(really_dostat(name,&buf));
}


/* Touch existing file to force modify time to present.
 */
PUBLIC int
Do_touch(name, lib, member)
char *name;
char *lib;
char **member;
{
   if( member != NIL(char *) )
      Fatal("Library symbol names not supported");

   if (lib != NIL(char))
      return( touch_arch(Basename(name), lib) );
   else if( strlen(Basename(name)) > NameMax ) {
      Warning( "Filename [%s] longer than value of NAMEMAX [%d].\n\
      File timestamp not updated to present time.\n", Basename(name), NameMax );
      return(-1);
   }
   else
#ifdef HAVE_UTIME_NULL
      return( utime(name, NULL) );
#else
#   error "Utime NULL not supported"
#endif
}



PUBLIC void
Void_lib_cache( lib_name, member_name )/*
=========================================
   Void the library cache for lib lib_name, and member member_name. */
char *lib_name;
char *member_name;
{
   VOID_LCACHE( lib_name, member_name );
}



/*
** return the current time
*/
PUBLIC time_t
Do_time()
{
   return (time( NIL(time_t) ));
}



/*
** Print profiling information
*/
PUBLIC void
Do_profile_output( text, mtype, target )
char *text;
uint16 mtype;
CELLPTR target;
{

   time_t time_sec;
   uint32 time_msec;
   char *tstrg;
   char *tname;

#ifdef HAVE_GETTIMEOFDAY
   struct timeval timebuffer;
   gettimeofday(&timebuffer, NULL);
   time_sec = timebuffer.tv_sec;
   time_msec = timebuffer.tv_usec/1000;
#else
#if defined(_MSC_VER) || defined(__MINGW32__)
   struct _timeb timebuffer;
   _ftime( &timebuffer );
   time_sec = timebuffer.time;
   time_msec = timebuffer.millitm;
#   else
   time_sec = time( NIL(time_t) );
   time_msec = 0;
#   endif
#endif

   tname = target->CE_NAME;
   if( mtype & M_TARGET ) {
      tstrg = "target";
      /* Don't print special targets .TARGETS and .ROOT */
      if( tname[0] == '.' && (strcmp(".TARGETS", tname) == 0 || \
                  strcmp(".ROOT", tname) == 0) ) {
     return;
      }
   } else {
      tstrg = "recipe";
   }

   /* Don't print shell escape targets if not especially requested. */
   if( (target->ce_attr & A_SHELLESC) && !(Measure & M_SHELLESC) ) {
      return;
   }

   /* Print absolute path if requested. */
   if( !(target->ce_attr & A_SHELLESC) && (Measure & M_ABSPATH) ) {
      printf("%s %s %lu.%.3u %s%s%s\n",text, tstrg, time_sec, time_msec, Pwd, DirSepStr, tname);
   } else {
      printf("%s %s %lu.%.3u %s\n",text, tstrg, time_sec, time_msec, tname);
   }
}



PUBLIC int
Do_cmnd(cmd, group, do_it, target, cmnd_attr, last)/*
=====================================================
  Execute the string passed in as a command and return
  the return code. The command line arguments are
  assumed to be separated by spaces or tabs.  The first
  such argument is assumed to be the command.

  If group is true then this is a group of commands to be fed to the
  the shell as a single unit.  In this case cmd is of the form
  "file" indicating the file that should be read by the shell
  in order to execute the command group.

  If Wait_for_completion is TRUE add the A_WFC attribute to the new
  process.
*/
char  **cmd; /* Simulate a reference to *cmd. */
int     group;  /* if set cmd contains the filename of a (group-)shell
         * script. */
int     do_it;  /* Only execute cmd if not set to null. */
CELLPTR target;
t_attr  cmnd_attr; /* Attributes for current cmnd. */
int     last;   /* Last recipe line in target. */
{
   int  i;

   DB_ENTER( "Do_cmnd" );

   if( !do_it ) {
      if( last && !Doing_bang ) {
     /* Don't execute, just update the target when using '-t'
      * switch. */
         Update_time_stamp( target );
      }
      DB_RETURN( 0 );
   }

   /* Stop making the rest of the recipies for this target if an error occurred
    * but the Continue (-k) flag is set to build as much as possible. */
   if ( target->ce_attr & A_ERROR ) {
      if ( last ) {
     Update_time_stamp( target );
      }
      DB_RETURN( 0 );
   }

   if( Max_proc == 1 ) Wait_for_completion = TRUE;

   /* Tell runargv() to wait if needed. */
   if( Wait_for_completion ) cmnd_attr |= A_WFC;

   /* remove leading whitespace - This should never trigger! */
   if( iswhite(**cmd) ) {
      char *p;
      if( (p = DmStrSpn(*cmd," \t") ) != *cmd )
     strcpy(*cmd,p);
   }

   /* set shell if shell metas are found */
   if( (cmnd_attr & A_SHELL) || group || (*DmStrPbrk(*cmd, Shell_metas)!='\0') )
      cmnd_attr |= A_SHELL; /* If group is TRUE this doesn't hurt. */

   /* runargv() returns either 0 or 1, 0 ==> command executed, and
    * we waited for it to return, 1 ==> command started and is still
    * running. */
   i = runargv(target, group, last, cmnd_attr, cmd);

   DB_RETURN( i );
}


#define MINARGV 64

PUBLIC char **
Pack_argv( group, shell, cmd )/*
================================
  Take a command and pack it into an argument vector to be executed.
  If group is true cmd holds the group script file.
*/
int    group;
int    shell;
char **cmd; /* Simulate a reference to *cmd. */
{
   static char **av = NIL(char *);
   static int   avs = 0;
   int i = 0;
   char *s; /* Temporary string pointer. */

   if( av == NIL(char *) ) {
      TALLOC(av, MINARGV, char*);
      avs = MINARGV;
   }
   av[0] = NIL(char);

   if (**cmd) {
      if( shell||group ) {
     char* sh = group ? GShell : Shell;

     if( sh != NIL(char) ) {
        av[i++] = sh;
        if( (av[i] = (group?GShell_flags:Shell_flags)) != NIL(char) ) i++;

        if( shell && Shell_quote && *Shell_quote ) {
           /* Enclose the shell command with SHELLCMDQUOTE. */
           s = DmStrJoin(Shell_quote, *cmd, -1, FALSE);
           FREE(*cmd);
           *cmd = DmStrJoin(s, Shell_quote, -1, TRUE);
        }
        av[i++] = *cmd;

#if defined(USE_CREATEPROCESS)
        /* CreateProcess() needs one long command line. */
        av[0] = DmStrAdd(av[0], av[1], FALSE);
        av[1] = NIL(char);
        /* i == 3 means Shell_flags are given. */
        if( i == 3 ) {
           s = av[0];
           av[0] = DmStrAdd(s, av[2], FALSE);
           FREE(s);
           av[2] = NIL(char);
        }
        /* The final free of cmd will free the concated command line. */
        FREE(*cmd);
        *cmd = av[0];
#endif
        av[i]   = NIL(char);
     }
     else
        Fatal("%sSHELL macro not defined", group?"GROUP":"");
      }
      else {
     char *tcmd = *cmd;

#if defined(USE_CREATEPROCESS)
     /* CreateProcess() needs one long command line, fill *cmd
      * into av[0]. */
     while( iswhite(*tcmd) ) ++tcmd;
     if( *tcmd ) av[i++] = tcmd;
#else
     /* All other exec/spawn functions need the parameters separated
      * in the argument vector. */
     do {
        /* Fill *cmd into av[]. Whitespace is converted into '\0' to
         * terminate each av[] member. */
        while( iswhite(*tcmd) ) ++tcmd;
        if( *tcmd ) av[i++] = tcmd;

        while( *tcmd != '\0' && !iswhite(*tcmd) ) ++tcmd;
        if( *tcmd ) *tcmd++ = '\0';

        /* dynamically increase av size. */
        if( i == avs ) {
           avs += MINARGV;
           av = (char **) realloc( av, avs*sizeof(char *) );
        }
     } while( *tcmd );
#endif

     av[i] = NIL(char);
      }
   }

   return(av);
}


/*
** Return the value of ename from the environment
** if ename is not defined in the environment then
** NIL(char) should be returned
*/
PUBLIC char *
Read_env_string(ename)
char *ename;
{
   return( getenv(ename) );
}



/*
** Set the value of the environment string ename to value.
**  Returns 0 if success, non-zero if failure
*/
PUBLIC int
Write_env_string(ename, value)
char *ename;
char *value;
{
   char*   p;
   char*   envstr = DmStrAdd(ename, value, FALSE);

   p = envstr+strlen(ename);    /* Don't change this code, DmStrAdd does not */
   *p++ = '=';          /* add the space if *value is 0, it does    */
   if( !*value ) *p = '\0'; /* allocate enough memory for one though.   */

   return( putenv(envstr) );
}



PUBLIC void
ReadEnvironment()
{
   extern char **Rule_tab;
#if !defined(_MSC_VER)
#if defined(__BORLANDC__) && __BORLANDC__ >= 0x500
   extern char ** _RTLENTRY _EXPDATA environ;
#else
   extern char **environ;
#endif
#endif
   char **rsave;

#if !defined(__ZTC__) && !defined(_MPW)
# define make_env()
# define free_env()
#else
   void make_env();
   void free_env();
#endif

   make_env();

   rsave    = Rule_tab;
   Rule_tab = environ;
   Readenv  = TRUE;

   Parse( NIL(FILE) );

   Readenv  = FALSE;
   Rule_tab = rsave;

   free_env();
}



/*
** All we have to catch is SIGINT
*/
PUBLIC void
Catch_signals(fn)
void (*fn)(int);
{
   /* FIXME: Check this and add error handling. */
   if( (void (*)(int)) signal(SIGINT, SIG_IGN) != (void (*)(int))SIG_IGN )
      signal( SIGINT, fn );
   if( (void (*)(int)) signal(SIGQUIT, SIG_IGN) != (void (*)(int))SIG_IGN )
      signal( SIGQUIT, fn );
}



/*
** Clear any previously set signals
*/
PUBLIC void
Clear_signals()
{
   if( (void (*)())signal(SIGINT, SIG_IGN) != (void (*)())SIG_IGN )
      signal( SIGINT, SIG_DFL );
   if( (void (*)())signal(SIGQUIT, SIG_IGN) != (void (*)())SIG_IGN )
      signal( SIGQUIT, SIG_DFL );
}



/*
** Set program name
*/
PUBLIC void
Prolog(argc, argv)
int   argc;
char* argv[];
{
   Pname = (argc == 0) ? DEF_MAKE_PNAME : argv[0];

   /* Only some native Windows compilers provide this functionality. */
#ifdef HAVE_GETMODULEFILENAMEFUNC
   if( (AbsPname = MALLOC( PATH_MAX, char)) == NIL(char) ) No_ram();
   GetModuleFileName(NULL, AbsPname, PATH_MAX*sizeof(char));
#else
   AbsPname = "";
#endif

#if __CYGWIN__
   /* Get the drive letter prefix used by cygwin. */
   if ( (CygDrvPre = MALLOC( PATH_MAX, char)) == NIL(char) )
      No_ram();
   else {
      int err = cygwin_conv_to_posix_path("c:", CygDrvPre);
      if (err)
     Fatal( "error converting \"%s\" - %s\n",
        CygDrvPre, strerror (errno));
      if( (CygDrvPreLen = strlen(CygDrvPre)) == 2 ) {
     /* No prefix */
     *CygDrvPre = '\0';
     CygDrvPreLen = 0;
      } else {
     /* Cut away the directory letter. */
     CygDrvPre[CygDrvPreLen-2] = '\0';
     /* Cut away the leading '/'. We don't free the pointer, i.e. choose
      * the easy way. */
     CygDrvPre++;
     CygDrvPreLen -= 3;
      }
   }
#endif

   /* DirSepStr is used from Clean_path() in Def_cell(). Set it preliminary
    * here, it will be redefined later in Create_macro_vars() in imacs.c. */
   DirSepStr = "/";

   Root = Def_cell( ".ROOT" );
   Targets = Def_cell( ".TARGETS" );
   Add_prerequisite(Root, Targets, FALSE, FALSE);

   Targets->ce_flag = Root->ce_flag = F_RULES|F_TARGET|F_STAT;
   Targets->ce_attr = Root->ce_attr = A_NOSTATE|A_PHONY;

   Root->ce_flag |= F_MAGIC;
   Root->ce_attr |= A_SEQ;

   tzset();
}



/*
** Do any clean up for exit.
*/
PUBLIC void
Epilog(ret_code)
int ret_code;
{
   Write_state();
   Unlink_temp_files(Root);
   Hook_std_writes(NIL(char));      /* For MSDOS tee (-F option) */
   exit( ret_code );
}



/*
** Use the built-in functions of the operating system to get the current
** working directory.
*/
PUBLIC char *
Get_current_dir()
{
   static char buf[PATH_MAX+2];

   if( !getcwd(buf, sizeof(buf)) )
      Fatal("Internal Error: Error when calling getcwd()!");

#ifdef __EMX__
   char *slash;
   slash = buf;
   while( (slash=strchr(slash,'/')) )
      *slash = '\\';
#endif

   return buf;
}



/*
** change working directory
*/
PUBLIC int
Set_dir(path)
char*   path;
{
   return( chdir(path) );
}



/*
** return switch char
*/
PUBLIC char
Get_switch_char()
{
   return( getswitchar() );
}


int Create_temp(tmpdir, path)/*
===============================
  Create a temporary file and open with exclusive access
  Path is updated with the filename and the file descriptor
  is returned.  Note that the new name should be freed when
  the file is removed.
*/
char *tmpdir;
char **path;
{
   int fd; /* file descriptor */

#if defined(HAVE_MKSTEMP)
   mode_t       mask;

   *path = DmStrJoin( tmpdir, DirSepStr, -1, FALSE);
   *path = DmStrJoin( *path, "mkXXXXXX", -1, TRUE );

   mask = umask(0066);
   fd = mkstemp( *path );
   umask(mask);

#elif defined(HAVE_TEMPNAM)
   char pidbuff[32];
#if _MSC_VER >= 1300
   /* Create more unique filename for .NET2003 and newer. */
   long npid;
   long nticks;

   npid = _getpid();
   nticks = GetTickCount() & 0xfff;
   sprintf(pidbuff,"mk%d_%d_",npid,nticks);
#else
   sprintf(pidbuff,"mk");
#endif
   *path = tempnam(tmpdir, pidbuff);
   fd = open(*path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0600);
#else

#error mkstemp() or tempnam() is needed

#endif

   return fd;
}


PUBLIC FILE*
Get_temp(path, mode)/*
======================
  Generate a temporary file name and open the file for writing.
  If a name cannot be generated or the file cannot be opened
  return -1, else return the fileno of the open file.
  and update the source file pointer to point at the new file name.
  Note that the new name should be freed when the file is removed.
  The file stream is opened with the given mode.
*/
char **path;
char *mode;
{
   int          fd;
   FILE         *fp;
   char         *tmpdir;
   int          tries = 20;

   tmpdir = Read_env_string( "TMPDIR" );
   if( tmpdir == NIL(char) )
      tmpdir = "/tmp";

   while( --tries )
   {
      /* This sets path to the name of the created temp file. */
      if( (fd = Create_temp(tmpdir, path)) != -1)
         break;

      free(*path); /* free var if creating temp failed. */
   }

   if( fd != -1)
   {
      Def_macro( "TMPFILE", DO_WINPATH(*path), M_MULTI|M_EXPANDED );
      /* associate stream with file descriptor */
      fp = fdopen(fd, mode);
   }
   else
      fp = NIL(FILE);

   return fp;
}


PUBLIC FILE *
Start_temp( suffix, cp, fname )/*
=================================
  Open a new temporary file and set it up for writing. The file is linked
  to cp and will be removed if once the target is finished.
  If a suffix for the temporary files is requested two temporary files are
  created. This is done because the routines that create a save temporary
  file do not provide a definable suffix. The first (provided by Get_temp())
  is save and unique and the second file is generated by adding the desired
  suffix the the first temporary file. The extra file is also linked to cp
  so that it gets removed later.
  The function returns the FILE pointer to the temporary file (with suffix
  if specified) and leaves the file name in *fname.
*/
char     *suffix;
CELLPTR   cp;
char    **fname;
{
   FILE        *fp, *fp2;
   char        *tmpname;
   char        *name;
   char        *fname_suff;

   name = (cp != NIL(CELL))?cp->CE_NAME:"makefile text";

   /* This sets tmpname to the name that was used. */
   if( (fp = Get_temp(&tmpname, "w")) == NIL(FILE) )
      Open_temp_error( tmpname, name );

   /* Don't free tmpname, it's stored in a FILELIST member in Link_temp(). */
   Link_temp( cp, fp, tmpname );
   *fname = tmpname;

   /* As Get_temp() doesn't provide a definable suffix (anymore) we create an
    * additional temporary file with that suffix. */
   if ( suffix && *suffix ) {

#ifdef HAVE_MKSTEMP
      /* Only use umask if we are also using mkstemp - this basically
       * avoids using the incompatible implementation from MSVC. */
      mode_t mask;

      mask = umask(0066);
#endif

      fname_suff = DmStrJoin( tmpname, suffix, -1, FALSE );

      /* Overwrite macro, Get_temp didn't know of the suffix. */
      Def_macro( "TMPFILE", DO_WINPATH(fname_suff), M_MULTI|M_EXPANDED );

      if( (fp2 = fopen(fname_suff, "w" )) == NIL(FILE) )
         Open_temp_error( fname_suff, name );
#ifdef HAVE_MKSTEMP
      umask(mask);
#endif

      /* Don't free fname_suff. */
      Link_temp( cp, fp2, fname_suff );
      fp = fp2;
      *fname = fname_suff;
   }

   return( fp );
}


/*
** Issue an error on failing to open a temporary file
*/
PUBLIC void
Open_temp_error( tmpname, name )
char *tmpname;
char *name;
{
   Fatal("Cannot open temp file `%s' while processing `%s'", tmpname, name );
}


/*
** Link a temp file onto the list of files.
*/
PUBLIC void
Link_temp( cp, fp, fname )
CELLPTR cp;
FILE   *fp;
char   *fname;
{
   FILELISTPTR new;

   if( cp == NIL(CELL) ) cp = Root;

   TALLOC( new, 1, FILELIST );

   new->fl_next = cp->ce_files;
   new->fl_name = fname;
   new->fl_file = fp;       /* indicates temp file is open */

   cp->ce_files = new;
}


/*
** Close a previously used temporary file.
*/
PUBLIC void
Close_temp(cp, file)
CELLPTR cp;
FILE    *file;
{
   FILELISTPTR fl;
   if( cp == NIL(CELL) ) cp = Root;

   for( fl=cp->ce_files; fl && fl->fl_file != file; fl=fl->fl_next );
   if( fl ) {
      fl->fl_file = NIL(FILE);
      fclose(file);
   }
}


/*
** Clean-up, and close all temporary files associated with a target.
*/
PUBLIC void
Unlink_temp_files( cp )/*
==========================
   Unlink the tempfiles if any exist.  Make sure you close the files first
   though.  This ensures that under DOS there is no disk space lost. */
CELLPTR cp;
{
   FILELISTPTR cur, next;

   if( cp == NIL(CELL) || cp->ce_files == NIL(FILELIST) ) return;

   for( cur=cp->ce_files; cur != NIL(FILELIST); cur=next ) {
      next = cur->fl_next;

      if( cur->fl_file ) fclose( cur->fl_file );

      if( Verbose & V_LEAVE_TMP )
         fprintf( stderr, "%s:  Left temp file [%s]\n", Pname, cur->fl_name );
      else
         (void) Remove_file( cur->fl_name );

      FREE(cur->fl_name);
      FREE(cur);
   }

   cp->ce_files = NIL(FILELIST);
}


PUBLIC void
Handle_result(status, ignore, abort_flg, target)/*
==================================================
  Handle return value of recipe.
*/
int status;
int ignore;
int abort_flg;
CELLPTR target;
{
   status = ((status&0xff)==0 ? status>>8   /* return from exit()      */
         : (status & 0xff)==SIGTERM ? -1 /* terminated from SIGTERM */
         : (status & 0x7f)+128);         /* terminated from signal
                          *         ( =status-128 ) */

   if( status ) {
      if( !abort_flg ) {
     char buf[512];

     sprintf(buf, "%s:  Error code %d, while making '%s'",
         Pname, status, target->ce_fname );

     if( ignore || Continue ) {
        if (!(Glob_attr & A_SILENT)) {
           strcat(buf, " (Ignored" );

           if ( Continue ) {
          /* Continue after error if '-k' was used. */
          strcat(buf,",Continuing");
          target->ce_attr |= A_ERROR;
           }
           strcat(buf,")");
           if (Verbose)
          fprintf(stderr, "%s\n", buf);
        }

        if( target->ce_attr & A_ERRREMOVE
        && Remove_file( target->ce_fname ) == 0
        && !(Glob_attr & A_SILENT))
           fprintf(stderr,"%s:  '%s' removed.\n", Pname, target->ce_fname);
     }
     else {
        fprintf(stderr, "%s\n",buf);

        if(!(target->ce_attr & A_PRECIOUS)||(target->ce_attr & A_ERRREMOVE))
           if( Remove_file( target->ce_fname ) == 0 )
          fprintf(stderr,"%s:  '%s' removed.\n", Pname,
              target->ce_fname);

        Quit(0);
     }
      }
      else if(!(target->ce_attr & A_PRECIOUS)||(target->ce_attr & A_ERRREMOVE))
     Remove_file( target->ce_fname );
   }
}


PUBLIC void
Update_time_stamp( cp )/*
=========================
   Update the time stamp of cp and scan the list of its prerequisites for
   files being marked as removable (ie. an inferred intermediate node).
   Remove them if there are any. */
CELLPTR cp;
{
   HASHPTR hp;
   LINKPTR dp;
   CELLPTR tcp;
   time_t  mintime;
   int     phony = ((cp->ce_attr&A_PHONY) != 0);

   for(dp=CeMeToo(cp); dp; dp=dp->cl_next) {
      tcp=dp->cl_prq;
      /* When calling Make() on this target ce_time was set to the minimal
       * required time the target should have after building, i.e. the time
       * stamp of the newest prerequisite or 1L if there is no
       * prerequisite. */
      mintime = tcp->ce_time;

      if( tcp->ce_attr & A_LIBRARY )
     Void_lib_cache( tcp->ce_fname, NIL(char) );
      else if( !Touch && (tcp->ce_attr & A_LIBRARYM) )
     Void_lib_cache( tcp->ce_lib, tcp->ce_fname );

      /* phony targets are treated as if they were recently made
       * and get the current time assigned. */
      if( phony ) {
     tcp->ce_time = Do_time();
      }
      else if (Trace) {
     tcp->ce_time = Do_time();
      }
      else {
     Stat_target(tcp, -1, TRUE);

     if( tcp->ce_time == (time_t) 0L ) {
        /* If the target does not exist after building set its
         * time stamp depending if it has recipes or not. Virtual
         * Targets (without recipes) get the newest time stamp of
         * its prerequisites assigned. (This was conveniently stored
         * in mintime.)
         * Targets with recipes are treated as if they were recently
         * made and get the current time assigned. */
        if( cp->ce_recipe == NIL(STRING) && mintime > 1 ) {
           tcp->ce_time = mintime;
        }
        else {
           tcp->ce_time = Do_time();
        }
     }
     else {
        /* The target exist. If the target does not have recipe
         * lines use the newest time stamp of either the target or
         * the newest time stamp of its prerequisites and issue
         * a warning. */
        if( cp->ce_recipe == NIL(STRING) ) {
           time_t  newtime = ( mintime > 1 ? mintime : Do_time() );

           if( !(tcp->ce_attr & A_SILENT) )
          Warning( "Found file corresponding to virtual target [%s].",
               tcp->CE_NAME );

           if( newtime > tcp->ce_time )
          tcp->ce_time = mintime;
        }
     }
      }

      if( Trace ) {
     tcp->ce_flag |= F_STAT;        /* pretend we stated ok */
      }

      if( Verbose & V_MAKE )
     printf( "%s:  <<<< Set [%s] time stamp to %lu\n",
         Pname, tcp->CE_NAME, tcp->ce_time );

      if( Measure & M_TARGET )
     Do_profile_output( "e", M_TARGET, tcp );

      /* At this point cp->ce_time is updated to either the actual file
       * time or the current time. */
      DB_PRINT( "make", ("time stamp: %ld, required mintime: %ld",
             cp->ce_time, mintime) );
      if( tcp->ce_time < mintime && !(tcp->ce_attr & A_SILENT) ) {
     Warning( "Target [%s] was made but the time stamp has not been updated.",
          tcp->CE_NAME );
      }

      /* The target was made, remove the temp files now. */
      Unlink_temp_files( tcp );
      tcp->ce_flag |= F_MADE;
      tcp->ce_attr |= A_UPDATED;
   }

   /* Scan the list of prerequisites and if we find one that is
    * marked as being removable, (ie. an inferred intermediate node)
    * then remove it.  We remove a prerequisite by running the recipe
    * associated with the special target .REMOVE.
    * Typically .REMOVE is defined in the startup file as:
    *  .REMOVE :; $(RM) $<
    * with $< being the list of prerequisites specified in the current
    * target. (Make() sets $< .) */

   /* Make sure we don't try to remove prerequisites for the .REMOVE
    * target. */
   if( strcmp(cp->CE_NAME,".REMOVE") != 0 &&
       (hp = Get_name(".REMOVE", Defs, FALSE)) != NIL(HASH) ) {
      register LINKPTR dp;
      int flag = FALSE;
      int rem;
      t_attr attr;

      tcp = hp->CP_OWNR;

      /* The .REMOVE target is re-used. Remove old prerequisites. */
      tcp->ce_flag |= F_TARGET;
      Clear_prerequisites( tcp );

      for(dp=cp->ce_prq; dp != NIL(LINK); dp=dp->cl_next) {
     register CELLPTR prq = dp->cl_prq;

     attr = Glob_attr | prq->ce_attr;
     /* We seem to have problems here that F_MULTI subtargets get removed
      * that even though they are still needed because the A_PRECIOUS
      * was not propagated correctly. Solution: Don't remove subtargets, the
      * master target will be removed if is not needed. */
     rem  = (prq->ce_flag & F_REMOVE) &&
        (prq->ce_flag & F_MADE  ) &&
            !(prq->ce_count ) && /* Don't remove F_MULTI subtargets. */
        !(prq->ce_attr & A_PHONY) &&
        !(attr & A_PRECIOUS);

     /* remove if rem is != 0 */
     if(rem) {
        LINKPTR tdp;

        /* Add the target plus all that are linked to it with the .UPDATEALL
         * attribute. */
        for(tdp=CeMeToo(prq); tdp; tdp=tdp->cl_next) {
           CELLPTR tmpcell=tdp->cl_prq;

           (Add_prerequisite(tcp,tmpcell,FALSE,FALSE))->cl_flag|=F_TARGET;
           tmpcell->ce_flag &= ~F_REMOVE;
        }
        flag = TRUE;
     }
      }

      if( flag ) {
     int sv_force = Force;

     Force = FALSE;
     Remove_prq( tcp );
     Force = sv_force;

     for(dp=tcp->ce_prq; dp != NIL(LINK); dp=dp->cl_next) {
        register CELLPTR prq = dp->cl_prq;

        prq->ce_flag &= ~(F_MADE|F_VISITED|F_STAT);
        prq->ce_flag |= F_REMOVE;
        prq->ce_time  = (time_t)0L;
     }
      }
   }
}


PUBLIC int
Remove_file( name )
char *name;
{
   struct stat buf;

   if( stat(name, &buf) != 0 )
      return 1;
   if( (buf.st_mode & S_IFMT) == S_IFDIR )
      return 1;
   return(unlink(name));
}


#if defined(__CYGWIN__)
char *
cygdospath(char *src, int winpath)/*
====================================
   Convert to DOS path if winpath is true. The returned pointer is
   either the original pointer or a pointer to a static buffer.
*/
{
   static char *buf = NIL(char);

   if ( !buf && ( (buf = MALLOC( PATH_MAX, char)) == NIL(char) ) )
      No_ram();

   DB_PRINT( "cygdospath", ("converting [%s] with winpath [%d]", src, winpath ) );

   /* Return immediately on NULL pointer or when .WINPATH is
    * not set. */
   if( !src || !winpath )
      return src;

   if( *src && src[0] == '/' ) {
      char *tmp;
      int err = cygwin_conv_to_win32_path(src, buf);
      if (err)
     Fatal( "error converting \"%s\" - %s\n",
        src, strerror (errno));

      tmp = buf;
      while ((tmp = strchr (tmp, '\\')) != NULL) {
     *tmp = '/';
     tmp++;
      }

      return buf;
   }
   else
      return src;
}
#endif