/* FreeTDS - Library of routines accessing Sybase and Microsoft databases * Copyright (C) 1998-1999 Brian Bruns * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include #include #ifdef __DGUX__ #include #endif #ifdef __FreeBSD__ #include #endif #ifdef WIN32 #include #include #define PATH_MAX 255 #endif #ifndef WIN32 #include #include #include #include #endif #include "tdsutil.h" static char software_version[] = "$Id: util.c,v 1.5 2001/11/10 02:39:01 brianb Exp $"; static void *no_unused_var_warn[] = {software_version, no_unused_var_warn}; /* for now all messages go to the log */ int g_debug_lvl = 99; int g_append_mode = 0; static char *g_dump_filename; void tds_set_parent(TDSSOCKET* tds, void* the_parent) { if (tds) tds->parent = the_parent; } void* tds_get_parent(TDSSOCKET* tds) { return( tds->parent); } int tds_swap_bytes(unsigned char *buf, int bytes) { unsigned char tmp; int i; /* if (bytes % 2) { return 0 }; */ for (i=0;ig_debug_lvl) return; if (g_append_mode) { ret = tdsdump_append(); } if (write_dump && dumpfile!=NULL) { const char *ptr; va_list ap; va_start(ap, fmt); if (g_append_mode) { fprintf(dumpfile, "pid: %d:", pid); } for(ptr = fmt; *ptr != '\0'; ptr++) { if (*ptr == '%') { ptr++; switch(*ptr) { case 's': { char *s = va_arg(ap, char *); fputs(s, dumpfile); break; } case 'd': { int i = va_arg(ap, int); fprintf(dumpfile, "%d", i); break; } case 'x': { int i = va_arg(ap, int); fprintf(dumpfile, "%x", i); break; } case 'D': { char *buf = va_arg(ap, char *); int len = va_arg(ap, int); tdsdump_dump_buf(buf, len); break; } case 'L': /* current local time */ { char buf[1024]; struct tm *tm; time_t t; #ifdef __FreeBSD__ struct timeval tv; #endif #ifdef __FreeBSD__ gettimeofday(&tv, NULL); t = tv.tv_sec; #else /* * XXX Need to get a better time resolution for * non-FreeBSD systems. */ time(&t); #endif tm = localtime(&t); strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S", tm); fputs(buf, dumpfile); #ifdef __FreeBSD__ fprintf(dumpfile, ".%06lu", tv.tv_usec); #endif } default: { break; } } } else { fputc(*ptr, dumpfile); } } } fflush(dumpfile); if (g_append_mode && ret) { fclose(dumpfile); } } /* tdsdump_log() */ /* Jeff's hack*** NEW CODE *** */ int tds_msleep(long usec) /* returns 0 if ok, else -1 */ { #ifdef WIN32 Sleep(0); return 0; #else /* try to select stdin for writing just to create a delay */ /* fd_set fd_in; */ struct timeval delay; /* _select() timeout */ /* FD_ZERO (&fd_in); */ /* FD_SET (fileno(stdin), &fd_in); */ delay.tv_sec = usec / 1000000L; delay.tv_usec = usec % 1000000L; return(select(0, (fd_set *)NULL, (fd_set *)NULL, (fd_set *)NULL, &delay)); #endif }; /* Jeff's hack ***NEW CODE END**** */