summaryrefslogtreecommitdiffstats
path: root/disk-utils/setfdprm.c
blob: 73b3b7174e680f9afaaab8e1ed1d39eff91781b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* setfdprm.c  -  Sets user-provided floppy disk parameters, re-activates
		  autodetection and switches diagnostic messages. */

/* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
 * - added Native Language Support
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fd.h>
#include "nls.h"

#define FDPRMFILE "/etc/fdprm"
#define MAXLINE   200


static int convert(char *arg)
{
    long result;
    char *end;

    result = strtol(arg,&end,0);
    if (!*end) return (int) result;
    fprintf(stderr,_("Invalid number: %s\n"),arg);
    exit(1);
}


static void cmd_without_param(int cmd,int fd)
{
    if (ioctl(fd,cmd,NULL) >= 0) exit(0);
    perror("ioctl");
    exit(1);
}


static void set_params(int cmd,int fd,char **params)
{
    struct floppy_struct ft;

    ft.size = convert(params[0]);
    ft.sect = convert(params[1]);
    ft.head = convert(params[2]);
    ft.track = convert(params[3]);
    ft.stretch = convert(params[4]);
    ft.gap = convert(params[5]);
    ft.rate = convert(params[6]);
    ft.spec1 = convert(params[7]);
    ft.fmt_gap = convert(params[8]);
    ft.name = NULL;
    if (ioctl(fd,cmd,&ft) >= 0) exit(0);
    perror("ioctl");
    exit(1);
}


static void find_params(int cmd,int fd,char *name)
{
    FILE *file;
    char line[MAXLINE+2],this[MAXLINE+2],param[9][MAXLINE+2];
    char *params[9],*start;
    int count;

    if ((file = fopen(FDPRMFILE,"r")) == NULL) {
	perror(FDPRMFILE);
	exit(1);
    }
    while (fgets(line,MAXLINE,file)) {
	for (start = line; *start == ' ' || *start == '\t'; start++);
	if (*start && *start != '\n' && *start != '#') {
	    if (sscanf(start,"%s %s %s %s %s %s %s %s %s %s",this,param[0],
	      param[1],param[2],param[3],param[4],param[5],param[6],param[7],
	      param[8]) != 10) {
		fprintf(stderr,_("Syntax error: '%s'\n"),line);
		exit(1);
	    }
	    if (!strcmp(this,name)) {
		for (count = 0; count < 9; count++)
		    params[count] = param[count];
		set_params(cmd,fd,params);
	    }
	}
    }
    fprintf(stderr,_("No such parameter set: '%s'\n"),name);
    exit(1);
}


static void usage(char *name)
{
    char *this;

    if ((this = strrchr(name,'/')) != NULL) name = this+1;
    fprintf(stderr,_("usage: %s [ -p ] dev name\n"),name);
    fprintf(stderr,_("       %s [ -p ] dev size sect heads tracks stretch \
gap rate spec1 fmt_gap\n"),name);
#ifdef FDMEDCNG
    fprintf(stderr,_("       %s [ -c | -y | -n | -d ] dev\n"),name);
#else
    fprintf(stderr,_("       %s [ -c | -y | -n ] dev\n"),name);
#endif
    exit(1);
}

int
main(int argc, char **argv)
{
    int fd;
    unsigned int cmd;
    char *progname, *p;

    progname = argv[0];
    if ((p = strrchr(progname, '/')) != NULL)
	    progname = p+1;

    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);

    if (argc == 2 &&
	(!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
	    printf(_("%s from %s\n"), progname, util_linux_version);
	    exit(0);
    }

    if (argc < 3) usage(progname);
    cmd = FDSETPRM;
    if (*argv[1] == '-') {
	switch (argv[1][1]) {
	    case 'c':
		cmd = FDCLRPRM;
		break;
	    case 'p':
		cmd = FDDEFPRM;
		break;
	    case 'y':
		cmd = FDMSGON;
		break;
	    case 'n':
		cmd = FDMSGOFF;
		break;
#ifdef FDMEDCNG
	    case 'd':
		cmd = FDMEDCNG;
		break;
#endif
	    default:
		usage(progname);
	}
	argc--;
	argv++;
    }
    if ((fd = open(argv[1],3)) < 0) { /* 3 == no access at all */
	perror(argv[1]);
	exit(1);
    }
    if (cmd != FDSETPRM && cmd != FDDEFPRM) {
	if (argc != 2) usage(progname);
	cmd_without_param(cmd,fd);
    }
    if (argc != 11 && argc != 3)
	usage(progname);
    else if (argc == 11)
	set_params(cmd,fd,&argv[2]);
    else
	find_params(cmd,fd,argv[2]);
    /* not reached */
    return 0;
}