File: files.c

package info (click to toggle)
mawk 1.3.3-17
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd, wheezy
  • size: 1,484 kB
  • ctags: 1,506
  • sloc: ansic: 13,008; yacc: 994; awk: 629; makefile: 113; sh: 107
file content (621 lines) | stat: -rw-r--r-- 12,089 bytes parent folder | download | duplicates (6)
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

/********************************************
files.c
copyright 1991-94.  Michael D. Brennan

This is a source file for mawk, an implementation of
the AWK programming language.

Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/

/*$Log: files.c,v $
 * Revision 1.9  1996/01/14  17:14:10  mike
 * flush_all_output()
 *
 * Revision 1.8  1995/06/06  00:18:27  mike
 * change mawk_exit(1) to mawk_exit(2)
 *
 * Revision 1.7  1994/12/11  20:48:50  mike
 * fflush builtin
 *
 * Revision 1.6  1994/10/08  19:15:40  mike
 * remove SM_DOS
 *
 * Revision 1.5  1994/04/17  20:01:37  mike
 * recognize filename "/dev/stdout"
 *
 * Revision 1.4  1994/02/21  00:11:07  mike
 * code cleanup
 *
 * Revision 1.3  1993/07/16  01:00:36  mike
 * cleanup and indent
 *
 * Revision 5.5	 1992/12/17  02:48:01  mike
 * 1.1.2d changes for DOS
 *
 * Revision 5.4	 1992/07/10  16:10:30  brennan
 * patch2
 * MsDOS: remove useless NO_BINMODE macro
 * get process exit code on in pipes
 *
 * Revision 5.3	 1992/04/07  20:21:17  brennan
 * patch 2
 * unbuffered output to a tty
 *
 * Revision 5.2	 1992/04/07  16:03:08  brennan
 * patch 2
 * allow same filename for output and input, but use different descriptors
 * E.g. < "/dev/tty" and > "/dev/tty"
 *
 * Revision 5.1	 91/12/05  07:56:00  brennan
 * 1.1 pre-release
 *
*/

/* files.c */

#include "mawk.h"
#include "files.h"
#include "memory.h"
#include "fin.h"

static FILE *PROTO(tfopen, (char *, char *)) ;
static void PROTO(efflush, (FILE*)) ;
static void PROTO(add_to_child_list, (int, int)) ;
static struct child *PROTO(remove_from_child_list, (int)) ;
extern int PROTO(isatty, (int)) ;

#ifdef	V7
#include  <sgtty.h>		/* defines FIOCLEX */
#endif


#ifndef	 NO_FCNTL_H

#include <fcntl.h>
#define	 CLOSE_ON_EXEC(fd)    fcntl(fd, F_SETFD, 1)

#else
#define	 CLOSE_ON_EXEC(fd) ioctl(fd, FIOCLEX, (PTR) 0)
#endif


/* We store dynamically created files on a linked linear
   list with move to the front (big surprise)  */

typedef struct file
{
   struct file *link ;
   STRING *name ;
   short type ;
   int pid ;			 /* we need to wait() when we close a pipe */
   /* holds temp file index under MSDOS */

#if  HAVE_FAKE_PIPES
   int inpipe_exit ;
#endif

   PTR ptr ;			 /* FIN*   or  FILE*   */
}
FILE_NODE ;

static FILE_NODE *file_list ;


