sec = (time_t) (t / USEC_PER_SEC);
- if (strftime(buf, l, "%a, %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) <= 0)
+ if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) <= 0)
return NULL;
return buf;
return ret;
}
+static int is_temporary_fs(struct statfs *s) {
+ assert(s);
+ return s->f_type == TMPFS_MAGIC ||
+ (long)s->f_type == (long)RAMFS_MAGIC;
+}
+
int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
struct statfs s;
/* We refuse to clean disk file systems with this call. This
* is extra paranoia just to be sure we never ever remove
* non-state data */
-
- if (s.f_type != TMPFS_MAGIC &&
- s.f_type != RAMFS_MAGIC) {
+ if (!is_temporary_fs(&s)) {
log_error("Attempted to remove disk file system, and we can't allow that.");
close_nointr_nofail(fd);
return -EPERM;
if (statfs(path, &s) < 0)
return -errno;
- if (s.f_type != TMPFS_MAGIC &&
- s.f_type != RAMFS_MAGIC) {
+ if (!is_temporary_fs(&s)) {
log_error("Attempted to remove disk file system, and we can't allow that.");
return -EPERM;
}
return -errno;
}
- if (s.f_type != TMPFS_MAGIC &&
- s.f_type != RAMFS_MAGIC) {
+ if (!is_temporary_fs(&s)) {
log_error("Attempted to remove disk file system, and we can't allow that.");
close_nointr_nofail(fd);
return -EPERM;
unsigned columns(void) {
const char *e;
- unsigned c;
+ int c;
if (_likely_(cached_columns > 0))
return cached_columns;
c = 0;
e = getenv("COLUMNS");
if (e)
- safe_atou(e, &c);
+ safe_atoi(e, &c);
if (c <= 0)
c = fd_columns(STDOUT_FILENO);
saved = access("/etc/initrd-release", F_OK) >= 0 &&
statfs("/", &s) >= 0 &&
- (s.f_type == TMPFS_MAGIC || s.f_type == RAMFS_MAGIC);
+ is_temporary_fs(&s);
return saved;
}
const char *draw_special_char(DrawSpecialChar ch) {
static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = {
/* UTF-8 */ {
- [DRAW_BOX_VERT] = "\342\224\202", /* │ */
- [DRAW_BOX_VERT_AND_RIGHT] = "\342\224\234", /* ├ */
- [DRAW_BOX_UP_AND_RIGHT] = "\342\224\224", /* └ */
- [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
+ [DRAW_TREE_VERT] = "\342\224\202 ", /* │ */
+ [DRAW_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
+ [DRAW_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
+ [DRAW_TRIANGULAR_BULLET] = "\342\200\243 ", /* ‣ */
},
/* ASCII fallback */ {
- [DRAW_BOX_VERT] = "|",
- [DRAW_BOX_VERT_AND_RIGHT] = "+",
- [DRAW_BOX_UP_AND_RIGHT] = "\\",
- [DRAW_TRIANGULAR_BULLET] = ">",
+ [DRAW_TREE_VERT] = "| ",
+ [DRAW_TREE_BRANCH] = "|-",
+ [DRAW_TREE_RIGHT] = "`-",
+ [DRAW_TRIANGULAR_BULLET] = "> ",
}
};
return draw_table[!is_locale_utf8()][ch];
}
+
+char *strreplace(const char *text, const char *old_string, const char *new_string) {
+ const char *f;
+ char *t, *r;
+ size_t l, old_len, new_len;
+
+ assert(text);
+ assert(old_string);
+ assert(new_string);
+
+ old_len = strlen(old_string);
+ new_len = strlen(new_string);
+
+ l = strlen(text);
+ r = new(char, l+1);
+ if (!r)
+ return NULL;
+
+ f = text;
+ t = r;
+ while (*f) {
+ char *a;
+ size_t d, nl;
+
+ if (!startswith(f, old_string)) {
+ *(t++) = *(f++);
+ continue;
+ }
+
+ d = t - r;
+ nl = l - old_len + new_len;
+ a = realloc(r, nl + 1);
+ if (!a)
+ goto oom;
+
+ l = nl;
+ r = a;
+ t = r + d;
+
+ t = stpcpy(t, new_string);
+ f += old_len;
+ }
+
+ *t = 0;
+ return r;
+
+oom:
+ free(r);
+ return NULL;
+}