home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / disk-utils / setfdprm.c < prev   
Encoding:
C/C++ Source or Header  |  1995-02-22  |  3.2 KB  |  150 lines

  1. /* setfdprm.c  -  Sets user-provided floppy disk parameters, re-activates
  2.           autodetection and switches diagnostic messages. */
  3.  
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <linux/fd.h>
  11.  
  12. #define FDPRMFILE "/etc/fdprm"
  13. #define MAXLINE   200
  14.  
  15.  
  16. static int convert(char *arg)
  17. {
  18.     long result;
  19.     char *end;
  20.  
  21.     result = strtol(arg,&end,0);
  22.     if (!*end) return (int) result;
  23.     fprintf(stderr,"Invalid number: %s\n",arg);
  24.     exit(1);
  25. }
  26.  
  27.  
  28. static void cmd_without_param(int cmd,int fd)
  29. {
  30.     if (ioctl(fd,cmd,NULL) >= 0) exit(0);
  31.     perror("ioctl");
  32.     exit(1);
  33. }
  34.  
  35.  
  36. static void set_params(int cmd,int fd,char **params)
  37. {
  38.     struct floppy_struct ft;
  39.  
  40.     ft.size = convert(params[0]);
  41.     ft.sect = convert(params[1]);
  42.     ft.head = convert(params[2]);
  43.     ft.track = convert(params[3]);
  44.     ft.stretch = convert(params[4]);
  45.     ft.gap = convert(params[5]);
  46.     ft.rate = convert(params[6]);
  47.     ft.spec1 = convert(params[7]);
  48.     ft.fmt_gap = convert(params[8]);
  49.     ft.name = NULL;
  50.     if (ioctl(fd,cmd,&ft) >= 0) exit(0);
  51.     perror("ioctl");
  52.     exit(1);
  53. }
  54.  
  55.  
  56. static void find_params(int cmd,int fd,char *name)
  57. {
  58.     FILE *file;
  59.     char line[MAXLINE+2],this[MAXLINE+2],param[9][MAXLINE+2];
  60.     char *params[9],*start;
  61.     int count;
  62.  
  63.     if ((file = fopen(FDPRMFILE,"r")) == NULL) {
  64.     perror(FDPRMFILE);
  65.     exit(1);
  66.     }
  67.     while (fgets(line,MAXLINE,file)) {
  68.     for (start = line; *start == ' ' || *start == '\t'; start++);
  69.     if (*start && *start != '\n' && *start != '#') {
  70.         if (sscanf(start,"%s %s %s %s %s %s %s %s %s %s",this,param[0],
  71.           param[1],param[2],param[3],param[4],param[5],param[6],param[7],
  72.           param[8]) != 10) {
  73.         fprintf(stderr,"Syntax error: '%s'\n",line);
  74.         exit(1);
  75.         }
  76.         if (!strcmp(this,name)) {
  77.         for (count = 0; count < 9; count++)
  78.             params[count] = param[count];
  79.         set_params(cmd,fd,params);
  80.         }
  81.     }
  82.     }
  83.     fprintf(stderr,"No such parameter set: '%s'\n",name);
  84.     exit(1);
  85. }
  86.  
  87.  
  88. static void usage(char *name)
  89. {
  90.     char *this;
  91.  
  92.     if (this = strrchr(name,'/')) name = this+1;
  93.     fprintf(stderr,"usage: %s [ -p ] dev name\n",name);
  94.     fprintf(stderr,"       %s [ -p ] dev size sect heads tracks stretch \
  95. gap rate spec1 fmt_gap\n",name);
  96. #ifdef FDMEDCNG
  97.     fprintf(stderr,"       %s [ -c | -y | -n | -d ] dev\n",name);
  98. #else
  99.     fprintf(stderr,"       %s [ -c | -y | -n ] dev\n",name);
  100. #endif
  101.     exit(1);
  102. }
  103.  
  104.  
  105. main(int argc,char **argv)
  106. {
  107.     int cmd,fd;
  108.     char *name;
  109.  
  110.     name = argv[0];
  111.     if (argc < 3) usage(name);
  112.     cmd = FDSETPRM;
  113.     if (*argv[1] == '-') {
  114.     switch (argv[1][1]) {
  115.         case 'c':
  116.         cmd = FDCLRPRM;
  117.         break;
  118.         case 'p':
  119.         cmd = FDDEFPRM;
  120.         break;
  121.         case 'y':
  122.         cmd = FDMSGON;
  123.         break;
  124.         case 'n':
  125.         cmd = FDMSGOFF;
  126.         break;
  127. #ifdef FDMEDCNG
  128.         case 'd':
  129.         cmd = FDMEDCNG;
  130.         break;
  131. #endif
  132.         default:
  133.         usage(name);
  134.     }
  135.     argc--;
  136.     argv++;
  137.     }
  138.     if ((fd = open(argv[1],3)) < 0) { /* 3 == no access at all */
  139.     perror(argv[1]);
  140.     exit(1);
  141.     }
  142.     if (cmd != FDSETPRM && cmd != FDDEFPRM) {
  143.     if (argc != 2) usage(name);
  144.     cmd_without_param(cmd,fd);
  145.     }
  146.     if (argc != 11 && argc != 3) usage(name);
  147.     if (argc == 11) set_params(cmd,fd,&argv[2]);
  148.     else find_params(cmd,fd,argv[2]);
  149. }
  150.