/* find a file on file_list */
PTR
file_find(sval, type)
   STRING *sval ;
   int type ;
{
   register FILE_NODE *p = file_list ;
   FILE_NODE *q = (FILE_NODE *) 0 ;
   char *name = sval->str ;
   char *ostr ;

   while (1)
   {
      if (!p)
      {
	 /* open a new one */
	 p = ZMALLOC(FILE_NODE) ;

	 switch (p->type = type)
	 {
	    case F_TRUNC:
#if MSDOS
	       ostr = (binmode() & 2) ? "wb" : "w" ;
#else
	       ostr = "w" ;
#endif
	       if (!(p->ptr = (PTR) tfopen(name, ostr)))
		  goto out_failure ;
	       break ;

	    case F_APPEND:
#if MSDOS
	       ostr = (binmode() & 2) ? "ab" : "a" ;
#else
	       ostr = "a" ;
#endif
	       if (!(p->ptr = (PTR) tfopen(name, ostr)))
		  goto out_failure ;
	       break ;

	    case F_IN:
	       if (!(p->ptr = (PTR) FINopen(name, 0)))
	       {
		  zfree(p, sizeof(FILE_NODE)) ;
		  return (PTR) 0 ;
	       }
	       break ;

	    case PIPE_OUT:
	    case PIPE_IN:

#if    HAVE_REAL_PIPES || HAVE_FAKE_PIPES

	       if (!(p->ptr = get_pipe(name, type, &p->pid)))
	       {
		  if (type == PIPE_OUT)	 goto out_failure ;
		  else
		  {
		     zfree(p, sizeof(FILE_NODE)) ;
		     return (PTR) 0 ;
		  }
	       }
#else
	       rt_error("pipes not supported") ;
#endif
	       break ;

#ifdef	DEBUG
	    default:
	       bozo("bad file type") ;
#endif
	 }
	 /* successful open */
	 p->name = sval ;
	 sval->ref_cnt++ ;
	 break ;		 /* while loop */
      }

      /* search is by name and type */
      if (strcmp(name, p->name->str) == 0 &&
	  (p->type == type ||
      /* no distinction between F_APPEND and F_TRUNC here */
	   p->type >= F_APPEND && type >= F_APPEND))

      {
	 /* found */
	 if (!q)		/*at front of list */
	    return p->ptr ;
	 /* delete from list for move to front */
	 q->link = p->link ;
	 break ;		 /* while loop */
      }

      q = p ; p = p->link ;
   }				/* end while loop */

   /* put p at the front of the list */
   p->link = file_list ;
   return (PTR) (file_list = p)->ptr ;

out_failure:
   errmsg(errno, "cannot open \"%s\" for output", name) ;
   mawk_exit(2) ;

}


/* Close a file and delete it's node from the file_list.
   Walk the whole list, in case a name has two nodes,
   e.g. < "/dev/tty" and > "/dev/tty"
*/

int
file_close(sval)
   STRING *sval ;
{
   FILE_NODE dummy ;
   register FILE_NODE *p ;
   FILE_NODE *q = &dummy ;	 /* trails p */
   FILE_NODE *hold ;
   char *name = sval->str ;
   int retval = -1 ;

   dummy.link = p = file_list ;
   while (p)
   {
      if (strcmp(name, p->name->str) == 0)
      {
	 /* found */
	 switch (p->type)
	 {
	    case F_TRUNC:
	    case F_APPEND:
	       fclose((FILE *) p->ptr) ;
	       retval = 0 ;
	       break ;

	    case PIPE_OUT:
	       fclose((FILE *) p->ptr) ;

#if  HAVE_REAL_PIPES
	       retval = wait_for(p->pid) ;
#endif
#if  HAVE_FAKE_PIPES
	       retval = close_fake_outpipe(p->name->str, p->pid) ;
#endif
	       break ;

	    case F_IN:
	       FINclose((FIN *) p->ptr) ;
	       retval = 0 ;
	       break ;

	    case PIPE_IN:
	       FINclose((FIN *) p->ptr) ;

#if  HAVE_REAL_PIPES
	       retval = wait_for(p->pid) ;
#endif
#if  HAVE_FAKE_PIPES
	       {
		  char xbuff[100] ;
		  unlink(tmp_file_name(p->pid, xbuff)) ;
		  retval = p->inpipe_exit ;
	       }
#endif
	       break ;
	 }

	 free_STRING(p->name) ;
	 hold = p ;
	 q->link = p = p->link ;
	 ZFREE(hold) ;
      }
      else
      {
	 q = p ; p = p->link ; 
      }
   }

   file_list = dummy.link ;
   return retval ;
}

