Annotation of parser3/src/main/pa_common.C, revision 1.143.2.19

1.15      paf         1: /** @file
1.16      paf         2:        Parser: commonly functions.
                      3: 
1.143.2.10  paf         4:        Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.101     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.111     paf         6: */
1.16      paf         7: 
1.143.2.19! paf         8: static const char* IDENT_COMMON_C="$Date: 2003/02/14 17:28:19 $"; 
1.1       paf         9: 
                     10: #include "pa_common.h"
1.4       paf        11: #include "pa_exception.h"
1.126     paf        12: #include "pa_hash.h"
1.143.2.3  paf        13: #include "pa_vstring.h"
1.143.2.4  paf        14: #include "pa_vint.h"
1.143.2.14  paf        15: #include "pa_globals.h"
1.1       paf        16: 
1.143.2.12  paf        17: #ifdef CYGWIN
                     18: #define _GNU_H_WINDOWS32_SOCKETS
                     19: // for PASCAL
                     20: #include <windows.h>
                     21: // SOCKET
                     22: typedef u_int  SOCKET;
                     23: int PASCAL closesocket(SOCKET);
1.126     paf        24: #else
1.143.2.12  paf        25: #      if defined(WIN32)
                     26: #              include <windows.h>
                     27: #      else
                     28: #              define closesocket close
                     29: #      endif
1.98      paf        30: #endif
                     31: 
1.93      paf        32: // some maybe-undefined constants
                     33: 
1.82      paf        34: #ifndef _O_TEXT
                     35: #      define _O_TEXT 0
                     36: #endif
                     37: #ifndef _O_BINARY
                     38: #      define _O_BINARY 0
1.47      paf        39: #endif
1.80      paf        40: 
1.138     paf        41: #ifdef HAVE_FTRUNCATE
                     42: #      define PA_O_TRUNC 0
                     43: #else
                     44: #      ifdef _O_TRUNC
                     45: #              define PA_O_TRUNC _O_TRUNC
                     46: #      else
                     47: #              error you must have either ftruncate function or _O_TRUNC bit declared
                     48: #      endif
                     49: #endif
                     50: 
1.93      paf        51: // locking constants
                     52: 
1.99      paf        53: #ifdef HAVE_FLOCK
                     54: 
                     55: static int lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); }
                     56: static int lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); }
                     57: static int lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); }
                     58: static int unlock(int fd) { return flock(fd, LOCK_UN); }
                     59: 
1.98      paf        60: #else
1.99      paf        61: #ifdef HAVE__LOCKING
1.98      paf        62: 
1.126     paf        63: #define FLOCK(operation) lseek(fd, 0, SEEK_SET);  return _locking(fd, operation, 1)
1.99      paf        64: static int lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); }
                     65: static int lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); }
                     66: static int lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); }
                     67: static int unlock(int fd) { FLOCK(_LK_UNLCK); }
1.93      paf        68: 
1.99      paf        69: #else
                     70: #ifdef HAVE_FCNTL
1.93      paf        71: 
1.126     paf        72: #define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET};  return fcntl(fd, cmd, &ls)
1.99      paf        73: static int lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); }
                     74: static int lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); }
                     75: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); }
                     76: static int unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); }
1.93      paf        77: 
                     78: #else
                     79: #ifdef HAVE_LOCKF
1.99      paf        80: 
1.126     paf        81: #define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET);  return lockf(fd, operation, 1)
1.99      paf        82: static int lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking
                     83: static int lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); }
                     84: static int lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); }
                     85: static int unlock(int fd) { FLOCK(F_TLOCK); }
                     86: 
1.93      paf        87: #else
1.99      paf        88: 
                     89: #error unable to find file locking func
                     90: 
                     91: #endif
1.93      paf        92: #endif
                     93: #endif
                     94: #endif
                     95: 
1.143.2.16  paf        96: // defines for globals
                     97: 
                     98: #define FILE_STATUS_NAME  "status"
                     99: 
                    100: // globals
                    101: 
                    102: StringPtr file_status_name(new String(FILE_STATUS_NAME));
                    103: 
1.143.2.14  paf       104: // defines for statics
                    105: 
                    106: #define HTTP_METHOD_NAME  "method"
                    107: #define HTTP_TIMEOUT_NAME    "timeout"
                    108: #define HTTP_HEADERS_NAME "headers"
                    109: #define HTTP_ANY_STATUS_NAME "any-status"
                    110: 
                    111: // statics
                    112: 
                    113: static StringPtr http_method_name(new String(HTTP_METHOD_NAME));
                    114: static StringPtr http_timeout_name(new String(HTTP_TIMEOUT_NAME));
                    115: static StringPtr http_headers_name(new String(HTTP_HEADERS_NAME));
                    116: static StringPtr http_any_status_name(new String(HTTP_ANY_STATUS_NAME));
                    117: 
                    118: // defines
                    119: 
1.127     paf       120: #define DEFAULT_USER_AGENT "parser3"
                    121: 
