util.c 94 KB
Newer Older
1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
Lennart Poettering's avatar
Lennart Poettering committed
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/***
  This file is part of systemd.

  Copyright 2010 Lennart Poettering

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  systemd 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
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

Lennart Poettering's avatar
Lennart Poettering committed
22
23
24
25
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
26
#include <stdlib.h>
27
28
#include <signal.h>
#include <stdio.h>
29
30
31
#include <syslog.h>
#include <sched.h>
#include <sys/resource.h>
Kay Sievers's avatar
Kay Sievers committed
32
#include <linux/sched.h>
33
34
#include <sys/types.h>
#include <sys/stat.h>
35
#include <fcntl.h>
36
#include <dirent.h>
37
38
39
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <linux/tiocl.h>
Lennart Poettering's avatar
Lennart Poettering committed
40
41
42
43
#include <termios.h>
#include <stdarg.h>
#include <sys/inotify.h>
#include <sys/poll.h>
44
#include <libgen.h>
45
#include <ctype.h>
46
#include <sys/prctl.h>
47
48
#include <sys/utsname.h>
#include <pwd.h>
49
#include <netinet/ip.h>
50
#include <linux/kd.h>
51
#include <dlfcn.h>
52
#include <sys/wait.h>
Lennart Poettering's avatar
Lennart Poettering committed
53
54
55

#include "macro.h"
#include "util.h"
56
57
#include "ioprio.h"
#include "missing.h"
58
#include "log.h"
59
#include "strv.h"
60
#include "label.h"
61
#include "exit-status.h"
62

63
64
65
66
67
68
69
70
71
72
73
74
75
bool streq_ptr(const char *a, const char *b) {

        /* Like streq(), but tries to make sense of NULL pointers */

        if (a && b)
                return streq(a, b);

        if (!a && !b)
                return true;

        return false;
}

76
usec_t now(clockid_t clock_id) {
Lennart Poettering's avatar
Lennart Poettering committed
77
78
        struct timespec ts;

79
        assert_se(clock_gettime(clock_id, &ts) == 0);
Lennart Poettering's avatar
Lennart Poettering committed
80
81
82
83

        return timespec_load(&ts);
}

84
dual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
85
86
87
88
89
90
91
92
        assert(ts);

        ts->realtime = now(CLOCK_REALTIME);
        ts->monotonic = now(CLOCK_MONOTONIC);

        return ts;
}

Lennart Poettering's avatar
Lennart Poettering committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
usec_t timespec_load(const struct timespec *ts) {
        assert(ts);

        return
                (usec_t) ts->tv_sec * USEC_PER_SEC +
                (usec_t) ts->tv_nsec / NSEC_PER_USEC;
}

struct timespec *timespec_store(struct timespec *ts, usec_t u)  {
        assert(ts);

        ts->tv_sec = (time_t) (u / USEC_PER_SEC);
        ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);

        return ts;
}

usec_t timeval_load(const struct timeval *tv) {
        assert(tv);

        return
                (usec_t) tv->tv_sec * USEC_PER_SEC +
                (usec_t) tv->tv_usec;
}

struct timeval *timeval_store(struct timeval *tv, usec_t u) {
        assert(tv);

        tv->tv_sec = (time_t) (u / USEC_PER_SEC);
        tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);

        return tv;
}

bool endswith(const char *s, const char *postfix) {
        size_t sl, pl;

        assert(s);
        assert(postfix);

        sl = strlen(s);
        pl = strlen(postfix);

136
137
138
        if (pl == 0)
                return true;

Lennart Poettering's avatar
Lennart Poettering committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        if (sl < pl)
                return false;

        return memcmp(s + sl - pl, postfix, pl) == 0;
}

bool startswith(const char *s, const char *prefix) {
        size_t sl, pl;

        assert(s);
        assert(prefix);

        sl = strlen(s);
        pl = strlen(prefix);

154
155
156
        if (pl == 0)
                return true;

Lennart Poettering's avatar
Lennart Poettering committed
157
158
159
160
161
162
        if (sl < pl)
                return false;

        return memcmp(s, prefix, pl) == 0;
}

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
bool startswith_no_case(const char *s, const char *prefix) {
        size_t sl, pl;
        unsigned i;

        assert(s);
        assert(prefix);

        sl = strlen(s);
        pl = strlen(prefix);

        if (pl == 0)
                return true;

        if (sl < pl)
                return false;

        for(i = 0; i < pl; ++i) {
                if (tolower(s[i]) != tolower(prefix[i]))
                        return false;
        }

        return true;
}