/*
find an output file with name == sval and fflush it
*/

int
file_flush(sval)
   STRING *sval ;
{
   int ret = -1 ;
   register FILE_NODE *p = file_list ;
   unsigned len = sval->len ;
   char *str = sval->str ;

   if (len==0) 
   {
      /* for consistency with gawk */
      flush_all_output() ;
      return 0 ;
   }
      
   while( p )
   {
      if ( IS_OUTPUT(p->type) &&
	   len == p->name->len &&
	   strcmp(str,p->name->str) == 0 )
      {
	 ret = 0 ;
	 efflush((FILE*)p->ptr) ;
         /* it's possible for a command and a file to have the same
	    name -- so keep looking */
      }
      p = p->link ;
   }
   return ret ;
}

void
flush_all_output() 
{
   FILE_NODE *p ;

   for(p=file_list; p ; p = p->link)
      if (IS_OUTPUT(p->type)) efflush((FILE*)p->ptr) ;
}

static void
efflush(fp)
   FILE *fp ;
{
   if (fflush(fp) < 0)
   {
      errmsg(errno, "unexpected write error") ;
      mawk_exit(2) ;
   }
}


/* When we exit, we need to close and wait for all output pipes */

#if   HAVE_REAL_PIPES

/* work around for bug in AIX 4.1 -- If there are exactly 16 or 
   32 or 48 ..., open files then the last one doesn't get flushed on
   exit.  So the following is now a misnomer as we'll really close
   all output.
*/

void
close_out_pipes()
{
   register FILE_NODE *p = file_list ;

   while (p)
   {
      if (IS_OUTPUT(p->type))
      {
	 fclose((FILE *) p->ptr) ;   
	 if (p->type == PIPE_OUT) wait_for(p->pid) ; 
      }

      p = p->link ;
   }
}

#else
#if  HAVE_FAKE_PIPES		/* pipes are faked with temp files */

void
close_fake_pipes()
{
   register FILE_NODE *p = file_list ;
   char xbuff[100] ;

   /* close input pipes first to free descriptors for children */
   while (p)
   {
      if (p->type == PIPE_IN)
      {
	 FINclose((FIN *) p->ptr) ;
	 unlink(tmp_file_name(p->pid, xbuff)) ;
      }
      p = p->link ;
   }
   /* doit again */
   p = file_list ;
   while (p)
   {
      if (p->type == PIPE_OUT)
      {
	 fclose(p->ptr) ;
	 close_fake_outpipe(p->name->str, p->pid) ;
      }
      p = p->link ;
   }
}
#endif /* HAVE_FAKE_PIPES */
#endif /* ! HAVE_REAL_PIPES */

/* hardwire to /bin/sh for portability of programs */
char *shell = "/bin/sh" ;

#if  HAVE_REAL_PIPES

PTR
get_pipe(name, type, pid_ptr)
   char *name ;
   int type ;
   int *pid_ptr ;
{
   int the_pipe[2], local_fd, remote_fd ;

   if (pipe(the_pipe) == -1)  return (PTR) 0 ;
   local_fd = the_pipe[type == PIPE_OUT] ;
   remote_fd = the_pipe[type == PIPE_IN] ;
   /* to keep output ordered correctly */
   fflush(stdout) ; fflush(stderr) ;

   switch (*pid_ptr = fork())
   {
      case -1:
	 close(local_fd) ;
	 close(remote_fd) ;
	 return (PTR) 0 ;

      case 0:
	 close(local_fd) ;
	 close(type == PIPE_IN) ;
	 dup(remote_fd) ;
	 close(remote_fd) ;
	 execl(shell, shell, "-c", name, (char *) 0) ;
	 errmsg(errno, "failed to exec %s -c %s", shell, name) ;
	 fflush(stderr) ;
	 _exit(128) ;

      default:
	 close(remote_fd) ;
	 /* we could deadlock if future child inherit the local fd ,
	   set close on exec flag */
	 CLOSE_ON_EXEC(local_fd) ;
	 break ;
   }

   return type == PIPE_IN ? (PTR) FINdopen(local_fd, 0) :
      (PTR) fdopen(local_fd, "w") ;
}