1.143.2.14  paf       122: // functions
1.127     paf       123: 
1.126     paf       124: void fix_line_breaks(char* buf, size_t& size) {
1.139     paf       125:        if(size==0)
                    126:                return;
                    127: 
1.87      paf       128:        //_asm int 3;
1.126     paf       129:        const char* const eob=buf+size;
                    130:        char* dest=buf;
1.72      parser    131:        // fix DOS: \r\n -> \n
                    132:        // fix Macintosh: \r -> \n
1.126     paf       133:        char* bol=buf;
1.137     paf       134:        while(char* eol=(char*)memchr(bol, '\r', eob -bol)) {
1.72      parser    135:                size_t len=eol-bol;
                    136:                if(dest!=bol)
1.126     paf       137:                        memcpy(dest, bol, len); 
1.72      parser    138:                dest+=len;
1.126     paf       139:                *dest++='\n'; 
1.72      parser    140: 
1.126     paf       141:                if(&eol[1]<eob && eol[1]=='\n') { // \r, \n = DOS
1.72      parser    142:                        bol=eol+2;
1.126     paf       143:                        size--; 
                    144:                } else // \r, not \n = Macintosh
1.72      parser    145:                        bol=eol+1;
                    146:        }
                    147:        // last piece without \r, including terminating 0
                    148:        if(dest!=bol)
1.126     paf       149:                memcpy(dest, bol, eob-bol); 
1.72      parser    150: }
1.18      paf       151: 
1.143.2.3  paf       152: char* file_read_text(Pool& pool, Charset& charset, 
1.143.2.9  paf       153:                                         StringPtr file_spec, 
1.126     paf       154:                                         bool fail_on_read_problem,
1.143.2.16  paf       155:                                         HashStringValue* params/*, HashStringValuePtr* out_fields*/) {
1.143.2.18  paf       156:        File_read_result file=
                    157:                file_read(pool, charset, file_spec, true, params, fail_on_read_problem);
                    158:        return file.success?file.data:0;
1.126     paf       159: }
                    160: 
                    161: //http request stuff
                    162: /* ************************ http stuff *********************** */
                    163: 
                    164: static bool set_addr(struct sockaddr_in *addr, const char* host, const short port){
                    165:     memset(addr, 0, sizeof(*addr)); 
                    166:     addr->sin_family=AF_INET;
                    167:     addr->sin_port=htons(port); 
                    168:     if(host) {
                    169:                if(struct hostent *hostIP=gethostbyname(host)) 
                    170:                        memcpy(&addr->sin_addr, hostIP->h_addr, hostIP->h_length); 
                    171:                else
                    172:                        return false;
                    173:     } else 
                    174:                addr->sin_addr.s_addr=INADDR_ANY;
                    175:     return true;
                    176: }
                    177: 
1.143.2.1  paf       178: static int http_read_response(Pool& pool, String& response, int sock, bool fail_on_status_ne_200){
1.143.2.2  paf       179:        int result=0;
1.130     paf       180:        ssize_t EOLat=0;
1.126     paf       181:        while(true) {
1.143.2.1  paf       182:                char *buf=(char *)pool.malloc(MAX_STRING); 
1.126     paf       183:                ssize_t size=recv(sock, buf, MAX_STRING, 0); 
                    184:                if(size<=0)
                    185:                        break;
1.130     paf       186:                response.APPEND_TAINTED(buf, size, "remote HTTP server response", 0); 
1.143.2.2  paf       187:                if(!result && (EOLat=response.pos("\r\n", 2))>=0) { // checking status in first response
1.143.2.9  paf       188:                        StringPtr status_line=response.mid(0, (size_t)EOLat);
1.143.2.15  paf       189:                        ArrayString astatus; 
1.143.2.3  paf       190:                        status_line->split(astatus, 0, " ", 1); 
1.143.2.9  paf       191:                        StringPtr status_code=astatus.get(1);
1.143.2.3  paf       192:                        result=status_code->as_int(); 
1.142     paf       193: 
1.143.2.3  paf       194:                        if(fail_on_status_ne_200 && result!=200)
1.142     paf       195:                                throw Exception("http.status",
                    196:                                        status_code,
                    197:                                        "invalid HTTP response status");
                    198:                }
                    199:        }
1.143.2.2  paf       200:        if(result)
                    201:                return result;
1.142     paf       202:        else
                    203:                throw Exception("http.response",
1.143.2.3  paf       204:                        Exception::undefined_source,
1.142     paf       205:                        "bad response from host - no status found (size=%lu)", response.size()); 
1.126     paf       206: }
                    207: 
                    208: /* ********************** request *************************** */
                    209: 
                    210: #if defined(SIGALRM) && defined(HAVE_SIGSETJMP) && defined(HAVE_SIGLONGJMP)
                    211: #      define WE_CAN_USE_ALARM
                    212: #endif
                    213: 
                    214: #ifdef WE_CAN_USE_ALARM
                    215: static sigjmp_buf timeout_env;
                    216: static void timeout_handler(int sig){
                    217:     siglongjmp(timeout_env, 1); 
                    218: }
                    219: #endif
                    220: 
1.143.2.3  paf       221: static int http_request(Pool& pool,
                    222:                                                String& response,
1.143.2.9  paf       223:                                                StringPtr origin_string, 
1.143.2.3  paf       224:                                                const char* host, int port, 
                    225:                                                const char* request, 
                    226:                                                int timeout,
                    227:                                                bool fail_on_status_ne_200) {
1.126     paf       228:        if(!host)
                    229:                throw Exception("http.host", 
                    230:                        origin_string, 
                    231:                        "zero hostname");  //never
                    232: 
                    233: #ifdef WE_CAN_USE_ALARM
                    234:     signal(SIGALRM, timeout_handler); 
                    235: #endif
                    236:        int sock=-1;
                    237:        try {
1.142     paf       238:                int result;
1.126     paf       239: #ifdef WE_CAN_USE_ALARM
                    240:                if(sigsetjmp(timeout_env, 1))
                    241:                        throw Exception("http.timeout", 
                    242:                                origin_string, 
                    243:                                "timeout occured while retrieving document"); 
                    244:                else {
                    245:                        alarm(timeout); 
                    246: #endif
                    247:                        struct sockaddr_in dest;
                    248:                
                    249:                if(!set_addr(&dest, host, port))
                    250:                                throw Exception("http.host", 
                    251:                                        origin_string, 
1.127     paf       252:                                        "can not resolve hostname \"%s\"", host); 
1.126     paf       253:                        
                    254:                        if((sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP/*0*/))<0)
                    255:                                throw Exception("http.connect", 
                    256:                                        origin_string, 
1.127     paf       257:                                        "can not make socket: %s (%d)", strerror(errno), errno); 
1.126     paf       258:                        if(connect(sock, (struct sockaddr *)&dest, sizeof(dest)))
                    259:                                throw Exception("http.connect", 
                    260:                                        origin_string, 
1.127     paf       261:                                        "can not connect to host \"%s\": %s (%d)", host, strerror(errno), errno); 
1.126     paf       262:                        size_t request_size=strlen(request);
                    263:                        if(send(sock, request, request_size, 0)!=(ssize_t)request_size)
                    264:                                throw Exception("http.connect", 
                    265:                                        origin_string, 
1.127     paf       266:                                        "error sending request: %s (%d)", strerror(errno), errno); 
1.126     paf       267: 
1.143.2.3  paf       268:                        result=http_read_response(pool, response, sock, fail_on_status_ne_200); 
1.142     paf       269:                        closesocket(sock); 
1.126     paf       270: #ifdef WE_CAN_USE_ALARM
1.142     paf       271:                        alarm(0)1.126     paf       272:                }
                    273: #endif
1.142     paf       274:                return result;
1.126     paf       275:        } catch(...) {
                    276:                if(sock>=0) 
                    277:                        closesocket(sock); 
                    278: #ifdef WE_CAN_USE_ALARM
                    279:                alarm(0); 
                    280: #endif
1.143.2.11  paf       281:                rethrow;
1.126     paf       282:        }
                    283: }
                    284: 
