Main Page | Class List | Directories | File List | Class Members | File Members

readfile.c

Go to the documentation of this file.
00001 /*
00002  * readfile.c - Read an entire file into a string.
00003  *
00004  * Arnold Robbins
00005  * Tue Apr 23 17:43:30 IDT 2002
00006  * Revised per Peter Tillier
00007  * Mon Jun  9 17:05:11 IDT 2003
00008  */
00009 
00010 /*
00011  * Copyright (C) 2002, 2003 the Free Software Foundation, Inc.
00012  * 
00013  * This file is part of GAWK, the GNU implementation of the
00014  * AWK Programming Language.
00015  * 
00016  * GAWK is free software; you can redistribute it and/or modify
00017  * it under the terms of the GNU General Public License as published by
00018  * the Free Software Foundation; either version 2 of the License, or
00019  * (at your option) any later version.
00020  * 
00021  * GAWK is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU General Public License
00027  * along with this program; if not, write to the Free Software
00028  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
00029  */
00030 
00031 #include "awk.h"
00032 #include <fcntl.h>
00033 
00034 #ifndef O_BINARY
00035 #define O_BINARY 0
00036 #endif
00037 
00038 /* do_readfile --- read a file into memory */
00039 
00040 NODE *
00041 do_readfile(tree)
00042 NODE *tree;
00043 {
00044         NODE *filename;
00045         int ret = -1;
00046         struct stat sbuf;
00047         char *text;
00048         int fd;
00049 
00050         if  (do_lint && tree->param_cnt > 1)
00051                 lintwarn("readfile: called with too many arguments");
00052 
00053         filename = get_argument(tree, 0);
00054         if (filename != NULL) {
00055                 (void) force_string(filename);
00056 
00057                 ret = stat(filename->stptr, & sbuf);
00058                 if (ret < 0) {
00059                         update_ERRNO();
00060                         free_temp(filename);
00061                         goto done;
00062                 } else if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
00063                         errno = EINVAL;
00064                         ret = -1;
00065                         update_ERRNO();
00066                         free_temp(filename);
00067                         goto done;
00068                 }
00069 
00070                 if ((fd = open(filename->stptr, O_RDONLY|O_BINARY)) < 0) {
00071                         ret = -1;
00072                         update_ERRNO();
00073                         free_temp(filename);
00074                         goto done;
00075                 }
00076 
00077                 emalloc(text, char *, sbuf.st_size + 2, "do_readfile");
00078                 memset(text, '\0', sbuf.st_size + 2);
00079 
00080                 if ((ret = read(fd, text, sbuf.st_size)) != sbuf.st_size) {
00081                         (void) close(fd);
00082                         ret = -1;
00083                         update_ERRNO();
00084                         free_temp(filename);
00085                         goto done;
00086                 }
00087 
00088                 close(fd);
00089                 free_temp(filename);
00090                 set_value(tmp_string(text, sbuf.st_size));
00091                 return tmp_number((AWKNUM) 0);
00092         } else if (do_lint)
00093                 lintwarn("filename: called with no arguments");
00094 
00095 
00096 done:
00097         /* Set the return value */
00098         set_value(tmp_number((AWKNUM) ret));
00099 
00100         /* Just to make the interpreter happy */
00101         return tmp_number((AWKNUM) 0);
00102 }
00103 
00104 
00105 /* dlload --- load new builtins in this library */
00106 
00107 NODE *
00108 dlload(tree, dl)
00109 NODE *tree;
00110 void *dl;
00111 {
00112         make_builtin("readfile", do_readfile, 1);
00113 
00114         return tmp_number((AWKNUM) 0);
00115 }

© sourcejam.com 2005-2008