~vcs-imports/gawk/master

« back to all changes in this revision

Viewing changes to extension/readfile.c

  • Committer: Arnold D. Robbins
  • Date: 2010-11-18 21:00:31 UTC
  • Revision ID: git-v1:6f3612539c425da2bc1d34db621696e6a273b01c
Bring latest byte code gawk into git. Hurray!

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
/* do_readfile --- read a file into memory */
41
41
 
42
42
NODE *
43
 
do_readfile(tree)
44
 
NODE *tree;
 
43
do_readfile(int nargs)
45
44
{
46
45
        NODE *filename;
47
46
        int ret = -1;
52
51
        if  (do_lint && get_curfunc_arg_count() > 1)
53
52
                lintwarn("readfile: called with too many arguments");
54
53
 
55
 
        filename = get_argument(tree, 0);
 
54
        filename = get_scalar_argument(0, FALSE);
56
55
        if (filename != NULL) {
57
56
                (void) force_string(filename);
58
57
 
59
58
                ret = stat(filename->stptr, & sbuf);
60
59
                if (ret < 0) {
61
60
                        update_ERRNO();
62
 
                        free_temp(filename);
63
61
                        goto done;
64
62
                } else if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
65
63
                        errno = EINVAL;
66
64
                        ret = -1;
67
65
                        update_ERRNO();
68
 
                        free_temp(filename);
69
66
                        goto done;
70
67
                }
71
68
 
72
69
                if ((fd = open(filename->stptr, O_RDONLY|O_BINARY)) < 0) {
73
70
                        ret = -1;
74
71
                        update_ERRNO();
75
 
                        free_temp(filename);
76
72
                        goto done;
77
73
                }
78
74
 
83
79
                        (void) close(fd);
84
80
                        ret = -1;
85
81
                        update_ERRNO();
86
 
                        free_temp(filename);
87
82
                        goto done;
88
83
                }
89
84
 
90
85
                close(fd);
91
 
                free_temp(filename);
92
 
                set_value(tmp_string(text, sbuf.st_size));
93
 
                return tmp_number((AWKNUM) 0);
 
86
                return make_string(text, sbuf.st_size);
94
87
        } else if (do_lint)
95
88
                lintwarn("filename: called with no arguments");
96
89
 
97
90
 
98
91
done:
99
92
        /* Set the return value */
100
 
        set_value(tmp_number((AWKNUM) ret));
101
 
 
102
 
        /* Just to make the interpreter happy */
103
 
        return tmp_number((AWKNUM) 0);
 
93
        return make_number((AWKNUM) ret);
104
94
}
105
95
 
106
96
 
113
103
{
114
104
        make_builtin("readfile", do_readfile, 1);
115
105
 
116
 
        return tmp_number((AWKNUM) 0);
 
106
        return make_number((AWKNUM) 0);
117
107
}