nis-util
1.0.D108
|
00001 // 00002 // nis-util - NIS Administration Utilities 00003 // Copyright (C) 2012 Peter Miller 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation; either version 2 of the License, or (at 00008 // your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License along 00016 // with this program. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 00019 #include <lib/ac/stdio.h> 00020 #include <lib/ac/unistd.h> 00021 #include <libexplain/fread.h> 00022 00023 #include <lib/input/stdin.h> 00024 00025 00026 input_stdin::~input_stdin() 00027 { 00028 } 00029 00030 00031 input_stdin::input_stdin() : 00032 filename("stdin"), 00033 line_number(1) 00034 { 00035 // Try reading /proc/self/fd/0 to obtain a more useful file name. 00036 char lbuf[2000]; 00037 int n = readlink("/proc/self/fd/0", lbuf, sizeof(lbuf)); 00038 if (n > 0 && lbuf[0] == '/') 00039 filename = rcstring(lbuf, n); 00040 } 00041 00042 00043 bool 00044 input_stdin::candidate(const rcstring &filename) 00045 { 00046 if (filename.empty()) 00047 return true; 00048 if (filename == "-") 00049 return true; 00050 if (filename == "/dev/stdin") 00051 return true; 00052 if (filename == "/dev/fd/0") 00053 return true; 00054 if (filename == "/proc/self/fd/0") 00055 return true; 00056 return false; 00057 } 00058 00059 00060 input_stdin::pointer 00061 input_stdin::create(void) 00062 { 00063 return pointer(new input_stdin()); 00064 } 00065 00066 00067 size_t 00068 input_stdin::read_inner(source_location &locn, void *data, size_t data_size) 00069 { 00070 // FIXME: if stdin is line buffered, should we be too? 00071 locn = source_location(filename, line_number); 00072 size_t nbytes = explain_fread_or_die(data, 1, data_size, stdin); 00073 line_number += count_newlines(data, nbytes); 00074 return nbytes; 00075 } 00076 00077 00078 // vim: set ts=8 sw=4 et :