1.127     paf       285: #ifndef DOXYGEN
                    286: struct Http_pass_header_info {
1.143.2.3  paf       287:        Pool* pool;
                    288:        Charset* charset;
1.127     paf       289:        String* request;
                    290:        bool user_agent_specified;
                    291: };
                    292: #endif
1.143.2.3  paf       293: static void http_pass_header(HashStringValue::key_type key, HashStringValue::value_type value, 
                    294:                                                         Http_pass_header_info *info) {
                    295:     *(info->request)<<*key<<": "
1.143.2.9  paf       296:                << *attributed_meaning_to_string(*info->pool, value, String::UL_HTTP_HEADER, false)
1.135     paf       297:                <<"\n"; 
                    298:        
1.143.2.3  paf       299:        if(*key->change_case(*info->pool, *info->charset, String::CC_UPPER)=="USER-AGENT")
                    300:                info->user_agent_specified=true;
1.126     paf       301: }
1.143.2.16  paf       302: #ifndef DOXYGEN
                    303: struct File_read_http_info {
                    304:        char **data; size_t *data_size;
                    305:        HashStringValuePtr headers;
                    306: }; 
                    307: #endif
                    308: static File_read_http_info file_read_http(Pool& pool, Charset& charset, StringPtr file_spec, 
                    309:                                        HashStringValue *options=0) {
                    310:        File_read_http_info result;
1.126     paf       311:        char host[MAX_STRING]; 
1.129     paf       312:        const char* uri; 
1.126     paf       313:        int port;
1.143.2.5  paf       314:        CharPtr method(0);
1.126     paf       315:        int timeout=2;
1.142     paf       316:        bool fail_on_status_ne_200=true;
1.143.2.3  paf       317:        ValuePtr vheaders(0);
1.126     paf       318: 
1.143.2.3  paf       319:        StringPtr connect_string(new String());
1.133     paf       320:        // not in ^sql{... UL_SQL ...} spirit, but closer to ^file::load one
1.143.2.4  paf       321:        connect_string->append(*file_spec, String::UL_URI); // tainted pieces -> URI pieces
1.133     paf       322: 
1.143.2.3  paf       323:        CharPtr connect_string_cstr=connect_string->cstr(String::UL_UNSPECIFIED); 
1.143.2.10  paf       324:        const char* current=connect_string_cstr;
1.143.2.3  paf       325:        if(strncmp(current, "http://", 7)!=0)
1.126     paf       326:                throw Exception(0, 
1.143.2.3  paf       327:                        connect_string, 
1.126     paf       328:                        "does not start with http://"); //never
1.143.2.3  paf       329:        current+=7;
1.126     paf       330: 
1.143.2.3  paf       331:        strncpy(host, current, sizeof(host)-1);  host[sizeof(host)-1]=0;
1.126     paf       332:        char* host_uri=lsplit(host, '/'); 
1.143.2.3  paf       333:        uri=host_uri?current+(host_uri-1-host):"/"; 
1.126     paf       334:        char* port_cstr=lsplit(host, ':'); 
                    335:        char* error_pos=0;
                    336:        port=port_cstr?strtol(port_cstr, &error_pos, 0):80;
                    337: 
1.127     paf       338:        if(options) {
                    339:                int valid_options=0;
1.143.2.3  paf       340:                if(ValuePtr vmethod=options->get(http_method_name)) {
1.127     paf       341:                        valid_options++;
1.143.2.8  paf       342:                        method=vmethod->as_string(&pool)->cstr();
1.127     paf       343:                }
1.143.2.3  paf       344:                if(ValuePtr vtimeout=options->get(http_timeout_name)) {
1.127     paf       345:                        valid_options++;
                    346:                        timeout=vtimeout->as_int(); 
                    347:                }
1.143.2.3  paf       348:                if(vheaders=options->get(http_headers_name)) {
1.127     paf       349:                        valid_options++;
                    350:                }
1.143.2.3  paf       351:                if(ValuePtr vany_status=options->get(http_any_status_name)) {
1.142     paf       352:                        valid_options++;
                    353:                        fail_on_status_ne_200=!vany_status->as_bool(); 
                    354:                }
                    355: 
1.143.2.3  paf       356:                if(valid_options!=options->count())
1.127     paf       357:                        throw Exception("parser.runtime",
1.143.2.3  paf       358:                                Exception::undefined_source,
1.127     paf       359:                                "invalid option passed");
1.133     paf       360:        } 
1.126     paf       361: 
                    362:        //making request
1.143.2.3  paf       363:        String request;
                    364:        if(method)
1.143.2.4  paf       365:                request<<method;
1.143.2.3  paf       366:        else
                    367:                request<<"GET";
                    368:        request<< " "<< uri <<" HTTP/1.0\nHost: "<< host<<"\n"; 
1.127     paf       369:        bool user_agent_specified=false;
                    370:        if(vheaders && !vheaders->is_string()) { // allow empty
1.143.2.8  paf       371:                if(HashStringValue *headers=vheaders->get_hash(connect_string)) {
1.143.2.3  paf       372:                        Http_pass_header_info info={&pool, &charset, &request};
1.127     paf       373:                        headers->for_each(http_pass_header, &info); 
                    374:                        user_agent_specified=info.user_agent_specified;
                    375:                } else
                    376:                        throw Exception("parser.runtime", 
1.143.2.3  paf       377:                                connect_string,
1.127     paf       378:                                "headers param must be hash"); 
                    379:        };
                    380:        if(!user_agent_specified) // defaulting
                    381:                request << "user-agent: " DEFAULT_USER_AGENT "\n";
1.126     paf       382:        request<<"\n"; 
                    383:        
                    384:        //sending request
1.143.2.3  paf       385:        String response;
1.143.2.5  paf       386:        CharPtr request_cstr=request.cstr(String::UL_UNSPECIFIED);
1.143.2.3  paf       387:        int status_code=http_request(pool, response,
1.143.2.4  paf       388:                connect_string, host, port, request_cstr, 
1.142     paf       389:                timeout, fail_on_status_ne_200); 
1.126     paf       390:        
                    391:        //processing results    
                    392:        int pos=response.pos("\r\n\r\n", 4); 
                    393:        if(pos<1){
                    394:                throw Exception("http.response", 
1.143.2.3  paf       395:                        connect_string,
1.126     paf       396:                        "bad response from host - no headers found"); 
                    397:        }
1.143.2.3  paf       398:        StringPtr header_block=response.mid(0, pos); 
                    399:        StringPtr body=response.mid(pos+4, response.size()); 
1.126     paf       400:        
1.143.2.15  paf       401:        ArrayString aheaders;
1.143.2.16  paf       402:        result.headers=HashStringValuePtr(new HashStringValue); 
1.143.2.3  paf       403:        header_block->split(aheaders, 0, "\r\n", 2); 
1.126     paf       404:        
                    405:        //processing headers
1.143.2.3  paf       406:        for(int i=1; i<aheaders.count(); i++) {
1.143.2.9  paf       407:                StringPtr line=aheaders.get(i);
1.143.2.3  paf       408:                pos=line->pos(": ", 2); 
                    409:                if(pos<1)
1.126     paf       410:                        throw Exception("http.response", 
1.143.2.3  paf       411:                                connect_string,
1.143.2.12  paf       412:                                "bad response from host - bad header \"%s\"", line->cstr().get());
1.143.2.3  paf       413:                        
1.143.2.16  paf       414:                result.headers->put(
1.143.2.3  paf       415:                        line->mid(0, pos)->change_case(pool, charset, String::CC_UPPER), 
                    416:                        ValuePtr(new VString(line->mid(pos+2, line->size())))); 
1.126     paf       417:        }
                    418: 
                    419:        // output response
1.143.2.16  paf       420:        *result.data=new(pool) char[*result.data_size=body->size()];
                    421:        body->store_to(*result.data, String::UL_AS_IS);
                    422:        result.headers->put(file_status_name, ValuePtr(new VInt(status_code))); 
                    423:        return result;
1.34      paf       424: }
1.123     paf       425: 
                    426: #ifndef DOXYGEN
                    427: struct File_read_action_info {
1.143.2.4  paf       428:        char **data; size_t *data_size;
1.126     paf       429: }; 
1.123     paf       430: #endif
1.143.2.4  paf       431: static void file_read_action(Pool& pool,
1.126     paf       432:                                                         struct stat& finfo, 
1.123     paf       433:                                                         int f, 
1.143.2.9  paf       434:                                                         StringPtr file_spec, const char* fname, bool as_text, 
1.123     paf       435:                                                         void *context) {
1.126     paf       436:        File_read_action_info& info=*static_cast<File_read_action_info *>(context); 
1.123     paf       437:        if(size_t to_read_size=(size_t)finfo.st_size) { 
1.143.2.4  paf       438:                *info.data=pool.malloc(to_read_size+(as_text?1:0)); 
1.126     paf       439:                *info.data_size=(size_t)read(f, *info.data, to_read_size); 
1.123     paf       440: 
                    441:                if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size)
1.126     paf       442:                        throw Exception(0, 
1.143.2.4  paf       443:                                file_spec, 
1.123     paf       444:                                "read failed: actually read %lu bytes count not in [0..%lu] valid range", 
1.126     paf       445:                                        *info.data_size, to_read_size); 
1.123     paf       446:        } else { // empty file
                    447:                if(as_text) {
1.126     paf       448:                        *info.data=pool.malloc(1)1.123     paf       449:                        *(char*)(*info.data)=0;
                    450:                } else 
                    451:                        *info.data=0;
                    452:                *info.data_size=0;
                    453:                return;
                    454:        }
