Useful UNIX Programs
by Marshall Plann
This program lets you see what any word will look like after it's run through crypt(). This is particularly useful for the next program.
#include <pwd.h> main(argc, argv) int argc; char *argv[]; { if (argc > 2) printf("%s\n", crypt(argv[1], argv[2])); else if (argc > 1) printf("%s\n", crypt(argv[1], "hi")); }This program will allow you to lock up any terminal on a UNIX system until the secret word is entered. In this case, the secret word is: dog
The word is listed in encrypted form, otherwise anyone listing your program would be able to see it.
/* Save this as 'secret.c' and type 'cc secret.c -o secret' to compile. */ #include <stdio.h> #include <sys/ioctl.h> #include <signal.h> #include <pwd.h> main(argc, argv) int argc; char **argv; { struct sgttyb basic; short flags; char str[32]; int i; /* Ignore all interrupts that may try to mess up this program like ^C, ^Z */ for (i = 1; i < 33; signal(i++, SIG_IGN)); /* Shut off echo and receive key strokes as they are hit from the terminal */ ioctl(fileno(stdin), TIOCGETP, &basic); flags = basic.sg_flags; basic.sg_flags |= CBREAK; basic.sg_flags &= ~ECHO; ioctl(fileno(stdin), TIOCSETP, &basic); do { /* Prompt for the input */ fprintf(stdout, "Enter Secret Word> "), fflush(stdout); /* Get the input until a return or a lot of keys are hit */ for (i = 0; (i < 31) && ((str[i] = getchar()) != '\n'); i++); /* Terminate the string */ str[i] = '\0'; /* Acknowledge that you have accepted the return by echoing it */ fprintf(stdout, "\n"); /* Repeat until the word has been entered */ } while (strcmp(crypt(str, "hi"), "higSfxQjrCp7Y")); /* Restore the terminal back to the original settings */ basic.sg_flags = flags; ioctl(fileno(stdin), TIOCSETP, &basic); /* Do something with the secret string here, I just print it out... You could have fun with it */ fprintf(stdout, "->%s\n", str); fflush(stdout); }Code: word.c
Code: secret.c