]>
git.ipfire.org Git - people/ms/systemd.git/blob - util.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
14 usec_t
now(clockid_t clock
) {
17 assert_se(clock_gettime(clock
, &ts
) == 0);
19 return timespec_load(&ts
);
22 usec_t
timespec_load(const struct timespec
*ts
) {
26 (usec_t
) ts
->tv_sec
* USEC_PER_SEC
+
27 (usec_t
) ts
->tv_nsec
/ NSEC_PER_USEC
;
30 struct timespec
*timespec_store(struct timespec
*ts
, usec_t u
) {
33 ts
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
34 ts
->tv_nsec
= (long int) ((u
% USEC_PER_SEC
) * NSEC_PER_USEC
);
39 usec_t
timeval_load(const struct timeval
*tv
) {
43 (usec_t
) tv
->tv_sec
* USEC_PER_SEC
+
47 struct timeval
*timeval_store(struct timeval
*tv
, usec_t u
) {
50 tv
->tv_sec
= (time_t) (u
/ USEC_PER_SEC
);
51 tv
->tv_usec
= (suseconds_t
) (u
% USEC_PER_SEC
);
56 bool endswith(const char *s
, const char *postfix
) {
68 return memcmp(s
+ sl
- pl
, postfix
, pl
) == 0;
71 bool startswith(const char *s
, const char *prefix
) {
83 return memcmp(s
, prefix
, pl
) == 0;
86 int close_nointr(int fd
) {
92 if ((r
= close(fd
)) >= 0)
100 void close_nointr_nofail(int fd
) {
102 /* like close_nointr() but cannot fail, and guarantees errno
105 assert_se(close_nointr(fd
) == 0);
108 int parse_boolean(const char *v
) {
111 if (streq(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
113 else if (streq(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
119 int safe_atou(const char *s
, unsigned *ret_u
) {
127 l
= strtoul(s
, &x
, 0);
129 if (!x
|| *x
|| errno
)
130 return errno
? -errno
: -EINVAL
;
132 if ((unsigned long) (unsigned) l
!= l
)
135 *ret_u
= (unsigned) l
;
139 int safe_atoi(const char *s
, int *ret_i
) {
147 l
= strtol(s
, &x
, 0);
149 if (!x
|| *x
|| errno
)
150 return errno
? -errno
: -EINVAL
;
152 if ((long) (int) l
!= l
)
159 int safe_atolu(const char *s
, long unsigned *ret_lu
) {
167 l
= strtoul(s
, &x
, 0);
169 if (!x
|| *x
|| errno
)
170 return errno
? -errno
: -EINVAL
;
176 int safe_atoli(const char *s
, long int *ret_li
) {
184 l
= strtol(s
, &x
, 0);
186 if (!x
|| *x
|| errno
)
187 return errno
? -errno
: -EINVAL
;
193 int safe_atollu(const char *s
, long long unsigned *ret_llu
) {
195 unsigned long long l
;
201 l
= strtoull(s
, &x
, 0);
203 if (!x
|| *x
|| errno
)
204 return errno
? -errno
: -EINVAL
;
210 int safe_atolli(const char *s
, long long int *ret_lli
) {
218 l
= strtoll(s
, &x
, 0);
220 if (!x
|| *x
|| errno
)
221 return errno
? -errno
: -EINVAL
;
227 /* Split a string into words. */
228 char *split_spaces(const char *c
, size_t *l
, char **state
) {
231 current
= *state
? *state
: (char*) c
;
233 if (!*current
|| *c
== 0)
236 current
+= strspn(current
, WHITESPACE
);
237 *l
= strcspn(current
, WHITESPACE
);
240 return (char*) current
;
243 /* Split a string into words, but consider strings enclosed in '' and
244 * "" as words even if they include spaces. */
245 char *split_quoted(const char *c
, size_t *l
, char **state
) {
248 current
= *state
? *state
: (char*) c
;
250 if (!*current
|| *c
== 0)
253 current
+= strspn(current
, WHITESPACE
);
255 if (*current
== '\'') {
257 *l
= strcspn(current
, "'");
262 } else if (*current
== '\"') {
264 *l
= strcspn(current
, "\"");
270 *l
= strcspn(current
, WHITESPACE
);
274 /* FIXME: Cannot deal with strings that have spaces AND ticks
277 return (char*) current
;
280 const char *sigchld_code(int code
) {
282 if (code
== CLD_EXITED
)
284 else if (code
== CLD_KILLED
)
286 else if (code
== CLD_DUMPED
)
288 else if (code
== CLD_TRAPPED
)
290 else if (code
== CLD_STOPPED
)
292 else if (code
== CLD_CONTINUED
)
298 int get_parent_of_pid(pid_t pid
, pid_t
*_ppid
) {
301 char fn
[132], line
[256], *p
;
302 long long unsigned ppid
;
307 assert_se(snprintf(fn
, sizeof(fn
)-1, "/proc/%llu/stat", (unsigned long long) pid
) < (int) (sizeof(fn
)-1));
308 fn
[sizeof(fn
)-1] = 0;
310 if (!(f
= fopen(fn
, "r")))
313 if (!(fgets(line
, sizeof(line
), f
))) {
321 /* Let's skip the pid and comm fields. The latter is enclosed
322 * in () but does not escape any () in its value, so let's
323 * skip over it manually */
325 if (!(p
= strrchr(line
, ')')))
336 if ((long long unsigned) (pid_t
) ppid
!= ppid
)
339 *_ppid
= (pid_t
) ppid
;
344 int write_one_line_file(const char *fn
, const char *line
) {
351 if (!(f
= fopen(fn
, "we")))
354 if (fputs(line
, f
) < 0) {
365 int read_one_line_file(const char *fn
, char **line
) {
373 if (!(f
= fopen(fn
, "re")))
376 if (!(fgets(t
, sizeof(t
), f
))) {
381 if (!(c
= strdup(t
))) {
394 char *strappend(const char *s
, const char *suffix
) {
404 if (!(r
= new(char, a
+b
+1)))
408 memcpy(r
+a
, suffix
, b
);
414 int readlink_malloc(const char *p
, char **r
) {
424 if (!(c
= new(char, l
)))
427 if ((n
= readlink(p
, c
, l
-1)) < 0) {
433 if ((size_t) n
< l
-1) {
444 char *file_name_from_path(const char *p
) {
449 if ((r
= strrchr(p
, '/')))
455 bool path_is_absolute(const char *p
) {
461 bool is_path(const char *p
) {
463 return !!strchr(p
, '/');
466 char *path_make_absolute(const char *p
, const char *prefix
) {
471 if (path_is_absolute(p
) || !prefix
)
474 if (asprintf(&r
, "%s/%s", prefix
, p
) < 0)
480 int reset_all_signal_handlers(void) {
483 for (sig
= 1; sig
< _NSIG
; sig
++) {
486 if (sig
== SIGKILL
|| sig
== SIGSTOP
)
490 sa
.sa_handler
= SIG_DFL
;
491 sa
.sa_flags
= SA_RESTART
;
493 /* On Linux the first two RT signals are reserved by
494 * glibc, and sigaction() will return EINVAL for them. */
495 if ((sigaction(sig
, &sa
, NULL
) < 0))
503 char *strstrip(char *s
) {
506 /* Drops trailing whitespace. Modifies the string in
507 * place. Returns pointer to first non-space character */
509 s
+= strspn(s
, WHITESPACE
);
512 if (!strchr(WHITESPACE
, *e
))
524 char *file_in_same_dir(const char *path
, const char *filename
) {
531 /* This removes the last component of path and appends
532 * filename, unless the latter is absolute anyway or the
535 if (path_is_absolute(filename
))
536 return strdup(filename
);
538 if (!(e
= strrchr(path
, '/')))
539 return strdup(filename
);
541 k
= strlen(filename
);
542 if (!(r
= new(char, e
-path
+1+k
+1)))
545 memcpy(r
, path
, e
-path
+1);
546 memcpy(r
+(e
-path
)+1, filename
, k
+1);
551 char hexchar(int x
) {
552 static const char table
[16] = "0123456789abcdef";
554 return table
[x
& 15];
557 int unhexchar(char c
) {
559 if (c
>= '0' && c
<= '9')
562 if (c
>= 'a' && c
<= 'f')
565 if (c
>= 'A' && c
<= 'F')
571 char octchar(int x
) {
572 return '0' + (x
& 7);
575 int unoctchar(char c
) {
577 if (c
>= '0' && c
<= '7')
583 char *cescape(const char *s
) {
589 /* Does C style string escaping. */
591 if (!(r
= new(char, strlen(s
)*4 + 1)))
594 for (f
= s
, t
= r
; *f
; f
++)
640 /* For special chars we prefer octal over
641 * hexadecimal encoding, simply because glib's
642 * g_strescape() does the same */
643 if ((*f
< ' ') || (*f
>= 127)) {
645 *(t
++) = octchar((unsigned char) *f
>> 6);
646 *(t
++) = octchar((unsigned char) *f
>> 3);
647 *(t
++) = octchar((unsigned char) *f
);
658 char *cunescape(const char *s
) {
664 /* Undoes C style string escaping */
666 if (!(r
= new(char, strlen(s
)+1)))
669 for (f
= s
, t
= r
; *f
; f
++) {
712 /* hexadecimal encoding */
715 if ((a
= unhexchar(f
[1])) < 0 ||
716 (b
= unhexchar(f
[2])) < 0) {
717 /* Invalid escape code, let's take it literal then */
721 *(t
++) = (char) ((a
<< 4) | b
);
739 if ((a
= unoctchar(f
[0])) < 0 ||
740 (b
= unoctchar(f
[1])) < 0 ||
741 (c
= unoctchar(f
[2])) < 0) {
742 /* Invalid escape code, let's take it literal then */
746 *(t
++) = (char) ((a
<< 6) | (b
<< 3) | c
);
754 /* premature end of string.*/
759 /* Invalid escape code, let's take it literal then */
772 char *xescape(const char *s
, const char *bad
) {
776 /* Escapes all chars in bad, in addition to \ and all special
777 * chars, in \xFF style escaping. May be reversed with
780 if (!(r
= new(char, strlen(s
)*4+1)))
783 for (f
= s
, t
= r
; *f
; f
++) {
785 if (*f
< ' ' || *f
>= 127 ||
786 *f
== '\\' || strchr(bad
, *f
)) {
789 *(t
++) = hexchar(*f
>> 4);
790 *(t
++) = hexchar(*f
);
800 char *path_kill_slashes(char *path
) {
804 /* Removes redundant inner and trailing slashes. Modifies the
805 * passed string in-place.
807 * ///foo///bar/ becomes /foo/bar
810 for (f
= path
, t
= path
; *f
; f
++) {
825 /* Special rule, if we are talking of the root directory, a
826 trailing slash is good */
828 if (t
== path
&& slash
)
835 bool path_startswith(const char *path
, const char *prefix
) {
839 if ((path
[0] == '/') != (prefix
[0] == '/'))
845 path
+= strspn(path
, "/");
846 prefix
+= strspn(prefix
, "/");
854 a
= strcspn(path
, "/");
855 b
= strcspn(prefix
, "/");
860 if (memcmp(path
, prefix
, a
) != 0)
868 char *ascii_strlower(char *path
) {
873 for (p
= path
; *p
; p
++)
874 if (*p
>= 'A' && *p
<= 'Z')