1.126     paf       455: }
1.143.2.9  paf       456: File_read_result file_read(Pool& pool, Charset& charset, StringPtr file_spec, 
1.143.2.16  paf       457:                           bool as_text, HashStringValue *params,
1.126     paf       458:                           bool fail_on_read_problem) {
1.143.2.4  paf       459:        File_read_result result;
                    460:        if(file_spec->starts_with("http://", 7)) {
1.126     paf       461:                // fail on read problem
1.143.2.16  paf       462:                result.headers=file_read_http(pool, charset, file_spec, params).headers; 
1.143.2.4  paf       463:                result.success=true;
1.126     paf       464:        } else {
1.143.2.18  paf       465:                File_read_action_info info={&result.data, &result.size}; 
1.143.2.4  paf       466:                result.success=file_read_action_under_lock(pool, file_spec, 
1.126     paf       467:                        "read", file_read_action, &info, 
                    468:                        as_text, fail_on_read_problem); 
                    469:        }
1.123     paf       470: 
1.143.2.4  paf       471:        if(result.success && as_text) {
1.131     paf       472:                // UTF-8 signature: EF BB BF
1.143.2.18  paf       473:                if(result.size>=3) {
1.143.2.16  paf       474:                        char *in=(char *)result.data;
1.131     paf       475:                        if((in[0] == '\xEF') && (in[1] == '\xBB') &&
                    476:                                (in[2] == '\xBF')) {
1.143.2.18  paf       477:                                result.data=in+3; result.size-=3;// skip prefix
1.131     paf       478:                        }
                    479:                }
                    480: 
1.143.2.18  paf       481:                fix_line_breaks((char *)(result.data), result.size); 
1.123     paf       482:                // note: after fixing
1.143.2.18  paf       483:                ((char*&)(result.data))[result.size]=0;
1.123     paf       484:        }
1.126     paf       485: 
                    486:        return result;
1.123     paf       487: }
                    488: 
