dosfslabel.c 3.0 KB

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