76 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <stdio.h>
 | |
| #include <stdlib.h>
 | |
| #include <stdint.h>
 | |
| #include <fcntl.h> // creat() and co.
 | |
| #include <sys/stat.h> // S_IRUSR
 | |
| #include <unistd.h>
 | |
| #include <errno.h>
 | |
| #include <string.h>
 | |
| 
 | |
| #define USAGE "Usage: %s output input\n", argv[0]
 | |
| #define PERMS S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
 | |
| 
 | |
| const uint64_t magic = 0x25524573; // sER%
 | |
| 
 | |
| off_t getfilesize(const char* filename);
 | |
| int fileexists(const char* filename);
 | |
| 
 | |
| int main(int argc, char** argv) {
 | |
|   switch(argc) {
 | |
|     case 1:
 | |
|       printf("Missing output file\n");
 | |
|     case 2:
 | |
|       printf("Missing input file/s\n\n");
 | |
|       goto usage;
 | |
|       break;
 | |
|   }
 | |
| 
 | |
|   int i, namesize;
 | |
|   int outfd, infd;
 | |
|   uint32_t nfiles = (uint32_t)argc - 2;
 | |
| 
 | |
|   for(namesize = 0; i < nfiles; i++) {
 | |
|     // Make sure file exists before writing.
 | |
|     if(!fileexists(argv[i+2])) {
 | |
|       printf("File %s does not exist!\n", argv[i+2]);
 | |
|       goto failure;
 | |
|     }
 | |
|     namesize += strlen(argv[i+2]);
 | |
|   }
 | |
| 
 | |
|   // Create the output file.
 | |
|   outfd = creat(argv[1], PERMS);
 | |
|   if(outfd == -1) goto failure;
 | |
| 
 | |
|   // Magic number.
 | |
|   write(outfd, &magic, sizeof(magic));
 | |
| 
 | |
|   close(outfd);
 | |
| 
 | |
|   exit(EXIT_SUCCESS);
 | |
| 
 | |
| usage:
 | |
|   printf(USAGE);
 | |
| failure:
 | |
|   exit(EXIT_FAILURE);
 | |
| }
 | |
| 
 | |
| // Grab file size.
 | |
| off_t getfilesize(const char* filename) {
 | |
|   struct stat file;
 | |
|   if(!stat(filename, &file))
 | |
|     return file.st_size;
 | |
| 
 | |
|   printf("Unable to get filesize of %s\n", filename);
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| // Does the file exist?
 | |
| int fileexists(const char* filename) {
 | |
|   struct stat file;
 | |
|   if(!stat(filename, &file))
 | |
|     return 1;
 | |
|   return 0;
 | |
| }
 | |
| 
 | 
