]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/setfdprm.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / disk-utils / setfdprm.c
1 /* setfdprm.c - Sets user-provided floppy disk parameters, re-activates
2 autodetection and switches diagnostic messages. */
3
4 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
5 * - added Native Language Support
6 */
7
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/ioctl.h>
16 #include <linux/fd.h>
17 #include "nls.h"
18
19 #define FDPRMFILE "/etc/fdprm"
20 #define MAXLINE 200
21
22
23 static int
24 convert(char *arg) {
25 long result;
26 char *end;
27
28 result = strtol(arg,&end,0);
29 if (!*end)
30 return (int) result;
31 fprintf(stderr,_("Invalid number: %s\n"),arg);
32 exit(1);
33 }
34
35 static void
36 cmd_without_param(int cmd,int fd) {
37 if (ioctl(fd,cmd,NULL) >= 0)
38 exit(0);
39 perror("ioctl");
40 exit(1);
41 }
42
43 /* set given fd parameters */
44 static void
45 set_params(int cmd,int fd,char **params) {
46 struct floppy_struct ft;
47
48 ft.size = convert(params[0]);
49 ft.sect = convert(params[1]);
50 ft.head = convert(params[2]);
51 ft.track = convert(params[3]);
52 ft.stretch = convert(params[4]);
53 ft.gap = convert(params[5]);
54 ft.rate = convert(params[6]);
55 ft.spec1 = convert(params[7]);
56 ft.fmt_gap = convert(params[8]);
57 ft.name = NULL;
58 if (ioctl(fd,cmd,&ft) >= 0) exit(0);
59 perror("ioctl");
60 exit(1);
61 }
62
63 /* find parameter set in file, and use it */
64 static void
65 find_params(int cmd,int fd,char *name) {
66 FILE *file;
67 char line[MAXLINE+2],this[MAXLINE+2],param[9][MAXLINE+2];
68 char *params[9],*start;
69 int count;
70
71 if ((file = fopen(FDPRMFILE,"r")) == NULL) {
72 perror(FDPRMFILE);
73 exit(1);
74 }
75 while (fgets(line,MAXLINE,file)) {
76 for (start = line; *start == ' ' || *start == '\t'; start++);
77 if (*start && *start != '\n' && *start != '#') {
78 if (sscanf(start,"%s %s %s %s %s %s %s %s %s %s",this,param[0],
79 param[1],param[2],param[3],param[4],param[5],param[6],param[7],
80 param[8]) != 10) {
81 fprintf(stderr,_("Syntax error: '%s'\n"),line);
82 exit(1);
83 }
84 if (!strcmp(this,name)) {
85 for (count = 0; count < 9; count++)
86 params[count] = param[count];
87 set_params(cmd,fd,params);
88 }
89 }
90 }
91 fprintf(stderr,_("No such parameter set: '%s'\n"),name);
92 exit(1);
93 }
94
95 static void
96 usage(char *name) {
97 char *this;
98
99 if ((this = strrchr(name,'/')) != NULL) name = this+1;
100 fprintf(stderr,_("usage: %s [ -p ] dev name\n"),name);
101 fprintf(stderr,_(" %s [ -p ] dev size sect heads tracks stretch \
102 gap rate spec1 fmt_gap\n"),name);
103 #ifdef FDMEDCNG
104 fprintf(stderr,_(" %s [ -c | -y | -n | -d ] dev\n"),name);
105 #else
106 fprintf(stderr,_(" %s [ -c | -y | -n ] dev\n"),name);
107 #endif
108 exit(1);
109 }
110
111 int
112 main(int argc, char **argv) {
113 int fd;
114 unsigned int cmd;
115 char *progname, *p;
116
117 progname = argv[0];
118 if ((p = strrchr(progname, '/')) != NULL)
119 progname = p+1;
120
121 setlocale(LC_ALL, "");
122 bindtextdomain(PACKAGE, LOCALEDIR);
123 textdomain(PACKAGE);
124
125 if (argc == 2 &&
126 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
127 printf(_("%s from %s\n"), progname, util_linux_version);
128 exit(0);
129 }
130
131 if (argc < 2)
132 usage(progname);
133 cmd = FDSETPRM;
134 if (*argv[1] == '-') {
135 switch (argv[1][1]) {
136 case 'c':
137 cmd = FDCLRPRM;
138 break;
139 case 'p':
140 cmd = FDDEFPRM;
141 break;
142 case 'y':
143 cmd = FDMSGON;
144 break;
145 case 'n':
146 cmd = FDMSGOFF;
147 break;
148 #ifdef FDMEDCNG
149 case 'd':
150 cmd = FDMEDCNG;
151 break;
152 #endif
153 default:
154 usage(progname);
155 }
156 argc--;
157 argv++;
158 }
159 if ((fd = open(argv[1],3)) < 0) { /* O_WRONLY needed in a few kernels */
160 perror(argv[1]);
161 exit(1);
162 }
163 if (cmd != FDSETPRM && cmd != FDDEFPRM) {
164 if (argc != 2) usage(progname);
165 cmd_without_param(cmd,fd);
166 }
167 if (argc != 11 && argc != 3)
168 usage(progname);
169 else if (argc == 11)
170 set_params(cmd,fd,&argv[2]);
171 else
172 find_params(cmd,fd,argv[2]);
173 /* not reached */
174 return 0;
175 }