Lennart Poettering's avatar
Lennart Poettering committed
187
188
189
190
191
192
193
194
195
196
197
198
bool first_word(const char *s, const char *word) {
        size_t sl, wl;

        assert(s);
        assert(word);

        sl = strlen(s);
        wl = strlen(word);

        if (sl < wl)
                return false;

199
200
201
        if (wl == 0)
                return true;

Lennart Poettering's avatar
Lennart Poettering committed
202
203
204
        if (memcmp(s, word, wl) != 0)
                return false;

205
206
        return s[wl] == 0 ||
                strchr(WHITESPACE, s[wl]);
Lennart Poettering's avatar
Lennart Poettering committed
207
208
}

209
int close_nointr(int fd) {
Lennart Poettering's avatar
Lennart Poettering committed
210
211
212
213
214
215
216
217
218
219
220
221
        assert(fd >= 0);

        for (;;) {
                int r;

                if ((r = close(fd)) >= 0)
                        return r;

                if (errno != EINTR)
                        return r;
        }
}
222

223
void close_nointr_nofail(int fd) {
Lennart Poettering's avatar
Lennart Poettering committed
224
        int saved_errno = errno;
225
226
227
228
229

        /* like close_nointr() but cannot fail, and guarantees errno
         * is unchanged */

        assert_se(close_nointr(fd) == 0);
Lennart Poettering's avatar
Lennart Poettering committed
230
231

        errno = saved_errno;
232
233
}

234
235
236
237
238
239
240
void close_many(const int fds[], unsigned n_fd) {
        unsigned i;

        for (i = 0; i < n_fd; i++)
                close_nointr_nofail(fds[i]);
}

241
242
243
int parse_boolean(const char *v) {
        assert(v);

Lennart Poettering's avatar
Lennart Poettering committed
244
        if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
245
                return 1;
Lennart Poettering's avatar
Lennart Poettering committed
246
        else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
247
248
249
250
251
                return 0;

        return -EINVAL;
}

252
int parse_pid(const char *s, pid_t* ret_pid) {
Lennart Poettering's avatar
Lennart Poettering committed
253
        unsigned long ul = 0;
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
        pid_t pid;
        int r;

        assert(s);
        assert(ret_pid);

        if ((r = safe_atolu(s, &ul)) < 0)
                return r;

        pid = (pid_t) ul;

        if ((unsigned long) pid != ul)
                return -ERANGE;

        if (pid <= 0)
                return -ERANGE;

        *ret_pid = pid;
        return 0;
}

275
276
int safe_atou(const char *s, unsigned *ret_u) {
        char *x = NULL;
277
        unsigned long l;
278
279
280
281
282
283
284
285
286
287

        assert(s);
        assert(ret_u);

        errno = 0;
        l = strtoul(s, &x, 0);

        if (!x || *x || errno)
                return errno ? -errno : -EINVAL;

288
        if ((unsigned long) (unsigned) l != l)
289
290
291
292
293
294
295
296
                return -ERANGE;

        *ret_u = (unsigned) l;
        return 0;
}

int safe_atoi(const char *s, int *ret_i) {
        char *x = NULL;
297
        long l;
298
299
300
301
302
303
304
305
306
307

        assert(s);
        assert(ret_i);

        errno = 0;
        l = strtol(s, &x, 0);

        if (!x || *x || errno)
                return errno ? -errno : -EINVAL;

308
        if ((long) (int) l != l)
309
310
                return -ERANGE;

311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        *ret_i = (int) l;
        return 0;
}

int safe_atollu(const char *s, long long unsigned *ret_llu) {
        char *x = NULL;
        unsigned long long l;

        assert(s);
        assert(ret_llu);

        errno = 0;
        l = strtoull(s, &x, 0);

        if (!x || *x || errno)
                return errno ? -errno : -EINVAL;

        *ret_llu = l;
        return 0;
}

int safe_atolli(const char *s, long long int *ret_lli) {
        char *x = NULL;
        long long l;

        assert(s);
        assert(ret_lli);

        errno = 0;
        l = strtoll(s, &x, 0);

        if (!x || *x || errno)
                return errno ? -errno : -EINVAL;

        *ret_lli = l;
346
347
        return 0;
}
348
349

/* Split a string into words. */
350
char *split(const char *c, size_t *l, const char *separator, char **state) {
351
352
353
354
355
356
357
        char *current;

        current = *state ? *state : (char*) c;

        if (!*current || *c == 0)
                return NULL;

358
359
        current += strspn(current, separator);
        *l = strcspn(current, separator);
360
361
362
363
364
        *state = current+*l;

        return (char*) current;
}

365
366
367
/* Split a string into words, but consider strings enclosed in '' and
 * "" as words even if they include spaces. */
