write.c

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.0 kB
3
Indexable
Never
  1 #include "write.h"
  2 #include <fcntl.h>
  3 /*this function verifies a header, exits on failure, void on success*/
  4 /*DIFFERENT CHECKS BASED ON -S OPTION*/
  5 void verify_header(header *curr_header) {
  6     if (strcmp(curr_header->version, "00") != 0) {
  7         perror("Wrong version");
  8         exit(EXIT_FAILURE);
  9     }
 10     if (strcmp(curr_header->magic, "ustar") != 0) {
 11         perror("Wrong magic number");
 12         exit(EXIT_FAILURE);
 13     }
 14     /*ADD CHKSUM CHECK*/
 15 }
 16
 17 /*this function writes a header to a tar file*/
 18 /*write two null blocks*/
 19 void write_header(int tar_fd, header *curr_header) {
 20     verify_header(curr_header);
 21     printf("Write name %s\n", curr_header->name);
 22     printf("Write gname %s\n", curr_header->gname);
 23     printf("Write uname %s\n", curr_header->uname);
 24     write_field(tar_fd, curr_header->name, NAME_SIZE);
"write.c" 74L, 2500C
 44     write_field(tar_fd, curr_header->uname, UNAME_SIZE);
 45     write_field(tar_fd, curr_header->gname, GNAME_SIZE);
 46     write_field(tar_fd, curr_header->devmajor, DEVMAJOR_SIZE);
 47     write_field(tar_fd, curr_header->devminor, DEVMINOR_SIZE);
 48     write_field(tar_fd, curr_header->prefix, PREFIX_SIZE);
 49 }
 50
 51 /*this function writes a single field of a header to a tar file and frees it    */
 52 void write_field(int fd, char *field, unsigned field_size) {
 53     if (field == NULL) {
 54         char buffer[field_size];
 55         int i;
 56         for (i = 0; i < field_size; i++) {
 57             buffer[i] = '\0';
 58         }
 59         if (write(fd, buffer, field_size) == -1) {
 60             perror("Buffer Write Failure");
 61             exit(EXIT_FAILURE);
 62         }
 63         return;
 64     }
 65     printf("field: %s\n", field);
 66     if (write(fd, field, field_size) == -1) {
 67         perror("Write Failure");
 68         exit(EXIT_FAILURE);
 69     }
 70 }
                                                              70,1          92%
Leave a Comment