/*------------ children ------------------*/

/* we need to wait for children at the end of output pipes to
   complete so we know any files they have created are complete */

/* dead children are kept on this list */

static struct child
{
   int pid ;
   int exit_status ;
   struct child *link ;
} *child_list ;

static void
add_to_child_list(pid, exit_status)
   int pid, exit_status ;
{
   register struct child *p = ZMALLOC(struct child) ;

   p->pid = pid ; p->exit_status = exit_status ;
   p->link = child_list ; child_list = p ;
}

static struct child *
remove_from_child_list(pid)
   int pid ;
{
   struct child dummy ;
   register struct child *p ;
   struct child *q = &dummy ;

   dummy.link = p = child_list ;
   while (p)
   {
      if (p->pid == pid)
      {
	 q->link = p->link ;
	 break ;
      }
      else
      {
	 q = p ; p = p->link ; 
      }
   }

   child_list = dummy.link ;
   return p ;	
   /* null return if not in the list */
}


/* wait for a specific child to complete and return its
   exit status

   If pid is zero, wait for any single child and
   put it on the dead children list
*/

int
wait_for(pid)
   int pid ;
{
   int exit_status ;
   struct child *p ;
   int id ;

   if (pid == 0)
   {
      id = wait(&exit_status) ;
      add_to_child_list(id, exit_status) ;
   }
   /* see if an earlier wait() caught our child */
   else if (p = remove_from_child_list(pid))
   {
      exit_status = p->exit_status ;
      ZFREE(p) ;
   }
   else
   {
      /* need to really wait */
      while ((id = wait(&exit_status)) != pid)
      {
	 if (id == -1)		/* can't happen */
	    bozo("wait_for") ;
	 else
	 {
	    /* we got the exit status of another child
	    put it on the child list and try again */
	    add_to_child_list(id, exit_status) ;
	 }
      }
   }

   if (exit_status & 0xff)  exit_status = 128 + (exit_status & 0xff) ;
   else	 exit_status = (exit_status & 0xff00) >> 8 ;

   return exit_status ;
}

#endif /* HAVE_REAL_PIPES */


void
set_stderr()   /* and stdout */
{
   FILE_NODE *p, *q ; 
   
   p = ZMALLOC(FILE_NODE) ;
   p->link = (FILE_NODE*) 0 ;
   p->type = F_TRUNC ;
   p->name = new_STRING("/dev/stdout") ;
   p->ptr = (PTR) stdout ;
   q = ZMALLOC(FILE_NODE);
   q->link = p ;
   q->type = F_TRUNC ;
   q->name = new_STRING("/dev/stderr") ;
   q->ptr = (PTR) stderr ;
   file_list = q ;
}

/* fopen() but no buffering to ttys */
static FILE *
tfopen(name, mode)
   char *name, *mode ;
{
   FILE *retval = fopen(name, mode) ;

   if (retval)
   {
      if (isatty(fileno(retval)))  setbuf(retval, (char *) 0) ;
      else
      {
#ifdef MSDOS
	 enlarge_output_buffer(retval) ;
#endif
      }
   }
   return retval ;
}

#ifdef  MSDOS
void
enlarge_output_buffer(fp)
   FILE *fp ;
{
   if (setvbuf(fp, (char *) 0, _IOFBF, BUFFSZ) < 0)
   {
      errmsg(errno, "setvbuf failed on fileno %d", fileno(fp)) ;
      mawk_exit(2) ;
   }
}

void
stdout_init()
{
   if (!isatty(1))  enlarge_output_buffer(stdout) ;
   if (binmode() & 2)
   {
      setmode(1,O_BINARY) ; setmode(2,O_BINARY) ; 
   }
}
#endif /* MSDOS */