~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to disk-utils/fdformat.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* fdformat.c  -  Low-level formats a floppy disk - Werner Almesberger */
2
 
 
3
 
/* 1999-02-22 Arkadiusz Miļæ½kiewicz <misiek@pld.ORG.PL>
4
 
 * - added Native Language Support
5
 
 * 1999-03-20 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6
 
 & - more i18n/nls translatable strings marked
 
1
/*
 
2
 * fdformat.c  -  Low-level formats a floppy disk - Werner Almesberger
7
3
 */
8
 
 
 
4
#include <errno.h>
 
5
#include <fcntl.h>
 
6
#include <getopt.h>
 
7
#include <linux/fd.h>
9
8
#include <stdio.h>
10
 
#include <string.h>
11
 
#include <fcntl.h>
12
 
#include <errno.h>
13
 
#include <unistd.h>
14
9
#include <stdlib.h>
 
10
#include <sys/ioctl.h>
15
11
#include <sys/stat.h>
16
 
#include <sys/ioctl.h>
17
 
#include <linux/fd.h>
 
12
#include <unistd.h>
18
13
 
 
14
#include "c.h"
19
15
#include "nls.h"
 
16
#include "xalloc.h"
20
17
 
21
18
struct floppy_struct param;
22
19
 
23
20
#define SECTOR_SIZE 512
24
 
#define PERROR(msg) { perror(msg); exit(1); }
25
21
 
26
 
static void format_disk(int ctrl, char *name)
 
22
static void format_disk(int ctrl)
27
23
{
28
 
    struct format_descr descr;
29
 
    int track;
30
 
 
31
 
    printf(_("Formatting ... "));
32
 
    fflush(stdout);
33
 
    if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)");
34
 
    for (track = 0; track < param.track; track++) {
35
 
        descr.track = track;
36
 
        descr.head = 0;
37
 
        if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
38
 
          PERROR("\nioctl(FDFMTTRK)");
39
 
 
40
 
        printf("%3d\b\b\b",track);
 
24
        struct format_descr descr;
 
25
        unsigned int track;
 
26
 
 
27
        printf(_("Formatting ... "));
41
28
        fflush(stdout);
42
 
        if (param.head == 2) {
43
 
            descr.head = 1;
44
 
            if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
45
 
              PERROR("\nioctl(FDFMTTRK)");
 
29
        if (ioctl(ctrl, FDFMTBEG, NULL) < 0)
 
30
                err(EXIT_FAILURE, "ioctl: FDFMTBEG");
 
31
        for (track = 0; track < param.track; track++) {
 
32
                descr.track = track;
 
33
                descr.head = 0;
 
34
                if (ioctl(ctrl, FDFMTTRK, (long) &descr) < 0)
 
35
                        err(EXIT_FAILURE, "ioctl: FDFMTTRK");
 
36
 
 
37
                printf("%3d\b\b\b", track);
 
38
                fflush(stdout);
 
39
                if (param.head == 2) {
 
40
                        descr.head = 1;
 
41
                        if (ioctl(ctrl, FDFMTTRK, (long)&descr) < 0)
 
42
                                err(EXIT_FAILURE, "ioctl: FDFMTTRK");
 
43
                }
46
44
        }
47
 
    }
48
 
    if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)");
49
 
    printf(_("done\n"));
 
45
        if (ioctl(ctrl, FDFMTEND, NULL) < 0)
 
46
                err(EXIT_FAILURE, "ioctl: FDFMTEND");
 
47
        printf(_("done\n"));
50
48
}
51
49
 
