#include #include #include #include #include #include const int default_chars = 10; const char CHARS[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" ".,-+_/"; #define RNDFILE "/dev/random" void help() { fprintf(stderr, "Usage: passgen [number of chars]\n"); } int main(int argc, char * argv[]) { int fd, chars = default_chars; if (argc > 1 && *argv[1] != 0) { char *endptr; chars = strtol(argv[1], &endptr, 10); if (*endptr != 0) { fprintf(stderr, "invalid char '%c' in argument '%s'\n", *endptr, argv[1]); help(); return 255; } if (chars <= 0) { fprintf(stderr, "invalid number %i, must be > 0\n", chars); help(); return 255; } } if ((fd = open(RNDFILE, O_RDONLY)) < 0) { perror("open "RNDFILE); return 255; } for (; chars > 0; --chars) { char idx; if (read(fd, &idx, 1) < 1) { perror("read from "RNDFILE); return 255; } printf("%c", CHARS[idx % sizeof(CHARS)]); } printf("\n"); return 0; }