-//usage:#define ubiattach_trivial_usage
-//usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
-//usage:#define ubiattach_full_usage "\n\n"
-//usage: "Attach MTD device to UBI\n"
-//usage: "\n -m MTD_NUM MTD device number to attach"
-//usage: "\n -d UBI_NUM UBI device number to assign"
-//usage:
-//usage:#define ubidetach_trivial_usage
-//usage: "-d UBI_NUM UBI_CTRL_DEV"
-//usage:#define ubidetach_full_usage "\n\n"
-//usage: "Detach MTD device from UBI\n"
-//usage: "\n -d UBI_NUM UBI device number"
-//usage:
-//usage:#define ubimkvol_trivial_usage
-//usage: "UBI_DEVICE -N NAME [-s SIZE | -m]"
-//usage:#define ubimkvol_full_usage "\n\n"
-//usage: "Create UBI volume\n"
-//usage: "\n -a ALIGNMENT Volume alignment (default 1)"
-//usage: "\n -m Set volume size to maximum available"
-//usage: "\n -n VOLID Volume ID, if not specified, it"
-//usage: "\n will be assigned automatically"
-//usage: "\n -N NAME Volume name"
-//usage: "\n -s SIZE Size in bytes"
-//usage: "\n -t TYPE Volume type (static|dynamic)"
-//usage:
-//usage:#define ubirmvol_trivial_usage
-//usage: "UBI_DEVICE -n VOLID"
-//usage:#define ubirmvol_full_usage "\n\n"
-//usage: "Remove UBI volume\n"
-//usage: "\n -n VOLID Volume ID"
-//usage:
-//usage:#define ubirsvol_trivial_usage
-//usage: "UBI_DEVICE -n VOLID -s SIZE"
-//usage:#define ubirsvol_full_usage "\n\n"
-//usage: "Resize UBI volume\n"
-//usage: "\n -n VOLID Volume ID to resize"
-//usage: "\n -s SIZE Size in bytes"
-//usage:
-//usage:#define ubiupdatevol_trivial_usage
-//usage: "UBI_DEVICE [IMG_FILE]"
-//usage:#define ubiupdatevol_full_usage "\n\n"
-//usage: "Update UBI volume\n"
-//usage: "\n -t Truncate UBI volume"
-//usage: "\n -s SIZE Bytes in input (if reading stdin)"
+static unsigned get_num_from_file(const char *path, unsigned max, const char *errmsg)
+{
+ char buf[sizeof(long long)*3];
+ unsigned long long num;
+
+ if (open_read_close(path, buf, sizeof(buf)) < 0)
+ bb_perror_msg_and_die(errmsg, path);
+ /* It can be \n terminated, xatoull won't work well */
+ if (sscanf(buf, "%llu", &num) != 1 || num > max)
+ bb_error_msg_and_die(errmsg, path);
+ return num;
+}