1.143.2.9  paf       489: bool file_read_action_under_lock(Pool& pool, StringPtr file_spec, 
1.126     paf       490:                                const char* action_name, File_read_action action, void *context, 
                    491:                                bool as_text, 
1.123     paf       492:                                bool fail_on_read_problem) {
1.143.2.5  paf       493:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.33      paf       494:        int f;
                    495: 
                    496:        // first open, next stat:
1.45      paf       497:        // directory update of NTFS hard links performed on open.
1.33      paf       498:        // ex: 
                    499:        //   a.html:^test[] and b.html hardlink to a.html
                    500:        //   user inserts ! before ^test in a.html
1.126     paf       501:        //   directory entry of b.html in NTFS not updated at once, 
1.35      paf       502:        //   they delay update till open, so we would receive "!^test[" string
                    503:        //   if would do stat, next open.
1.123     paf       504:        // later: it seems, even this does not help sometimes
1.98      paf       505:     if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.123     paf       506:                try {
                    507:                        if(lock_shared_blocking(f)!=0)
1.126     paf       508:                                throw Exception("file.lock", 
1.143.2.4  paf       509:                                                file_spec, 
1.123     paf       510:                                                "shared lock failed: %s (%d), actual filename '%s'", 
1.143.2.12  paf       511:                                                        strerror(errno), errno, fname.get());
1.123     paf       512: 
1.124     paf       513:                        struct stat finfo;
                    514:                        if(stat(fname, &finfo)!=0)
                    515:                                throw Exception("file.missing", // hardly possible: we just opened it OK
1.143.2.4  paf       516:                                        file_spec, 
1.124     paf       517:                                        "stat failed: %s (%d), actual filename '%s'", 
1.143.2.12  paf       518:                                                strerror(errno), errno, fname.get());
1.124     paf       519: 
1.140     paf       520: #ifdef PA_SAFE_MODE
                    521:                        if(finfo.st_uid/*foreign?*/!=geteuid()
                    522:                                && finfo.st_gid/*foreign?*/!=getegid())
1.126     paf       523:                                throw Exception("parser.runtime", 
1.143.2.4  paf       524:                                        file_spec, 
1.140     paf       525:                                        "parser is in safe mode: reading files of foreign group and user disabled [recompile parser with --disable-safe-mode configure option], actual filename '%s'", 
1.143.2.12  paf       526:                                                fname.get());
1.105     paf       527: #endif
1.32      paf       528: 
1.126     paf       529:                        action(pool, finfo, f, file_spec, fname, as_text, context); 
1.123     paf       530:                } catch(...) {
1.126     paf       531:                        unlock(f);close(f); 
1.123     paf       532:                        if(fail_on_read_problem)
1.143.2.11  paf       533:                                rethrow;
1.123     paf       534:                        return false;                   
                    535:                } 
1.87      paf       536: 
1.126     paf       537:                unlock(f);close(f); 
1.72      parser    538:                return true;
1.118     paf       539:     } else {
                    540:                if(fail_on_read_problem)
1.126     paf       541:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.143.2.4  paf       542:                                file_spec, 
1.123     paf       543:                                "%s failed: %s (%d), actual filename '%s'", 
1.143.2.12  paf       544:                                        action_name, strerror(errno), errno, fname.get());
1.118     paf       545:                return false;
                    546:        }
1.8       paf       547: }
                    548: 
1.143.2.9  paf       549: static void create_dir_for_file(StringPtr file_spec) {
1.63      parser    550:        size_t pos_after=1;
                    551:        int pos_before;
1.143.2.4  paf       552:        while((pos_before=file_spec->pos("/", 1, pos_after))>=0) {
                    553:                mkdir(file_spec->mid(0, pos_before)->cstr(String::UL_FILE_SPEC), 0775); 
1.63      parser    554:                pos_after=pos_before+1;
                    555:        }
                    556: }
                    557: 