char *split_quoted(const char *c, size_t *l, char **state) {
368
369
        char *current, *e;
        bool escaped = false;
370
371
372
373
374
375
376
377
378
379
380

        current = *state ? *state : (char*) c;

        if (!*current || *c == 0)
                return NULL;

        current += strspn(current, WHITESPACE);

        if (*current == '\'') {
                current ++;

381
382
383
384
385
386
387
388
389
390
391
                for (e = current; *e; e++) {
                        if (escaped)
                                escaped = false;
                        else if (*e == '\\')
                                escaped = true;
                        else if (*e == '\'')
                                break;
                }

                *l = e-current;
                *state = *e == 0 ? e : e+1;
392
393
394
        } else if (*current == '\"') {
                current ++;

395
396
397
398
399
400
401
402
403
404
405
                for (e = current; *e; e++) {
                        if (escaped)
                                escaped = false;
                        else if (*e == '\\')
                                escaped = true;
                        else if (*e == '\"')
                                break;
                }

                *l = e-current;
                *state = *e == 0 ? e : e+1;
406
        } else {
407
408
409
410
411
412
413
414
415
416
                for (e = current; *e; e++) {
                        if (escaped)
                                escaped = false;
                        else if (*e == '\\')
                                escaped = true;
                        else if (strchr(WHITESPACE, *e))
                                break;
                }
                *l = e-current;
                *state = e;
417
418
419
420
421
        }

        return (char*) current;
}

422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
char **split_path_and_make_absolute(const char *p) {
        char **l;
        assert(p);

        if (!(l = strv_split(p, ":")))
                return NULL;

        if (!strv_path_make_absolute_cwd(l)) {
                strv_free(l);
                return NULL;
        }

        return l;
}

437
438
439
440
int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
        int r;
        FILE *f;
        char fn[132], line[256], *p;
441
        long unsigned ppid;
442
443
444
445

        assert(pid >= 0);
        assert(_ppid);

446
        assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
        fn[sizeof(fn)-1] = 0;

        if (!(f = fopen(fn, "r")))
                return -errno;

        if (!(fgets(line, sizeof(line), f))) {
                r = -errno;
                fclose(f);
                return r;
        }

        fclose(f);

        /* Let's skip the pid and comm fields. The latter is enclosed
         * in () but does not escape any () in its value, so let's
         * skip over it manually */

        if (!(p = strrchr(line, ')')))
                return -EIO;

        p++;

        if (sscanf(p, " "
                   "%*c "  /* state */
471
                   "%lu ", /* ppid */
472
473
474
                   &ppid) != 1)
                return -EIO;

475
        if ((long unsigned) (pid_t) ppid != ppid)
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
                return -ERANGE;

        *_ppid = (pid_t) ppid;

        return 0;
}

int write_one_line_file(const char *fn, const char *line) {
        FILE *f;
        int r;

        assert(fn);
        assert(line);

        if (!(f = fopen(fn, "we")))
                return -errno;

        if (fputs(line, f) < 0) {
                r = -errno;
                goto finish;
        }

498
499
500
        if (!endswith(line, "\n"))
                fputc('\n', f);

501
502
503
504
505
506
507
508
509
        r = 0;
finish:
        fclose(f);
        return r;
}

int read_one_line_file(const char *fn, char **line) {
        FILE *f;
        int r;
510
        char t[LINE_MAX], *c;
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

        assert(fn);
        assert(line);

        if (!(f = fopen(fn, "re")))
                return -errno;

        if (!(fgets(t, sizeof(t), f))) {
                r = -errno;
                goto finish;
        }

        if (!(c = strdup(t))) {
                r = -ENOMEM;
                goto finish;
        }

        *line = c;
        r = 0;

finish:
        fclose(f);
        return r;
}
Lennart Poettering's avatar
Lennart Poettering committed
535

536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
int read_full_file(const char *fn, char **contents) {
        FILE *f;
        int r;
        size_t n, l;
        char *buf = NULL;
        struct stat st;

        if (!(f = fopen(fn, "re")))
                return -errno;

        if (fstat(fileno(f), &st) < 0) {
                r = -errno;
                goto finish;
        }

        n = st.st_size > 0 ? st.st_size : LINE_MAX;
        l = 0;

        for (;;) {
                char *t;
                size_t k;

                if (!(t = realloc(buf, n+1))) {
                        r = -ENOMEM;
                        goto finish;
                }

                buf = t;
                k = fread(buf + l, 1, n - l, f);

                if (k <= 0) {
                        if (ferror(f)) {
                                r = -errno;
                                goto finish;
                        }

                        break;
                }

                l += k;
                n *= 2;

                /* Safety check */
                if (n > 4*1024*1024) {
                        r = -E2BIG;
                        goto finish;
                }
        }

        if (buf)
                buf[l] = 0;
        else if (!(buf = calloc(1, 1))) {
                r = -errno;
                goto finish;
        }

        *contents = buf;
        buf = NULL;

        r = 0;

finish:
        fclose(f);
        free(buf);

        return r;
}

