dup-keygen.c (918B)
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 4 #include <err.h> 5 #include <fcntl.h> 6 #include <stdint.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 11 #include "arg.h" 12 #include "config.h" 13 #include "key.h" 14 #include "misc.h" 15 #include "state.h" 16 17 struct param param; /* unused but needed for linking */ 18 int verbose; 19 char *argv0; 20 21 static void 22 usage(void) 23 { 24 fprintf(stderr, "usage: %s [-v] keyfile\n", argv0); 25 exit(1); 26 } 27 28 int 29 main(int argc, char *argv[]) 30 { 31 unsigned char key[KEYSIZE]; 32 int fd; 33 34 ARGBEGIN { 35 case 'v': 36 verbose++; 37 break; 38 default: 39 usage(); 40 } ARGEND 41 42 if (argc != 1) 43 usage(); 44 45 fd = open(argv[0], O_RDWR | O_CREAT | O_EXCL, 0600); 46 if (fd < 0) 47 err(1, "open: %s", argv[0]); 48 if (keygen(key, sizeof(key)) < 0) 49 printerr("keygen"); 50 if (writekey(fd, key, sizeof(key)) < 0) 51 printerr("writekey: %s", argv[0]); 52 fsync(fd); 53 if (close(fd) < 0) 54 err(1, "close: %s", argv[0]); 55 return 0; 56 }