1.98      paf       558: bool file_write_action_under_lock(
1.143.2.9  paf       559:                                StringPtr file_spec, 
1.126     paf       560:                                const char* action_name, File_write_action action, void *context, 
                    561:                                bool as_text, 
                    562:                                bool do_append, 
                    563:                                bool do_block, 
1.110     paf       564:                                bool fail_on_lock_problem) {
1.143.2.5  paf       565:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.28      paf       566:        int f;
1.80      paf       567:        if(access(fname, W_OK)!=0) // no
1.126     paf       568:                create_dir_for_file(file_spec); 
1.50      paf       569: 
1.80      paf       570:        if((f=open(fname, 
                    571:                O_CREAT|O_RDWR
                    572:                |(as_text?_O_TEXT:_O_BINARY)
1.138     paf       573:                |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) {
1.99      paf       574:                if((do_block?lock_exclusive_blocking(f):lock_exclusive_nonblocking(f))!=0) {
1.126     paf       575:                        Exception e("file.lock", 
1.143.2.4  paf       576:                                file_spec, 
1.110     paf       577:                                "shared lock failed: %s (%d), actual filename '%s'", 
1.143.2.12  paf       578:                                strerror(errno), errno, fname.get());
1.126     paf       579:                        close(f); 
1.110     paf       580:                        if(fail_on_lock_problem)
                    581:                                throw e;
1.98      paf       582:                        return false;
                    583:                }
1.96      paf       584: 
                    585:                try {
1.126     paf       586:                        action(f, context); 
1.96      paf       587:                } catch(...) {
1.138     paf       588: #ifdef HAVE_FTRUNCATE
1.104     paf       589:                        if(!do_append)
1.125     paf       590:                                ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower
1.138     paf       591: #endif
1.126     paf       592:                        unlock(f);close(f); 
1.143.2.11  paf       593:                        rethrow;
1.96      paf       594:                }
1.80      paf       595:                
1.138     paf       596: #ifdef HAVE_FTRUNCATE
1.104     paf       597:                if(!do_append)
1.125     paf       598:                        ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov <virtan@rotabanner.com> for discovering]
1.138     paf       599: #endif
1.126     paf       600:                unlock(f);close(f); 
1.98      paf       601:                return true;
1.80      paf       602:        } else
1.126     paf       603:                throw Exception(errno==EACCES?"file.access":0, 
1.143.2.4  paf       604:                        file_spec, 
1.96      paf       605:                        "%s failed: %s (%d), actual filename '%s'", 
1.143.2.12  paf       606:                                action_name, strerror(errno), errno, fname.get());
1.96      paf       607:        // here should be nothing, see rethrow above
                    608: }
                    609: 
                    610: #ifndef DOXYGEN
                    611: struct File_write_action_info {
1.143.2.17  paf       612:        const char *data; size_t size;
1.126     paf       613: }; 
1.96      paf       614: #endif
                    615: static void file_write_action(int f, void *context) {
1.126     paf       616:        File_write_action_info& info=*static_cast<File_write_action_info *>(context); 
1.113     paf       617:        if(info.size) {
1.126     paf       618:                int written=write(f, info.data, info.size); 
1.116     paf       619:                if(written<0)
1.126     paf       620:                        throw Exception(0, 
1.143.2.4  paf       621:                                Exception::undefined_source, 
1.126     paf       622:                                "write failed: %s (%d)",  strerror(errno), errno); 
1.113     paf       623:        }
1.96      paf       624: }
                    625: void file_write(
1.143.2.9  paf       626:                                StringPtr file_spec, 
1.143.2.17  paf       627:                                const char *data, size_t size, 
1.126     paf       628:                                bool as_text, 
1.96      paf       629:                                bool do_append) {
1.126     paf       630:        File_write_action_info info={data, size}; 
1.98      paf       631:        file_write_action_under_lock(
1.143.2.4  paf       632:                file_spec, 
                    633:                "write", file_write_action, &info, 
                    634:                as_text, 
                    635:                do_append); 
1.30      paf       636: }
                    637: 
1.63      parser    638: // throws nothing! [this is required in file_move & file_delete]
1.143.2.9  paf       639: static void rmdir(StringPtr file_spec, size_t pos_after) {
1.50      paf       640:        int pos_before;
1.143.2.4  paf       641:        if((pos_before=file_spec->pos("/", 1, pos_after))>=0)
1.126     paf       642:                rmdir(file_spec, pos_before+1); 
1.50      paf       643:        
1.143.2.4  paf       644:        rmdir(file_spec->mid(0, pos_after-1/* / */)->cstr(String::UL_FILE_SPEC)); 
1.50      paf       645: }
1.143.2.9  paf       646: bool file_delete(StringPtr file_spec, bool fail_on_read_problem) {
1.143.2.5  paf       647:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.54      parser    648:        if(unlink(fname)!=0)
1.93      paf       649:                if(fail_on_read_problem)
1.126     paf       650:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.143.2.4  paf       651:                                file_spec, 
1.93      paf       652:                                "unlink failed: %s (%d), actual filename '%s'", 
1.143.2.12  paf       653:                                        strerror(errno), errno, fname.get());
1.93      paf       654:                else
                    655:                        return false;
1.50      paf       656: 
1.126     paf       657:        rmdir(file_spec, 1); 
1.93      paf       658:        return true;
1.60      parser    659: }
1.143.2.9  paf       660: void file_move(StringPtr old_spec, StringPtr new_spec) {
1.143.2.5  paf       661:        CharPtr old_spec_cstr=old_spec->cstr(String::UL_FILE_SPEC); 
                    662:        CharPtr new_spec_cstr=new_spec->cstr(String::UL_FILE_SPEC); 
1.63      parser    663:        
1.126     paf       664:        create_dir_for_file(new_spec); 
1.63      parser    665: 
1.60      parser    666:        if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.126     paf       667:                throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.143.2.4  paf       668:                        old_spec, 
1.60      parser    669:                        "rename failed: %s (%d), actual filename '%s' to '%s'", 
1.143.2.12  paf       670:                                strerror(errno), errno, old_spec_cstr.get(), new_spec_cstr.get());
1.63      parser    671: 
1.126     paf       672:        rmdir(old_spec, 1); 
1.31      paf       673: }
                    674: 
1.51      paf       675: 
1.126     paf       676: bool entry_exists(const char* fname, struct stat *afinfo) {
1.118     paf       677:        struct stat lfinfo;
                    678:        bool result=stat(fname, &lfinfo)==0;
                    679:        if(afinfo)
                    680:                *afinfo=lfinfo;
                    681:        return result;
1.119     paf       682: }
                    683: 
1.143.2.9  paf       684: bool entry_exists(StringPtr file_spec) {
1.143.2.5  paf       685:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.126     paf       686:        return entry_exists(fname, 0); 
1.118     paf       687: }
                    688: 
1.143.2.9  paf       689: static bool entry_readable(StringPtr file_spec, bool need_dir) {
1.143.2.4  paf       690:     CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.120     paf       691:        if(need_dir) {
1.126     paf       692:                size_t size=strlen(fname); 
1.120     paf       693:                while(size) {
1.126     paf       694:                        char c=fname[size-1]; 
1.120     paf       695:                        if(c=='/' || c=='\\')
                    696:                                fname[--size]=0;
                    697:                        else
                    698:                                break;
                    699:                }
                    700:        }
1.51      paf       701:        struct stat finfo;
1.118     paf       702:        if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109     paf       703:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51      paf       704:                return is_dir==need_dir;
                    705:        }
                    706:        return false;
                    707: }
