1 | /*
|
---|
2 | * readfile.c - Read an entire file into a string.
|
---|
3 | *
|
---|
4 | * Arnold Robbins
|
---|
5 | * Tue Apr 23 17:43:30 IDT 2002
|
---|
6 | * Revised per Peter Tillier
|
---|
7 | * Mon Jun 9 17:05:11 IDT 2003
|
---|
8 | * Revised for new dynamic function facilities
|
---|
9 | * Mon Jun 14 14:53:07 IDT 2004
|
---|
10 | */
|
---|
11 |
|
---|
12 | /*
|
---|
13 | * Copyright (C) 2002, 2003, 2004 the Free Software Foundation, Inc.
|
---|
14 | *
|
---|
15 | * This file is part of GAWK, the GNU implementation of the
|
---|
16 | * AWK Programming Language.
|
---|
17 | *
|
---|
18 | * GAWK is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * GAWK is distributed in the hope that it will be useful,
|
---|
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
26 | * GNU General Public License for more details.
|
---|
27 | *
|
---|
28 | * You should have received a copy of the GNU General Public License
|
---|
29 | * along with this program; if not, write to the Free Software
|
---|
30 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include "awk.h"
|
---|
34 | #include <fcntl.h>
|
---|
35 |
|
---|
36 | #ifndef O_BINARY
|
---|
37 | #define O_BINARY 0
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | /* do_readfile --- read a file into memory */
|
---|
41 |
|
---|
42 | NODE *
|
---|
43 | do_readfile(tree)
|
---|
44 | NODE *tree;
|
---|
45 | {
|
---|
46 | NODE *filename;
|
---|
47 | int ret = -1;
|
---|
48 | struct stat sbuf;
|
---|
49 | char *text;
|
---|
50 | int fd;
|
---|
51 |
|
---|
52 | if (do_lint && get_curfunc_arg_count() > 1)
|
---|
53 | lintwarn("readfile: called with too many arguments");
|
---|
54 |
|
---|
55 | filename = get_argument(tree, 0);
|
---|
56 | if (filename != NULL) {
|
---|
57 | (void) force_string(filename);
|
---|
58 |
|
---|
59 | ret = stat(filename->stptr, & sbuf);
|
---|
60 | if (ret < 0) {
|
---|
61 | update_ERRNO();
|
---|
62 | free_temp(filename);
|
---|
63 | goto done;
|
---|
64 | } else if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
|
---|
65 | errno = EINVAL;
|
---|
66 | ret = -1;
|
---|
67 | update_ERRNO();
|
---|
68 | free_temp(filename);
|
---|
69 | goto done;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if ((fd = open(filename->stptr, O_RDONLY|O_BINARY)) < 0) {
|
---|
73 | ret = -1;
|
---|
74 | update_ERRNO();
|
---|
75 | free_temp(filename);
|
---|
76 | goto done;
|
---|
77 | }
|
---|
78 |
|
---|
79 | emalloc(text, char *, sbuf.st_size + 2, "do_readfile");
|
---|
80 | memset(text, '\0', sbuf.st_size + 2);
|
---|
81 |
|
---|
82 | if ((ret = read(fd, text, sbuf.st_size)) != sbuf.st_size) {
|
---|
83 | (void) close(fd);
|
---|
84 | ret = -1;
|
---|
85 | update_ERRNO();
|
---|
86 | free_temp(filename);
|
---|
87 | goto done;
|
---|
88 | }
|
---|
89 |
|
---|
90 | close(fd);
|
---|
91 | free_temp(filename);
|
---|
92 | set_value(tmp_string(text, sbuf.st_size));
|
---|
93 | return tmp_number((AWKNUM) 0);
|
---|
94 | } else if (do_lint)
|
---|
95 | lintwarn("filename: called with no arguments");
|
---|
96 |
|
---|
97 |
|
---|
98 | done:
|
---|
99 | /* 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);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /* dlload --- load new builtins in this library */
|
---|
108 |
|
---|
109 | NODE *
|
---|
110 | dlload(tree, dl)
|
---|
111 | NODE *tree;
|
---|
112 | void *dl;
|
---|
113 | {
|
---|
114 | make_builtin("readfile", do_readfile, 1);
|
---|
115 |
|
---|
116 | return tmp_number((AWKNUM) 0);
|
---|
117 | }
|
---|