home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / programs / comms_networking / users / Utilities / c / getty < prev    next >
Encoding:
Text File  |  1998-01-01  |  2.9 KB  |  160 lines

  1. /* getty.c
  2.  * RISC OS getty clone
  3.  * (c) Chris Rutter 1997
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "user.h"
  10.  
  11. #ifdef __riscos
  12. #include "kernel.h"
  13. #endif /* __riscos */
  14.  
  15. #define f_setDir 1u
  16. #define f_openDir 2u
  17.  
  18. #define ourLog(x) if(logFile[0])user_log(logFile,x)
  19. char *hostname (void);
  20. void getty (unsigned int);
  21. void restart (void);
  22.  
  23. unsigned int gettyFlags=0;
  24. int exitFlag=1;
  25. int numLogins=0;
  26. char gHostname[256];
  27. char logFile[256];
  28.  
  29. int main (int argc, char *argv[])
  30. {
  31.     int n;
  32.     for (n=1; n<argc; n++)
  33.     {
  34.         if         (!strcmp (argv[n], "-setDir")) gettyFlags |= f_setDir;
  35.         else if (!strcmp (argv[n], "-openDir")) gettyFlags |= f_openDir;
  36.         else if (!strcmp (argv[n], "-log")) strcpy (logFile, argv[++n]);
  37.         else if (!strcmp (argv[n], "-host"))
  38.         {
  39.             strcpy (gHostname, argv[++n]);
  40.             strcat (gHostname, " ");
  41.         }
  42.         else if (!strcmp (argv[n], "-help"))
  43.         {
  44.             #ifdef __riscos
  45.             printf ("Syntax: *getty [-setDir] [-openDir] [-log <file>] [-host <hostname>]\n");
  46.             #else
  47.             printf ("Usage: getty [-log <file>] [-host <hostname>]\n");
  48.             #endif /* __riscos */
  49.             exit (0);
  50.         }
  51.         else if (argv[n][0]=='-' && argv[n][1]=='v')
  52.         {
  53.             printf ("getty 0.01 (c) Chris Rutter 1997\n");
  54.             exit(0);
  55.         }
  56.         else printf ("Option `%s' not recognised.\n", argv[n]);
  57.  
  58.     }
  59.     if (gHostname[0]==0) strcpy (gHostname, hostname());
  60.     getty (gettyFlags);
  61. }
  62.  
  63. void restart ()
  64. {
  65.     if (exitFlag) getty (gettyFlags);
  66. }
  67.  
  68. void getty (unsigned int f)
  69. {
  70.     char nm[32], pw[32];
  71.     user *u;
  72.  
  73.     atexit (restart);
  74.  
  75.     do
  76.     {
  77.         if (numLogins++) putc ('\n', stdout);
  78.         printf ("%slogin: ",gHostname);
  79.         scanf ("%31s", nm);
  80.  
  81.         printf ("Password: ");
  82.         user_scanf (pw, 32, 0);
  83.  
  84.         u=user_verify (nm, pw);
  85.  
  86.         if (!u)
  87.         {
  88.             char b[256];
  89.             sprintf (b,"User `%s' failed to login with password `%s'", nm, pw);
  90.             ourLog(b);
  91.         }
  92.         else
  93.         {
  94.             char b[256];
  95.             sprintf (b,"User `%s' successfully logged in",nm);
  96.             ourLog(b);
  97.         }
  98.     }
  99.     while (!u);
  100.  
  101.     #ifdef __riscos
  102.     if (!u->homeDir[0]) goto skipDirs;
  103.  
  104.     if (f&f_setDir)
  105.     {
  106.         char c[256];
  107.         sprintf (c, "dir %s", u->homeDir);
  108.         system (c);
  109.     }
  110.  
  111.     if (f&f_openDir)
  112.     {
  113.         char c[256];
  114.         sprintf (c, "filer_opendir %s", u->homeDir);
  115.         system (c);
  116.     }
  117. skipDirs:
  118.     #endif /* __riscos */
  119.  
  120.     #ifdef __riscos
  121.     {
  122.         char c[256];
  123.         sprintf (c, "set User$Username \x22%s\x22", u->username);
  124.         system (c);
  125.         sprintf (c, "set User$HomeDir \x22%s\x22", u->homeDir);
  126.         system (c);
  127.         sprintf (c, "set User$RealName \x22%s\x22", u->realName);
  128.         system (c);
  129.         sprintf (c, "set User$UID %d", u->uid);
  130.         system (c);
  131.         sprintf (c, "set User$GID %d", u->gid);
  132.         system (c);
  133.     }
  134.     #endif /* __riscos */
  135.     
  136.     exitFlag=0;
  137. }
  138.  
  139. char *hostname (void)
  140. {
  141.     #ifdef __riscos
  142.     _kernel_swi_regs r;
  143.     static char buf[256];
  144.     r.r[0]=(int)"Inet$Hostname";
  145.     r.r[1]=(int)buf;
  146.     r.r[2]=256;
  147.     r.r[3]=0;
  148.     r.r[4]=0; /* string */
  149.     _kernel_swi(0x23, &r, &r); /* 0x23==OS_ReadVarVal */
  150.     if (buf[0])
  151.     {
  152.         buf[strlen(buf)+1]=0;
  153.         buf[strlen(buf)]=' ';
  154.     }
  155.     return buf;
  156.     #else
  157.     return "";
  158.     #endif /* __riscos */
  159. }
  160.