dosfslabel.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* dosfslabel.c - User interface
  2. Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
  3. Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  4. Copyright (C) 2007 Red Hat, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. On Debian systems, the complete text of the GNU General Public License
  16. can be found in /usr/share/common-licenses/GPL-3 file.
  17. */
  18. #include "version.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <getopt.h>
  25. #include "common.h"
  26. #include "dosfsck.h"
  27. #include "io.h"
  28. #include "boot.h"
  29. #include "fat.h"
  30. #include "file.h"
  31. #include "check.h"
  32. int interactive = 0, rw = 0, list = 0, test = 0, verbose = 0, write_immed = 0;
  33. int atari_format = 0;
  34. unsigned n_files = 0;
  35. void *mem_queue = NULL;
  36. static void usage(int error)
  37. {
  38. FILE *f = error ? stderr : stdout;
  39. int status = error ? 1 : 0;
  40. fprintf(f, "usage: dosfslabel device [label]\n");
  41. exit(status);
  42. }
  43. /*
  44. * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
  45. * of MS-DOS filesystem by default.
  46. */
  47. static void check_atari(void)
  48. {
  49. #ifdef __mc68000__
  50. FILE *f;
  51. char line[128], *p;
  52. if (!(f = fopen("/proc/hardware", "r"))) {
  53. perror("/proc/hardware");
  54. return;
  55. }
  56. while (fgets(line, sizeof(line), f)) {
  57. if (strncmp(line, "Model:", 6) == 0) {
  58. p = line + 6;
  59. p += strspn(p, " \t");
  60. if (strncmp(p, "Atari ", 6) == 0)
  61. atari_format = 1;
  62. break;
  63. }
  64. }
  65. fclose(f);
  66. #endif
  67. }
  68. int main(int argc, char *argv[])
  69. {
  70. DOS_FS fs = {0};
  71. rw = 0;
  72. char *device = NULL;
  73. char *label = NULL;
  74. check_atari();
  75. if (argc < 2 || argc > 3)
  76. usage(1);
  77. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
  78. usage(0);
  79. else if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
  80. printf("dosfslabel " VERSION ", " VERSION_DATE ", FAT32, LFN\n");
  81. exit(0);
  82. }
  83. device = argv[1];
  84. if (argc == 3) {
  85. label = argv[2];
  86. if (strlen(label) > 11) {
  87. fprintf(stderr,
  88. "dosfslabel: labels can be no longer than 11 characters\n");
  89. exit(1);
  90. }
  91. rw = 1;
  92. }
  93. fs_open(device, rw);
  94. read_boot(&fs);
  95. if (fs.fat_bits == 32)
  96. read_fat(&fs);
  97. if (!rw) {
  98. fprintf(stdout, "%s\n", fs.label);
  99. exit(0);
  100. }
  101. write_label(&fs, label);
  102. fs_close(rw);
  103. return 0;
  104. }