home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume32 / waituser / part01 next >
Text File  |  1992-10-06  |  6KB  |  202 lines

  1. Newsgroups: comp.sources.misc
  2. From: weigand@pecan.cns.udel.edu (Steve Weigand)
  3. Subject:  v32i095:  waituser - user logon detector, Part01/01
  4. Message-ID: <1992Oct4.171303.4437@sparky.imd.sterling.com>
  5. X-Md4-Signature: 9a144449b8004a1b63880c09122cc551
  6. Date: Sun, 4 Oct 1992 17:13:03 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: weigand@pecan.cns.udel.edu (Steve Weigand)
  10. Posting-number: Volume 32, Issue 95
  11. Archive-name: waituser/part01
  12. Environment: BSD
  13.  
  14. [ If you wish to have rapid turnaround on postings, please submit them  ]
  15. [ in the format described in the introduction posting.  Thanks.  -Kent+ ]
  16.  
  17. Summary:  waituser is a program which I created that will allow one to
  18. wait until until a certain user logs into the machine you are currently
  19. logged in on.
  20.  
  21.  USAGE: waituser <username>
  22.  
  23. It accomplishes this by repeatedly scanning the utmp file for any changes
  24. that happen to it. If the file is changed, waituser will recheck it to
  25. see if that user logged in. If not, it just keeps waiting for changes. 
  26.  
  27. Obviously, infinite loops like this tend to take up lots of processor 
  28. time, but scanning for changes to a file, rather than scanning the entire
  29. file, should reduce most of the overhead. The result is a program which
  30. eats up a little, but not a lot, of processor time.
  31.  
  32.  Example:
  33.  
  34.         Suppose you had a friend with username "jdoe", who logs into your
  35.         machine on occassion. You want to surprise him with a message the
  36.         very instant he logs in. How 'bout a really big message (banner)
  37.         to his screen? Here's what you'd do:
  38.  
  39.         Create the following shell script file using vi, calling it
  40.         "test.scr":
  41.                 waituser jdoe
  42.                 banner Hi there John | write jdoe
  43.  
  44.         Now, run this program in the background before you logout by doing
  45.         the following from your shell prompt:
  46.                 source test.scr &
  47.  
  48.         The ampersand will tell it to run in the background; so it will
  49.         continue to be there even when you logout.
  50.  
  51.         When John Doe logs into his account, he should instantly see 
  52.         a huge message on his screen, "Hi there John", even before seeing
  53.         anything in his .login (it should be that fast!).
  54.  
  55. I release this program public domain. I don't care what you all do to it,
  56. and I don't care if I am given credit. However, if you do find a way to
  57. do it better, without taking up processor time, please post it.
  58.  
  59. Thanx, 
  60.  -Steve Weigand
  61.  (weigand@ee.udel.edu)
  62.  
  63. =============================CUT HERE====================================
  64. #! /bin/sh
  65. # This is a shell archive.  Remove anything before this line, then feed it
  66. # into a shell via "sh file" or similar.  To overwrite existing files,
  67. # type "sh file -c".
  68. # Contents:  waituser.c
  69. # Wrapped by kent@sparky on Sun Oct  4 12:06:39 1992
  70. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  71. echo If this archive is complete, you will see the following message:
  72. echo '          "shar: End of archive."'
  73. if test -f 'waituser.c' -a "${1}" != "-c" ; then 
  74.   echo shar: Will not clobber existing file \"'waituser.c'\"
  75. else
  76.   echo shar: Extracting \"'waituser.c'\" \(2879 characters\)
  77.   sed "s/^X//" >'waituser.c' <<'END_OF_FILE'
  78. X/* Program: waituser.c 
  79. X** Author : Steven Weigand (weigand@ee.udel.edu)
  80. X** Date   : September 22, 1992
  81. X** Ver.   : 1.0
  82. X** Descr. :
  83. X**        This program will wait until the username you give it is
  84. X**        logged onto the system. The INSTANT that user logs in, this
  85. X**        program will exit, returning control back to your process.
  86. X**        The purpose of such a program is obvious, so I won't say
  87. X**        too much more on the subject. 
  88. X**
  89. X**        Reason for its invention: I didn't like constantly scanning
  90. X**         'users' for a certain names... when I did, it used up lots
  91. X**        of processor time, especially when the intervals between 
  92. X**        scans was small. This program will scan the /etc/utmp file
  93. X**        for the user in question. It will do this by checking it
  94. X**        for when the computer updates it, and then checking the 
  95. X**        list for the user you want. 
  96. X**
  97. X**        Example: 
  98. X**         If you wanted to write your friend (weigand) a "hello" to
  99. X**         his screen when he logged in, then create a script called
  100. X**         "hiweigand" and make it executeable:
  101. X**            waituser weigand
  102. X**            banner Hello | write weigand
  103. X**         When you activate this script, do so by using the & sign to
  104. X**         make it run in the background, even after you've left:
  105. X**             hiweigand& 
  106. X**         4 some odd hours/days later, your friend signs on and sees
  107. X**         a big Hello on his screen. Nifty, huh?
  108. X**
  109. X** USAGE:    waituser username
  110. X*/
  111. X#include <stdio.h>
  112. X#include <utmp.h>
  113. X#include <sys/types.h>
  114. X#include <sys/stat.h>
  115. X
  116. X#define EQ(a,b)  (strcmp(a,b) == 0)
  117. Xchar* sformat();
  118. X
  119. Xvoid main(i,v)
  120. Xint i;
  121. Xchar** v;
  122. X{
  123. X FILE* fp = NULL;
  124. X struct utmp * UT = NULL;
  125. X char* buffer = NULL;
  126. X long counter = 0L;
  127. X long index = 0L;
  128. X char c;
  129. X int endflag = 0;
  130. X time_t cur_date = 0;
  131. X struct stat stbufr;
  132. X
  133. X if (i<2) exit(-1);
  134. X if (i>2) printf("Extra input ignored.\n");
  135. X
  136. Xwhile(!endflag)
  137. X{
  138. X
  139. X fp = fopen("/etc/utmp","r");
  140. X fseek(fp,0L,2);
  141. X
  142. X if (fp == NULL) endflag = 1;
  143. X
  144. X if (fp != NULL)
  145. X {
  146. X   buffer = (char*)malloc(ftell(fp)*sizeof(char));
  147. X   fseek(fp,0L,0);
  148. X   counter = 0L;
  149. X   index = 0L; 
  150. X   while (!feof(fp))
  151. X     buffer[counter++] = fgetc(fp); 
  152. X   UT = (struct utmp*)buffer;
  153. X
  154. X   while (index < counter)
  155. X   {
  156. X        if (EQ(sformat(UT->ut_name,8),v[1]))
  157. X          exit(0);
  158. X        index = index + sizeof(struct utmp);
  159. X        UT++;
  160. X   } 
  161. X   free(buffer);
  162. X  
  163. X } /*End if*/
  164. X fclose(fp);
  165. X /* Now, wait until /etc/utmp is changed */
  166. X
  167. X cur_date = stbufr.st_mtime;
  168. X while(stbufr.st_mtime == cur_date)
  169. X    stat("/etc/utmp",&stbufr);
  170. X  /*It might take a lot of processor time, but this is better than opening
  171. X  **up the file and rechecking it every 1 millisecond! */
  172. X
  173. X
  174. X}/*End while*/
  175. X}/*End main()*/
  176. X
  177. Xchar* sformat(s,l)
  178. Xchar* s;
  179. Xint l;
  180. X{
  181. X /* Will return a string of l characters long from s*/
  182. X char* newstring;
  183. X int counter = 0;
  184. X
  185. X newstring = (char*)malloc((l+1)*sizeof(char));
  186. X
  187. X for (counter=0;counter<l;counter++)
  188. X   newstring[counter] = s[counter];
  189. X newstring[counter] = '\0';
  190. X return newstring;
  191. X}
  192. X
  193. END_OF_FILE
  194.   if test 2879 -ne `wc -c <'waituser.c'`; then
  195.     echo shar: \"'waituser.c'\" unpacked with wrong size!
  196.   fi
  197.   # end of 'waituser.c'
  198. fi
  199. echo shar: End of archive.
  200. exit 0
  201. exit 0 # Just in case...
  202.