Hi misc@,
while trying to use indent(1) on my lsblk.c, I get the following two
errors:> Error@150: Stmt nesting error.
Error@702: Missing braces at end of file.
It also doesn't handle unicode and breaks horribly at some points.
I have attached both the origin and the indented version
as Thunderbird doesn't seem to wanna deal with so much text.
For now, I'm just gonna do it myself according to style(9).
Thanks,
Benjamin Stürz
/*
* Copyright (c) 2023 Benjamin StГјrz
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define _XOPEN_SOURCE 700
#define _BSD_SOURCE 1
#define DKTYPENAMES
#define WSDEBUG 0
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int
diskcount(void)
{
const int mib [2] = {CTL_HW, HW_DISKCOUNT};
int diskcount;
size_t len = sizeof diskcount;
if (sysctl(mib, 2, &diskcount, &len, NULL, 0) == -1)
err(1, "sysctl(hw.diskcount)");
return diskcount;
}
static char*
disknames(void)
{
const int num = diskcount();
const int mib [2] = {CTL_HW, HW_DISKNAMES};
size_t len = 32 * num;
char *buffer = malloc(len);
if (sysctl(mib, 2, buffer, &len, NULL, 0) == -1)
err(1, "sysctl(hw.disknames)");
return buffer;
}
static char*
stripdisk(char *n)
{
const char sufx[] = " disk";
const size_t ln = strnlen(n, 16);
const size_t ls = sizeof sufx - 1;
if (memcmp(n + ln - ls, sufx, ls) == 0) {
n[ln - ls] = '\0';
}
return n;
}
static void
print_size(uint64_t sz)
{
const struct unit {
char sym;
uint64_t factor;
} units [] = {
{
'P', 1ull << 50
},
{
'T', 1ull << 40
},
{
'G', 1ull << 30
},
{
'M', 1ull << 20
},
{
'K', 1ull << 10
},
{
'0', 0
},
};
char sym = 'B';
uint64_t factor = 1;
for (const struct unit * u = &units[0]; u->factor; ++u) {
if (sz >= (u->factor * 9 / 10)) {
sym = u->sym;
factor = u->factor;
break;
}
}
const unsigned scaled10 = sz * 10 / factor;
const unsigned scaled = sz / factor;
if (scaled10 >= 1000) {
printf("%u", scaled);
} else if (scaled10 >= 100) {
printf(" %u", scaled);
} else {
printf("%u.%u", scaled, scaled10 % 10);
}
putchar(sym);
putchar(' ');
}
enum {
FIELD_NAME = 0x01,
FIELD_DUID = 0x02,
FIELD_SIZE = 0x04,
FIELD_USED = 0x08,
FIELD_FREE = 0x10,
FIELD_TYPE = 0x20,
FIELD_COMMENT = 0x40,
FIELD_DEFAULT = FIELD_NAME | FIELD_SIZE | FIELD_TYPE | FIELD_COMMENT,
};
enum {
OPT_NOHEADER = 0x01,
OPT_NOUNICODE = 0x02,
OPT_NOBIO = 0x04,
};
struct my_diskinfo;
struct my_partinfo {
char letter;
uint64_t size;
uint64_t fssize;
uint64_t free;
const char *fstype;
const char *mount;
const struct my_diskinfo *sub;
//If this is part of a RAID
const char *raidstatus;
//Only available if (sub != NULL)
};
struct my_diskinfo {
char type [16];
char label[16];
char name [8];
uint64_t size;
uint64_t used;
u_char duid [8];
uint8_t num_parts;
struct my_partinfo parts[MAXPARTITIONS];
const char *raidstatus;
//If this is a RAID device
};
struct padding {
int name;
/* duid = 8 */
/* size = 4 */
/* used = 4 */
/* free = 4 */
int type;
int comment;
};
static void print_header(int fields, const struct padding * p) {
if (fields & FIELD_NAME)
printf("%-*s ", p->name, "NAME");
if (fields & FIELD_DUID)
printf("%-18s ", "DUID");
if (fields & FIELD_SIZE)
printf("%-4s ", "SIZE");
if (fields & FIELD_USED)
printf("%-4s ", "USED");
if (fields & FIELD_FREE)
printf("%-4s ", "FREE");
if (fields & FIELD_TYPE)
printf("%-*s ", p->type, "TYPE");
if (fields & FIELD_COMMENT)
printf("%-*s ", p->comment, "COMMENT");
#if WSDEBUG
putchar('X');
#endif
putchar('\n');
}
static void print_duid(const u_char * duid) {
for (size_t i = 0; i < 8; ++i) {
printf("%02x", duid[i]);
}
}
static void print_disk(const struct my_diskinfo *, int, int, const char *, const struct padding *);
static void print_part(
const struct my_diskinfo * disk,
const struct my_partinfo * part,
int fields ,
int