home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / finger / part02 / ttyloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  4.4 KB  |  216 lines

  1. /*
  2.  * ttyloc.c -- Set Terminal Location (December 1985)
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: ttyloc.c,v 3.0 90/07/06 13:12:02 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include <sys/types.h>
  20. # include <sys/stat.h>
  21. # include <strings.h>
  22. # include <errno.h>
  23. # include <ctype.h>
  24. # include <stdio.h>
  25. # include "finger.h"
  26.  
  27. extern int errno;
  28.  
  29. # define LCLEN 100            /* location length */
  30. # define FNLEN 100            /* file name length */
  31. char *argv0;
  32.  
  33. GLOBAL char localhost[ MAXHOSTLEN+1 ];
  34.  
  35. extern char *ttyname();            /* from library */
  36. extern char *locname();            /* from locname.c! */
  37. FORWARD LOCAL char *gettname();
  38. FORWARD LOCAL void getlocation(), fatal(), checkown(), pfatal(), checkdevice();
  39.  
  40. GLOBAL int main( argc, argv )
  41.     int argc;
  42.     char *argv[];
  43. {
  44.     int len;                /* length of ttyloc string */
  45.     FILE *f;
  46.     BOOL frobbing;            /* true if ttyfrob */
  47.     char *tname;
  48.     char locstr[LCLEN], *tfname;
  49.  
  50.     gethostname( localhost, MAXHOSTLEN );
  51.  
  52.     argv0 = "ttyloc?";
  53.     frobbing = FALSE;
  54.  
  55.     if( argc > 0 ) {
  56.     argv0 = rindex( argv[0], '/' );
  57.     if( argv0 == NULL )
  58.         argv0 = argv[0];
  59.     else
  60.         argv0++;
  61.  
  62.     if( strcmp(argv0, "ttyfrob") == 0 )
  63.         frobbing = TRUE;
  64.  
  65.     argc--;
  66.     argv++;
  67.     }
  68.  
  69.     if( frobbing ) {
  70.     if( argc > 0 ) {
  71.         tname = argv[0];
  72.         argc--;
  73.         argv++;
  74.         checkdevice( tname );
  75.     } /* more args */
  76.     else
  77.         fatal("must have tty name");
  78.     } /* frobbing */
  79.     else {
  80.     char *dev;
  81. # if 0                    /* returns /dev/tty! */
  82.     dev = ctermid( NULL );
  83.     if( dev == NULL )
  84. # endif /* 0 */
  85.         dev = ttyname( 0 );
  86.     if( dev == NULL )
  87.         fatal("could not find tty!");
  88.     checkown( dev );
  89.     tname = gettname( dev );
  90.     }
  91.     getlocation(locstr, argc, argv);
  92.  
  93.     tfname = locname( tname );        /* get spool file name */
  94.  
  95.     len = strlen(locstr);
  96.     if( len == 0 ) {
  97.     if( unlink( tfname ) < 0 && errno != ENOENT )
  98.         pfatal("could not unlink %s", tfname );
  99.     exit( 0 );
  100.     }
  101.  
  102.     f = fopen( tfname, "w" );
  103.     if( f == NULL )
  104.     pfatal("could not open %s");
  105.  
  106.     fputs( locstr, f );            /* check return? */
  107.  
  108. # ifdef USG
  109.     chmod( tfname, TTYLOC_MODE );
  110. # else  /* USG not defined */
  111.     fchmod(fileno(f), TTYLOC_MODE);
  112. # endif /* USG not defined */
  113.     fclose( f );            /* check for err? */
  114.     return( 0 );            /* ANSI! */
  115. } /* main */
  116.  
  117. LOCAL void checkdevice( s )
  118.     char *s;
  119. {
  120.     char devname[20];
  121.  
  122.     strcpy(devname, "/dev/");
  123.     strcat(devname, s);
  124.     checkown( devname );
  125. } /* checkdevice */
  126.  
  127. LOCAL char *gettname( device )
  128.     char *device;
  129. {
  130.     device = rindex(device, '/');
  131.     if( device != NULL )
  132.     device++;            /* skip over last slash */
  133.     return( device );
  134. } /* getttname */
  135.  
  136. LOCAL void getlocation(locstr, count, vec)
  137. char *locstr;
  138. int count;
  139. char *vec[];
  140. {
  141.     register char *cp, *sp;
  142.  
  143.     cp = locstr;
  144.     *cp = EOS;
  145.  
  146.     while( count > 0 ) {
  147.     sp = vec[0];
  148.     if( cp != locstr )
  149.         *cp++ = ' ';
  150.  
  151.     while( *sp != EOS ) {
  152.         if( isprint( *sp ) )
  153.         *cp++ = *sp;
  154.         else
  155.         fprintf( stderr, "%%%s: ignoring non printing character 0%o\n",
  156.             argv0, *sp & 0xff );
  157.         sp++;
  158.     }
  159.     count--;
  160.     vec++;
  161.     }
  162.     if( cp != locstr )
  163.     *cp++ = '\n';
  164.     *cp = EOS;
  165. } /* getlocation */
  166.  
  167. LOCAL void fatal( s, x )
  168. char *s;
  169. int x;
  170. {
  171.     char line[ 1024 ];
  172.     sprintf( line, s, x );
  173.     fprintf(stderr, "?%s: %s\n", argv0, line );
  174.     exit( 1 );
  175. } /* fatal */
  176.  
  177. LOCAL void pfatal( s, x )
  178. char *s;
  179. int x;
  180. {
  181.     extern int errno, sys_nerr;
  182.     extern char *sys_errlist[];
  183.     char line[ 1024 ];
  184.  
  185.     sprintf( line, s, x );
  186.     if( errno > 0 && errno < sys_nerr )
  187.     fprintf(stderr, "?%s: %s; %s\n", argv0, line, sys_errlist[ errno ] );
  188.     else
  189.     fprintf(stderr, "?%s: %s; error %d\n", argv0, line, errno );
  190.     exit( 1 );
  191. } /* pfatal */
  192.  
  193. LOCAL void checkown( name )
  194.     char *name;
  195. {
  196.     struct stat stb;
  197.     int uid;
  198.  
  199.     if( stat( name, &stb ) < 0 ) {
  200.     pfatal("could not stat %s", name );
  201.     exit( 1 );
  202.     } /* stat ok */
  203.     if( (stb.st_mode & S_IFMT) != S_IFCHR )
  204.     fatal("%s is not a character device!");
  205.     uid = getuid();            /* get invoker's uid */
  206.     if( uid != 0 && stb.st_uid != uid )
  207.     fatal( "not owner of %s", name );
  208. } /* checkown */
  209.  
  210.  
  211. /*
  212.  * Local variables:
  213.  * comment-column: 40
  214.  * End:
  215.  */
  216.