/* NetHalt - Library functions * Copyright (C) 2008 Daniel Collins * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * 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. * * * Neither the name of the author 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. */ #include #include #include #include #include #include "lib.h" /* Parse a time in the HH:MM[:SS] format * If the supplied string is not a valid time, -1 will be returned. */ int parse_time(char const *tstr) { int hours, mins, secs = 0; if(!isdigit(tstr[0]) || (!isdigit(tstr[1]) && tstr[1] != ':')) { return -1; } hours = atoi(tstr); tstr += (tstr[1] == ':' ? 2 : 3); if(!isdigit(tstr[0]) || (!isdigit(tstr[1]) && tstr[1] != ':')) { return -1; } mins = atoi(tstr); tstr += (tstr[1] == ':' ? 2 : 3); if(isdigit(tstr[0])) { secs = atoi(tstr); } if(hours > 23 || mins > 59 || secs > 59) { return -1; } return (3600*hours) + (60*mins) + secs; } /* Convert a windows error number to an error message */ char const *w32_error(DWORD errnum) { static char buf[1024] = {'\0'}; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errnum, 0, buf, 1023, NULL); buf[strcspn(buf, "\r\n")] = '\0'; return buf; } /* Compare two strings, ignoring case * Returns zero if the strings don't match, 1 otherwise. */ int ncase_eq(char const *str1, char const *str2) { size_t pos = 0; while(tolower(str1[pos]) == tolower(str2[pos])) { if(str1[pos] == '\0') { return 1; } pos++; } return 0; } /* Allocate and zero memory, or die */ void *allocate(size_t size) { void *ptr = malloc(size); if(!ptr) { die("Failed to malloc() %u bytes", size); } memset(ptr, '\0', size); return ptr; } /* Check if a year is a leap-year * Returns 1 if the supplied year is a leap-year, zero otherwise. */ int is_leapyear(int year) { if((year % 400) == 0) return 1; if((year % 100) == 0) return 0; if((year % 4) == 0) return 1; return 0; } /* Return the number of days in a month * Month should be 0-11 */ int month_days(int year, int month) { int mdays[2][12] = { {31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31} }; return mdays[is_leapyear(year)][month]; } /* Convert a time in seconds to a string * THIS IS NOT THREADSAFE */ char *fmt_time(int n) { static char buf[32]; int days, hours, mins, secs; days = n / 86400; hours = (n % 86400) / 3600; mins = (n % 3600) / 60; secs = n % 60; if(days == 0) { strcpy(buf, ""); }else if(days == 1) { strcpy(buf, "1 day, "); }else{ sprintf(buf, "%d days, ", days); } if(hours > 0) { sprintf(buf+strlen(buf), "%d:%02d:%02d", hours, mins, secs); }else{ sprintf(buf+strlen(buf), "%d:%02d", mins, secs); } return buf; }