chiark / gitweb /
util: add decchar()/undecchar() calls
[elogind.git] / util.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <syslog.h>
30 #include <sched.h>
31 #include <sys/resource.h>
32 #include <linux/sched.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include "macro.h"
37 #include "util.h"
38 #include "ioprio.h"
39 #include "missing.h"
40 #include "log.h"
41 #include "strv.h"
42
43 usec_t now(clockid_t clock_id) {
44         struct timespec ts;
45
46         assert_se(clock_gettime(clock_id, &ts) == 0);
47
48         return timespec_load(&ts);
49 }
50
51 usec_t timespec_load(const struct timespec *ts) {
52         assert(ts);
53
54         return
55                 (usec_t) ts->tv_sec * USEC_PER_SEC +
56                 (usec_t) ts->tv_nsec / NSEC_PER_USEC;
57 }
58
59 struct timespec *timespec_store(struct timespec *ts, usec_t u)  {
60         assert(ts);
61
62         ts->tv_sec = (time_t) (u / USEC_PER_SEC);
63         ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
64
65         return ts;
66 }
67
68 usec_t timeval_load(const struct timeval *tv) {
69         assert(tv);
70
71         return
72                 (usec_t) tv->tv_sec * USEC_PER_SEC +
73                 (usec_t) tv->tv_usec;
74 }
75
76 struct timeval *timeval_store(struct timeval *tv, usec_t u) {
77         assert(tv);
78
79         tv->tv_sec = (time_t) (u / USEC_PER_SEC);
80         tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
81
82         return tv;
83 }
84
85 bool endswith(const char *s, const char *postfix) {
86         size_t sl, pl;
87
88         assert(s);
89         assert(postfix);
90
91         sl = strlen(s);
92         pl = strlen(postfix);
93
94         if (sl < pl)
95                 return false;
96
97         return memcmp(s + sl - pl, postfix, pl) == 0;
98 }
99
100 bool startswith(const char *s, const char *prefix) {
101         size_t sl, pl;
102
103         assert(s);
104         assert(prefix);
105
106         sl = strlen(s);
107         pl = strlen(prefix);
108
109         if (sl < pl)
110                 return false;
111
112         return memcmp(s, prefix, pl) == 0;
113 }
114
115 bool first_word(const char *s, const char *word) {
116         size_t sl, wl;
117
118         assert(s);
119         assert(word);
120
121         sl = strlen(s);
122         wl = strlen(word);
123
124         if (sl < wl)
125                 return false;
126
127         if (memcmp(s, word, wl) != 0)
128                 return false;
129
130         return (s[wl] == 0 ||
131                 strchr(WHITESPACE, s[wl]));
132 }
133
134 int close_nointr(int fd) {
135         assert(fd >= 0);
136
137         for (;;) {
138                 int r;
139
140                 if ((r = close(fd)) >= 0)
141                         return r;
142
143                 if (errno != EINTR)
144                         return r;
145         }
146 }
147
148 void close_nointr_nofail(int fd) {
149
150         /* like close_nointr() but cannot fail, and guarantees errno
151          * is unchanged */
152
153         assert_se(close_nointr(fd) == 0);
154 }
155
156 int parse_boolean(const char *v) {
157         assert(v);
158
159         if (streq(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
160                 return 1;
161         else if (streq(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
162                 return 0;
163
164         return -EINVAL;
165 }
166
167 int safe_atou(const char *s, unsigned *ret_u) {
168         char *x = NULL;
169         unsigned long l;
170
171         assert(s);
172         assert(ret_u);
173
174         errno = 0;
175         l = strtoul(s, &x, 0);
176
177         if (!x || *x || errno)
178                 return errno ? -errno : -EINVAL;
179
180         if ((unsigned long) (unsigned) l != l)
181                 return -ERANGE;
182
183         *ret_u = (unsigned) l;
184         return 0;
185 }
186
187 int safe_atoi(const char *s, int *ret_i) {
188         char *x = NULL;
189         long l;
190
191         assert(s);
192         assert(ret_i);
193
194         errno = 0;
195         l = strtol(s, &x, 0);
196
197         if (!x || *x || errno)
198                 return errno ? -errno : -EINVAL;
199
200         if ((long) (int) l != l)
201                 return -ERANGE;
202
203         *ret_i = (int) l;
204         return 0;
205 }
206
207 int safe_atolu(const char *s, long unsigned *ret_lu) {
208         char *x = NULL;
209         unsigned long l;
210
211         assert(s);
212         assert(ret_lu);
213
214         errno = 0;
215         l = strtoul(s, &x, 0);
216
217         if (!x || *x || errno)
218                 return errno ? -errno : -EINVAL;
219
220         *ret_lu = l;
221         return 0;
222 }
223
224 int safe_atoli(const char *s, long int *ret_li) {
225         char *x = NULL;
226         long l;
227
228         assert(s);
229         assert(ret_li);
230
231         errno = 0;
232         l = strtol(s, &x, 0);
233
234         if (!x || *x || errno)
235                 return errno ? -errno : -EINVAL;
236
237         *ret_li = l;
238         return 0;
239 }
240
241 int safe_atollu(const char *s, long long unsigned *ret_llu) {
242         char *x = NULL;
243         unsigned long long l;
244
245         assert(s);
246         assert(ret_llu);
247
248         errno = 0;
249         l = strtoull(s, &x, 0);
250
251         if (!x || *x || errno)
252                 return errno ? -errno : -EINVAL;
253
254         *ret_llu = l;
255         return 0;
256 }
257
258 int safe_atolli(const char *s, long long int *ret_lli) {
259         char *x = NULL;
260         long long l;
261
262         assert(s);
263         assert(ret_lli);
264
265         errno = 0;
266         l = strtoll(s, &x, 0);
267
268         if (!x || *x || errno)
269                 return errno ? -errno : -EINVAL;
270
271         *ret_lli = l;
272         return 0;
273 }
274
275 /* Split a string into words. */
276 char *split(const char *c, size_t *l, const char *separator, char **state) {
277         char *current;
278
279         current = *state ? *state : (char*) c;
280
281         if (!*current || *c == 0)
282                 return NULL;
283
284         current += strspn(current, separator);
285         *l = strcspn(current, separator);
286         *state = current+*l;
287
288         return (char*) current;
289 }
290
291 /* Split a string into words, but consider strings enclosed in '' and
292  * "" as words even if they include spaces. */
293 char *split_quoted(const char *c, size_t *l, char **state) {
294         char *current;
295
296         current = *state ? *state : (char*) c;
297
298         if (!*current || *c == 0)
299                 return NULL;
300
301         current += strspn(current, WHITESPACE);
302
303         if (*current == '\'') {
304                 current ++;
305                 *l = strcspn(current, "'");
306                 *state = current+*l;
307
308                 if (**state == '\'')
309                         (*state)++;
310         } else if (*current == '\"') {
311                 current ++;
312                 *l = strcspn(current, "\"");
313                 *state = current+*l;
314
315                 if (**state == '\"')
316                         (*state)++;
317         } else {
318                 *l = strcspn(current, WHITESPACE);
319                 *state = current+*l;
320         }
321
322         /* FIXME: Cannot deal with strings that have spaces AND ticks
323          * in them */
324
325         return (char*) current;
326 }
327
328 char **split_path_and_make_absolute(const char *p) {
329         char **l;
330         assert(p);
331
332         if (!(l = strv_split(p, ":")))
333                 return NULL;
334
335         if (!strv_path_make_absolute_cwd(l)) {
336                 strv_free(l);
337                 return NULL;
338         }
339
340         return l;
341 }
342
343 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
344         int r;
345         FILE *f;
346         char fn[132], line[256], *p;
347         long long unsigned ppid;
348
349         assert(pid >= 0);
350         assert(_ppid);
351
352         assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%llu/stat", (unsigned long long) pid) < (int) (sizeof(fn)-1));
353         fn[sizeof(fn)-1] = 0;
354
355         if (!(f = fopen(fn, "r")))
356                 return -errno;
357
358         if (!(fgets(line, sizeof(line), f))) {
359                 r = -errno;
360                 fclose(f);
361                 return r;
362         }
363
364         fclose(f);
365
366         /* Let's skip the pid and comm fields. The latter is enclosed
367          * in () but does not escape any () in its value, so let's
368          * skip over it manually */
369
370         if (!(p = strrchr(line, ')')))
371                 return -EIO;
372
373         p++;
374
375         if (sscanf(p, " "
376                    "%*c "  /* state */
377                    "%llu ", /* ppid */
378                    &ppid) != 1)
379                 return -EIO;
380
381         if ((long long unsigned) (pid_t) ppid != ppid)
382                 return -ERANGE;
383
384         *_ppid = (pid_t) ppid;
385
386         return 0;
387 }
388
389 int write_one_line_file(const char *fn, const char *line) {
390         FILE *f;
391         int r;
392
393         assert(fn);
394         assert(line);
395
396         if (!(f = fopen(fn, "we")))
397                 return -errno;
398
399         if (fputs(line, f) < 0) {
400                 r = -errno;
401                 goto finish;
402         }
403
404         r = 0;
405 finish:
406         fclose(f);
407         return r;
408 }
409
410 int read_one_line_file(const char *fn, char **line) {
411         FILE *f;
412         int r;
413         char t[64], *c;
414
415         assert(fn);
416         assert(line);
417
418         if (!(f = fopen(fn, "re")))
419                 return -errno;
420
421         if (!(fgets(t, sizeof(t), f))) {
422                 r = -errno;
423                 goto finish;
424         }
425
426         if (!(c = strdup(t))) {
427                 r = -ENOMEM;
428                 goto finish;
429         }
430
431         *line = c;
432         r = 0;
433
434 finish:
435         fclose(f);
436         return r;
437 }
438
439 char *strappend(const char *s, const char *suffix) {
440         size_t a, b;
441         char *r;
442
443         assert(s);
444         assert(suffix);
445
446         a = strlen(s);
447         b = strlen(suffix);
448
449         if (!(r = new(char, a+b+1)))
450                 return NULL;
451
452         memcpy(r, s, a);
453         memcpy(r+a, suffix, b);
454         r[a+b] = 0;
455
456         return r;
457 }
458
459 int readlink_malloc(const char *p, char **r) {
460         size_t l = 100;
461
462         assert(p);
463         assert(r);
464
465         for (;;) {
466                 char *c;
467                 ssize_t n;
468
469                 if (!(c = new(char, l)))
470                         return -ENOMEM;
471
472                 if ((n = readlink(p, c, l-1)) < 0) {
473                         int ret = -errno;
474                         free(c);
475                         return ret;
476                 }
477
478                 if ((size_t) n < l-1) {
479                         c[n] = 0;
480                         *r = c;
481                         return 0;
482                 }
483
484                 free(c);
485                 l *= 2;
486         }
487 }
488
489 char *file_name_from_path(const char *p) {
490         char *r;
491
492         assert(p);
493
494         if ((r = strrchr(p, '/')))
495                 return r + 1;
496
497         return (char*) p;
498 }
499
500 bool path_is_absolute(const char *p) {
501         assert(p);
502
503         return p[0] == '/';
504 }
505
506 bool is_path(const char *p) {
507
508         return !!strchr(p, '/');
509 }
510
511 char *path_make_absolute(const char *p, const char *prefix) {
512         char *r;
513
514         assert(p);
515
516         /* Makes every item in the list an absolute path by prepending
517          * the prefix, if specified and necessary */
518
519         if (path_is_absolute(p) || !prefix)
520                 return strdup(p);
521
522         if (asprintf(&r, "%s/%s", prefix, p) < 0)
523                 return NULL;
524
525         return r;
526 }
527
528 char *path_make_absolute_cwd(const char *p) {
529         char *cwd, *r;
530
531         assert(p);
532
533         /* Similar to path_make_absolute(), but prefixes with the
534          * current working directory. */
535
536         if (path_is_absolute(p))
537                 return strdup(p);
538
539         if (!(cwd = get_current_dir_name()))
540                 return NULL;
541
542         r = path_make_absolute(p, cwd);
543         free(cwd);
544
545         return r;
546 }
547
548 char **strv_path_make_absolute_cwd(char **l) {
549         char **s;
550
551         /* Goes through every item in the string list and makes it
552          * absolute. This works in place and won't rollback any
553          * changes on failure. */
554
555         STRV_FOREACH(s, l) {
556                 char *t;
557
558                 if (!(t = path_make_absolute_cwd(*s)))
559                         return NULL;
560
561                 free(*s);
562                 *s = t;
563         }
564
565         return l;
566 }
567
568 int reset_all_signal_handlers(void) {
569         int sig;
570
571         for (sig = 1; sig < _NSIG; sig++) {
572                 struct sigaction sa;
573
574                 if (sig == SIGKILL || sig == SIGSTOP)
575                         continue;
576
577                 zero(sa);
578                 sa.sa_handler = SIG_DFL;
579                 sa.sa_flags = SA_RESTART;
580
581                 /* On Linux the first two RT signals are reserved by
582                  * glibc, and sigaction() will return EINVAL for them. */
583                 if ((sigaction(sig, &sa, NULL) < 0))
584                         if (errno != EINVAL)
585                                 return -errno;
586         }
587
588         return 0;
589 }
590
591 char *strstrip(char *s) {
592         char *e, *l = NULL;
593
594         /* Drops trailing whitespace. Modifies the string in
595          * place. Returns pointer to first non-space character */
596
597         s += strspn(s, WHITESPACE);
598
599         for (e = s; *e; e++)
600                 if (!strchr(WHITESPACE, *e))
601                         l = e;
602
603         if (l)
604                 *(l+1) = 0;
605         else
606                 *s = 0;
607
608         return s;
609
610 }
611
612 char *file_in_same_dir(const char *path, const char *filename) {
613         char *e, *r;
614         size_t k;
615
616         assert(path);
617         assert(filename);
618
619         /* This removes the last component of path and appends
620          * filename, unless the latter is absolute anyway or the
621          * former isn't */
622
623         if (path_is_absolute(filename))
624                 return strdup(filename);
625
626         if (!(e = strrchr(path, '/')))
627                 return strdup(filename);
628
629         k = strlen(filename);
630         if (!(r = new(char, e-path+1+k+1)))
631                 return NULL;
632
633         memcpy(r, path, e-path+1);
634         memcpy(r+(e-path)+1, filename, k+1);
635
636         return r;
637 }
638
639 int mkdir_parents(const char *path, mode_t mode) {
640         const char *p, *e;
641
642         assert(path);
643
644         /* Creates every parent directory in the path except the last
645          * component. */
646
647         p = path + strspn(path, "/");
648         for (;;) {
649                 int r;
650                 char *t;
651
652                 e = p + strcspn(p, "/");
653                 p = e + strspn(e, "/");
654
655                 /* Is this the last component? If so, then we're
656                  * done */
657                 if (*p == 0)
658                         return 0;
659
660                 if (!(t = strndup(path, e - path)))
661                         return -ENOMEM;
662
663                 r = mkdir(t, mode);
664
665                 free(t);
666
667                 if (r < 0 && errno != EEXIST)
668                         return -errno;
669         }
670 }
671
672 char hexchar(int x) {
673         static const char table[16] = "0123456789abcdef";
674
675         return table[x & 15];
676 }
677
678 int unhexchar(char c) {
679
680         if (c >= '0' && c <= '9')
681                 return c - '0';
682
683         if (c >= 'a' && c <= 'f')
684                 return c - 'a' + 10;
685
686         if (c >= 'A' && c <= 'F')
687                 return c - 'A' + 10;
688
689         return -1;
690 }
691
692 char octchar(int x) {
693         return '0' + (x & 7);
694 }
695
696 int unoctchar(char c) {
697
698         if (c >= '0' && c <= '7')
699                 return c - '0';
700
701         return -1;
702 }
703
704 char decchar(int x) {
705         return '0' + (x % 10);
706 }
707
708 int undecchar(char c) {
709
710         if (c >= '0' && c <= '9')
711                 return c - '0';
712
713         return -1;
714 }
715
716 char *cescape(const char *s) {
717         char *r, *t;
718         const char *f;
719
720         assert(s);
721
722         /* Does C style string escaping. */
723
724         if (!(r = new(char, strlen(s)*4 + 1)))
725                 return NULL;
726
727         for (f = s, t = r; *f; f++)
728
729                 switch (*f) {
730
731                 case '\a':
732                         *(t++) = '\\';
733                         *(t++) = 'a';
734                         break;
735                 case '\b':
736                         *(t++) = '\\';
737                         *(t++) = 'b';
738                         break;
739                 case '\f':
740                         *(t++) = '\\';
741                         *(t++) = 'f';
742                         break;
743                 case '\n':
744                         *(t++) = '\\';
745                         *(t++) = 'n';
746                         break;
747                 case '\r':
748                         *(t++) = '\\';
749                         *(t++) = 'r';
750                         break;
751                 case '\t':
752                         *(t++) = '\\';
753                         *(t++) = 't';
754                         break;
755                 case '\v':
756                         *(t++) = '\\';
757                         *(t++) = 'v';
758                         break;
759                 case '\\':
760                         *(t++) = '\\';
761                         *(t++) = '\\';
762                         break;
763                 case '"':
764                         *(t++) = '\\';
765                         *(t++) = '"';
766                         break;
767                 case '\'':
768                         *(t++) = '\\';
769                         *(t++) = '\'';
770                         break;
771
772                 default:
773                         /* For special chars we prefer octal over
774                          * hexadecimal encoding, simply because glib's
775                          * g_strescape() does the same */
776                         if ((*f < ' ') || (*f >= 127)) {
777                                 *(t++) = '\\';
778                                 *(t++) = octchar((unsigned char) *f >> 6);
779                                 *(t++) = octchar((unsigned char) *f >> 3);
780                                 *(t++) = octchar((unsigned char) *f);
781                         } else
782                                 *(t++) = *f;
783                         break;
784                 }
785
786         *t = 0;
787
788         return r;
789 }
790
791 char *cunescape(const char *s) {
792         char *r, *t;
793         const char *f;
794
795         assert(s);
796
797         /* Undoes C style string escaping */
798
799         if (!(r = new(char, strlen(s)+1)))
800                 return r;
801
802         for (f = s, t = r; *f; f++) {
803
804                 if (*f != '\\') {
805                         *(t++) = *f;
806                         continue;
807                 }
808
809                 f++;
810
811                 switch (*f) {
812
813                 case 'a':
814                         *(t++) = '\a';
815                         break;
816                 case 'b':
817                         *(t++) = '\b';
818                         break;
819                 case 'f':
820                         *(t++) = '\f';
821                         break;
822                 case 'n':
823                         *(t++) = '\n';
824                         break;
825                 case 'r':
826                         *(t++) = '\r';
827                         break;
828                 case 't':
829                         *(t++) = '\t';
830                         break;
831                 case 'v':
832                         *(t++) = '\v';
833                         break;
834                 case '\\':
835                         *(t++) = '\\';
836                         break;
837                 case '"':
838                         *(t++) = '"';
839                         break;
840                 case '\'':
841                         *(t++) = '\'';
842                         break;
843
844                 case 'x': {
845                         /* hexadecimal encoding */
846                         int a, b;
847
848                         if ((a = unhexchar(f[1])) < 0 ||
849                             (b = unhexchar(f[2])) < 0) {
850                                 /* Invalid escape code, let's take it literal then */
851                                 *(t++) = '\\';
852                                 *(t++) = 'x';
853                         } else {
854                                 *(t++) = (char) ((a << 4) | b);
855                                 f += 2;
856                         }
857
858                         break;
859                 }
860
861                 case '0':
862                 case '1':
863                 case '2':
864                 case '3':
865                 case '4':
866                 case '5':
867                 case '6':
868                 case '7': {
869                         /* octal encoding */
870                         int a, b, c;
871
872                         if ((a = unoctchar(f[0])) < 0 ||
873                             (b = unoctchar(f[1])) < 0 ||
874                             (c = unoctchar(f[2])) < 0) {
875                                 /* Invalid escape code, let's take it literal then */
876                                 *(t++) = '\\';
877                                 *(t++) = f[0];
878                         } else {
879                                 *(t++) = (char) ((a << 6) | (b << 3) | c);
880                                 f += 2;
881                         }
882
883                         break;
884                 }
885
886                 case 0:
887                         /* premature end of string.*/
888                         *(t++) = '\\';
889                         goto finish;
890
891                 default:
892                         /* Invalid escape code, let's take it literal then */
893                         *(t++) = '\\';
894                         *(t++) = 'f';
895                         break;
896                 }
897         }
898
899 finish:
900         *t = 0;
901         return r;
902 }
903
904
905 char *xescape(const char *s, const char *bad) {
906         char *r, *t;
907         const char *f;
908
909         /* Escapes all chars in bad, in addition to \ and all special
910          * chars, in \xFF style escaping. May be reversed with
911          * cunescape. */
912
913         if (!(r = new(char, strlen(s)*4+1)))
914                 return NULL;
915
916         for (f = s, t = r; *f; f++) {
917
918                 if ((*f < ' ') || (*f >= 127) ||
919                     (*f == '\\') || strchr(bad, *f)) {
920                         *(t++) = '\\';
921                         *(t++) = 'x';
922                         *(t++) = hexchar(*f >> 4);
923                         *(t++) = hexchar(*f);
924                 } else
925                         *(t++) = *f;
926         }
927
928         *t = 0;
929
930         return r;
931 }
932
933 char *bus_path_escape(const char *s) {
934         char *r, *t;
935         const char *f;
936
937         assert(s);
938
939         /* Escapes all chars that D-Bus' object path cannot deal
940          * with. Can be reverse with bus_path_unescape() */
941
942         if (!(r = new(char, strlen(s)*3+1)))
943                 return NULL;
944
945         for (f = s, t = r; *f; f++) {
946
947                 if (!(*f >= 'A' && *f <= 'Z') &&
948                     !(*f >= 'a' && *f <= 'z') &&
949                     !(*f >= '0' && *f <= '9')) {
950                         *(t++) = '_';
951                         *(t++) = hexchar(*f >> 4);
952                         *(t++) = hexchar(*f);
953                 } else
954                         *(t++) = *f;
955         }
956
957         *t = 0;
958
959         return r;
960 }
961
962 char *bus_path_unescape(const char *s) {
963         char *r, *t;
964         const char *f;
965
966         assert(s);
967
968         if (!(r = new(char, strlen(s)+1)))
969                 return NULL;
970
971         for (f = s, t = r; *f; f++) {
972
973                 if (*f == '_') {
974                         int a, b;
975
976                         if ((a = unhexchar(f[1])) < 0 ||
977                             (b = unhexchar(f[2])) < 0) {
978                                 /* Invalid escape code, let's take it literal then */
979                                 *(t++) = '_';
980                         } else {
981                                 *(t++) = (char) ((a << 4) | b);
982                                 f += 2;
983                         }
984                 } else
985                         *(t++) = *f;
986         }
987
988         *t = 0;
989
990         return r;
991 }
992
993 char *path_kill_slashes(char *path) {
994         char *f, *t;
995         bool slash = false;
996
997         /* Removes redundant inner and trailing slashes. Modifies the
998          * passed string in-place.
999          *
1000          * ///foo///bar/ becomes /foo/bar
1001          */
1002
1003         for (f = path, t = path; *f; f++) {
1004
1005                 if (*f == '/') {
1006                         slash = true;
1007                         continue;
1008                 }
1009
1010                 if (slash) {
1011                         slash = false;
1012                         *(t++) = '/';
1013                 }
1014
1015                 *(t++) = *f;
1016         }
1017
1018         /* Special rule, if we are talking of the root directory, a
1019         trailing slash is good */
1020
1021         if (t == path && slash)
1022                 *(t++) = '/';
1023
1024         *t = 0;
1025         return path;
1026 }
1027
1028 bool path_startswith(const char *path, const char *prefix) {
1029         assert(path);
1030         assert(prefix);
1031
1032         if ((path[0] == '/') != (prefix[0] == '/'))
1033                 return false;
1034
1035         for (;;) {
1036                 size_t a, b;
1037
1038                 path += strspn(path, "/");
1039                 prefix += strspn(prefix, "/");
1040
1041                 if (*prefix == 0)
1042                         return true;
1043
1044                 if (*path == 0)
1045                         return false;
1046
1047                 a = strcspn(path, "/");
1048                 b = strcspn(prefix, "/");
1049
1050                 if (a != b)
1051                         return false;
1052
1053                 if (memcmp(path, prefix, a) != 0)
1054                         return false;
1055
1056                 path += a;
1057                 prefix += b;
1058         }
1059 }
1060
1061 char *ascii_strlower(char *path) {
1062         char *p;
1063
1064         assert(path);
1065
1066         for (p = path; *p; p++)
1067                 if (*p >= 'A' && *p <= 'Z')
1068                         *p = *p - 'A' + 'a';
1069
1070         return p;
1071 }
1072
1073 bool ignore_file(const char *filename) {
1074         assert(filename);
1075
1076         return
1077                 filename[0] == '.' ||
1078                 endswith(filename, "~") ||
1079                 endswith(filename, ".rpmnew") ||
1080                 endswith(filename, ".rpmsave") ||
1081                 endswith(filename, ".rpmorig") ||
1082                 endswith(filename, ".dpkg-old") ||
1083                 endswith(filename, ".dpkg-new") ||
1084                 endswith(filename, ".swp");
1085 }
1086
1087 static const char *const ioprio_class_table[] = {
1088         [IOPRIO_CLASS_NONE] = "none",
1089         [IOPRIO_CLASS_RT] = "realtime",
1090         [IOPRIO_CLASS_BE] = "best-effort",
1091         [IOPRIO_CLASS_IDLE] = "idle"
1092 };
1093
1094 DEFINE_STRING_TABLE_LOOKUP(ioprio_class, int);
1095
1096 static const char *const sigchld_code_table[] = {
1097         [CLD_EXITED] = "exited",
1098         [CLD_KILLED] = "killed",
1099         [CLD_DUMPED] = "dumped",
1100         [CLD_TRAPPED] = "trapped",
1101         [CLD_STOPPED] = "stopped",
1102         [CLD_CONTINUED] = "continued",
1103 };
1104
1105 DEFINE_STRING_TABLE_LOOKUP(sigchld_code, int);
1106
1107 static const char *const log_facility_table[LOG_NFACILITIES] = {
1108         [LOG_FAC(LOG_KERN)] = "kern",
1109         [LOG_FAC(LOG_USER)] = "user",
1110         [LOG_FAC(LOG_MAIL)] = "mail",
1111         [LOG_FAC(LOG_DAEMON)] = "daemon",
1112         [LOG_FAC(LOG_AUTH)] = "auth",
1113         [LOG_FAC(LOG_SYSLOG)] = "syslog",
1114         [LOG_FAC(LOG_LPR)] = "lpr",
1115         [LOG_FAC(LOG_NEWS)] = "news",
1116         [LOG_FAC(LOG_UUCP)] = "uucp",
1117         [LOG_FAC(LOG_CRON)] = "cron",
1118         [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
1119         [LOG_FAC(LOG_FTP)] = "ftp",
1120         [LOG_FAC(LOG_LOCAL0)] = "local0",
1121         [LOG_FAC(LOG_LOCAL1)] = "local1",
1122         [LOG_FAC(LOG_LOCAL2)] = "local2",
1123         [LOG_FAC(LOG_LOCAL3)] = "local3",
1124         [LOG_FAC(LOG_LOCAL4)] = "local4",
1125         [LOG_FAC(LOG_LOCAL5)] = "local5",
1126         [LOG_FAC(LOG_LOCAL6)] = "local6",
1127         [LOG_FAC(LOG_LOCAL7)] = "local7"
1128 };
1129
1130 DEFINE_STRING_TABLE_LOOKUP(log_facility, int);
1131
1132 static const char *const log_level_table[] = {
1133         [LOG_EMERG] = "emerg",
1134         [LOG_ALERT] = "alert",
1135         [LOG_CRIT] = "crit",
1136         [LOG_ERR] = "err",
1137         [LOG_WARNING] = "warning",
1138         [LOG_NOTICE] = "notice",
1139         [LOG_INFO] = "info",
1140         [LOG_DEBUG] = "debug"
1141 };
1142
1143 DEFINE_STRING_TABLE_LOOKUP(log_level, int);
1144
1145 static const char* const sched_policy_table[] = {
1146         [SCHED_OTHER] = "other",
1147         [SCHED_BATCH] = "batch",
1148         [SCHED_IDLE] = "idle",
1149         [SCHED_FIFO] = "fifo",
1150         [SCHED_RR] = "rr"
1151 };
1152
1153 DEFINE_STRING_TABLE_LOOKUP(sched_policy, int);
1154
1155 static const char* const rlimit_table[] = {
1156         [RLIMIT_CPU] = "LimitCPU",
1157         [RLIMIT_FSIZE] = "LimitFSIZE",
1158         [RLIMIT_DATA] = "LimitDATA",
1159         [RLIMIT_STACK] = "LimitSTACK",
1160         [RLIMIT_CORE] = "LimitCORE",
1161         [RLIMIT_RSS] = "LimitRSS",
1162         [RLIMIT_NOFILE] = "LimitNOFILE",
1163         [RLIMIT_AS] = "LimitAS",
1164         [RLIMIT_NPROC] = "LimitNPROC",
1165         [RLIMIT_MEMLOCK] = "LimitMEMLOCK",
1166         [RLIMIT_LOCKS] = "LimitLOCKS",
1167         [RLIMIT_SIGPENDING] = "LimitSIGPENDING",
1168         [RLIMIT_MSGQUEUE] = "LimitMSGQUEUE",
1169         [RLIMIT_NICE] = "LimitNICE",
1170         [RLIMIT_RTPRIO] = "LimitRTPRIO",
1171         [RLIMIT_RTTIME] = "LimitRTTIME"
1172 };
1173
1174 DEFINE_STRING_TABLE_LOOKUP(rlimit, int);