int parse_env_file(
                const char *fname,
Dave Reisner's avatar
Dave Reisner committed
606
                const char *separator, ...) {
607

608
        int r = 0;
609
610
611
        char *contents, *p;

        assert(fname);
Dave Reisner's avatar
Dave Reisner committed
612
        assert(separator);
613
614
615
616
617
618
619
620

        if ((r = read_full_file(fname, &contents)) < 0)
                return r;

        p = contents;
        for (;;) {
                const char *key = NULL;

Dave Reisner's avatar
Dave Reisner committed
621
                p += strspn(p, separator);
622
623
624
625
626
627
628
629
630
                p += strspn(p, WHITESPACE);

                if (!*p)
                        break;

                if (!strchr(COMMENTS, *p)) {
                        va_list ap;
                        char **value;

Dave Reisner's avatar
Dave Reisner committed
631
                        va_start(ap, separator);
632
633
634
635
636
637
638
639
640
641
642
643
                        while ((key = va_arg(ap, char *))) {
                                size_t n;
                                char *v;

                                value = va_arg(ap, char **);

                                n = strlen(key);
                                if (strncmp(p, key, n) != 0 ||
                                    p[n] != '=')
                                        continue;

                                p += n + 1;
Dave Reisner's avatar
Dave Reisner committed
644
                                n = strcspn(p, separator);
645
646

                                if (n >= 2 &&
647
648
                                    strchr(QUOTES, p[0]) &&
                                    p[n-1] == p[0])
649
650
651
652
653
654
655
656
657
658
                                        v = strndup(p+1, n-2);
                                else
                                        v = strndup(p, n);

                                if (!v) {
                                        r = -ENOMEM;
                                        va_end(ap);
                                        goto fail;
                                }

659
660
661
662
663
664
                                if (v[0] == '\0') {
                                        /* return empty value strings as NULL */
                                        free(v);
                                        v = NULL;
                                }

665
666
667
668
                                free(*value);
                                *value = v;

                                p += n;
669
670

                                r ++;
671
672
673
674
675
676
                                break;
                        }
                        va_end(ap);
                }

                if (!key)
Dave Reisner's avatar
Dave Reisner committed
677
                        p += strcspn(p, separator);
678
679
680
681
682
683
684
        }

fail:
        free(contents);
        return r;
}

685
686
687
688
689
690
691
692
693
694
695
696
697
698
char *truncate_nl(char *s) {
        assert(s);

        s[strcspn(s, NEWLINE)] = 0;
        return s;
}

int get_process_name(pid_t pid, char **name) {
        char *p;
        int r;

        assert(pid >= 1);
        assert(name);

699
        if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
700
701
702
703
704
705
706
707
708
709
710
711
                return -ENOMEM;

        r = read_one_line_file(p, name);
        free(p);

        if (r < 0)
                return r;

        truncate_nl(*name);
        return 0;
}

712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
        char *p, *r, *k;
        int c;
        bool space = false;
        size_t left;
        FILE *f;

        assert(pid >= 1);
        assert(max_length > 0);
        assert(line);

        if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
                return -ENOMEM;

        f = fopen(p, "r");
        free(p);

        if (!f)
                return -errno;

        if (!(r = new(char, max_length))) {
                fclose(f);
                return -ENOMEM;
        }

        k = r;
        left = max_length;
        while ((c = getc(f)) != EOF) {

                if (isprint(c)) {
                        if (space) {
                                if (left <= 4)
                                        break;

                                *(k++) = ' ';
747
                                left--;
748
749
750
751
752
753
754
                                space = false;
                        }

                        if (left <= 4)
                                break;

                        *(k++) = (char) c;
755
                        left--;
756
757
758
759
760
761
762
763
764
765
766
767
768
                }  else
                        space = true;
        }

        if (left <= 4) {
                size_t n = MIN(left-1, 3U);
                memcpy(k, "...", n);
                k[n] = 0;
        } else
                *k = 0;

        fclose(f);

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
        /* Kernel threads have no argv[] */
        if (r[0] == 0) {
                char *t;
                int h;

                free(r);

                if ((h = get_process_name(pid, &t)) < 0)
                        return h;

                h = asprintf(&r, "[%s]", t);
                free(t);

                if (h < 0)
                        return -ENOMEM;
        }