52
 
 
53
50
static void verify_disk(char *name)
54
51
{
55
 
    unsigned char *data;
56
 
    int fd,cyl_size,cyl,count;
57
 
 
58
 
    cyl_size = param.sect*param.head*512;
59
 
    if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc");
60
 
    printf(_("Verifying ... "));
61
 
    fflush(stdout);
62
 
    if ((fd = open(name,O_RDONLY)) < 0) PERROR(name);
63
 
    for (cyl = 0; cyl < param.track; cyl++) {
64
 
        int read_bytes;
65
 
 
66
 
        printf("%3d\b\b\b",cyl);
 
52
        unsigned char *data;
 
53
        unsigned int cyl;
 
54
        int fd, cyl_size, count;
 
55
 
 
56
        cyl_size = param.sect * param.head * 512;
 
57
        data = xmalloc(cyl_size);
 
58
        printf(_("Verifying ... "));
67
59
        fflush(stdout);
68
 
        read_bytes = read(fd,data,cyl_size);
69
 
        if(read_bytes != cyl_size) {
70
 
            if(read_bytes < 0)
71
 
                    perror(_("Read: "));
72
 
            fprintf(stderr,
73
 
                    _("Problem reading cylinder %d, expected %d, read %d\n"),
74
 
                    cyl, cyl_size, read_bytes);
75
 
            free(data);
76
 
            exit(1);
 
60
        if ((fd = open(name, O_RDONLY)) < 0)
 
61
                err(EXIT_FAILURE, _("cannot open file %s"), name);
 
62
        for (cyl = 0; cyl < param.track; cyl++) {
 
63
                int read_bytes;
 
64
 
 
65
                printf("%3d\b\b\b", cyl);
 
66
                fflush(stdout);
 
67
                read_bytes = read(fd, data, cyl_size);
 
68
                if (read_bytes != cyl_size) {
 
69
                        if (read_bytes < 0)
 
70
                                perror(_("Read: "));
 
71
                        fprintf(stderr,
 
72
                                _("Problem reading cylinder %d,"
 
73
                                  " expected %d, read %d\n"),
 
74
                                cyl, cyl_size, read_bytes);
 
75
                        free(data);
 
76
                        exit(EXIT_FAILURE);
 
77
                }
 
78
                for (count = 0; count < cyl_size; count++)
 
79
                        if (data[count] != FD_FILL_BYTE) {
 
80
                                printf(_("bad data in cyl %d\n"
 
81
                                         "Continuing ... "), cyl);
 
82
                                fflush(stdout);
 
83
                                break;
 
84
                        }
77
85
        }
78
 
        for (count = 0; count < cyl_size; count++)
79
 
            if (data[count] != FD_FILL_BYTE) {
80
 
                printf(_("bad data in cyl %d\nContinuing ... "),cyl);
81
 
                fflush(stdout);
82
 
                break;
83
 
            }
84
 
    }
85
 
    free(data);
86
 
    printf(_("done\n"));
87
 
    if (close(fd) < 0) PERROR("close");
88
 
}
89
 
 
90
 
 
91
 
static void usage(char *name)
92
 
{
93
 
    char *this;
94
 
 
95
 
    if ((this = strrchr(name,'/')) != NULL) name = this+1;
96
 
    fprintf(stderr,_("usage: %s [ -n ] device\n"),name);
97
 
    exit(1);
98
 
}
99
 
 
100
 
 
101
 
int main(int argc,char **argv)
102
 
{
103
 
    int ctrl;
104
 
    int verify;
105
 
    struct stat st;
106
 
    char *progname, *p;
107
 
 
108
 
    progname = argv[0];
109
 
    if ((p = strrchr(progname, '/')) != NULL)
110
 
            progname = p+1;
111
 
 
112
 
    setlocale(LC_ALL, "");
113
 
    bindtextdomain(PACKAGE, LOCALEDIR);
114
 
    textdomain(PACKAGE);
115
 
 
116
 
    if (argc == 2 &&
117
 
        (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
118
 
            printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
119
 
            exit(0);
120
 
    }
121
 
 
122
 
    verify = 1;
123
 
    if (argc > 1 && argv[1][0] == '-') {
124
 
        if (argv[1][1] != 'n') usage(progname);
125
 
        verify = 0;
126
 
        argc--;
127
 
        argv++;
128
 
    }
129
 
    if (argc != 2) usage(progname);
130
 
    if (stat(argv[1],&st) < 0) PERROR(argv[1]);
131
 
    if (!S_ISBLK(st.st_mode)) {
132
 
        fprintf(stderr,_("%s: not a block device\n"),argv[1]);
133
 
        exit(1);
134
 
        /* do not test major - perhaps this was an USB floppy */
135
 
    }
136
 
    if (access(argv[1],W_OK) < 0) PERROR(argv[1]);
137
 
 
138
 
    ctrl = open(argv[1],O_WRONLY);
139
 
    if (ctrl < 0)
140
 
            PERROR(argv[1]);
141
 
    if (ioctl(ctrl,FDGETPRM,(long) &param) < 0) 
142
 
            PERROR(_("Could not determine current format type"));
143
 
    printf(_("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n"),
144
 
           (param.head == 2) ? _("Double") : _("Single"),
145
 
           param.track, param.sect,param.size >> 1);
146
 
    format_disk(ctrl, argv[1]);
147
 
    close(ctrl);
148
 
 
149
 
    if (verify)
150
 
            verify_disk(argv[1]);
151
 
    return 0;
 
86
        free(data);
 
87
        printf(_("done\n"));
 
88
        if (close(fd) < 0)
 
89
                err(EXIT_FAILURE, "close");
 
90
}
 
91
 
 
92
static void __attribute__ ((__noreturn__)) usage(FILE * out)
 
93
{
 
94
        fprintf(out, _("Usage: %s [options] device\n"),
 
95
                program_invocation_short_name);
 
96
 
 
97
        fprintf(out, _("\nOptions:\n"
 
98
                       " -n, --no-verify  disable the verification after the format\n"
 
99
                       " -V, --version    output version information and exit\n"
 
100
                       " -h, --help       display this help and exit\n\n"));
 
101
 
 
102
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 
103
}
 
104
 
 
105
int main(int argc, char **argv)
 
106
{
 
107
        int ch;
 
108
        int ctrl;
 
109
        int verify = 1;
 
110
        struct stat st;
 
111
 
 
112
        static const struct option longopts[] = {
 
113
                {"no-verify", no_argument, NULL, 'n'},
 
114
                {"version", no_argument, NULL, 'V'},
 
115
                {"help", no_argument, NULL, 'h'},
 
116
                {NULL, 0, NULL, 0}
 
117
        };
 
118
 
 
119
        setlocale(LC_ALL, "");
 
120
        bindtextdomain(PACKAGE, LOCALEDIR);
 
121
        textdomain(PACKAGE);
 
122
 
 
123
        while ((ch = getopt_long(argc, argv, "nVh", longopts, NULL)) != -1)
 
124
                switch (ch) {
 
125
                case 'n':
 
126
                        verify = 0;
 
127
                        break;
 
128
                case 'V':
 
129
                        printf(_("%s from %s\n"), program_invocation_short_name,
 
130
                               PACKAGE_STRING);
 
131
                        exit(EXIT_SUCCESS);
 
132
                case 'h':
 
133
                        usage(stdout);
 
134
                default:
 
135
                        usage(stderr);
 
136
                }
 
137
 
 
138
        argc -= optind;
 
139
        argv += optind;
 
140
 
 
141
        if (argc < 1)
 
142
                usage(stderr);
 
143
        if (stat(argv[0], &st) < 0)
 
144
                err(EXIT_FAILURE, _("cannot stat file %s"), argv[0]);
 
145
        if (!S_ISBLK(st.st_mode))
 
146
                /* do not test major - perhaps this was an USB floppy */
 
147
                errx(EXIT_FAILURE, _("%s: not a block device"), argv[0]);
 
148
        if (access(argv[0], W_OK) < 0)
 
149
                err(EXIT_FAILURE, _("cannot access file %s"), argv[0]);
 
150
 
 
151
        ctrl = open(argv[0], O_WRONLY);
 
152
        if (ctrl < 0)
 
153
                err(EXIT_FAILURE, _("cannot open file %s"), argv[0]);
 
154
        if (ioctl(ctrl, FDGETPRM, (long)&param) < 0)
 
155
                err(EXIT_FAILURE, _("Could not determine current format type"));
 
156
 
 
157
        printf(_("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n"),
 
158
               (param.head == 2) ? _("Double") : _("Single"),
 
159
               param.track, param.sect, param.size >> 1);
 
160
        format_disk(ctrl);
 
161
        close(ctrl);
 
162
 
 
163
        if (verify)
 
164
                verify_disk(argv[0]);
 
165
        return EXIT_SUCCESS;
152
166
}