names.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /* Various processing of names.
  2. Copyright (C) 1988, 92, 94, 96, 97, 98, 1999 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Enable GNU extensions in fnmatch.h. */
  15. #ifndef _GNU_SOURCE
  16. # define _GNU_SOURCE 1
  17. #endif
  18. #include "system.h"
  19. #include <pwd.h>
  20. #include <grp.h>
  21. #include <fnmatch.h>
  22. #include "common.h"
  23. /* User and group names. */
  24. extern struct group *getgrnam ();
  25. extern struct passwd *getpwnam ();
  26. #if !HAVE_GETPWUID
  27. extern struct passwd *getpwuid ();
  28. #endif
  29. #if !HAVE_GETGRGID
  30. extern struct group *getgrgid ();
  31. #endif
  32. /* Make sure you link with the proper libraries if you are running the
  33. Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
  34. This code should also be modified for non-UNIX systems to do something
  35. reasonable. */
  36. static char cached_uname[UNAME_FIELD_SIZE];
  37. static char cached_gname[GNAME_FIELD_SIZE];
  38. static uid_t cached_uid; /* valid only if cached_uname is not empty */
  39. static gid_t cached_gid; /* valid only if cached_gname is not empty */
  40. /* These variables are valid only if nonempty. */
  41. static char cached_no_such_uname[UNAME_FIELD_SIZE];
  42. static char cached_no_such_gname[GNAME_FIELD_SIZE];
  43. /* These variables are valid only if nonzero. It's not worth optimizing
  44. the case for weird systems where 0 is not a valid uid or gid. */
  45. static uid_t cached_no_such_uid;
  46. static gid_t cached_no_such_gid;
  47. /*------------------------------------------.
  48. | Given UID, find the corresponding UNAME. |
  49. `------------------------------------------*/
  50. void
  51. uid_to_uname (uid_t uid, char uname[UNAME_FIELD_SIZE])
  52. {
  53. struct passwd *passwd;
  54. if (uid != 0 && uid == cached_no_such_uid)
  55. {
  56. *uname = '\0';
  57. return;
  58. }
  59. if (!cached_uname[0] || uid != cached_uid)
  60. {
  61. passwd = getpwuid (uid);
  62. if (passwd)
  63. {
  64. cached_uid = uid;
  65. strncpy (cached_uname, passwd->pw_name, UNAME_FIELD_SIZE);
  66. }
  67. else
  68. {
  69. cached_no_such_uid = uid;
  70. *uname = '\0';
  71. return;
  72. }
  73. }
  74. strncpy (uname, cached_uname, UNAME_FIELD_SIZE);
  75. }
  76. /*------------------------------------------.
  77. | Given GID, find the corresponding GNAME. |
  78. `------------------------------------------*/
  79. void
  80. gid_to_gname (gid_t gid, char gname[GNAME_FIELD_SIZE])
  81. {
  82. struct group *group;
  83. if (gid != 0 && gid == cached_no_such_gid)
  84. {
  85. *gname = '\0';
  86. return;
  87. }
  88. if (!cached_gname[0] || gid != cached_gid)
  89. {
  90. setgrent (); /* FIXME: why?! */
  91. group = getgrgid (gid);
  92. if (group)
  93. {
  94. cached_gid = gid;
  95. strncpy (cached_gname, group->gr_name, GNAME_FIELD_SIZE);
  96. }
  97. else
  98. {
  99. cached_no_such_gid = gid;
  100. *gname = '\0';
  101. return;
  102. }
  103. }
  104. strncpy (gname, cached_gname, GNAME_FIELD_SIZE);
  105. }
  106. /*-------------------------------------------------------------------------.
  107. | Given UNAME, set the corresponding UID and return 1, or else, return 0. |
  108. `-------------------------------------------------------------------------*/
  109. int
  110. uname_to_uid (char uname[UNAME_FIELD_SIZE], uid_t *uidp)
  111. {
  112. struct passwd *passwd;
  113. if (cached_no_such_uname[0]
  114. && strncmp (uname, cached_no_such_uname, UNAME_FIELD_SIZE) == 0)
  115. return 0;
  116. if (!cached_uname[0]
  117. || uname[0] != cached_uname[0]
  118. || strncmp (uname, cached_uname, UNAME_FIELD_SIZE) != 0)
  119. {
  120. passwd = getpwnam (uname);
  121. if (passwd)
  122. {
  123. cached_uid = passwd->pw_uid;
  124. strncpy (cached_uname, uname, UNAME_FIELD_SIZE);
  125. }
  126. else
  127. {
  128. strncpy (cached_no_such_uname, uname, UNAME_FIELD_SIZE);
  129. return 0;
  130. }
  131. }
  132. *uidp = cached_uid;
  133. return 1;
  134. }
  135. /*-------------------------------------------------------------------------.
  136. | Given GNAME, set the corresponding GID and return 1, or else, return 0. |
  137. `-------------------------------------------------------------------------*/
  138. int
  139. gname_to_gid (char gname[GNAME_FIELD_SIZE], gid_t *gidp)
  140. {
  141. struct group *group;
  142. if (cached_no_such_gname[0]
  143. && strncmp (gname, cached_no_such_gname, GNAME_FIELD_SIZE) == 0)
  144. return 0;
  145. if (!cached_gname[0]
  146. || gname[0] != cached_gname[0]
  147. || strncmp (gname, cached_gname, GNAME_FIELD_SIZE) != 0)
  148. {
  149. group = getgrnam (gname);
  150. if (group)
  151. {
  152. cached_gid = group->gr_gid;
  153. strncpy (cached_gname, gname, GNAME_FIELD_SIZE);
  154. }
  155. else
  156. {
  157. strncpy (cached_no_such_gname, gname, GNAME_FIELD_SIZE);
  158. return 0;
  159. }
  160. }
  161. *gidp = cached_gid;
  162. return 1;
  163. }
  164. /* Names from the command call. */
  165. static const char **name_array; /* store an array of names */
  166. static int allocated_names; /* how big is the array? */
  167. static int names; /* how many entries does it have? */
  168. static int name_index; /* how many of the entries have we scanned? */
  169. /*------------------------.
  170. | Initialize structures. |
  171. `------------------------*/
  172. void
  173. init_names (void)
  174. {
  175. allocated_names = 10;
  176. name_array = xmalloc (sizeof (const char *) * allocated_names);
  177. names = 0;
  178. }
  179. /*--------------------------------------------------------------.
  180. | Add NAME at end of name_array, reallocating it as necessary. |
  181. `--------------------------------------------------------------*/
  182. void
  183. name_add (const char *name)
  184. {
  185. if (names == allocated_names)
  186. {
  187. allocated_names *= 2;
  188. name_array =
  189. xrealloc (name_array, sizeof (const char *) * allocated_names);
  190. }
  191. name_array[names++] = name;
  192. }
  193. /* Names from external name file. */
  194. static FILE *name_file; /* file to read names from */
  195. static char *name_buffer; /* buffer to hold the current file name */
  196. static size_t name_buffer_length; /* allocated length of name_buffer */
  197. /*---.
  198. | ? |
  199. `---*/
  200. /* FIXME: I should better check more closely. It seems at first glance that
  201. is_pattern is only used when reading a file, and ignored for all
  202. command line arguments. */
  203. static inline int
  204. is_pattern (const char *string)
  205. {
  206. return strchr (string, '*') || strchr (string, '[') || strchr (string, '?');
  207. }
  208. /*-----------------------------------------------------------------------.
  209. | Set up to gather file names for tar. They can either come from a file |
  210. | or were saved from decoding arguments. |
  211. `-----------------------------------------------------------------------*/
  212. void
  213. name_init (int argc, char *const *argv)
  214. {
  215. name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
  216. name_buffer_length = NAME_FIELD_SIZE;
  217. if (files_from_option)
  218. {
  219. if (!strcmp (files_from_option, "-"))
  220. {
  221. request_stdin ("-T");
  222. name_file = stdin;
  223. }
  224. else if (name_file = fopen (files_from_option, "r"), !name_file)
  225. FATAL_ERROR ((0, errno, _("Cannot open file %s"), files_from_option));
  226. }
  227. }
  228. /*---.
  229. | ? |
  230. `---*/
  231. void
  232. name_term (void)
  233. {
  234. free (name_buffer);
  235. free (name_array);
  236. }
  237. /*---------------------------------------------------------------------.
  238. | Read the next filename from name_file and null-terminate it. Put it |
  239. | into name_buffer, reallocating and adjusting name_buffer_length if |
  240. | necessary. Return 0 at end of file, 1 otherwise. |
  241. `---------------------------------------------------------------------*/
  242. static int
  243. read_name_from_file (void)
  244. {
  245. int character;
  246. size_t counter = 0;
  247. /* FIXME: getc may be called even if character was EOF the last time here. */
  248. /* FIXME: This + 2 allocation might serve no purpose. */
  249. while (character = getc (name_file),
  250. character != EOF && character != filename_terminator)
  251. {
  252. if (counter == name_buffer_length)
  253. {
  254. name_buffer_length += NAME_FIELD_SIZE;
  255. name_buffer = xrealloc (name_buffer, name_buffer_length + 2);
  256. }
  257. name_buffer[counter++] = character;
  258. }
  259. if (counter == 0 && character == EOF)
  260. return 0;
  261. if (counter == name_buffer_length)
  262. {
  263. name_buffer_length += NAME_FIELD_SIZE;
  264. name_buffer = xrealloc (name_buffer, name_buffer_length + 2);
  265. }
  266. name_buffer[counter] = '\0';
  267. return 1;
  268. }
  269. /*------------------------------------------------------------------------.
  270. | Get the next name from ARGV or the file of names. Result is in static |
  271. | storage and can't be relied upon across two calls. |
  272. | |
  273. | If CHANGE_DIRS is true, treat a filename of the form "-C" as meaning |
  274. | that the next filename is the name of a directory to change to. If |
  275. | `filename_terminator' is NUL, CHANGE_DIRS is effectively always false. |
  276. `------------------------------------------------------------------------*/
  277. char *
  278. name_next (int change_dirs)
  279. {
  280. const char *source;
  281. char *cursor;
  282. int chdir_flag = 0;
  283. if (filename_terminator == '\0')
  284. change_dirs = 0;
  285. while (1)
  286. {
  287. /* Get a name, either from file or from saved arguments. */
  288. if (name_file)
  289. {
  290. if (!read_name_from_file ())
  291. break;
  292. }
  293. else
  294. {
  295. if (name_index == names)
  296. break;
  297. source = name_array[name_index++];
  298. if (strlen (source) > name_buffer_length)
  299. {
  300. free (name_buffer);
  301. name_buffer_length = strlen (source);
  302. name_buffer = xmalloc (name_buffer_length + 2);
  303. }
  304. strcpy (name_buffer, source);
  305. }
  306. /* Zap trailing slashes. */
  307. cursor = name_buffer + strlen (name_buffer) - 1;
  308. while (cursor > name_buffer && *cursor == '/')
  309. *cursor-- = '\0';
  310. if (chdir_flag)
  311. {
  312. if (chdir (name_buffer) < 0)
  313. FATAL_ERROR ((0, errno, _("Cannot change to directory %s"),
  314. name_buffer));
  315. chdir_flag = 0;
  316. }
  317. else if (change_dirs && strcmp (name_buffer, "-C") == 0)
  318. chdir_flag = 1;
  319. else
  320. {
  321. unquote_string (name_buffer);
  322. return name_buffer;
  323. }
  324. }
  325. /* No more names in file. */
  326. if (name_file && chdir_flag)
  327. FATAL_ERROR ((0, 0, _("Missing file name after -C")));
  328. return 0;
  329. }
  330. /*------------------------------.
  331. | Close the name file, if any. |
  332. `------------------------------*/
  333. void
  334. name_close (void)
  335. {
  336. if (name_file && name_file != stdin)
  337. if (fclose (name_file) == EOF)
  338. ERROR ((0, errno, "%s", name_buffer));
  339. }
  340. /*-------------------------------------------------------------------------.
  341. | Gather names in a list for scanning. Could hash them later if we really |
  342. | care. |
  343. | |
  344. | If the names are already sorted to match the archive, we just read them |
  345. | one by one. name_gather reads the first one, and it is called by |
  346. | name_match as appropriate to read the next ones. At EOF, the last name |
  347. | read is just left in the buffer. This option lets users of small |
  348. | machines extract an arbitrary number of files by doing "tar t" and |
  349. | editing down the list of files. |
  350. `-------------------------------------------------------------------------*/
  351. void
  352. name_gather (void)
  353. {
  354. /* Buffer able to hold a single name. */
  355. static struct name *buffer;
  356. static size_t allocated_length;
  357. char const *name;
  358. if (same_order_option)
  359. {
  360. static int change_dir;
  361. if (allocated_length == 0)
  362. {
  363. allocated_length = sizeof (struct name) + NAME_FIELD_SIZE;
  364. buffer = xmalloc (allocated_length);
  365. /* FIXME: This memset is overkill, and ugly... */
  366. memset (buffer, 0, allocated_length);
  367. }
  368. while ((name = name_next (0)) && strcmp (name, "-C") == 0)
  369. {
  370. char const *dir = name_next (0);
  371. if (! dir)
  372. FATAL_ERROR ((0, 0, _("Missing file name after -C")));
  373. change_dir = chdir_arg (xstrdup (dir));
  374. }
  375. if (name)
  376. {
  377. buffer->length = strlen (name);
  378. if (sizeof (struct name) + buffer->length >= allocated_length)
  379. {
  380. allocated_length = sizeof (struct name) + buffer->length;
  381. buffer = xrealloc (buffer, allocated_length);
  382. }
  383. buffer->change_dir = change_dir;
  384. strncpy (buffer->name, name, buffer->length);
  385. buffer->name[buffer->length] = 0;
  386. buffer->next = 0;
  387. buffer->found = 0;
  388. /* FIXME: Poorly named globals, indeed... */
  389. namelist = buffer;
  390. namelast = namelist;
  391. }
  392. }
  393. else
  394. {
  395. /* Non sorted names -- read them all in. */
  396. int change_dir = 0;
  397. for (;;)
  398. {
  399. int change_dir0 = change_dir;
  400. while ((name = name_next (0)) && strcmp (name, "-C") == 0)
  401. {
  402. char const *dir = name_next (0);
  403. if (! dir)
  404. FATAL_ERROR ((0, 0, _("Missing file name after -C")));
  405. change_dir = chdir_arg (xstrdup (dir));
  406. }
  407. if (name)
  408. addname (name, change_dir);
  409. else
  410. {
  411. if (change_dir != change_dir0)
  412. addname (0, change_dir);
  413. break;
  414. }
  415. }
  416. }
  417. }
  418. /*-----------------------------.
  419. | Add a name to the namelist. |
  420. `-----------------------------*/
  421. void
  422. addname (char const *string, int change_dir)
  423. {
  424. struct name *name;
  425. size_t length;
  426. length = string ? strlen (string) : 0;
  427. name = xmalloc (sizeof (struct name) + length);
  428. memset (name, 0, sizeof (struct name) + length);
  429. name->next = 0;
  430. if (string)
  431. {
  432. name->fake = 0;
  433. name->length = length;
  434. memcpy (name->name, string, length + 1);
  435. }
  436. else
  437. name->fake = 1;
  438. name->found = 0;
  439. name->regexp = 0; /* assume not a regular expression */
  440. name->firstch = 1; /* assume first char is literal */
  441. name->change_dir = change_dir;
  442. name->dir_contents = 0;
  443. if (string && is_pattern (string))
  444. {
  445. name->regexp = 1;
  446. if (string[0] == '*' || string[0] == '[' || string[0] == '?')
  447. name->firstch = 0;
  448. }
  449. if (namelast)
  450. namelast->next = name;
  451. namelast = name;
  452. if (!namelist)
  453. namelist = name;
  454. }
  455. /*------------------------------------------------------------------------.
  456. | Return true if and only if name PATH (from an archive) matches any name |
  457. | from the namelist. |
  458. `------------------------------------------------------------------------*/
  459. int
  460. name_match (const char *path)
  461. {
  462. size_t length = strlen (path);
  463. while (1)
  464. {
  465. struct name *cursor = namelist;
  466. if (!cursor)
  467. return ! files_from_option;
  468. if (cursor->fake)
  469. {
  470. chdir_do (cursor->change_dir);
  471. namelist = 0;
  472. return ! files_from_option;
  473. }
  474. for (; cursor; cursor = cursor->next)
  475. {
  476. /* If first chars don't match, quick skip. */
  477. if (cursor->firstch && cursor->name[0] != path[0])
  478. continue;
  479. if (cursor->regexp
  480. ? fnmatch (cursor->name, path, FNM_LEADING_DIR) == 0
  481. : (cursor->length <= length
  482. && (path[cursor->length] == '\0'
  483. || path[cursor->length] == '/')
  484. && memcmp (path, cursor->name, cursor->length) == 0))
  485. {
  486. cursor->found = 1; /* remember it matched */
  487. if (starting_file_option)
  488. {
  489. free (namelist);
  490. namelist = 0;
  491. }
  492. chdir_do (cursor->change_dir);
  493. /* We got a match. */
  494. return 1;
  495. }
  496. }
  497. /* Filename from archive not found in namelist. If we have the whole
  498. namelist here, just return 0. Otherwise, read the next name in and
  499. compare it. If this was the last name, namelist->found will remain
  500. on. If not, we loop to compare the newly read name. */
  501. if (same_order_option && namelist->found)
  502. {
  503. name_gather (); /* read one more */
  504. if (namelist->found)
  505. return 0;
  506. }
  507. else
  508. return 0;
  509. }
  510. }
  511. /*------------------------------------------------------------------.
  512. | Print the names of things in the namelist that were not matched. |
  513. `------------------------------------------------------------------*/
  514. void
  515. names_notfound (void)
  516. {
  517. struct name *cursor;
  518. struct name *next;
  519. for (cursor = namelist; cursor; cursor = next)
  520. {
  521. next = cursor->next;
  522. if (!cursor->found && !cursor->fake)
  523. ERROR ((0, 0, _("%s: Not found in archive"), cursor->name));
  524. /* We could free the list, but the process is about to die anyway, so
  525. save some CPU time. Amigas and other similarly broken software
  526. will need to waste the time, though. */
  527. #ifdef amiga
  528. if (!same_order_option)
  529. free (cursor);
  530. #endif
  531. }
  532. namelist = 0;
  533. namelast = 0;
  534. if (same_order_option)
  535. {
  536. char *name;
  537. while (name = name_next (1), name)
  538. ERROR ((0, 0, _("%s: Not found in archive"), name));
  539. }
  540. }
  541. /* Sorting name lists. */
  542. /* Sort linked LIST of names, of given LENGTH, using COMPARE to order
  543. names. Return the sorted list. Apart from the type `struct name'
  544. and the definition of SUCCESSOR, this is a generic list-sorting
  545. function, but it's too painful to make it both generic and portable
  546. in C. */
  547. static struct name *
  548. merge_sort (struct name *list, int length,
  549. int (*compare) (struct name const*, struct name const*))
  550. {
  551. struct name *first_list;
  552. struct name *second_list;
  553. int first_length;
  554. int second_length;
  555. struct name *result;
  556. struct name **merge_point;
  557. struct name *cursor;
  558. int counter;
  559. # define SUCCESSOR(name) ((name)->next)
  560. if (length == 1)
  561. return list;
  562. if (length == 2)
  563. {
  564. if ((*compare) (list, SUCCESSOR (list)) > 0)
  565. {
  566. result = SUCCESSOR (list);
  567. SUCCESSOR (result) = list;
  568. SUCCESSOR (list) = 0;
  569. return result;
  570. }
  571. return list;
  572. }
  573. first_list = list;
  574. first_length = (length + 1) / 2;
  575. second_length = length / 2;
  576. for (cursor = list, counter = first_length - 1;
  577. counter;
  578. cursor = SUCCESSOR (cursor), counter--)
  579. continue;
  580. second_list = SUCCESSOR (cursor);
  581. SUCCESSOR (cursor) = 0;
  582. first_list = merge_sort (first_list, first_length, compare);
  583. second_list = merge_sort (second_list, second_length, compare);
  584. merge_point = &result;
  585. while (first_list && second_list)
  586. if ((*compare) (first_list, second_list) < 0)
  587. {
  588. cursor = SUCCESSOR (first_list);
  589. *merge_point = first_list;
  590. merge_point = &SUCCESSOR (first_list);
  591. first_list = cursor;
  592. }
  593. else
  594. {
  595. cursor = SUCCESSOR (second_list);
  596. *merge_point = second_list;
  597. merge_point = &SUCCESSOR (second_list);
  598. second_list = cursor;
  599. }
  600. if (first_list)
  601. *merge_point = first_list;
  602. else
  603. *merge_point = second_list;
  604. return result;
  605. #undef SUCCESSOR
  606. }
  607. /* A comparison function for sorting names. Put found names last;
  608. break ties by string comparison. */
  609. static int
  610. compare_names (struct name const *n1, struct name const *n2)
  611. {
  612. int found_diff = n2->found - n1->found;
  613. return found_diff ? found_diff : strcmp (n1->name, n2->name);
  614. }
  615. /* Add all the dirs in PATH, which is a directory, to the namelist.
  616. If any of the files is a directory, recurse on the subdirectory.
  617. CHANGE_DIR is the number of the directory that PATH is relative to.
  618. DEVICE is the device not to leave, if the -l option is specified. */
  619. static void
  620. add_hierarchy_to_namelist (char *path, int change_dir, dev_t device)
  621. {
  622. char *buffer = get_directory_contents (path, device);
  623. {
  624. struct name *name;
  625. for (name = namelist; name; name = name->next)
  626. if (strcmp (name->name, path) == 0)
  627. break;
  628. if (name)
  629. name->dir_contents = buffer ? buffer : "\0\0\0\0";
  630. }
  631. if (buffer)
  632. {
  633. size_t name_length = strlen (path);
  634. size_t allocated_length = (name_length >= NAME_FIELD_SIZE
  635. ? name_length + NAME_FIELD_SIZE
  636. : NAME_FIELD_SIZE);
  637. char *name_buffer = xmalloc (allocated_length + 1);
  638. /* FIXME: + 2 above? */
  639. char *string;
  640. size_t string_length;
  641. strcpy (name_buffer, path);
  642. if (name_buffer[name_length - 1] != '/')
  643. {
  644. name_buffer[name_length++] = '/';
  645. name_buffer[name_length] = '\0';
  646. }
  647. for (string = buffer; *string; string += string_length + 1)
  648. {
  649. string_length = strlen (string);
  650. if (*string == 'D')
  651. {
  652. if (name_length + string_length >= allocated_length)
  653. {
  654. while (name_length + string_length >= allocated_length)
  655. allocated_length += NAME_FIELD_SIZE;
  656. name_buffer = xrealloc (name_buffer, allocated_length + 1);
  657. }
  658. strcpy (name_buffer + name_length, string + 1);
  659. addname (name_buffer, change_dir);
  660. if (*string == 'D')
  661. add_hierarchy_to_namelist (name_buffer, change_dir, device);
  662. }
  663. }
  664. free (name_buffer);
  665. }
  666. }
  667. /* Collect all the names from argv[] (or whatever), expand them into a
  668. directory tree, and sort them. This gets only subdirectories, not
  669. all files. */
  670. void
  671. collect_and_sort_names (void)
  672. {
  673. struct name *name;
  674. struct name *next_name;
  675. int num_names;
  676. struct stat statbuf;
  677. name_gather ();
  678. if (listed_incremental_option)
  679. read_directory_file ();
  680. if (!namelist)
  681. addname (".", 0);
  682. for (name = namelist; name; name = next_name)
  683. {
  684. next_name = name->next;
  685. if (name->found || name->dir_contents)
  686. continue;
  687. if (name->regexp) /* FIXME: just skip regexps for now */
  688. continue;
  689. chdir_do (name->change_dir);
  690. if (name->fake)
  691. continue;
  692. if (deref_stat (dereference_option, name->name, &statbuf) != 0)
  693. {
  694. ERROR ((0, errno, _("Cannot stat %s"), name->name));
  695. continue;
  696. }
  697. if (S_ISDIR (statbuf.st_mode))
  698. {
  699. name->found = 1;
  700. add_hierarchy_to_namelist (name->name, name->change_dir,
  701. statbuf.st_dev);
  702. }
  703. }
  704. num_names = 0;
  705. for (name = namelist; name; name = name->next)
  706. num_names++;
  707. namelist = merge_sort (namelist, num_names, compare_names);
  708. for (name = namelist; name; name = name->next)
  709. name->found = 0;
  710. }
  711. /*-------------------------------------------------------------------------.
  712. | This is like name_match, except that it returns a pointer to the name it |
  713. | matched, and doesn't set FOUND in structure. The caller will have to do |
  714. | that if it wants to. Oh, and if the namelist is empty, it returns null, |
  715. | unlike name_match, which returns TRUE. |
  716. `-------------------------------------------------------------------------*/
  717. struct name *
  718. name_scan (const char *path)
  719. {
  720. size_t length = strlen (path);
  721. while (1)
  722. {
  723. struct name *cursor = namelist;
  724. if (!cursor)
  725. return 0;
  726. for (; cursor; cursor = cursor->next)
  727. {
  728. /* If first chars don't match, quick skip. */
  729. if (cursor->firstch && cursor->name[0] != path[0])
  730. continue;
  731. if (cursor->regexp
  732. ? fnmatch (cursor->name, path, FNM_LEADING_DIR) == 0
  733. : (cursor->length <= length
  734. && (path[cursor->length] == '\0'
  735. || path[cursor->length] == '/')
  736. && memcmp (path, cursor->name, cursor->length) == 0))
  737. return cursor; /* we got a match */
  738. }
  739. /* Filename from archive not found in namelist. If we have the whole
  740. namelist here, just return 0. Otherwise, read the next name in and
  741. compare it. If this was the last name, namelist->found will remain
  742. on. If not, we loop to compare the newly read name. */
  743. if (same_order_option && namelist->found)
  744. {
  745. name_gather (); /* read one more */
  746. if (namelist->found)
  747. return 0;
  748. }
  749. else
  750. return 0;
  751. }
  752. }
  753. /*-----------------------------------------------------------------------.
  754. | This returns a name from the namelist which doesn't have ->found set. |
  755. | It sets ->found before returning, so successive calls will find and |
  756. | return all the non-found names in the namelist |
  757. `-----------------------------------------------------------------------*/
  758. struct name *gnu_list_name;
  759. char *
  760. name_from_list (void)
  761. {
  762. if (!gnu_list_name)
  763. gnu_list_name = namelist;
  764. while (gnu_list_name && (gnu_list_name->found | gnu_list_name->fake))
  765. gnu_list_name = gnu_list_name->next;
  766. if (gnu_list_name)
  767. {
  768. gnu_list_name->found = 1;
  769. chdir_do (gnu_list_name->change_dir);
  770. return gnu_list_name->name;
  771. }
  772. return 0;
  773. }
  774. /*---.
  775. | ? |
  776. `---*/
  777. void
  778. blank_name_list (void)
  779. {
  780. struct name *name;
  781. gnu_list_name = 0;
  782. for (name = namelist; name; name = name->next)
  783. name->found = 0;
  784. }
  785. /*---.
  786. | ? |
  787. `---*/
  788. char *
  789. new_name (const char *path, const char *name)
  790. {
  791. char *buffer = xmalloc (strlen (path) + strlen (name) + 2);
  792. sprintf (buffer, "%s/%s", path, name);
  793. return buffer;
  794. }
  795. /* Return nonzero if file NAME is excluded. Exclude a name if its
  796. prefix matches a pattern that contains slashes, or if one of its
  797. components matches a pattern that contains no slashes. */
  798. int
  799. excluded_name (char const *name)
  800. {
  801. char const *p;
  802. name += FILESYSTEM_PREFIX_LEN (name);
  803. if (excluded_filename (excluded_with_slash, name,
  804. FNM_FILE_NAME | FNM_LEADING_DIR))
  805. return 1;
  806. for (p = name; *p; p++)
  807. if ((p == name || (ISSLASH (p[-1]) && !ISSLASH (p[0])))
  808. && excluded_filename (excluded_without_slash, p,
  809. FNM_FILE_NAME | FNM_LEADING_DIR))
  810. return 1;
  811. return 0;
  812. }
  813. /* Names to avoid dumping. */
  814. struct avoided_name
  815. {
  816. struct avoided_name const *next;
  817. char name[1];
  818. };
  819. static struct avoided_name *avoided_names;
  820. /* Remember to not archive NAME. */
  821. void
  822. add_avoided_name (char const *name)
  823. {
  824. struct avoided_name *p = xmalloc (sizeof *p + strlen (name));
  825. p->next = avoided_names;
  826. avoided_names = p;
  827. strcpy (p->name, name);
  828. }
  829. /* Should NAME be avoided when archiving? */
  830. int
  831. is_avoided_name (char const *name)
  832. {
  833. struct avoided_name const *p;
  834. for (p = avoided_names; p; p = p->next)
  835. if (strcmp (p->name, name) == 0)
  836. return 1;
  837. return 0;
  838. }