785

786
787
788
789
        *line = r;
        return 0;
}

790
791
char *strnappend(const char *s, const char *suffix, size_t b) {
        size_t a;
Lennart Poettering's avatar
Lennart Poettering committed
792
793
        char *r;

794
795
796
797
798
799
800
801
802
        if (!s && !suffix)
                return strdup("");

        if (!s)
                return strndup(suffix, b);

        if (!suffix)
                return strdup(s);

Lennart Poettering's avatar
Lennart Poettering committed
803
804
805
806
807
808
809
810
811
812
813
814
815
816
        assert(s);
        assert(suffix);

        a = strlen(s);

        if (!(r = new(char, a+b+1)))
                return NULL;

        memcpy(r, s, a);
        memcpy(r+a, suffix, b);
        r[a+b] = 0;

        return r;
}
Lennart Poettering's avatar
Lennart Poettering committed
817

818
819
820
821
char *strappend(const char *s, const char *suffix) {
        return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
}

Lennart Poettering's avatar
Lennart Poettering committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
int readlink_malloc(const char *p, char **r) {
        size_t l = 100;

        assert(p);
        assert(r);

        for (;;) {
                char *c;
                ssize_t n;

                if (!(c = new(char, l)))
                        return -ENOMEM;

                if ((n = readlink(p, c, l-1)) < 0) {
                        int ret = -errno;
                        free(c);
                        return ret;
                }

                if ((size_t) n < l-1) {
                        c[n] = 0;
                        *r = c;
                        return 0;
                }

                free(c);
                l *= 2;
        }
}

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
int readlink_and_make_absolute(const char *p, char **r) {
        char *target, *k;
        int j;

        assert(p);
        assert(r);

        if ((j = readlink_malloc(p, &target)) < 0)
                return j;

        k = file_in_same_dir(p, target);
        free(target);

        if (!k)
                return -ENOMEM;

        *r = k;
        return 0;
}

872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
int parent_of_path(const char *path, char **_r) {
        const char *e, *a = NULL, *b = NULL, *p;
        char *r;
        bool slash = false;

        assert(path);
        assert(_r);

        if (!*path)
                return -EINVAL;

        for (e = path; *e; e++) {

                if (!slash && *e == '/') {
                        a = b;
                        b = e;
                        slash = true;
                } else if (slash && *e != '/')
                        slash = false;
        }

        if (*(e-1) == '/')
                p = a;
        else
                p = b;

        if (!p)
                return -EINVAL;

        if (p == path)
                r = strdup("/");
        else
                r = strndup(path, p-path);

        if (!r)
                return -ENOMEM;

        *_r = r;
        return 0;
}


Lennart Poettering's avatar
Lennart Poettering committed
914
915
916
917
918
919
920
921
922
923
char *file_name_from_path(const char *p) {
        char *r;

        assert(p);

        if ((r = strrchr(p, '/')))
                return r + 1;

        return (char*) p;
}
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940

bool path_is_absolute(const char *p) {
        assert(p);

        return p[0] == '/';
}

bool is_path(const char *p) {

        return !!strchr(p, '/');
}

char *path_make_absolute(const char *p, const char *prefix) {
        char *r;

        assert(p);

941
942
943
        /* Makes every item in the list an absolute path by prepending
         * the prefix, if specified and necessary */

944
945
946
947
948
949
950
951
        if (path_is_absolute(p) || !prefix)
                return strdup(p);

        if (asprintf(&r, "%s/%s", prefix, p) < 0)
                return NULL;

        return r;
}
952

953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
char *path_make_absolute_cwd(const char *p) {
        char *cwd, *r;

        assert(p);

        /* Similar to path_make_absolute(), but prefixes with the
         * current working directory. */

        if (path_is_absolute(p))
                return strdup(p);

        if (!(cwd = get_current_dir_name()))
                return NULL;

        r = path_make_absolute(p, cwd);
        free(cwd);

        return r;
}

char **strv_path_make_absolute_cwd(char **l) {
        char **s;

        /* Goes through every item in the string list and makes it
         * absolute. This works in place and won't rollback any
         * changes on failure. */

        STRV_FOREACH(s, l) {
                char *t;

                if (!(t = path_make_absolute_cwd(*s)))
                        return NULL;

                free(*s);
                *s = t;
        }

        return l;
}

993
994
995
996
997
998
999
1000
char **strv_path_canonicalize(char **l) {
        char **s;
        unsigned k = 0;
        bool enomem = false;

        if (strv_isempty(l))
                return l;

For faster browsing, not all history is shown. View entire blame