name.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * File name.c - map full Unix file names to unique 8.3 names that
  3. * would be valid on DOS.
  4. *
  5. Written by Eric Youngdale (1993).
  6. Copyright 1993 Yggdrasil Computing, Incorporated
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. #include "config.h"
  19. #include "mkisofs.h"
  20. #include <ctype.h>
  21. extern int allow_leading_dots;
  22. /*
  23. * Function: iso9660_file_length
  24. *
  25. * Purpose: Map file name to 8.3 format, return length
  26. * of result.
  27. *
  28. * Arguments: name file name we need to map.
  29. * sresult directory entry structure to contain mapped name.
  30. * dirflag flag indicating whether this is a directory or not.
  31. *
  32. * Notes: This procedure probably needs to be rationalized somehow.
  33. * New options to affect the behavior of this function
  34. * would also be nice to have.
  35. */
  36. int FDECL3(iso9660_file_length,
  37. const char*, name,
  38. struct directory_entry *, sresult,
  39. int, dirflag)
  40. {
  41. char * c;
  42. int chars_after_dot = 0;
  43. int chars_before_dot = 0;
  44. int current_length = 0;
  45. int extra = 0;
  46. int ignore = 0;
  47. char * last_dot;
  48. const char * pnt;
  49. int priority = 32767;
  50. char * result;
  51. int seen_dot = 0;
  52. int seen_semic = 0;
  53. int tildes = 0;
  54. result = sresult->isorec.name;
  55. /*
  56. * For the '.' entry, generate the correct record, and return
  57. * 1 for the length.
  58. */
  59. if(strcmp(name,".") == 0)
  60. {
  61. if(result)
  62. {
  63. *result = 0;
  64. }
  65. return 1;
  66. }
  67. /*
  68. * For the '..' entry, generate the correct record, and return
  69. * 1 for the length.
  70. */
  71. if(strcmp(name,"..") == 0)
  72. {
  73. if(result)
  74. {
  75. *result++ = 1;
  76. *result++ = 0;
  77. }
  78. return 1;
  79. }
  80. /*
  81. * Now scan the directory one character at a time, and figure out
  82. * what to do.
  83. */
  84. pnt = name;
  85. /*
  86. * Find the '.' that we intend to use for the extension. Usually this
  87. * is the last dot, but if we have . followed by nothing or a ~, we
  88. * would consider this to be unsatisfactory, and we keep searching.
  89. */
  90. last_dot = strrchr (pnt,'.');
  91. if( (last_dot != NULL)
  92. && ( (last_dot[1] == '~')
  93. || (last_dot[1] == '\0')) )
  94. {
  95. c = last_dot;
  96. *c = '\0';
  97. last_dot = strrchr (pnt,'.');
  98. *c = '.';
  99. }
  100. while(*pnt)
  101. {
  102. #ifdef VMS
  103. if( strcmp(pnt,".DIR;1") == 0 )
  104. {
  105. break;
  106. }
  107. #endif
  108. /*
  109. * This character indicates a Unix style of backup file
  110. * generated by some editors. Lower the priority of
  111. * the file.
  112. */
  113. if(*pnt == '#')
  114. {
  115. priority = 1;
  116. pnt++;
  117. continue;
  118. }
  119. /*
  120. * This character indicates a Unix style of backup file
  121. * generated by some editors. Lower the priority of
  122. * the file.
  123. */
  124. if(*pnt == '~')
  125. {
  126. priority = 1;
  127. tildes++;
  128. pnt++;
  129. continue;
  130. }
  131. /*
  132. * This might come up if we had some joker already try and put
  133. * iso9660 version numbers into the file names. This would be
  134. * a silly thing to do on a Unix box, but we check for it
  135. * anyways. If we see this, then we don't have to add our
  136. * own version number at the end.
  137. * UNLESS the ';' is part of the filename and no version
  138. * number is following. [VK]
  139. */
  140. if(*pnt == ';')
  141. {
  142. /* [VK] */
  143. if (pnt[1] != '\0' && (pnt[1] < '0' || pnt[1] > '9'))
  144. {
  145. pnt++;
  146. ignore++;
  147. continue;
  148. }
  149. }
  150. /*
  151. * If we have a name with multiple '.' characters, we ignore everything
  152. * after we have gotten the extension.
  153. */
  154. if(ignore)
  155. {
  156. pnt++;
  157. continue;
  158. }
  159. /*
  160. * Spin past any iso9660 version number we might have.
  161. */
  162. if(seen_semic)
  163. {
  164. if(*pnt >= '0' && *pnt <= '9')
  165. {
  166. *result++ = *pnt;
  167. }
  168. extra++;
  169. pnt++;
  170. continue;
  171. }
  172. /*
  173. * If we have full names, the names we generate will not
  174. * work on a DOS machine, since they are not guaranteed
  175. * to be 8.3. Nonetheless, in many cases this is a useful
  176. * option. We still only allow one '.' character in the
  177. * name, however.
  178. */
  179. if(full_iso9660_filenames)
  180. {
  181. /* Here we allow a more relaxed syntax. */
  182. if(*pnt == '.')
  183. {
  184. if (seen_dot)
  185. {
  186. ignore++;
  187. continue;
  188. }
  189. seen_dot++;
  190. }
  191. if(current_length < 30)
  192. {
  193. if( !isascii (*pnt))
  194. {
  195. *result++ = '_';
  196. }
  197. else
  198. {
  199. *result++ = (islower((unsigned char)*pnt) ? toupper((unsigned char)*pnt) : *pnt);
  200. }
  201. }
  202. }
  203. else
  204. {
  205. /*
  206. * Dos style filenames. We really restrict the
  207. * names here.
  208. */
  209. /* It would be nice to have .tar.gz transform to .tgz,
  210. * .ps.gz to .psz, ...
  211. */
  212. if(*pnt == '.')
  213. {
  214. if (!chars_before_dot && !allow_leading_dots)
  215. {
  216. /* DOS can't read files with dot first */
  217. chars_before_dot++;
  218. if (result)
  219. {
  220. *result++ = '_'; /* Substitute underscore */
  221. }
  222. }
  223. else if( pnt != last_dot )
  224. {
  225. /*
  226. * If this isn't the dot that we use for the extension,
  227. * then change the character into a '_' instead.
  228. */
  229. if(chars_before_dot < 8)
  230. {
  231. chars_before_dot++;
  232. if(result)
  233. {
  234. *result++ = '_';
  235. }
  236. }
  237. }
  238. else
  239. {
  240. if (seen_dot)
  241. {
  242. ignore++; continue;
  243. }
  244. if(result)
  245. {
  246. *result++ = '.';
  247. }
  248. seen_dot++;
  249. }
  250. }
  251. else
  252. {
  253. if( (seen_dot && (chars_after_dot < 3) && ++chars_after_dot)
  254. || (!seen_dot && (chars_before_dot < 8) && ++chars_before_dot) )
  255. {
  256. if(result)
  257. {
  258. switch (*pnt)
  259. {
  260. default:
  261. if( !isascii (*pnt) )
  262. {
  263. *result++ = '_';
  264. }
  265. else
  266. {
  267. *result++ = islower((unsigned char)*pnt) ? toupper((unsigned char)*pnt) : *pnt;
  268. }
  269. break;
  270. /*
  271. * Descriptions of DOS's 'Parse Filename'
  272. * (function 29H) describes V1 and V2.0+
  273. * separator and terminator characters.
  274. * These characters in a DOS name make
  275. * the file visible but un-manipulable
  276. * (all useful operations error off.
  277. */
  278. /* separators */
  279. case '+':
  280. case '=':
  281. case '%': /* not legal DOS filename */
  282. case ':':
  283. case ';': /* already handled */
  284. case '.': /* already handled */
  285. case ',': /* already handled */
  286. case '\t':
  287. case ' ':
  288. /* V1 only separators */
  289. case '/':
  290. case '"':
  291. case '[':
  292. case ']':
  293. /* terminators */
  294. case '>':
  295. case '<':
  296. case '|':
  297. /* Hmm - what to do here? Skip?
  298. * Win95 looks like it substitutes '_'
  299. */
  300. *result++ = '_';
  301. break;
  302. } /* switch (*pnt) */
  303. } /* if (result) */
  304. } /* if (chars_{after,before}_dot) ... */
  305. } /* else *pnt == '.' */
  306. } /* else DOS file names */
  307. current_length++;
  308. pnt++;
  309. } /* while (*pnt) */
  310. /*
  311. * OK, that wraps up the scan of the name. Now tidy up a few other
  312. * things.
  313. */
  314. /*
  315. * Look for emacs style of numbered backups, like foo.c.~3~. If
  316. * we see this, convert the version number into the priority
  317. * number. In case of name conflicts, this is what would end
  318. * up being used as the 'extension'.
  319. */
  320. if(tildes == 2)
  321. {
  322. int prio1 = 0;
  323. pnt = name;
  324. while (*pnt && *pnt != '~')
  325. {
  326. pnt++;
  327. }
  328. if (*pnt)
  329. {
  330. pnt++;
  331. }
  332. while(*pnt && *pnt != '~')
  333. {
  334. prio1 = 10*prio1 + *pnt - '0';
  335. pnt++;
  336. }
  337. priority = prio1;
  338. }
  339. /*
  340. * If this is not a directory, force a '.' in case we haven't
  341. * seen one, and add a version number if we haven't seen one
  342. * of those either.
  343. */
  344. if (!dirflag)
  345. {
  346. if (!seen_dot && !omit_period)
  347. {
  348. if (result) *result++ = '.';
  349. extra++;
  350. }
  351. if(!omit_version_number && !seen_semic)
  352. {
  353. if(result)
  354. {
  355. *result++ = ';';
  356. *result++ = '1';
  357. };
  358. extra += 2;
  359. }
  360. }
  361. if(result)
  362. {
  363. *result++ = 0;
  364. }
  365. sresult->priority = priority;
  366. return (chars_before_dot + chars_after_dot + seen_dot + extra);
  367. }