/* $CoreSDI: misc.c,v 1.13 2001/09/03 17:54:19 claudio Exp $ */ /* * Copyright (c) 2000, 2001, Core SDI S.A., Argentina * All rights reserved * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither name of the Core SDI S.A. nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef WIN32 /* Unix */ #include #include #include #include #include #include #include #include #include #else /* Win32 */ #include #include #include #include #include #include #include #include #include "exits.h" #include "winsyslog.h" extern char *__progname = ""; #endif /* WIN32 */ #include "sysdep.h" #include "log.h" #ifdef WIN32 /* * init_windows_wsa() * Initialize that window thing. Return 0 on success and */ void init_windows_wsa() { WSADATA wsa_data; WORD w_version; int err; w_version = MAKEWORD(2, 2); err = WSAStartup(w_version, &wsa_data); if (err != 0) fatal(EX_OSERR, "WSASartup: %d.", err); } /* * getpass(): * Clone of unix getpass(3). */ const char * getpass(const char *prompt) { static char password[_PASSWORD_LEN + 1]; int i, ch; _cputs(prompt); for (i = 0; i < _PASSWORD_LEN; i++) { /* Get char from the console, skip function and arrow keys */ while ( (ch = _getch()) == 0 || ch == 0xE0) _getch(); if (ch == 13) { cputs("\n\r"); break; } password[i] = (char) ch; } password[i] = '\0'; return (password); } #endif /* WIN32 */ /* * init_socket(): */ int init_socket() { int val; socklen_t val_len; #ifndef WIN32 int fd; #else SOCKET fd; #endif /* WIN32 */ if ( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return (-1); val = 1; val_len = sizeof(val); if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (const void *) &val, val_len) < 0) { #ifndef WIN32 close(fd); #else closesocket(fd); #endif return (-1); } return (fd); } /* * strlcpy() */ #ifdef NOHAVE_STRLCPY size_t strlcpy(char *dst, const char *src, size_t size) { /* XXX: optimize */ strncpy(dst, src, size); if (strlen(src) >= size) *(dst + size - 1) = '\0'; return (strlen(dst)); } /* * strlcat() * strlcat(3) from OpenBSD. */ size_t strlcat(char *dst, const char *src, size_t size) { register char *d; register const char *s; register size_t n; size_t dlen; d = dst; s = src; n = size; while (*d != '\0' && n-- != 0) d++; dlen = d - dst; n = size - dlen; if (n == 0) return (dlen + strlen(s)); while (*s != '\0') { if (n != 1) { *d++ = *s; n--; } s++; } *d = '\0'; return (dlen + (s - src)); } #endif /* NOHAVE_STRLCPY */