file.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* file.c - Additional file attributes
  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. 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 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. On Debian systems, the complete text of the GNU General Public License
  15. can be found in /usr/share/common-licenses/GPL-3 file.
  16. */
  17. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  18. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <unistd.h>
  24. #define _LINUX_STAT_H /* hack to avoid inclusion of <linux/stat.h> */
  25. #define _LINUX_STRING_H_ /* hack to avoid inclusion of <linux/string.h>*/
  26. #define _LINUX_FS_H /* hack to avoid inclusion of <linux/fs.h> */
  27. # include <asm/types.h>
  28. #include <linux/msdos_fs.h>
  29. #include "common.h"
  30. #include "file.h"
  31. FDSC *fp_root = NULL;
  32. static void put_char(char **p,unsigned char c)
  33. {
  34. if ((c >= ' ' && c < 0x7f) || c >= 0xa0) *(*p)++ = c;
  35. else {
  36. *(*p)++ = '\\';
  37. *(*p)++ = '0'+(c >> 6);
  38. *(*p)++ = '0'+((c >> 3) & 7);
  39. *(*p)++ = '0'+(c & 7);
  40. }
  41. }
  42. char *file_name(unsigned char *fixed)
  43. {
  44. static char path[MSDOS_NAME*4+2];
  45. char *p;
  46. int i,j;
  47. p = path;
  48. for (i = j = 0; i < 8; i++)
  49. if (fixed[i] != ' ') {
  50. while (j++ < i) *p++ = ' ';
  51. put_char(&p,fixed[i]);
  52. }
  53. if (strncmp(fixed+8," ",3)) {
  54. *p++ = '.';
  55. for (i = j = 0; i < 3; i++)
  56. if (fixed[i+8] != ' ') {
  57. while (j++ < i) *p++ = ' ';
  58. put_char(&p,fixed[i+8]);
  59. }
  60. }
  61. *p = 0;
  62. return path;
  63. }
  64. int file_cvt(unsigned char *name,unsigned char *fixed)
  65. {
  66. unsigned char c;
  67. int size,ext,cnt;
  68. size = 8;
  69. ext = 0;
  70. while (*name) {
  71. c = *name;
  72. if (c < ' ' || c > 0x7e || strchr("*?<>|\"/",c)) {
  73. printf("Invalid character in name. Use \\ooo for special "
  74. "characters.\n");
  75. return 0;
  76. }
  77. if (c == '.') {
  78. if (ext) {
  79. printf("Duplicate dots in name.\n");
  80. return 0;
  81. }
  82. while (size--) *fixed++ = ' ';
  83. size = 3;
  84. ext = 1;
  85. name++;
  86. continue;
  87. }
  88. if (c == '\\') {
  89. c = 0;
  90. for (cnt = 3; cnt; cnt--) {
  91. if (*name < '0' || *name > '7') {
  92. printf("Invalid octal character.\n");
  93. return 0;
  94. }
  95. c = c*8+*name++-'0';
  96. }
  97. if (cnt < 4) {
  98. printf("Expected three octal digits.\n");
  99. return 0;
  100. }
  101. name += 3;
  102. }
  103. if (islower(c)) c = toupper(c);
  104. if (size) {
  105. *fixed++ = c;
  106. size--;
  107. }
  108. name++;
  109. }
  110. if (*name || size == 8) return 0;
  111. if (!ext) {
  112. while (size--) *fixed++ = ' ';
  113. size = 3;
  114. }
  115. while (size--) *fixed++ = ' ';
  116. return 1;
  117. }
  118. void file_add(char *path,FD_TYPE type)
  119. {
  120. FDSC **current,*walk;
  121. char name[MSDOS_NAME];
  122. char *here;
  123. current = &fp_root;
  124. if (*path != '/') die("%s: Absolute path required.",path);
  125. path++;
  126. while (1) {
  127. if ((here = strchr(path,'/'))) *here = 0;
  128. if (!file_cvt(path,name)) exit(2);
  129. for (walk = *current; walk; walk = walk->next)
  130. if (!here && (!strncmp(name,walk->name,MSDOS_NAME) || (type ==
  131. fdt_undelete && !strncmp(name+1,walk->name+1,MSDOS_NAME-1))))
  132. die("Ambiguous name: \"%s\"",path);
  133. else if (here && !strncmp(name,walk->name,MSDOS_NAME)) break;
  134. if (!walk) {
  135. walk = alloc(sizeof(FDSC));
  136. strncpy(walk->name,name,MSDOS_NAME);
  137. walk->type = here ? fdt_none : type;
  138. walk->first = NULL;
  139. walk->next = *current;
  140. *current = walk;
  141. }
  142. current = &walk->first;
  143. if (!here) break;
  144. *here = '/';
  145. path = here+1;
  146. }
  147. }
  148. FDSC **file_cd(FDSC **curr,char *fixed)
  149. {
  150. FDSC **walk;
  151. if (!curr || !*curr) return NULL;
  152. for (walk = curr; *walk; walk = &(*walk)->next)
  153. if (!strncmp((*walk)->name,fixed,MSDOS_NAME) && (*walk)->first)
  154. return &(*walk)->first;
  155. return NULL;
  156. }
  157. static FDSC **file_find(FDSC **dir,char *fixed)
  158. {
  159. if (!dir || !*dir) return NULL;
  160. if (*(unsigned char *) fixed == DELETED_FLAG) {
  161. while (*dir) {
  162. if (!strncmp((*dir)->name+1,fixed+1,MSDOS_NAME-1) && !(*dir)->first)
  163. return dir;
  164. dir = &(*dir)->next;
  165. }
  166. return NULL;
  167. }
  168. while (*dir) {
  169. if (!strncmp((*dir)->name,fixed,MSDOS_NAME) && !(*dir)->first)
  170. return dir;
  171. dir = &(*dir)->next;
  172. }
  173. return NULL;
  174. }
  175. FD_TYPE file_type(FDSC **curr,char *fixed)
  176. {
  177. FDSC **this;
  178. if ((this = file_find(curr,fixed))) return (*this)->type;
  179. return fdt_none;
  180. }
  181. void file_modify(FDSC **curr,char *fixed)
  182. {
  183. FDSC **this,*next;
  184. if (!(this = file_find(curr,fixed)))
  185. die("Internal error: file_find failed");
  186. switch ((*this)->type) {
  187. case fdt_drop:
  188. printf("Dropping %s\n",file_name(fixed));
  189. *(unsigned char *) fixed = DELETED_FLAG;
  190. break;
  191. case fdt_undelete:
  192. *fixed = *(*this)->name;
  193. printf("Undeleting %s\n",file_name(fixed));
  194. break;
  195. default:
  196. die("Internal error: file_modify");
  197. }
  198. next = (*this)->next;
  199. free(*this);
  200. *this = next;
  201. }
  202. static void report_unused(FDSC *this)
  203. {
  204. FDSC *next;
  205. while (this) {
  206. next = this->next;
  207. if (this->first) report_unused(this->first);
  208. else if (this->type != fdt_none)
  209. printf("Warning: did not %s file %s\n",this->type == fdt_drop ?
  210. "drop" : "undelete",file_name(this->name));
  211. free(this);
  212. this = next;
  213. }
  214. }
  215. void file_unused(void)
  216. {
  217. report_unused(fp_root);
  218. }
  219. /* Local Variables: */
  220. /* tab-width: 8 */
  221. /* End: */