home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / PAX20.ZIP / CPIO.C < prev    next >
C/C++ Source or Header  |  1990-11-12  |  6KB  |  268 lines

  1. /* $Source: /u/mark/src/pax/RCS/cpio.c,v $
  2.  *
  3.  * $Revision: 2.0.0.4 $
  4.  *
  5.  * cpio.c - Cpio specific functions for archive handling
  6.  *
  7.  * DESCRIPTION
  8.  *
  9.  *    These function provide a cpio conformant interface to the pax
  10.  *    program.
  11.  *
  12.  * AUTHOR
  13.  *
  14.  *     Mark H. Colburn, Open Systems Architects, Inc. (mark@minnetech.mn.org)
  15.  *
  16.  * COPYRIGHT
  17.  *
  18.  *    Copyright (c) 1989 Mark H. Colburn.  All rights reserved.
  19.  *
  20.  *    Redistribution and use in source and binary forms are permitted
  21.  *    provided that the above copyright notice and this paragraph are
  22.  *    duplicated in all such forms and that any documentation,
  23.  *    advertising materials, and other materials related to such
  24.  *    distribution and use acknowledge that the software was developed
  25.  *    by Mark H. Colburn.
  26.  *
  27.  *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  28.  *    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  29.  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  30.  *
  31.  * $Log:    cpio.c,v $
  32.  * Revision 2.0.0.4  89/10/13  02:34:31  mark
  33.  * Beta Test Freeze
  34.  *
  35.  * Revision 2.0.0.3  89/10/12  16:29:22  mark
  36.  */
  37.  
  38. #ifndef lint
  39. static char        *ident = "$Id: cpio.c,v 2.0.0.4 89/10/13 02:34:31 mark Exp Locker: mark $";
  40. static char        *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  41. #endif /* ! lint */
  42.  
  43.  
  44. /* Headers */
  45.  
  46. #include "pax.h"
  47.  
  48.  
  49. /* Function Prototypes */
  50.  
  51. #ifdef __STDC__
  52.  
  53. static void        usage(void);
  54.  
  55. #else /* !__STDC__ */
  56.  
  57. static void         usage();
  58.  
  59. #endif /* __STDC__ */
  60.  
  61.  
  62. /* do_cpio - handle cpio format archives
  63.  *
  64.  * DESCRIPTION
  65.  *
  66.  *    Do_cpio provides a standard CPIO interface to the PAX program.  All
  67.  *    of the standard cpio flags are available, and the behavior of the
  68.  *    program mimics traditonal cpio.
  69.  *
  70.  * PARAMETERS
  71.  *
  72.  *    int    argc    - command line argument count
  73.  *    char    **argv    - pointer to command line arguments
  74.  *
  75.  * RETURNS
  76.  *
  77.  *    Nothing.
  78.  */
  79.  
  80. #ifdef __STDC__
  81.  
  82. int
  83. do_cpio(int argc, char **argv)
  84.  
  85. #else
  86.  
  87. int
  88. do_cpio(argc, argv)
  89.     int                 argc;
  90.     char              **argv;
  91.  
  92. #endif
  93. {
  94.     int                 c;
  95.     char               *dirname;
  96.     Stat                st;
  97.  
  98.     DBUG_ENTER("do_cpio");
  99.     /* default input/output file for CPIO is STDIN/STDOUT */
  100.     ar_file = "-";
  101.     names_from_stdin = 1;
  102.  
  103.     /* set up the flags to reflect the default CPIO inteface. */
  104.     blocksize = BLOCKSIZE;
  105.     ar_interface = CPIO;
  106.     ar_format = CPIO;
  107.     msgfile = stderr;
  108.  
  109.     while ((c = getopt(argc, argv, "#:D:Bacdfilmoprtuv")) != EOF) {
  110.     switch (c) {
  111.  
  112.     case '#':
  113.         DBUG_PUSH(optarg);
  114.         break;
  115.  
  116.     case 'i':
  117.         f_extract = 1;
  118.         break;
  119.  
  120.     case 'o':
  121.         f_create = 1;
  122.         f_mtime = 1;    /* automatically save mtime on write */
  123.         break;
  124.  
  125.     case 'p':
  126.         f_pass = 1;
  127.         dirname = argv[--argc];
  128.  
  129.         /* check to make sure that the argument is a directory */
  130.         if (LSTAT(dirname, &st) < 0) {
  131.         fatal(strerror());
  132.         }
  133.         if ((st.sb_mode & S_IFMT) != S_IFDIR) {
  134.         fatal("Not a directory");
  135.         }
  136.         break;
  137.  
  138.     case 'B':
  139.         blocksize = BLOCK;
  140.         break;
  141.  
  142.     case 'a':
  143.         f_access_time = 1;
  144.         break;
  145.  
  146.     case 'c':
  147.         break;
  148.  
  149.     case 'D':
  150.         ar_file = optarg;
  151.         break;
  152.  
  153.     case 'd':
  154.         f_dir_create = 1;
  155.         break;
  156.  
  157.     case 'f':
  158.         f_reverse_match = 1;
  159.         break;
  160.  
  161.     case 'l':
  162.         f_link = 1;
  163.         break;
  164.  
  165.     case 'm':
  166.         f_mtime = 1;
  167.         break;
  168.  
  169.     case 'r':
  170.         f_interactive = 1;
  171.         break;
  172.  
  173.     case 't':
  174.         f_list = 1;
  175.         break;
  176.  
  177.     case 'u':
  178.         f_unconditional = 1;
  179.         break;
  180.  
  181.     case 'v':
  182.         f_verbose = 1;
  183.         break;
  184.  
  185.     default:
  186.         usage();
  187.     }
  188.     }
  189.  
  190. #ifdef MSDOS
  191.     setmode(fileno(msgfile), O_TEXT);
  192.     if (names_from_stdin) {
  193.         setmode(fileno(stdin), O_TEXT);
  194.     }
  195. #endif /* MSDOS */
  196.  
  197.     if (f_create + f_pass + f_extract != 1) {
  198.     usage();
  199.     }
  200.     if (!f_pass) {
  201.     buf_allocate((OFFSET) blocksize);
  202.     }
  203.     if (f_extract) {
  204.     open_archive(AR_READ);    /* Open for reading */
  205.     read_archive();
  206.     } else if (f_create) {
  207.     open_archive(AR_WRITE);
  208.     create_archive();
  209.     } else if (f_pass) {
  210.     pass(dirname);
  211.     }
  212.     /* print out the total block count transfered */
  213.     fprintf(stderr, "%ld Blocks\n", ROUNDUP(total, BLOCKSIZE) / BLOCKSIZE);
  214.  
  215.     exit(0);
  216.     /* NOTREACHED */
  217. }
  218.  
  219.  
  220. /* usage - print a helpful message and exit
  221.  *
  222.  * DESCRIPTION
  223.  *
  224.  *    Usage prints out the usage message for the CPIO interface and then
  225.  *    exits with a non-zero termination status.  This is used when a user
  226.  *    has provided non-existant or incompatible command line arguments.
  227.  *
  228.  * RETURNS
  229.  *
  230.  *    Returns an exit status of 1 to the parent process.
  231.  *
  232.  */
  233.  
  234. #ifdef __STDC__
  235.  
  236. static void
  237. usage(void)
  238.  
  239. #else
  240.  
  241. static void
  242. usage()
  243.  
  244. #endif
  245. {
  246.     DBUG_ENTER("usage");
  247. #ifdef MSDOS
  248.     printf("\r\nPAX version 2.0 - POSIX conforming tar and cpio archiver for DOS and OS/2\r\n");
  249.  
  250.     printf("\r\nUsage: %s -o[Bacv]\r\n", myname);
  251.     printf("       %s -i[Bcdmrtuvf] [pattern...]\r\n", myname);
  252.     printf("       %s -p[adlmruv] directory\r\n", myname);
  253.  
  254.     printf("\r\nUse the nonstandard option \"-D file\" for files as input/output archives."
  255.            "\r\nRename CPIO.EXE to PAX.EXE or TAR.EXE to get the PAX or TAR user interface.\r\n");
  256.  
  257. #ifdef DISKACC
  258.     printf("\r\nUse the option -D with the special device names 0: and 1: to access Unix floppy"
  259.            "\r\ndisks with cpio archives. The disk type is automatically detected.\r\n");
  260. #endif
  261. #else
  262.     fprintf(stderr, "Usage: %s -o[Bacv]\n", myname);
  263.     fprintf(stderr, "       %s -i[Bcdmrtuvf] [pattern...]\n", myname);
  264.     fprintf(stderr, "       %s -p[adlmruv] directory\n", myname);
  265. #endif
  266.     exit(1);
  267. }
  268.