"Fossies" - the Fresh Open Source Software Archive

Member "ms-sys-2.7.0/src/main.c" (28 Jun 2020, 25139 Bytes) of package /linux/privat/ms-sys-2.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.4.1_vs_2.6.0.

    1 /******************************************************************
    2     Copyright (C) 2009-2016 Henrik Carlqvist
    3 
    4     This program is free software; you can redistribute it and/or modify
    5     it under the terms of the GNU General Public License as published by
    6     the Free Software Foundation; either version 2 of the License, or
    7     (at your option) any later version.
    8 
    9     This program is distributed in the hope that it will be useful,
   10     but WITHOUT ANY WARRANTY; without even the implied warranty of
   11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12     GNU General Public License for more details.
   13 
   14     You should have received a copy of the GNU General Public License
   15     along with this program; if not, write to the Free Software
   16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   17 ******************************************************************/
   18 #include <stdio.h>
   19 #include <stdlib.h>
   20 #include <string.h>
   21 #include <errno.h>
   22 
   23 #include "fat12.h"
   24 #include "fat16.h"
   25 #include "fat32.h"
   26 #include "exfat.h"
   27 #include "ntfs.h"
   28 #include "oem_id.h"
   29 #include "br.h"
   30 #include "identify.h"
   31 #include "nls.h"
   32 #include "partition_info.h"
   33 
   34 #define VERSION "2.7.0"
   35 
   36 void print_help(const char *szCommand);
   37 void print_version(void);
   38 int parse_switches(int argc, char **argv, int *piBr,
   39            int *pbForce, int *pbPrintVersion,
   40            int *pbKeepLabel, int *pbWritePartitionInfo, int *piHeads,
   41            char **pszOemId,
   42            int *pbWriteWindowsDiskSignature,
   43            uint32_t *tWindowsDiskSignature);
   44 int isnumber(const char *szString);
   45 
   46 int main(int argc, char **argv)
   47 {
   48    FILE *fp;
   49    int iBr = NO_WRITING;
   50    int bForce = 0;
   51    int bPrintVersion = 0;
   52    int bKeepLabel = 1;
   53    int bWritePartitionInfo = 0;
   54    int iHeads = -1;
   55    char *szOemId = NULL;
   56    int bWriteWindowsDiskSignature = 0;
   57    uint32_t tWindowsDiskSignature = 0x0;
   58    int iRet = 0;
   59 
   60    nls_init();
   61    if(parse_switches(argc, argv, &iBr, &bForce, &bPrintVersion,
   62              &bKeepLabel, &bWritePartitionInfo, &iHeads,
   63              &szOemId,
   64              &bWriteWindowsDiskSignature, &tWindowsDiskSignature))
   65    {
   66       print_help(argv[0]);
   67       return 0;
   68    }
   69    if(bPrintVersion)
   70    {
   71       print_version();
   72       if(argc < 3)
   73      return 0;
   74    }
   75    fp=fopen(argv[argc-1],
   76         (iBr || bWritePartitionInfo || szOemId || bWriteWindowsDiskSignature) ?
   77         "r+b" : "rb");
   78    if(!fp)
   79    {
   80       printf(_("Unable to open %s, %s\n"), argv[argc-1], strerror(errno));
   81       return 1;
   82    }
   83    if(iBr == AUTO_BR)
   84    {
   85 #ifdef __OpenBSD__
   86       iBr = smart_select(fp, argv[argc-1]);
   87 #else
   88       iBr = smart_select(fp);
   89 #endif
   90       if(!iBr)
   91      printf(_("Unable to automaticly select boot record for %s\n"),
   92         argv[argc-1]);
   93    }
   94    if(iBr && !bForce)
   95    {
   96       if( ! sanity_check(fp, argv[argc-1], iBr, 1) )
   97       {
   98      fclose(fp);
   99      return 1;
  100       }
  101    }
  102    if(szOemId && !bForce)
  103    {
  104       if( ! ok_to_write_oem_id(fp, argv[argc-1], 1) )
  105       {
  106      fclose(fp);
  107      return 1;
  108       }
  109    }
  110    if(bWriteWindowsDiskSignature && !bForce)
  111    {
  112       if( ! sanity_check(fp, argv[argc-1], MBR_ZERO, 1) )
  113       {
  114      fclose(fp);
  115      return 1;
  116       }
  117    }
  118    if( bWritePartitionInfo )
  119    {
  120       if( !iBr && !bForce)
  121       {
  122         if (! is_fat_16_fs(fp) && ! is_fat_32_fs(fp) && ! is_exfat_fs(fp) && ! is_ntfs_fs(fp))
  123         {
  124            printf(_("Failed writing partition information to %s\n"), argv[argc-1]);
  125            printf(_("Device must be formatted as FAT16, FAT32, EXFAT or NTFS\n"));
  126            fclose(fp);
  127            return 1;
  128         }
  129       }
  130 #ifdef __OpenBSD__
  131       if( write_partition_start_sector_number(fp, argv[argc-1]) )
  132 #else
  133       if( write_partition_start_sector_number(fp) )
  134 #endif
  135       {
  136         printf(_("Start sector %ld (nr of hidden sectors) successfully written to %s\n"),
  137 #ifdef __OpenBSD__
  138         partition_start_sector(fp, argv[argc-1]),
  139 #else
  140         partition_start_sector(fp),
  141 #endif
  142         argv[argc-1]);
  143         if ((is_fat_16_fs(fp)) || (is_fat_32_fs(fp)) || is_ntfs_fs(fp))
  144         {
  145           if( write_partition_number_of_heads(fp, iHeads))
  146           {
  147             printf(_("Number of heads (%d) successfully written to %s\n"),
  148                 iHeads != -1 ? iHeads : partition_number_of_heads(fp),
  149                 argv[argc-1]);
  150           }
  151           else
  152           {
  153             printf(_("Failed writing number of heads to %s\n"),
  154                 argv[argc-1]);
  155           }
  156         }
  157         if (is_fat_16_fs(fp))
  158         {
  159           if( write_partition_physical_disk_drive_id_fat16(fp) )
  160           {
  161             printf(_("Physical disk drive id 0x80 (C:) successfully written to %s\n"),
  162                 argv[argc-1]);
  163           }
  164           else
  165           {
  166             printf(_("Failed writing physical disk drive id to %s\n"),
  167                 argv[argc-1]);
  168           }
  169         }
  170         if (is_fat_32_fs(fp))
  171         {
  172           if( write_partition_physical_disk_drive_id_fat32(fp) )
  173           {
  174             printf(_("Physical disk drive id 0x80 (C:) successfully written to %s\n"),
  175                 argv[argc-1]);
  176           }
  177           else
  178           {
  179             printf(_("Failed writing physical disk drive id to %s\n"),
  180                 argv[argc-1]);
  181           }
  182         }
  183         if(is_exfat_fs(fp))
  184         {
  185           if( write_partition_physical_disk_drive_id_exfat(fp) )
  186           {
  187             printf(_("Physical disk drive id 0x80 (C:) successfully written to %s\n"),
  188                 argv[argc-1]);
  189           }
  190           else
  191           {
  192             printf(_("Failed writing physical disk drive id to %s\n"),
  193                 argv[argc-1]);
  194           }
  195           if (!iBr)
  196           {
  197             if(write_exfat_br_checksum(fp))
  198               printf(_("EXFAT VBR checksum successfully written to %s\n"),
  199                   argv[argc-1]);
  200             else
  201             {
  202               printf(_("Failed writing EXFAT VBR checksum to %s\n"),
  203                   argv[argc-1]);
  204             }
  205           }
  206         }
  207       }
  208       else
  209       {
  210        printf(_("Failed writing start sector to %s, this is only possible to do with\n"),
  211            argv[argc-1]);
  212        printf(_("real partitions!\n"));
  213        iRet = 1;
  214       }
  215    }
  216    switch(iBr)
  217    {
  218       case NO_WRITING:
  219       {
  220      if( (!bWritePartitionInfo) && (!szOemId) && (!bWriteWindowsDiskSignature) )
  221      {
  222         diagnose(fp, argv[argc-1]);
  223      }
  224      break;
  225       }
  226       case MBR_WIN7:
  227       {
  228          if(write_win7_mbr(fp))
  229             printf(_("Windows 7 master boot record successfully written to %s\n"),
  230                    argv[argc-1]);
  231          else
  232          {
  233             printf(_("Failed writing Windows 7 master boot record to %s\n"),
  234                    argv[argc-1]);
  235             iRet = 1;
  236          }
  237       }
  238       break;
  239       case MBR_VISTA:
  240       {
  241          if(write_vista_mbr(fp))
  242             printf(_("Windows Vista master boot record successfully written to %s\n"),
  243                    argv[argc-1]);
  244          else
  245          {
  246             printf(_("Failed writing Windows Vista master boot record to %s\n"),
  247                    argv[argc-1]);
  248             iRet = 1;
  249          }
  250       }
  251       break;
  252       case MBR_2000:
  253       {
  254      if(write_2000_mbr(fp))
  255         printf(_("Windows 2000/XP/2003 master boot record successfully written to %s\n"),
  256            argv[argc-1]);
  257      else
  258      {
  259         printf(_("Failed writing Windows 2000/XP/2003 master boot record to %s\n"),
  260            argv[argc-1]);
  261         iRet = 1;
  262      }
  263       }
  264       break;
  265       case MBR_95B:
  266       {
  267      if(write_95b_mbr(fp))
  268         printf(_("Windows 95B/98/98SE/ME master boot record successfully written to %s\n"),
  269            argv[argc-1]);
  270      else
  271      {
  272         printf(_("Failed writing Windows 95B/98/98SE/ME master boot record to %s\n"),
  273            argv[argc-1]);
  274         iRet = 1;
  275      }
  276       }
  277       break;
  278       case MBR_DOS:
  279       {
  280      if(write_dos_mbr(fp))
  281         printf(_("DOS/Windows NT master boot record successfully written to %s\n"),
  282            argv[argc-1]);
  283      else
  284      {
  285         printf(_("Failed writing DOS/Windows NT master boot record to %s\n"),
  286            argv[argc-1]);
  287         iRet = 1;
  288      }
  289       }
  290       break;
  291       case MBR_SYSLINUX:
  292       {
  293      if(write_syslinux_mbr(fp))
  294         printf(_("Syslinux master boot record successfully written to %s\n"),
  295            argv[argc-1]);
  296      else
  297      {
  298         printf(_("Failed writing Syslinux master boot record to %s\n"),
  299            argv[argc-1]);
  300         iRet = 1;
  301      }
  302       }
  303       break;
  304       case MBR_GPT_SYSLINUX:
  305       {
  306      if(write_syslinux_gpt_mbr(fp))
  307         printf(_("Syslinux GPT master boot record successfully written to %s\n"),
  308            argv[argc-1]);
  309      else
  310      {
  311         printf(_("Failed writing Syslinux GPT master boot record to %s\n"),
  312            argv[argc-1]);
  313         iRet = 1;
  314      }
  315       }
  316       break;
  317       case MBR_RUFUS:
  318       {
  319      if(write_rufus_mbr(fp))
  320         printf(_("Rufus master boot record successfully written to %s\n"),
  321            argv[argc-1]);
  322      else
  323      {
  324         printf(_("Failed writing Rufus master boot record to %s\n"),
  325            argv[argc-1]);
  326         iRet = 1;
  327      }
  328       }
  329       break;
  330       case MBR_REACTOS:
  331       {
  332      if(write_reactos_mbr(fp))
  333         printf(_("ReactOS master boot record successfully written to %s\n"),
  334            argv[argc-1]);
  335      else
  336      {
  337         printf(_("Failed writing ReactOS master boot record to %s\n"),
  338            argv[argc-1]);
  339         iRet = 1;
  340      }
  341       }
  342       break;
  343       case MBR_KOLIBRIOS:
  344       {
  345      if(write_kolibrios_mbr(fp))
  346         printf(_("KolibriOS master boot record successfully written to %s\n"),
  347            argv[argc-1]);
  348      else
  349      {
  350         printf(_("Failed writing KolibriOS master boot record to %s\n"),
  351            argv[argc-1]);
  352         iRet = 1;
  353      }
  354       }
  355       break;
  356       case MBR_GRUB4DOS:
  357       {
  358      if(write_grub4dos_mbr(fp))
  359         printf(_("Grub4DOS master boot record successfully written to %s\n"),
  360            argv[argc-1]);
  361      else
  362      {
  363         printf(_("Failed writing Grub4DOS master boot record to %s\n"),
  364            argv[argc-1]);
  365         iRet = 1;
  366      }
  367       }
  368       break;
  369       case MBR_GRUB2:
  370       {
  371      if(write_grub2_mbr(fp))
  372         printf(_("GRUB 2 master boot record successfully written to %s\n"),
  373            argv[argc-1]);
  374      else
  375      {
  376         printf(_("Failed writing GRUB2 master boot record to %s\n"),
  377            argv[argc-1]);
  378         iRet = 1;
  379      }
  380       }
  381       break;
  382       case MBR_ZERO:
  383       {
  384      if(write_zero_mbr(fp))
  385         printf(_("Empty (zeroed) master boot record successfully written to %s\n"),
  386            argv[argc-1]);
  387      else
  388      {
  389         printf(_("Failed writing empty (zeroed) master boot record to %s\n"),
  390            argv[argc-1]);
  391         iRet = 1;
  392      }
  393       }
  394       break;
  395       case FAT12_BR:
  396       {
  397      if(write_fat_12_br(fp, bKeepLabel))
  398         printf(_("FAT12 boot record successfully written to %s\n"),
  399            argv[argc-1]);
  400      else
  401      {
  402         printf(_("Failed writing FAT12 boot record to %s\n"),
  403            argv[argc-1]);
  404         iRet = 1;
  405      }
  406       }
  407       break;
  408       case FAT16_BR:
  409       {
  410      if(write_fat_16_br(fp, bKeepLabel))
  411         printf(_("FAT16 boot record successfully written to %s\n"),
  412            argv[argc-1]);
  413      else
  414      {
  415         printf(_("Failed writing FAT16 boot record to %s\n"),
  416            argv[argc-1]);
  417         iRet = 1;
  418      }
  419       }
  420       break;
  421       case FAT16FD_BR:
  422       {
  423      if(write_fat_16_fd_br(fp, bKeepLabel))
  424         printf(_("FAT16 FreeDOS boot record successfully written to %s\n"),
  425            argv[argc-1]);
  426      else
  427      {
  428         printf(_("Failed writing FAT16 FreeDOS boot record to %s\n"),
  429            argv[argc-1]);
  430         iRet = 1;
  431      }
  432       }
  433       break;
  434       case FAT16ROS_BR:
  435       {
  436      if(write_fat_16_ros_br(fp, bKeepLabel))
  437         printf(_("FAT16 ReactOS boot record successfully written to %s\n"),
  438            argv[argc-1]);
  439      else
  440      {
  441         printf(_("Failed writing FAT16 ReactOS boot record to %s\n"),
  442            argv[argc-1]);
  443         iRet = 1;
  444      }
  445       }
  446       break;
  447       case FAT32NT5_BR:
  448       {
  449      if(write_fat_32_nt5_br(fp, bKeepLabel))
  450         printf(_("FAT32 NT5.0 boot record successfully written to %s\n"),
  451            argv[argc-1]);
  452      else
  453      {
  454         printf(_("Failed writing FAT32 NT5.0 boot record to %s\n"),
  455            argv[argc-1]);
  456         iRet = 1;
  457      }
  458       }
  459       break;
  460       case FAT32NT6_BR:
  461       {
  462      if(write_fat_32_nt6_br(fp, bKeepLabel))
  463         printf(_("FAT32 NT6.0 boot record successfully written to %s\n"),
  464            argv[argc-1]);
  465      else
  466      {
  467         printf(_("Failed writing FAT32 NT6.0 boot record to %s\n"),
  468            argv[argc-1]);
  469         iRet = 1;
  470      }
  471       }
  472       break;
  473       case EXFATNT6_BR:
  474       {
  475      if(write_exfat_nt6_br(fp))
  476         printf(_("EXFAT NT6.0 boot record successfully written to %s\n"),
  477            argv[argc-1]);
  478      else
  479      {
  480         printf(_("Failed writing EXFAT NT6.0 boot record to %s\n"),
  481            argv[argc-1]);
  482         iRet = 1;
  483      }
  484      if(write_exfat_br_checksum(fp))
  485          printf(_("EXFAT VBR checksum successfully written to %s\n"),
  486                 argv[argc-1]);
  487      else
  488      {
  489         printf(_("Failed writing EXFAT VBR checksum to %s\n"),
  490            argv[argc-1]);
  491         iRet = 1;
  492      }
  493       }
  494       break;
  495       case FAT32PE_BR:
  496       {
  497      if(write_fat_32_pe_br(fp, bKeepLabel))
  498         printf(_("FAT32 PE boot record successfully written to %s\n"),
  499            argv[argc-1]);
  500      else
  501      {
  502         printf(_("Failed writing FAT32 PE boot record to %s\n"),
  503            argv[argc-1]);
  504         iRet = 1;
  505      }
  506       }
  507       break;
  508       case FAT32FD_BR:
  509       {
  510      if(write_fat_32_fd_br(fp, bKeepLabel))
  511         printf(_("FAT32 FreeDOS boot record successfully written to %s\n"),
  512            argv[argc-1]);
  513      else
  514      {
  515         printf(_("Failed writing FAT32 FreeDOS boot record to %s\n"),
  516            argv[argc-1]);
  517         iRet = 1;
  518      }
  519       }
  520       break;
  521       case FAT32KOS_BR:
  522       {
  523      if(write_fat_32_kos_br(fp, bKeepLabel))
  524         printf(
  525            _("FAT32 KolibriOS boot record successfully written to %s\n"),
  526            argv[argc-1]);
  527      else
  528      {
  529         printf(_("Failed writing FAT32 KolibriOS boot record to %s\n"),
  530            argv[argc-1]);
  531         iRet = 1;
  532      }
  533       }
  534       break;
  535       case FAT32ROS_BR:
  536       {
  537      if(write_fat_32_ros_br(fp, bKeepLabel))
  538         printf(_("FAT32 ReactOS boot record successfully written to %s\n"),
  539            argv[argc-1]);
  540      else
  541      {
  542         printf(_("Failed writing FAT32 ReactOS boot record to %s\n"),
  543            argv[argc-1]);
  544         iRet = 1;
  545      }
  546       }
  547       break;
  548       case FAT32_BR:
  549       {
  550      if(write_fat_32_br(fp, bKeepLabel))
  551         printf(_("FAT32 DOS boot record successfully written to %s\n"),
  552            argv[argc-1]);
  553      else
  554      {
  555         printf(_("Failed writing FAT32 DOS boot record to %s\n"),
  556            argv[argc-1]);
  557         iRet = 1;
  558      }
  559       }
  560       break;
  561       case NTFS_BR:
  562       {
  563      if(write_ntfs_br(fp))
  564         printf(_("NTFS Windows 7 boot record successfully written to %s\n"),
  565            argv[argc-1]);
  566      else
  567      {
  568         printf(_("Failed writing NTFS Windows 7 boot record to %s\n"),
  569            argv[argc-1]);
  570         iRet = 1;
  571      }
  572       }
  573       break;
  574       default:
  575       {
  576      printf(_("Whoops, internal error, unknown boot record\n"));
  577       }
  578       break;
  579    }
  580    if(szOemId)
  581    {
  582       if(write_oem_id(fp, szOemId))
  583      printf(_("OEM ID '%s' successfully written to %s\n"),
  584         szOemId, argv[argc-1]);
  585       else
  586       {
  587      printf(_("Failed writing OEM ID to %s\n"),
  588         argv[argc-1]);
  589      iRet = 1;
  590       }     
  591    }
  592    if(bWriteWindowsDiskSignature)
  593    {
  594       if(write_windows_disk_signature(fp, tWindowsDiskSignature))
  595      printf(_("Windows Disk Signature 0x%08x successfully written to %s\n"),
  596         tWindowsDiskSignature, argv[argc-1]);
  597      else
  598      {
  599         printf(_("Failed writing Windows Disk Signature to %s\n"),
  600            argv[argc-1]);
  601         iRet = 1;
  602      }      
  603    }
  604    fclose(fp);
  605    return iRet;
  606 } /* main */
  607 
  608 void print_help(const char *szCommand)
  609 {
  610    printf(_("Usage:\n\t%s [options] [device]\nOptions:\n"), szCommand);
  611    printf(
  612       _("    -1, --fat12     Write a FAT12 floppy boot record to device\n"));
  613    printf(
  614       _("    -2, --fat32nt5  Write a FAT32 partition NT5.0 boot record to device\n"));
  615    printf(
  616       _("    -8, --fat32nt6  Write a FAT32 partition NT6.0 boot record to device\n"));
  617    printf(
  618       _("    -x, --exfatnt6  Write a EXFAT partition NT6.0 boot record to device\n"));
  619    printf(
  620       _("    -e, --fat32pe   Write a FAT32 partition PE boot record to device\n"));
  621    printf(
  622       _("    -3, --fat32     Write a FAT32 partition DOS boot record to device\n"));
  623    printf(
  624       _("    -4, --fat32free Write a FAT32 partition FreeDOS boot record to device\n"));
  625    printf(
  626       _("    -5, --fat16free Write a FAT16 partition FreeDOS boot record to device\n"));
  627    printf(
  628       _("    -6, --fat16     Write a FAT16 partition DOS boot record to device\n"));
  629    printf(
  630       _("    -n, --ntfs      Write a NTFS partition Windows 7 boot record to device\n"));
  631    printf(
  632       _("    -o, --fat16ros  Write a FAT16 partition ReactOS boot record to device\n"));
  633    printf(
  634       _("    -c, --fat32ros  Write a FAT32 partition ReactOS boot record to device\n"));
  635    printf(
  636       _("    -q, --fat32kos  Write a FAT32 partition KolibriOS boot record to device\n"));
  637    printf(
  638       _("    -l, --wipelabel Reset partition disk label in boot record\n"));
  639    printf(
  640       _("    -p, --partition Write partition info (hidden sectors, heads and drive id)\n"));
  641    printf(
  642       _("                    to boot record\n"));
  643    printf(
  644       _("    -H, --heads <n> Manually set number of heads if partition info is written\n"));
  645    printf(
  646       _("    -B, --bps <n>   Manually set number of bytes per sector (default 512)\n"));
  647    printf(
  648       _("    -O, --writeoem <s>   Write OEM ID string <s> to file system\n"));
  649    printf(
  650       _("    -S, --writewds <x>   Write Windows Disk Signature hexadecimal <x> to MBR\n"));
  651    printf(
  652       _("    -7, --mbr7      Write a Windows 7 MBR to device\n"));
  653    printf(
  654       _("    -i, --mbrvista  Write a Windows Vista MBR to device\n"));
  655    printf(
  656       _("    -m, --mbr       Write a Windows 2000/XP/2003 MBR to device\n"));
  657    printf(
  658       _("    -9, --mbr95b    Write a Windows 95B/98/98SE/ME MBR to device\n"));
  659    printf(
  660       _("    -d, --mbrdos    Write a DOS/Windows NT MBR to device\n"));
  661    printf(
  662       _("    -s, --mbrsyslinux    Write a Syslinux MBR to device\n"));
  663    printf(
  664       _("    -t, --mbrgptsyslinux Write a Syslinux GPT MBR to device\n"));
  665    printf(
  666       _("    -a, --mbrreactos     Write a ReactOS MBR to device\n"));
  667    printf(
  668       _("    -k, --mbrkolibrios   Write a KolibriOS MBR to device\n"));
  669    printf(
  670       _("    -r, --mbrrufus  Write a Rufus MBR to device\n"));
  671    printf(
  672       _("    -g, --mbrgrub4dos    Write a Grub4Dos MBR to device\n"));
  673    printf(
  674       _("    -b, --mbrgrub2  Write a Grub 2 MBR to device\n"));
  675    printf(
  676       _("    -z, --mbrzero   Write an empty (zeroed) MBR to device\n"));
  677    printf(
  678       _("    -f, --force     Force writing of boot record\n"));
  679    printf(
  680       _("    -h, --help      Display this help and exit\n"));
  681    printf(
  682       _("    -v, --version   Show program version\n"));
  683    printf(
  684       _("    -w, --write     Write automatically selected boot record to device\n\n"));
  685    printf(
  686       _("    Default         Inspect current boot record\n\n"));
  687    printf(
  688       _("Warning: Writing the wrong kind of boot record to a device might\n"));
  689    printf(
  690       _("destroy partition information or file system!\n\n"));
  691 } /* print_help */
  692 
  693 void print_version(void)
  694 {
  695    printf(_("ms-sys version %s\n"), VERSION);
  696    printf(_("Written by Henrik Carlqvist\n\n"));
  697    printf(_("Copyright (C) 2009-2020 Free Software Foundation, Inc.\n"));
  698    printf(_("This is free software; see the source for copying conditions.  There is NO\n"));
  699    printf(_("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
  700 } /* print_version */
  701 
  702 int parse_switches(int argc, char **argv, int *piBr,
  703            int *pbForce, int *pbPrintVersion, int *pbKeepLabel,
  704            int *pbWritePartitionInfo, int *piHeads,
  705            char **pszOemId,
  706            int *pbWriteWindowsDiskSignature,
  707            uint32_t *ptWindowsDiskSignature)
  708 {
  709    int bHelp = 0;
  710    int i;
  711 
  712    *piBr = NO_WRITING;
  713    *pbForce = 0;
  714    *pbPrintVersion = 0;
  715    *pbKeepLabel = 1;
  716    *pbWritePartitionInfo = 0;
  717    *piHeads = -1;
  718    *pbWriteWindowsDiskSignature = 0;
  719    *ptWindowsDiskSignature = 0;
  720 
  721    if(argc < 2)
  722       return 1;
  723    /* Don't parse the device */
  724    argc--;
  725    if(( ! strcmp("-h", argv[argc])) ||
  726       ( ! strcmp("--help", argv[argc])))
  727       bHelp = 1;
  728    else if(( ! strcmp("-v", argv[argc])) ||
  729        ( ! strcmp("--version", argv[argc])))
  730       *pbPrintVersion = 1;
  731    /* Don't parse the name of the program */
  732    while(--argc)
  733    {
  734       if( ! strcmp("--fat12", argv[argc]))
  735      *piBr = FAT12_BR;
  736       else if( ! strcmp("--fat32nt5", argv[argc]))
  737      *piBr = FAT32NT5_BR;
  738       else if( ! strcmp("--fat32nt6", argv[argc]))
  739      *piBr = FAT32NT6_BR;
  740       else if( ! strcmp("--exfatnt6", argv[argc]))
  741      *piBr = EXFATNT6_BR;
  742       else if( ! strcmp("--fat32pe", argv[argc]))
  743      *piBr = FAT32PE_BR;
  744       else if( ! strcmp("--fat32", argv[argc]))
  745      *piBr = FAT32_BR;
  746       else if( ! strcmp("--fat32free", argv[argc]))
  747      *piBr = FAT32FD_BR;
  748       else if( ! strcmp("--fat32ros", argv[argc]))
  749      *piBr = FAT32ROS_BR;
  750       else if( ! strcmp("--fat32kos", argv[argc]))
  751      *piBr = FAT32KOS_BR;
  752       else if( ! strcmp("--fat16ros", argv[argc]))
  753      *piBr = FAT16ROS_BR;
  754       else if( ! strcmp("--fat16free", argv[argc]))
  755      *piBr = FAT16FD_BR;
  756       else if( ! strcmp("--fat16", argv[argc]))
  757      *piBr = FAT16_BR;
  758       else if( ! strcmp("--ntfs", argv[argc]))
  759      *piBr = NTFS_BR;
  760       else if( ! strcmp("--force", argv[argc]))
  761      *pbForce = 1;
  762       else if( ! strcmp("--wipelabel", argv[argc]))
  763      *pbKeepLabel = 0;
  764       else if( ! strcmp("--partition", argv[argc]))
  765      *pbWritePartitionInfo = 1;
  766       else if( ! strcmp("--mbr7", argv[argc]))
  767      *piBr = MBR_WIN7;
  768       else if( ! strcmp("--mbrvista", argv[argc]))
  769      *piBr = MBR_VISTA;
  770       else if( ! strcmp("--mbr", argv[argc]))
  771      *piBr = MBR_2000;
  772       else if( ! strcmp("--mbr95b", argv[argc]))
  773      *piBr = MBR_95B;
  774       else if( ! strcmp("--mbrdos", argv[argc]))
  775      *piBr = MBR_DOS;
  776       else if( ! strcmp("--mbrsyslinux", argv[argc]))
  777      *piBr = MBR_SYSLINUX;
  778       else if( ! strcmp("--mbrgptsyslinux", argv[argc]))
  779      *piBr = MBR_GPT_SYSLINUX;
  780       else if( ! strcmp("--mbrrufus", argv[argc]))
  781      *piBr = MBR_RUFUS;
  782       else if( ! strcmp("--mbrreactos", argv[argc]))
  783      *piBr = MBR_REACTOS;
  784       else if( ! strcmp("--mbrkolibrios", argv[argc]))
  785      *piBr = MBR_KOLIBRIOS;
  786       else if( ! strcmp("--mbrgrub4dos", argv[argc]))
  787      *piBr = MBR_GRUB4DOS;
  788       else if( ! strcmp("--mbrgrub2", argv[argc]))
  789      *piBr = MBR_GRUB2;
  790       else if( ! strcmp("--mbrzero", argv[argc]))
  791      *piBr = MBR_ZERO;
  792       else if( ! strcmp("--write", argv[argc]))
  793      *piBr = AUTO_BR;
  794       else if( ! strcmp("--version", argv[argc]))
  795      *pbPrintVersion = 1;
  796       else if( (argv[argc][0] == '-') && (argv[argc][1] != '-') && !argv[argc][2])
  797       {
  798      for(i=1; argv[argc][i]; i++)
  799      {
  800         switch(argv[argc][i])
  801         {
  802            case 'a':
  803           *piBr = MBR_REACTOS;
  804           break;
  805            case '1':
  806           *piBr = FAT12_BR;
  807           break;
  808            case '2':
  809           *piBr = FAT32NT5_BR;
  810           break;
  811            case '8':
  812           *piBr = FAT32NT6_BR;
  813           break;
  814            case 'x':
  815           *piBr = EXFATNT6_BR;
  816           break;
  817            case 'e':
  818           *piBr = FAT32PE_BR;
  819           break;
  820            case '3':
  821           *piBr = FAT32_BR;
  822           break;
  823            case '4':
  824           *piBr = FAT32FD_BR;
  825           break;
  826            case '5':
  827           *piBr = FAT16FD_BR;
  828           break;
  829            case '6':
  830           *piBr = FAT16_BR;
  831           break;
  832            case 'o':
  833           *piBr = FAT16ROS_BR;
  834           break;
  835            case 'c':
  836           *piBr = FAT32ROS_BR;
  837           break;
  838            case 'q':
  839           *piBr = FAT32KOS_BR;
  840           break;
  841            case 'n':
  842           *piBr = NTFS_BR;
  843           break;
  844            case 'f':
  845           *pbForce = 1;
  846           break;
  847            case 'l':
  848           *pbKeepLabel = 0;
  849           break;
  850            case 'p':
  851           *pbWritePartitionInfo = 1;
  852           break;
  853            case '7':
  854           *piBr = MBR_WIN7;
  855           break;
  856            case 'i':
  857           *piBr = MBR_VISTA;
  858           break;
  859            case 'm':
  860           *piBr = MBR_2000;
  861           break;
  862            case '9':
  863           *piBr = MBR_95B;
  864           break;
  865            case 'd':
  866           *piBr = MBR_DOS;
  867           break;
  868            case 'r':
  869           *piBr = MBR_RUFUS;
  870           break;
  871            case 's':
  872           *piBr = MBR_SYSLINUX;
  873           break;
  874            case 't':
  875           *piBr = MBR_GPT_SYSLINUX;
  876           break;
  877            case 'g':
  878           *piBr = MBR_GRUB4DOS;
  879           break;
  880            case 'b':
  881           *piBr = MBR_GRUB2;
  882           break;
  883            case 'k':
  884           *piBr = MBR_KOLIBRIOS;
  885           break;
  886            case 'z':
  887           *piBr = MBR_ZERO;
  888           break;
  889            case 'w':
  890           *piBr = AUTO_BR;
  891           break;
  892            case 'v':
  893           *pbPrintVersion = 1;
  894           break;
  895            default:
  896           bHelp=1;
  897           break;
  898         }
  899      }
  900       }
  901       else if((!strcmp("--heads", argv[argc-1]) || !strcmp("-H", argv[argc-1])) &&
  902           isnumber(argv[argc]))
  903      *piHeads = atoi(argv[argc--]);
  904       else if((!strcmp("--bps", argv[argc-1]) || !strcmp("-B", argv[argc-1])) &&
  905           isnumber(argv[argc]))
  906      set_bytes_per_sector(strtoul(argv[argc--], NULL, 0));
  907       else if((!strcmp("--writeoem", argv[argc-1]) || !strcmp("-O", argv[argc-1])))
  908      *pszOemId = argv[argc--];
  909       else if((!strcmp("--writewds", argv[argc-1]) || !strcmp("-S", argv[argc-1])))
  910       {
  911      *pbWriteWindowsDiskSignature = 1;
  912      *ptWindowsDiskSignature = strtoul(argv[argc--], NULL, 16);
  913       }
  914       else
  915      bHelp = 1;
  916    }
  917    return bHelp;
  918 } /* parse_switches */
  919 
  920 int isnumber(const char *szString)
  921 {
  922    int bRet = (szString && *szString);
  923    while(bRet && *szString)
  924    {
  925       if((*szString < '0') || (*szString > '9'))
  926      bRet = 0;
  927       szString++;
  928    }
  929    return bRet;
  930 } /* isnumber */