1 /* 2 * printmsg.c: print a message on the console 3 */ 4 #include <paths.h> 5 #include <stdio.h> 6 7 main(argc, argv) 8 int argc; 9 char *argv[]; 10 { 11 char *message; 12 13 if (argc < 2) { 14 fprintf(stderr, "usage: %s <message>\n", argv[0]); 15 exit(1); 16 } 17 message = argv[1]; 18 19 if (!printmessage(message)) { 20 fprintf(stderr, "%s: sorry, couldn't print your message\n", 21 argv[0]); 22 exit(1); 23 } 24 printf("Message delivered!\n"); 25 } 26 27 /* 28 * Print a message to the console. 29 * Return a boolean indicating whether the message was actually printed. 30 */ 31 printmessage(msg) 32 char *msg; 33 { 34 FILE *f; 35 36 f = fopen(_PATH_CONSOLE, "w"); 37 if (f == NULL) { 38 return (0); 39 } 40 fprintf(f, "%s\n", msg); 41 fclose(f); 42 return(1); 43 } 44