1.143.2.9  paf       708: bool file_readable(StringPtr file_spec) {
1.126     paf       709:        return entry_readable(file_spec, false); 
1.51      paf       710: }
1.143.2.9  paf       711: bool dir_readable(StringPtr file_spec) {
1.126     paf       712:        return entry_readable(file_spec, true); 
1.65      parser    713: }
1.143.2.19! paf       714: StringPtr file_readable(String& path, String& name) {
        !           715:        StringPtr result(new String(path));
1.126     paf       716:        *result << "/"; 
1.65      parser    717:        *result << name;
1.143.2.9  paf       718:        return file_readable(result)?result:StringPtr(0);
1.43      paf       719: }
1.143.2.9  paf       720: bool file_executable(StringPtr file_spec) {
1.143.2.4  paf       721:     return access(file_spec->cstr(String::UL_FILE_SPEC), X_OK)==0;
1.44      paf       722: }
                    723: 
1.143.2.9  paf       724: bool file_stat(StringPtr file_spec, 
1.58      parser    725:                           size_t& rsize, 
1.126     paf       726:                           time_t& ratime, 
                    727:                           time_t& rmtime, 
                    728:                           time_t& rctime, 
1.64      parser    729:                           bool fail_on_read_problem) {
1.143.2.5  paf       730:        CharPtr fname=file_spec->cstr(String::UL_FILE_SPEC); 
1.44      paf       731:     struct stat finfo;
                    732:        if(stat(fname, &finfo)!=0)
1.64      parser    733:                if(fail_on_read_problem)
1.126     paf       734:                        throw Exception("file.missing", 
1.143.2.4  paf       735:                                file_spec, 
1.67      parser    736:                                "getting file size failed: %s (%d), real filename '%s'", 
1.143.2.12  paf       737:                                        strerror(errno), errno, fname.get());
1.64      parser    738:                else
                    739:                        return false;
1.58      parser    740:        rsize=finfo.st_size;
                    741:        ratime=finfo.st_atime;
                    742:        rmtime=finfo.st_mtime;
                    743:        rctime=finfo.st_ctime;
1.64      parser    744:        return true;
1.18      paf       745: }
                    746: 
1.126     paf       747: char* getrow(char* *row_ref, char delim) {
                    748:     char* result=*row_ref;
1.8       paf       749:     if(result) {
1.126     paf       750:                *row_ref=strchr(result, delim); 
1.8       paf       751:                if(*row_ref) 
                    752:                        *((*row_ref)++)=0; 
                    753:                else if(!*result) 
                    754:                        return 0;
                    755:     }
                    756:     return result;
                    757: }
                    758: 
1.126     paf       759: char* lsplit(char* string, char delim) {
1.23      paf       760:     if(string) {
1.126     paf       761:                char* v=strchr(string, delim); 
1.8       paf       762:                if(v) {
                    763:                        *v=0;
                    764:                        return v+1;
                    765:                }
                    766:     }
                    767:     return 0;
                    768: }
                    769: 
1.126     paf       770: char* lsplit(char* *string_ref, char delim) {
                    771:     char* result=*string_ref;
                    772:        char* next=lsplit(*string_ref, delim); 
1.8       paf       773:     *string_ref=next;
                    774:     return result;
1.9       paf       775: }
                    776: 
1.126     paf       777: char* rsplit(char* string, char delim) {
1.18      paf       778:     if(string) {
1.126     paf       779:                char* v=strrchr(string, delim); 
1.18      paf       780:                if(v) {
1.9       paf       781:                        *v=0;
                    782:                        return v+1;
                    783:                }
                    784:     }
                    785:     return NULL;       
1.10      paf       786: }
                    787: 
1.37      paf       788: /// @todo less stupid type detection
1.143.2.10  paf       789: const char* format(Pool& pool, double value, char* fmt) {
1.126     paf       790:        char local_buf[MAX_NUMBER]; 
1.108     paf       791:        size_t size;
                    792:        
1.10      paf       793:        if(fmt)
                    794:                if(strpbrk(fmt, "diouxX"))
                    795:                        if(strpbrk(fmt, "ouxX"))
1.126     paf       796:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value); 
1.10      paf       797:                        else
1.126     paf       798:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value); 
1.10      paf       799:                else
1.126     paf       800:                        size=snprintf(local_buf, sizeof(local_buf), fmt, value); 
1.10      paf       801:        else
1.126     paf       802:                size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value); 
1.10      paf       803:        
1.143.2.7  paf       804:        return pool.copy(local_buf, size+1); 
1.12      paf       805: }
                    806: 
1.36      paf       807: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       808: #ifdef WIN32
                    809:        do{
1.126     paf       810:                int chunk_written=fwrite(buf, 1, min(8*0x400, size), stdout); 
1.12      paf       811:                if(chunk_written<=0)
                    812:                        break;
                    813:                size-=chunk_written;
1.36      paf       814:                buf=((const char*)buf)+chunk_written;
1.126     paf       815:        } while(size>0); 
1.12      paf       816: 
                    817:        return size;
                    818: #else
