acpyacc.y (d20e9e02db3dde383c3de1ce8cec3a8c35b3eee6) acpyacc.y (c5bf58add0d523fc6e6546f3fc10d0c8b972e8e7)
1%{
2/*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 2008 Kai Wang
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 28 unchanged lines hidden (view full) ---

37#include <archive.h>
38#include <archive_entry.h>
39#include <dirent.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
1%{
2/*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 2008 Kai Wang
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 28 unchanged lines hidden (view full) ---

37#include <archive.h>
38#include <archive_entry.h>
39#include <dirent.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <sysexits.h>
45#include <unistd.h>
46
47#include "ar.h"
48
49#define TEMPLATE "arscp.XXXXXXXX"
50
51struct list {
52 char *str;

--- 191 unchanged lines hidden (view full) ---

244static void
245arscp_open(char *fname)
246{
247 struct archive *a;
248 struct archive_entry *entry;
249 int r;
250
251 if ((a = archive_read_new()) == NULL)
46#include <unistd.h>
47
48#include "ar.h"
49
50#define TEMPLATE "arscp.XXXXXXXX"
51
52struct list {
53 char *str;

--- 191 unchanged lines hidden (view full) ---

245static void
246arscp_open(char *fname)
247{
248 struct archive *a;
249 struct archive_entry *entry;
250 int r;
251
252 if ((a = archive_read_new()) == NULL)
252 bsdar_errc(bsdar, 0, "archive_read_new failed");
253 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
253 archive_read_support_format_ar(a);
254 AC(archive_read_open_filename(a, fname, DEF_BLKSZ));
255 if ((r = archive_read_next_header(a, &entry)))
256 bsdar_warnc(bsdar, archive_errno(a), "%s",
257 archive_error_string(a));
258 AC(archive_read_close(a));
259 AC(archive_read_free(a));
260 if (r != ARCHIVE_OK)

--- 10 unchanged lines hidden (view full) ---

271arscp_create(char *in, char *out)
272{
273 struct archive *a;
274 int ifd, ofd;
275
276 /* Delete previously created temporary archive, if any. */
277 if (tmpac) {
278 if (unlink(tmpac) < 0)
254 archive_read_support_format_ar(a);
255 AC(archive_read_open_filename(a, fname, DEF_BLKSZ));
256 if ((r = archive_read_next_header(a, &entry)))
257 bsdar_warnc(bsdar, archive_errno(a), "%s",
258 archive_error_string(a));
259 AC(archive_read_close(a));
260 AC(archive_read_free(a));
261 if (r != ARCHIVE_OK)

--- 10 unchanged lines hidden (view full) ---

272arscp_create(char *in, char *out)
273{
274 struct archive *a;
275 int ifd, ofd;
276
277 /* Delete previously created temporary archive, if any. */
278 if (tmpac) {
279 if (unlink(tmpac) < 0)
279 bsdar_errc(bsdar, errno, "unlink failed");
280 bsdar_errc(bsdar, EX_IOERR, errno, "unlink failed");
280 free(tmpac);
281 }
282
283 tmpac = strdup(TEMPLATE);
284 if (tmpac == NULL)
281 free(tmpac);
282 }
283
284 tmpac = strdup(TEMPLATE);
285 if (tmpac == NULL)
285 bsdar_errc(bsdar, errno, "strdup failed");
286 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
286 if ((ofd = mkstemp(tmpac)) < 0)
287 if ((ofd = mkstemp(tmpac)) < 0)
287 bsdar_errc(bsdar, errno, "mkstemp failed");
288 bsdar_errc(bsdar, EX_IOERR, errno, "mkstemp failed");
288
289 if (in) {
290 /*
291 * Command OPEN creates a temporary copy of the
292 * input archive.
293 */
294 if ((ifd = open(in, O_RDONLY)) < 0) {
295 bsdar_warnc(bsdar, errno, "open failed");

--- 6 unchanged lines hidden (view full) ---

302 close(ifd);
303 close(ofd);
304 } else {
305 /*
306 * Command CREATE creates an "empty" archive.
307 * (archive with only global header)
308 */
309 if ((a = archive_write_new()) == NULL)
289
290 if (in) {
291 /*
292 * Command OPEN creates a temporary copy of the
293 * input archive.
294 */
295 if ((ifd = open(in, O_RDONLY)) < 0) {
296 bsdar_warnc(bsdar, errno, "open failed");

--- 6 unchanged lines hidden (view full) ---

303 close(ifd);
304 close(ofd);
305 } else {
306 /*
307 * Command CREATE creates an "empty" archive.
308 * (archive with only global header)
309 */
310 if ((a = archive_write_new()) == NULL)
310 bsdar_errc(bsdar, 0, "archive_write_new failed");
311 bsdar_errc(bsdar, EX_SOFTWARE, 0,
312 "archive_write_new failed");
311 archive_write_set_format_ar_svr4(a);
312 AC(archive_write_open_fd(a, ofd));
313 AC(archive_write_close(a));
314 AC(archive_write_free(a));
315 }
316
317 /* Override previous target, if any. */
318 if (target)

--- 24 unchanged lines hidden (view full) ---

343 for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) {
344 w = write(ofd, buf, bytes);
345 if (w <= 0) {
346 bsdar_warnc(bsdar, errno, "write failed");
347 break;
348 }
349 }
350 if (munmap(p, sb.st_size) < 0)
313 archive_write_set_format_ar_svr4(a);
314 AC(archive_write_open_fd(a, ofd));
315 AC(archive_write_close(a));
316 AC(archive_write_free(a));
317 }
318
319 /* Override previous target, if any. */
320 if (target)

--- 24 unchanged lines hidden (view full) ---

345 for (buf = p, bytes = sb.st_size; bytes > 0; bytes -= w) {
346 w = write(ofd, buf, bytes);
347 if (w <= 0) {
348 bsdar_warnc(bsdar, errno, "write failed");
349 break;
350 }
351 }
352 if (munmap(p, sb.st_size) < 0)
351 bsdar_errc(bsdar, errno, "munmap failed");
353 bsdar_errc(bsdar, EX_SOFTWARE, errno, "munmap failed");
352 if (bytes > 0)
353 return (1);
354
355 return (0);
356}
357
358/*
359 * Add all modules of archive to current archive, if list != NULL,
360 * only those modules specified in 'list' will be added.
361 */
362static void
363arscp_addlib(char *archive, struct list *list)
364{
365
366 if (!arscp_target_exist())
367 return;
368 arscp_mlist2argv(list);
369 bsdar->addlib = archive;
354 if (bytes > 0)
355 return (1);
356
357 return (0);
358}
359
360/*
361 * Add all modules of archive to current archive, if list != NULL,
362 * only those modules specified in 'list' will be added.
363 */
364static void
365arscp_addlib(char *archive, struct list *list)
366{
367
368 if (!arscp_target_exist())
369 return;
370 arscp_mlist2argv(list);
371 bsdar->addlib = archive;
370 ar_write_archive(bsdar, 'A');
372 ar_mode_A(bsdar);
371 arscp_free_argv();
372 arscp_free_mlist(list);
373}
374
375/* Add modules into current archive. */
376static void
377arscp_addmod(struct list *list)
378{
379
380 if (!arscp_target_exist())
381 return;
382 arscp_mlist2argv(list);
373 arscp_free_argv();
374 arscp_free_mlist(list);
375}
376
377/* Add modules into current archive. */
378static void
379arscp_addmod(struct list *list)
380{
381
382 if (!arscp_target_exist())
383 return;
384 arscp_mlist2argv(list);
383 ar_write_archive(bsdar, 'q');
385 ar_mode_q(bsdar);
384 arscp_free_argv();
385 arscp_free_mlist(list);
386}
387
388/* Delete modules from current archive. */
389static void
390arscp_delete(struct list *list)
391{
392
393 if (!arscp_target_exist())
394 return;
395 arscp_mlist2argv(list);
386 arscp_free_argv();
387 arscp_free_mlist(list);
388}
389
390/* Delete modules from current archive. */
391static void
392arscp_delete(struct list *list)
393{
394
395 if (!arscp_target_exist())
396 return;
397 arscp_mlist2argv(list);
396 ar_write_archive(bsdar, 'd');
398 ar_mode_d(bsdar);
397 arscp_free_argv();
398 arscp_free_mlist(list);
399}
400
401/* Extract modules from current archive. */
402static void
403arscp_extract(struct list *list)
404{
405
406 if (!arscp_target_exist())
407 return;
408 arscp_mlist2argv(list);
399 arscp_free_argv();
400 arscp_free_mlist(list);
401}
402
403/* Extract modules from current archive. */
404static void
405arscp_extract(struct list *list)
406{
407
408 if (!arscp_target_exist())
409 return;
410 arscp_mlist2argv(list);
409 ar_read_archive(bsdar, 'x');
411 ar_mode_x(bsdar);
410 arscp_free_argv();
411 arscp_free_mlist(list);
412}
413
414/* List modules of archive. (Simple Mode) */
415static void
416arscp_list(void)
417{
418
419 if (!arscp_target_exist())
420 return;
421 bsdar->argc = 0;
422 bsdar->argv = NULL;
423 /* Always verbose. */
424 bsdar->options |= AR_V;
412 arscp_free_argv();
413 arscp_free_mlist(list);
414}
415
416/* List modules of archive. (Simple Mode) */
417static void
418arscp_list(void)
419{
420
421 if (!arscp_target_exist())
422 return;
423 bsdar->argc = 0;
424 bsdar->argv = NULL;
425 /* Always verbose. */
426 bsdar->options |= AR_V;
425 ar_read_archive(bsdar, 't');
427 ar_mode_t(bsdar);
426 bsdar->options &= ~AR_V;
427}
428
429/* List modules of archive. (Advance Mode) */
430static void
431arscp_dir(char *archive, struct list *list, char *rlt)
432{
433 FILE *out;
434
435 /* If rlt != NULL, redirect output to it */
436 out = NULL;
437 if (rlt) {
438 out = stdout;
439 if ((stdout = fopen(rlt, "w")) == NULL)
428 bsdar->options &= ~AR_V;
429}
430
431/* List modules of archive. (Advance Mode) */
432static void
433arscp_dir(char *archive, struct list *list, char *rlt)
434{
435 FILE *out;
436
437 /* If rlt != NULL, redirect output to it */
438 out = NULL;
439 if (rlt) {
440 out = stdout;
441 if ((stdout = fopen(rlt, "w")) == NULL)
440 bsdar_errc(bsdar, errno, "fopen %s failed", rlt);
442 bsdar_errc(bsdar, EX_IOERR, errno,
443 "fopen %s failed", rlt);
441 }
442
443 bsdar->filename = archive;
444 if (list)
445 arscp_mlist2argv(list);
446 else {
447 bsdar->argc = 0;
448 bsdar->argv = NULL;
449 }
450 if (verbose)
451 bsdar->options |= AR_V;
444 }
445
446 bsdar->filename = archive;
447 if (list)
448 arscp_mlist2argv(list);
449 else {
450 bsdar->argc = 0;
451 bsdar->argv = NULL;
452 }
453 if (verbose)
454 bsdar->options |= AR_V;
452 ar_read_archive(bsdar, 't');
455 ar_mode_t(bsdar);
453 bsdar->options &= ~AR_V;
454
455 if (rlt) {
456 if (fclose(stdout) == EOF)
456 bsdar->options &= ~AR_V;
457
458 if (rlt) {
459 if (fclose(stdout) == EOF)
457 bsdar_errc(bsdar, errno, "fclose %s failed", rlt);
460 bsdar_errc(bsdar, EX_IOERR, errno,
461 "fclose %s failed", rlt);
458 stdout = out;
459 free(rlt);
460 }
461 free(archive);
462 bsdar->filename = tmpac;
463 arscp_free_argv();
464 arscp_free_mlist(list);
465}
466
467
468/* Replace modules of current archive. */
469static void
470arscp_replace(struct list *list)
471{
472
473 if (!arscp_target_exist())
474 return;
475 arscp_mlist2argv(list);
462 stdout = out;
463 free(rlt);
464 }
465 free(archive);
466 bsdar->filename = tmpac;
467 arscp_free_argv();
468 arscp_free_mlist(list);
469}
470
471
472/* Replace modules of current archive. */
473static void
474arscp_replace(struct list *list)
475{
476
477 if (!arscp_target_exist())
478 return;
479 arscp_mlist2argv(list);
476 ar_write_archive(bsdar, 'r');
480 ar_mode_r(bsdar);
477 arscp_free_argv();
478 arscp_free_mlist(list);
479}
480
481/* Rename the temporary archive to the target archive. */
482static void
483arscp_save(void)
484{
485 mode_t mask;
486
487 if (target) {
488 if (rename(tmpac, target) < 0)
481 arscp_free_argv();
482 arscp_free_mlist(list);
483}
484
485/* Rename the temporary archive to the target archive. */
486static void
487arscp_save(void)
488{
489 mode_t mask;
490
491 if (target) {
492 if (rename(tmpac, target) < 0)
489 bsdar_errc(bsdar, errno, "rename failed");
493 bsdar_errc(bsdar, EX_IOERR, errno, "rename failed");
490 /*
491 * mkstemp creates temp files with mode 0600, here we
492 * set target archive mode per process umask.
493 */
494 mask = umask(0);
495 umask(mask);
496 if (chmod(target, 0666 & ~mask) < 0)
494 /*
495 * mkstemp creates temp files with mode 0600, here we
496 * set target archive mode per process umask.
497 */
498 mask = umask(0);
499 umask(mask);
500 if (chmod(target, 0666 & ~mask) < 0)
497 bsdar_errc(bsdar, errno, "chmod failed");
501 bsdar_errc(bsdar, EX_IOERR, errno, "chmod failed");
498 free(tmpac);
499 free(target);
500 tmpac = NULL;
501 target= NULL;
502 bsdar->filename = NULL;
503 } else
504 bsdar_warnc(bsdar, 0, "no open output archive");
505}

--- 5 unchanged lines hidden (view full) ---

511static void
512arscp_clear(void)
513{
514 char *new_target;
515
516 if (target) {
517 new_target = strdup(target);
518 if (new_target == NULL)
502 free(tmpac);
503 free(target);
504 tmpac = NULL;
505 target= NULL;
506 bsdar->filename = NULL;
507 } else
508 bsdar_warnc(bsdar, 0, "no open output archive");
509}

--- 5 unchanged lines hidden (view full) ---

515static void
516arscp_clear(void)
517{
518 char *new_target;
519
520 if (target) {
521 new_target = strdup(target);
522 if (new_target == NULL)
519 bsdar_errc(bsdar, errno, "strdup failed");
523 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
520 arscp_create(NULL, new_target);
521 }
522}
523
524/*
525 * Quit ar(1). Note that END cmd will not SAVE current archive
526 * before exit.
527 */
528static void
529arscp_end(int eval)
530{
531
532 if (target)
533 free(target);
534 if (tmpac) {
535 if (unlink(tmpac) == -1)
524 arscp_create(NULL, new_target);
525 }
526}
527
528/*
529 * Quit ar(1). Note that END cmd will not SAVE current archive
530 * before exit.
531 */
532static void
533arscp_end(int eval)
534{
535
536 if (target)
537 free(target);
538 if (tmpac) {
539 if (unlink(tmpac) == -1)
536 bsdar_errc(bsdar, errno, "unlink %s failed",
540 bsdar_errc(bsdar, EX_IOERR, errno, "unlink %s failed",
537 tmpac);
538 free(tmpac);
539 }
540
541 exit(eval);
542}
543
544/*

--- 14 unchanged lines hidden (view full) ---

559/* Construct module list. */
560static struct list *
561arscp_mlist(struct list *list, char *str)
562{
563 struct list *l;
564
565 l = malloc(sizeof(*l));
566 if (l == NULL)
541 tmpac);
542 free(tmpac);
543 }
544
545 exit(eval);
546}
547
548/*

--- 14 unchanged lines hidden (view full) ---

563/* Construct module list. */
564static struct list *
565arscp_mlist(struct list *list, char *str)
566{
567 struct list *l;
568
569 l = malloc(sizeof(*l));
570 if (l == NULL)
567 bsdar_errc(bsdar, errno, "malloc failed");
571 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
568 l->str = str;
569 l->next = list;
570
571 return (l);
572}
573
574/* Calculate the length of a mlist. */
575static int

--- 25 unchanged lines hidden (view full) ---

601arscp_mlist2argv(struct list *list)
602{
603 char **argv;
604 int i, n;
605
606 n = arscp_mlist_len(list);
607 argv = malloc(n * sizeof(*argv));
608 if (argv == NULL)
572 l->str = str;
573 l->next = list;
574
575 return (l);
576}
577
578/* Calculate the length of a mlist. */
579static int

--- 25 unchanged lines hidden (view full) ---

605arscp_mlist2argv(struct list *list)
606{
607 char **argv;
608 int i, n;
609
610 n = arscp_mlist_len(list);
611 argv = malloc(n * sizeof(*argv));
612 if (argv == NULL)
609 bsdar_errc(bsdar, errno, "malloc failed");
613 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
610
611 /* Note that module names are stored in reverse order in mlist. */
612 for(i = n - 1; i >= 0; i--, list = list->next) {
613 if (list == NULL)
614
615 /* Note that module names are stored in reverse order in mlist. */
616 for(i = n - 1; i >= 0; i--, list = list->next) {
617 if (list == NULL)
614 bsdar_errc(bsdar, errno, "invalid mlist");
618 bsdar_errc(bsdar, EX_SOFTWARE, errno, "invalid mlist");
615 argv[i] = list->str;
616 }
617
618 bsdar->argc = n;
619 bsdar->argv = argv;
620}
621
622/* Free space allocated for argv array and its elements. */

--- 37 unchanged lines hidden ---
619 argv[i] = list->str;
620 }
621
622 bsdar->argc = n;
623 bsdar->argv = argv;
624}
625
626/* Free space allocated for argv array and its elements. */

--- 37 unchanged lines hidden ---