fslabel: add new tool
authorAlan Cox <alan@linux.intel.com>
Fri, 28 Sep 2018 20:17:11 +0000 (21:17 +0100)
committerAlan Cox <alan@linux.intel.com>
Fri, 28 Sep 2018 20:17:11 +0000 (21:17 +0100)
Applications/util/Makefile.6502
Applications/util/Makefile.68000
Applications/util/Makefile.6809
Applications/util/Makefile.pdp11
Applications/util/Makefile.z80
Applications/util/labelfs.c [new file with mode: 0644]

index 51e53ed..8211610 100644 (file)
@@ -74,6 +74,7 @@ SRCS  = banner.c \
        grep.c \
        id.c \
        kbdrate.c \
+       labelfs.c \
        ll.c \
        ls.c \
        man.c \
index b686837..43e64e7 100644 (file)
@@ -81,6 +81,7 @@ SRCS  = \
        grep.c \
        id.c \
        kbdrate.c \
+       labelfs.c \
        ll.c \
        ls.c \
        man.c \
index 35b7a4f..092d509 100644 (file)
@@ -82,6 +82,7 @@ SRCS  = \
        grep.c \
        id.c \
        kbdrate.c \
+       labelfs.c \
        ll.c \
        ls.c \
        man.c \
index c906761..20f74e7 100644 (file)
@@ -79,6 +79,7 @@ SRCS  = \
        grep.c \
        id.c \
        kbdrate.c \
+       labelfs.c \
        ll.c \
        ls.c \
        man.c \
index b2cbcad..cd89a87 100644 (file)
@@ -72,6 +72,7 @@ SRCS  = banner.c \
        id.c \
        jstest.c \
        kbdrate.c \
+       labelfs.c \
        ll.c \
        ls.c \
        man.c \
diff --git a/Applications/util/labelfs.c b/Applications/util/labelfs.c
new file mode 100644 (file)
index 0000000..30d2cd9
--- /dev/null
@@ -0,0 +1,123 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/super.h>
+
+static union {
+    struct fuzix_filesys fs;
+    char buf[512];
+} fs;
+
+
+void usage(void)
+{
+    fprintf(stderr, "Usage: labelfs [-l label] [-g heads,cyls,secs[,blocksize[,skew]] path,\n");
+    exit(1);
+}
+
+void set_fs_label(const char *p)
+{
+    strncmp(fs.fs.s_label_name, p, sizeof(fs.fs.s_label_name));
+    fs.fs.s_props |= S_PROP_LABEL;
+}
+
+void set_fs_geo(const char *p)
+{
+    unsigned int heads;
+    unsigned int cyls;
+    unsigned int sectors;
+    unsigned int bsize;
+    unsigned int skew;
+    int n = sscanf(p, "%d,%d,%d,%d,%d", &heads,&cyls,&sectors,&bsize,&skew);
+    if (n < 3)
+        usage();
+    if (bsize & (bsize - 1))
+        usage();
+    fs.fs.s_geo_heads = heads;
+    fs.fs.s_geo_cylinders = cyls;
+    fs.fs.s_geo_sectors = sectors;
+    if (n > 3)
+        fs.fs.s_geo_secsize = bsize;
+    else
+        fs.fs.s_geo_secsize = 512;
+    if (n == 5)
+        fs.fs.s_geo_skew = skew;
+    else
+        fs.fs.s_geo_skew = 0;
+    fs.fs.s_props |= S_PROP_GEO;
+}
+
+int main(int argc, char *argv[])
+{
+    char *geo = NULL;
+    char *label = NULL;
+    int opt;
+    int fd;
+
+    while((opt = getopt(argc, argv, "l:g:")) != -1) {
+        switch(opt) {
+        case 'g':
+            geo = optarg;
+            break;
+        case 'l':
+            label = optarg;
+            break;
+        default:
+            usage();
+        }
+    }
+    if (optind != argc -1)
+        usage();
+    fd = open(argv[optind], O_RDWR, 0600);
+    if (fd == -1) {
+        perror(argv[optind]);
+        exit(1);
+    }
+    if (lseek(fd, 512L , SEEK_SET) == -1) {
+        perror("lseek");
+        exit(1);
+    }
+    if (read(fd, fs.buf, 512) != 512) {
+        perror("read");
+        exit(1);
+    }
+    if (fs.fs.s_fs.s_mounted != SMOUNTED) {
+        fprintf(stderr, "%s: not a filesystem.\n", argv[optind]);
+        exit(1);
+    }
+    /* Don't scribble on a mounted fs it won't end well in all cases */
+    if (fs.fs.s_fs.s_fmod == FMOD_DIRTY) {
+        fprintf(stderr, "%s: dirty filesystem.\n", argv[optind]);
+        if (geo || label)
+            exit(1);
+    }
+    if (!geo && !label) {
+        if (fs.fs.s_props & S_PROP_LABEL)
+            printf("Label: %.32s.\n", fs.fs.s_label_name);
+        if (fs.fs.s_props & S_PROP_GEO)
+            printf("Geometry: %d heads, %d cylinders\n",
+                fs.fs.s_geo_heads, fs.fs.s_geo_cylinders);
+            printf("          %d sectors, blocksize %$d skew %d.\n",
+                fs.fs.s_geo_sectors, fs.fs.s_geo_secsize, fs.fs.s_geo_skew);
+        return 0;
+    }
+    if (lseek(fd, 512L , SEEK_SET) == -1) {
+        perror("lseek");
+        exit(1);
+    }
+    if (label)
+        set_fs_label(label);
+    if (geo)
+        set_fs_geo(geo);
+    if (write(fd, fs.buf, 512) != 512) {
+        perror("write");
+        exit(1);
+    }
+    if (close(fd) == -1) {
+        perror("close");
+        exit(1);
+    }
+    return 0;
+}