|  | 
|  | 1 | +/*************************************************************************\ | 
|  | 2 | +* Copyright (C) Michael Kerrisk, 2024. * | 
|  | 3 | +* * | 
|  | 4 | +* This program is free software. You may use, modify, and redistribute it * | 
|  | 5 | +* under the terms of the GNU Lesser General Public License as published * | 
|  | 6 | +* by the Free Software Foundation, either version 3 or (at your option) * | 
|  | 7 | +* any later version. This program is distributed without any warranty. * | 
|  | 8 | +* See the files COPYING.lgpl-v3 and COPYING.gpl-v3 for details. * | 
|  | 9 | +\*************************************************************************/ | 
|  | 10 | + | 
|  | 11 | +/* itimerspec_from_str.c | 
|  | 12 | + | 
|  | 13 | + Implement our itimerspecFromStr() function. | 
|  | 14 | +*/ | 
|  | 15 | +#ifndef __APPLE__ /* Mac OS X doesn't define the 'itimerspec' structure | 
|  | 16 | + (or the POSIX timer functions (timer_*()) */ | 
|  | 17 | +#include <string.h> | 
|  | 18 | +#include <stdlib.h> | 
|  | 19 | +#include "itimerspec_from_str.h" /* Declares function defined here */ | 
|  | 20 | + | 
|  | 21 | +/* Convert a string of the following form to an itimerspec structure: | 
|  | 22 | + "value.sec[/value.nanosec][:interval.sec[/interval.nanosec]]". | 
|  | 23 | + Optional components that are omitted cause 0 to be assigned to the | 
|  | 24 | + corresponding structure fields. */ | 
|  | 25 | + | 
|  | 26 | +void | 
|  | 27 | +itimerspecFromStr(char *str, struct itimerspec *tsp) | 
|  | 28 | +{ | 
|  | 29 | + char *dupstr ,*cptr, *sptr; | 
|  | 30 | + | 
|  | 31 | + dupstr = strdup(str); | 
|  | 32 | + | 
|  | 33 | + cptr = strchr(dupstr, ':'); | 
|  | 34 | + if (cptr != NULL) | 
|  | 35 | + *cptr = '0円'; | 
|  | 36 | + | 
|  | 37 | + sptr = strchr(dupstr, '/'); | 
|  | 38 | + if (sptr != NULL) | 
|  | 39 | + *sptr = '0円'; | 
|  | 40 | + | 
|  | 41 | + tsp->it_value.tv_sec = atoi(dupstr); | 
|  | 42 | + tsp->it_value.tv_nsec = (sptr != NULL) ? atoi(sptr + 1) : 0; | 
|  | 43 | + | 
|  | 44 | + if (cptr == NULL) { | 
|  | 45 | + tsp->it_interval.tv_sec = 0; | 
|  | 46 | + tsp->it_interval.tv_nsec = 0; | 
|  | 47 | + } else { | 
|  | 48 | + sptr = strchr(cptr + 1, '/'); | 
|  | 49 | + if (sptr != NULL) | 
|  | 50 | + *sptr = '0円'; | 
|  | 51 | + tsp->it_interval.tv_sec = atoi(cptr + 1); | 
|  | 52 | + tsp->it_interval.tv_nsec = (sptr != NULL) ? atoi(sptr + 1) : 0; | 
|  | 53 | + } | 
|  | 54 | + free(dupstr); | 
|  | 55 | +} | 
|  | 56 | +#endif | 
0 commit comments