kfan / rpms / util-linux

Forked from rpms/util-linux 3 years ago
Clone
35b2031
--- util-linux-2.12p/floppy-0.12/floppyfloppy.c.generic	2001-02-13 01:15:38.000000000 +0100
35b2031
+++ util-linux-2.12p/floppy-0.12/floppyfloppy.c	2005-09-30 15:38:08.000000000 +0200
35b2031
@@ -264,6 +264,33 @@
35b2031
 #endif
35b2031
 }
35b2031
 
35b2031
+/* -1=error, 1=true, 0=false */
35b2031
+static int check_generic(const char *dev, int n)
35b2031
+{
35b2031
+	struct floppy_struct param;
35b2031
+	int fd;
35b2031
+	
35b2031
+	if ((fd=open(dev, O_RDONLY)) < 0)
35b2031
+	{
35b2031
+		perror(dev);
35b2031
+		return -1;
35b2031
+	}
35b2031
+	if (ioctl(fd,FDGETPRM,(long) &param) < 0)
35b2031
+	{
35b2031
+		perror(dev);
35b2031
+		close(fd);
35b2031
+		return -1;
35b2031
+	}
35b2031
+	close(fd);
35b2031
+	
35b2031
+	if (param.sect==floppy_type[n].sectors && 
35b2031
+	    param.head==floppy_type[n].heads &&
35b2031
+	    param.track==floppy_type[n].tracks)
35b2031
+		/* generic device uses expected format */
35b2031
+		return 1;
35b2031
+
35b2031
+	return 0;
35b2031
+}
35b2031
 
35b2031
 static int do_format(const char *dev, int fmtnum,
35b2031
 		     int (*fmt_func)(const char *, int), int flags)
35b2031
@@ -275,6 +302,7 @@
35b2031
 	struct format_descr curtrack;
35b2031
 	int pct;
35b2031
 	struct stat stat_buf;
35b2031
+	int gen = 0;
35b2031
 
35b2031
 	int i, j;
35b2031
 	char *devname;
35b2031
@@ -297,23 +325,52 @@
35b2031
 
35b2031
 	strcat(strcpy(devname, dev), floppy_type[fmtnum].dev);
35b2031
 
35b2031
+	if (stat(devname, &stat_buf)==-1 && errno==ENOENT)
35b2031
+	{
35b2031
+		/* /dev/fd0xxxxx doesn't exist ...try to use generic device 
35b2031
+		 *
35b2031
+		 * Note: we needn't size specific device if the generic device uses
35b2031
+		 *       right floppy format (FDGETPRM).    -- Karel Zak [30/09/2005]
35b2031
+		 */
35b2031
+		if ((gen = check_generic(dev, fmtnum))==1)	/* true */
35b2031
+		{
35b2031
+			fprintf(stderr, _("WARNING: size specific device %s doesn't exist, using generic device: %s\n"),
35b2031
+					devname, dev);
35b2031
+			strcpy(devname, dev);
35b2031
+		}
35b2031
+		else if (gen==0)	/* false */
35b2031
+		{
35b2031
+			fprintf(stderr, _("ERROR: size specific device %1$s doesn't exist. Use \"MAKEDEV %1$s\" and try it again.\n"), devname);
35b2031
+			return (1);
35b2031
+		}
35b2031
+		else 			/* error -- no floppy medium or device? */
35b2031
+			return(1);
35b2031
+	}
35b2031
 	fd=open(devname, O_WRONLY);
35b2031
 	if (fd < 0)
35b2031
 	{
35b2031
 		perror(devname);
35b2031
 		return (1);
35b2031
 	}
35b2031
-
35b2031
-	if (fstat(fd, &stat_buf) ||
35b2031
-	    !S_ISBLK(stat_buf.st_mode) ||
35b2031
-	    MINOR_DEV(stat_buf.st_rdev) != fmtnum)
35b2031
+	if (fstat(fd, &stat_buf) < 0)
35b2031
+	{
35b2031
+		perror(devname);
35b2031
+		close(fd);
35b2031
+		return (1);
35b2031
+	}
35b2031
+	if (!S_ISBLK(stat_buf.st_mode))
35b2031
+	{
35b2031
+		fprintf(stderr,_("%s: not a block device\n"), devname);
35b2031
+		close(fd);
35b2031
+		return (1);
35b2031
+	}
35b2031
+	if (gen==0 && MINOR_DEV(stat_buf.st_rdev) != fmtnum)
35b2031
 	{
35b2031
 		errno=EINVAL;
35b2031
 		perror(devname);
35b2031
 		close(fd);
35b2031
 		return (1);
35b2031
 	}
35b2031
-
35b2031
 	if (ioctl(fd, FDGETPRM, &geo) < 0)
35b2031
 	{
35b2031
 		perror(devname);