home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / portar / arwrite.c next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  1.9 KB  |  92 lines

  1. /*
  2.  *        COPYRIGHT (c) HEWLETT-PACKARD COMPANY, 1984
  3.  *
  4.  *    Permission is granted for unlimited modification, use, and
  5.  *    distribution except that this software may not be sold for
  6.  *    profit.  No warranty is implied or expressed.
  7.  *
  8.  *Author:
  9.  *    Paul Bame, HEWLETT-PACKARD LOGIC SYSTEMS DIVISION - Colorado Springs
  10.  *    { ihnp4!hpfcla | hplabs | harpo!hp-pcd }!hp-lsd!paul
  11.  */
  12. #include <stdio.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15.  
  16. #define ARFMAG "`\n"
  17. #define ARMAG "!<arch>\n"
  18.  
  19. int
  20. arwrite(filename,stream)
  21. char *filename;
  22. FILE *stream;
  23. /*
  24.  *    Write the file denoted by 'filename' to 'stream' in portable ar format.
  25.  *    Any stream positioning is assumed to already be done.
  26.  *
  27.  *    arwrite() normally returns 0 but returns -1 in case of errors.  errno
  28.  *    may be inspected to determine the cause of the error.
  29.  *
  30.  *    If necessary, the stream is padded with \n's to match the even boundary
  31.  *    condition.
  32.  */
  33. {
  34.     struct stat statbuf;
  35.     FILE *instream;
  36.     long i;
  37.  
  38.     /* stat() the file to get the stuff for the header */
  39.     if( stat(filename,&statbuf) < 0 )
  40.     {
  41.         /* error! */
  42.         return(-1);
  43.     }
  44.  
  45.     /* Open file for reading */
  46.     if( (instream = fopen(filename,"r")) == NULL )
  47.     {
  48.         /* error! */
  49.         return(-1);
  50.     }
  51.  
  52.     /* Now write the header */
  53.     /* This information gleaned from ar(4) in V.2 */
  54.     fprintf(stream,
  55.         "%-16s%-12ld%-6d%-6d%-8o%-10ld%2s",
  56.         filename,
  57.         statbuf.st_mtime,
  58.         statbuf.st_uid,
  59.         statbuf.st_gid,
  60.         statbuf.st_mode,
  61.         statbuf.st_size,
  62.         ARFMAG
  63.         );
  64.  
  65.     /* And copy the file */
  66.     /*   Note that there is no error recovery here! */
  67.     for( i = 0 ; i < statbuf.st_size ; i++ )
  68.     {
  69.         fputc(fgetc(instream),stream);
  70.     }
  71.  
  72.     /* and close */
  73.     close(instream);
  74.  
  75.     /* and pad output stream */
  76.     if( (ftell(stream) & 0x1) != 0 )
  77.     {
  78.         /* if offset is odd, pad with a nuline */
  79.         fputc('\n',stream);
  80.     }
  81.     return(0);
  82. }
  83.  
  84. arhdwrite(stream)
  85. FILE *stream;
  86. /*
  87.  *    Write the archive header onto 'stream'
  88.  */
  89. {
  90.     fprintf(stream,"%s",ARMAG);
  91. }
  92.