File: fprin.c

package info (click to toggle)
icmake 6.30-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,360 kB
  • ctags: 1,415
  • sloc: ansic: 7,727; makefile: 1,465; sh: 244; asm: 126; cpp: 39
file content (42 lines) | stat: -rw-r--r-- 1,009 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
/*
\funcref{fun\_fprintf}{void fun\_fprintf ()}
    {}
    {}
    {fprintf(), xrealloc()}
    {fun\_exec()}
    {funfprin.c}
    {

        This function prints the arguments to a {\em fprint()} statement,
        pushed onto the stack previously. The number of arguments to print is
        the last pushed value.

        The arguments are: the number of arguments (a hidden parameter), a
        filename, and then the remaining arguments to print.

    }

*/

#include "builtin.ih"

void fun_fprintf()
{
    size_t i;
    int newelement;
    size_t nargs = intValue(top());
    char const *filename = stringStr(top() - 1);
    FILE *outf;

    if (! (outf = fopen (filename, "a")) )
        error ("failure to open file \"%s\"", filename);

    for (i = 2; i <= nargs; i += newelement)
    {
        char *string = getarg(i, &newelement);
        fprintf(outf, "%s%s", string, 
                        typeValue(top() - i) & e_list && *string ? " " : "");
        free(string);
    }
    fclose (outf);
}