/* * This file is part of fwm-sysdaemon * util.c * * Copyright (c) 2019, 2021, 2022 firk (firk@cantconnect.ru) * * 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. The origin of this software must not be misrepresented; you must * not claim that you wrote the original software. * 4. Altered versions in any form must be plainly marked as such, and * must not be misinterpreted as being the original software. * * This software is provided by the author and contributors `as is' * without any express or implied warranty. */ #include #include #include #include #include #include #include #include #include #include "util.h" extern char const * parse_uint(char const *st, uint *res) { uint r, t, c; r = 0; if(*st=='0') { if(st[1]>='0' && st[1]<='9') return NULL; *res = 0; return st+1; } while(1) { c = (unsigned char)*st; if(c<'0' || c>'9') break; c -= '0'; t = r*10; if(t/10!=r) return NULL; r = t+c; if(r='0' && st[1]<='9') return NULL; *res = 0; return st+1; } while(1) { c = (unsigned char)*st; if(c<'0' || c>'9') break; c -= '0'; t = r*10; if(t/10!=r) return NULL; r = t+c; if(r=max) return -1; while(p && (dest[p-1]=='\n' || dest[p-1]==' ')) p--; for(pp=0;pptm_year+1900), (unsigned)(tm->tm_mon+1), (unsigned)tm->tm_mday, (unsigned)tm->tm_hour, (unsigned)tm->tm_min, (unsigned)tm->tm_sec); } return timestr; } extern unsigned long get_ms(void) { struct timeval tv; struct timespec ts; if(clock_gettime(CLOCK_MONOTONIC, &ts)>=0) { return ((unsigned long)ts.tv_sec)*1000+((unsigned long)ts.tv_nsec)/1000000; } if(gettimeofday(&tv, NULL)>=0) { return ((unsigned long)tv.tv_sec)*1000+((unsigned long)tv.tv_usec)/1000; } return ((unsigned long)time(NULL))*1000; } extern int write_all(int fd, unsigned char const * buf, size_t n) { ssize_t r; while(n) { r = write(fd, buf, n); if(r<0) { if(errno==EINTR) continue; return -1; } /* assert(r>0 && n>=(size_t)r);*/ buf += r; n -= r; } return 0; } extern int fd_set_str(int fd, char const *st) { size_t len, pos; ssize_t r; if(ftruncate(fd, 0)<0) return -1; len = strlen(st); for(pos=0; pos=sizeof(st)-1) { errno=EFBIG; return -1; } st[len++] = '\n'; if(ftruncate(fd, 0)<0) return -1; for(pos=0; pos=sizeof(st)) { errno=EFBIG; return -1; } if(!p || st[p-1]!='\n') { errno=EINVAL; return -1; } st[--p] = 0; if(strlen(st)!=p) { errno=EINVAL; return -1; } if(!(ps = parse_uint(st,&rr)) || *ps || 0>(int)rr) { errno=EINVAL; return -1; } return (int)rr; } extern int fd_set_int_S(int fd, int v) { char st[30]; size_t len, pos; ssize_t r; snprintf(st, sizeof(st), "%d\n", v); len = strlen(st); if(len>=sizeof(st)-1) { errno=EFBIG; return -1; } for(pos=0; pos=sizeof(st)) { errno=EFBIG; return -1; } for(j=0; j

=p) { errno=EINVAL; return -1; } st[p = j] = 0; if(strlen(st)!=p) { errno=EINVAL; return -1; } if(!(ps = parse_uint(st,&rr)) || *ps || 0>(int)rr) { errno=EINVAL; return -1; } return (int)rr; } extern int fd_set_int_U(int fd, int v) { char st[30]; size_t len, pos; ssize_t r; snprintf(st, sizeof(st), "%d\n", v); len = strlen(st); if(len>=sizeof(st)-1) { errno=EFBIG; return -1; } for(pos=0; pos