File: file.5c

package info (click to toggle)
nickle 2.68-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,336 kB
  • ctags: 3,288
  • sloc: ansic: 31,198; yacc: 1,860; lex: 858; sh: 830; makefile: 229
file content (104 lines) | stat: -rw-r--r-- 1,893 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
/* $Header$*/
/*
 * Copyright  2003 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

extend namespace File {
    public file stdnull () { 
	static bool set = false;
	static file f;
	if (!set)
	    f = open("/dev/null", "r+");
	return f;
    }

    public typedef union {
	file input;
	file output;
	file error;
    } childfd;

    public int mkchild(string path, string[*] argv, childfd fds ...) 
	/*
	 * Call filter transforming 'fds' into an array of three files.
	 * This is what happens when you fork()
	 */
    {
	file[3] filter_fds = { stdnull(), ... };
	for (int i = 0; i < dim(fds); i++) {
	    union switch(fds[i]) {
	    case input f: filter_fds[0] = f; break;
	    case output f: filter_fds[1] = f; break;
	    case error f: filter_fds[2] = f; break;
	    }
	}
	return filter(path, argv, filter_fds);
    }

    public namespace FileGlobals {

	public int getchar ()
	    /* return getc (stdin); */
	{
	    return getc (stdin);
	}

	public void ungetchar (int ch)
	    /* ungetc (ch, stdin); */
	{
	    ungetc (ch, stdin);
	}

	public void putchar (int c)
	    /* putc (c, stdout) */
	{
	    putc (c, stdout);
	}

	public int getbyte ()
	    /* return getb (stdin) */
	{
	    return getb (stdin);
	}

	public void putbyte (int b)
	    /* putb (b, stdout) */
	{
	    putb (b, stdout);
	}

	public string fgets (file f)
	    /*
	     * Return a line from 'f' as a string.
	     * The trailing newline will be stripped off.
	     */
	{
	    string	s;
	    int	c;

	    s = "";
	    while (!end(f))
	    {
		c = getc (f);
		switch (c) {
		case '\n':
		    return s;
		default:
		    s = s + String::new (c);
		}
	    }
	    return s;
	}

	public string gets ()
	    /* return fgets (stdin); */
	{
	    return fgets (stdin);
	}
    }
    
    public import FileGlobals;
}
public import File::FileGlobals;