1.126     paf       819:        return fwrite(buf, 1, size, stdout); 
1.12      paf       820: #endif
1.2       paf       821: }
1.14      paf       822: 
1.143.2.4  paf       823: char* unescape_chars(Pool& pool, const char* cp, int len) {
                    824:        char* s=pool.malloc(len + 1); 
1.14      paf       825:        enum EscapeState {
1.33      paf       826:                EscapeRest, 
                    827:                EscapeFirst, 
1.14      paf       828:                EscapeSecond
                    829:        } escapeState=EscapeRest;
                    830:        int escapedValue=0;
                    831:        int srcPos=0;
                    832:        int dstPos=0;
                    833:        while(srcPos < len) {
1.126     paf       834:                int ch=cp[srcPos]; 
1.14      paf       835:                switch(escapeState) {
                    836:                        case EscapeRest:
                    837:                        if(ch=='%') {
                    838:                                escapeState=EscapeFirst;
                    839:                        } else if(ch=='+') {
1.126     paf       840:                                s[dstPos++]=' '; 
1.14      paf       841:                        } else {
                    842:                                s[dstPos++]=ch; 
                    843:                        }
                    844:                        break;
                    845:                        case EscapeFirst:
                    846:                        escapedValue=hex_value[ch] << 4;        
                    847:                        escapeState=EscapeSecond;
                    848:                        break;
                    849:                        case EscapeSecond:
1.126     paf       850:                        escapedValue +=hex_value[ch]; 
1.14      paf       851:                        s[dstPos++]=escapedValue;
                    852:                        escapeState=EscapeRest;
                    853:                        break;
                    854:                }
1.126     paf       855:                srcPos++; 
1.14      paf       856:        }
                    857:        s[dstPos]=0;
                    858:        return s;
1.24      paf       859: }
                    860: 
                    861: #ifdef WIN32
1.126     paf       862: void back_slashes_to_slashes(char* s) {
1.24      paf       863:        if(s)
                    864:                for(; *s; s++)
                    865:                        if(*s=='\\')
1.126     paf       866:                                *s='/'; 
1.24      paf       867: }
1.42      paf       868: /*
1.126     paf       869: void slashes_to_back_slashes(char* s) {
1.42      paf       870:        if(s)
                    871:                for(; *s; s++)
                    872:                        if(*s=='/')
1.126     paf       873:                                *s='\\'; 
1.42      paf       874: }
                    875: */
1.24      paf       876: #endif
1.41      paf       877: 
1.126     paf       878: bool StrEqNc(const char* s1, const char* s2, bool strict) {
1.41      paf       879:        while(true) {
                    880:                if(!(*s1)) {
                    881:                        if(!(*s2))
                    882:                                return true;
                    883:                        else
                    884:                                return !strict;
                    885:                } else if(!(*s2))
                    886:                        return !strict;
                    887:                if(isalpha(*s1)) {
                    888:                        if(tolower(*s1) !=tolower(*s2))
                    889:                                return false;
                    890:                } else if((*s1) !=(*s2))
                    891:                        return false;
1.126     paf       892:                s1++; 
                    893:                s2++; 
1.41      paf       894:        }
1.57      parser    895: }
                    896: 
1.84      paf       897: static bool isLeap(int year) {
1.57      parser    898:     return !(
                    899:              (year % 4) || ((year % 400) && !(year % 100))
1.126     paf       900:             ); 
1.57      parser    901: }
                    902: 
                    903: int getMonthDays(int year, int month) {
                    904:     int monthDays[]={
1.126     paf       905:         31, 
                    906:         isLeap(year) ? 29 : 28, 
                    907:         31, 
                    908:         30, 
                    909:         31, 
                    910:         30, 
                    911:         31, 
                    912:         31, 
                    913:         30, 
                    914:         31, 
                    915:         30, 
1.57      parser    916:         31
1.126     paf       917:     }; 
                    918:     return monthDays[month]; 
1.41      paf       919: }
1.69      parser    920: 
1.126     paf       921: void remove_crlf(char* start, char* end) {
                    922:        for(char* p=start; p<end; p++)
1.69      parser    923:                switch(*p) {
1.126     paf       924:                        case '\n': *p='|';  break;
                    925:                        case '\r': *p=' ';  break;
1.69      parser    926:                }
1.91      paf       927: }
                    928: 
                    929: 
                    930: /// must be last in this file
                    931: #undef vsnprintf
1.126     paf       932: int __vsnprintf(char* b, size_t s, const char* f, va_list l) {
1.91      paf       933:        if(!s)
                    934:                return 0;
                    935: 
                    936:        int r;
                    937:        // note: on win32& maybe somewhere else
                    938:        // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
                    939:        --s;
                    940: #if _MSC_VER
                    941:        /*
                    942:        win32: 
                    943:        mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
                    944: 
1.143.2.12  paf       945:          if the number of bytes to write exceeds buffer, then count bytes are written and Ö1 is returned
1.91      paf       946:        */
1.126     paf       947:        r=_vsnprintf(b, s, f, l); 
1.91      paf       948:        if(r<0) 
                    949:                r=s;
                    950: #else
1.126     paf       951:        r=vsnprintf(b, s, f, l); 
1.91      paf       952:        /*
                    953:        solaris: 
                    954:        man vsnprintf
                    955: 
                    956:          The snprintf() function returns  the  number  of  characters
                    957:        formatted, that is, the number of characters that would have
                    958:        been written to the buffer if it were large enough.  If  the
                    959:        value  of  n  is  0  on a call to snprintf(), an unspecified
                    960:        value less than 1 is returned.
                    961:        */
                    962: 
                    963:        if(r<0)
                    964:                r=0;
                    965:        else if(r>s)
                    966:                r=s;
                    967: #endif
                    968:        b[r]=0;
                    969:        return r;
                    970: }
                    971: 
1.126     paf       972: int __snprintf(char* b, size_t s, const char* f, ...) {
1.91      paf       973:        va_list l;
1.126     paf       974:     va_start(l, f); 
                    975:     int r=__vsnprintf(b, s, f, l); 
                    976:     va_end(l); 
1.91      paf       977:        return r;
1.98      paf       978: }
                    979: 
                    980: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.126     paf       981:        for (;  usecs >= 1000000; ++secs, usecs -= 1000000); 
1.98      paf       982: 
                    983: #ifdef WIN32
1.126     paf       984:        Sleep(secs * 1000 + usecs / 1000); 
1.98      paf       985:        return 0;
                    986: #else
                    987:        struct timeval t;
                    988:        t.tv_sec = secs;
                    989:        t.tv_usec = usecs;
1.126     paf       990:        return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0); 
1.98      paf       991: #endif
1.135     paf       992: }
                    993: 
                    994: 

E-mail: