home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume37 / newing / part03 < prev    next >
Encoding:
Text File  |  1993-05-29  |  12.2 KB  |  541 lines

  1. Newsgroups: comp.sources.misc
  2. From: slantin@eis.calstate.edu (Sam Lantinga)
  3. Subject: v37i094:  newing - interface for your program, Part03/03
  4. Message-ID: <1993May29.183612.27707@sparky.imd.sterling.com>
  5. X-Md4-Signature: fce430e7d32cd99a4cde92b0917ed52c
  6. Sender: kent@sparky.imd.sterling.com (Kent Landfield)
  7. Organization: Calif State Univ/Electronic Information Services
  8. Date: Sat, 29 May 1993 18:36:12 GMT
  9. Approved: kent@sparky.imd.sterling.com
  10.  
  11. Submitted-by: slantin@eis.calstate.edu (Sam Lantinga)
  12. Posting-number: Volume 37, Issue 94
  13. Archive-name: newing/part03
  14. Environment: UNIX
  15.  
  16. #! /bin/sh
  17. # This is a shell archive.  Remove anything before this line, then feed it
  18. # into a shell via "sh file" or similar.  To overwrite existing files,
  19. # type "sh file -c".
  20. # Contents:  COPYRIGHT Configure alias.c startup.c utmp.c
  21. # Wrapped by kent@sparky on Sat May 29 13:21:07 1993
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 3 (of 3)."'
  25. if test -f 'COPYRIGHT' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'COPYRIGHT'\"
  27. else
  28.   echo shar: Extracting \"'COPYRIGHT'\" \(942 characters\)
  29.   sed "s/^X//" >'COPYRIGHT' <<'END_OF_FILE'
  30. X            COPYRIGHT ALERT
  31. X
  32. XSTANDARD DISCLAIMER:
  33. X    No guarantee is stated or implied as to the functionality
  34. Xof this software, or its fitness for any purpose.  No responsibility
  35. Xis taken for any damage occurring as a result of using this software.
  36. X
  37. XCOPYRIGHT:
  38. X    The newing package, consisting of the following files:
  39. X
  40. XCOPYRIGHT    cmds.c        ile.c        newing.h    tty.c
  41. XConfigure    config.c    mem.c        newing.hlp    utmp.c
  42. XINSTALL        help-err.c    misc.c        startup.c
  43. Xalias.c        history.c    newing.c    trigger.c
  44. X
  45. Xare under a Shareware Copyright held by Sam Lantinga as of May 6, 1993.
  46. X
  47. X    A Shareware Copyright means that you are free to use or 
  48. Xdistribute any part of this package on the condition that no intent
  49. Xof exchanging money is held by any party, and that no money is ever
  50. Xexchanged, and upon the condition that this document accompanies the 
  51. Xexchange.  The intent of a Shareware Copyright is to allow anyone 
  52. Xfree access to source code if they so desire.
  53. X
  54. X    -Sam Lantinga    5/6/93
  55. END_OF_FILE
  56.   if test 942 -ne `wc -c <'COPYRIGHT'`; then
  57.     echo shar: \"'COPYRIGHT'\" unpacked with wrong size!
  58.   fi
  59.   # end of 'COPYRIGHT'
  60. fi
  61. if test -f 'Configure' -a "${1}" != "-c" ; then 
  62.   echo shar: Will not clobber existing file \"'Configure'\"
  63. else
  64.   echo shar: Extracting \"'Configure'\" \(510 characters\)
  65.   sed "s/^X//" >'Configure' <<'END_OF_FILE'
  66. X#!/bin/sh
  67. X#  Script to configure the newing package 
  68. X#  Shareware Copyright by Sam Lantinga    5/6/93
  69. X
  70. XCFLAGS=
  71. XMAKE="echo \"Done.  Type 'make' to build\"" 
  72. X
  73. Xecho "Making configuration..."
  74. X
  75. Xwhile [ $# -gt 0 ]
  76. X  do case $1 in
  77. X       -d) CFLAGS=-DDEBUG
  78. X           echo "Configuring for debug mode."
  79. X           shift;;
  80. X       -m) MAKE=make
  81. X           shift;;
  82. X        *)  echo "Usage: Configure [-d] [-m]"
  83. X            exit;;
  84. X     esac
  85. Xdone
  86. X
  87. Xcc $CFLAGS -o config config.c
  88. Xif ./config
  89. X  then eval $MAKE
  90. Xfi
  91. Xrm -f config
  92. X
  93. END_OF_FILE
  94.   if test 510 -ne `wc -c <'Configure'`; then
  95.     echo shar: \"'Configure'\" unpacked with wrong size!
  96.   fi
  97.   chmod +x 'Configure'
  98.   # end of 'Configure'
  99. fi
  100. if test -f 'alias.c' -a "${1}" != "-c" ; then 
  101.   echo shar: Will not clobber existing file \"'alias.c'\"
  102. else
  103.   echo shar: Extracting \"'alias.c'\" \(2803 characters\)
  104.   sed "s/^X//" >'alias.c' <<'END_OF_FILE'
  105. X
  106. X/* alias.c   Shareware Copyright by Sam Lantinga    5/6/93    */
  107. X
  108. X#include    "newing.h"
  109. X
  110. X#define MAXALIASES    10        /* The maximum number of aliasas */
  111. X
  112. Xstatic struct alias {
  113. X    char *name;
  114. X    char *def;
  115. X    } aliases[MAXALIASES];
  116. X
  117. Xchar alias_error[BUFSIZ];
  118. X
  119. Xstatic int have_aliased=0;
  120. X
  121. X
  122. X/* Print out the history buffers on standard output */
  123. X
  124. Xvoid showaliases()
  125. X{
  126. X    int i;
  127. X
  128. X    if ( ! have_aliased )
  129. X    {
  130. X        printf("No aliases have been defined.\r\n");
  131. X        return;
  132. X    }
  133. X
  134. X    printf("Aliases:\r\n");
  135. X    for ( i=0; i<MAXALIASES; ++i )
  136. X    {
  137. X        if ( aliases[i].name == NULL )
  138. X            continue;
  139. X        printf("\t%s:\t%s\r\n", aliases[i].name, aliases[i].def);
  140. X    }
  141. X}
  142. X
  143. X
  144. X/* Add a new alias definition */
  145. X
  146. Xint add_alias(name, definition)
  147. Xchar *name;
  148. Xchar *definition;
  149. X{
  150. X    int i;
  151. X
  152. X    if ( ! have_aliased )
  153. X    {
  154. X        for ( i=0; i<MAXALIASES; ++i )
  155. X            aliases[i].name=NULL;
  156. X        have_aliased=1;
  157. X    }
  158. X
  159. X    for ( i=0; i<MAXALIASES; ++i )
  160. X    {
  161. X        /* Add the new alias */
  162. X        if ( aliases[i].name == NULL )
  163. X        {
  164. X            if ( (aliases[i].name=myalloc(strlen(name)+1))
  165. X                                == NULL )
  166. X            {
  167. X                sprintf(alias_error, "Malloc error");
  168. X                return(-1);
  169. X            }
  170. X
  171. X            if ( (aliases[i].def=myalloc(strlen(definition)+1))
  172. X                                == NULL )
  173. X            {
  174. X                (void) myfree(aliases[i].name);
  175. X                sprintf(alias_error, "Malloc error");
  176. X                return(-1);
  177. X            }
  178. X
  179. X            strcpy(aliases[i].name, name);
  180. X            strcpy(aliases[i].def, definition);
  181. X            return(0);
  182. X        }
  183. X
  184. X        if ( strcmp(name, aliases[i].name) == 0 )
  185. X        {
  186. X            printf("Replacing existing alias: %s\r\n", name);
  187. X
  188. X            (void) myfree(aliases[i].name);
  189. X            (void) myfree(aliases[i].def);
  190. X
  191. X            if ( (aliases[i].name=myalloc(strlen(name)+1))
  192. X                                == NULL )
  193. X            {
  194. X                sprintf(alias_error, "Malloc error");
  195. X                return(-1);
  196. X            }
  197. X
  198. X            if ( (aliases[i].def=myalloc(strlen(definition)+1))
  199. X                                == NULL )
  200. X            {
  201. X                (void) myfree(aliases[i].name);
  202. X                sprintf(alias_error, "Malloc error");
  203. X                return(-1);
  204. X            }
  205. X
  206. X            strcpy(aliases[i].name, name);
  207. X            strcpy(aliases[i].def, definition);
  208. X            return(0);
  209. X        }
  210. X    }
  211. X
  212. X    /* We ran out of aliases */
  213. X    sprintf(alias_error, "Too many aliases");
  214. X    return(-1);
  215. X}
  216. X
  217. X
  218. X/* Remove an alias */
  219. X
  220. Xint del_alias(name)
  221. Xchar *name;
  222. X{
  223. X    int i;
  224. X
  225. X    if ( ! have_aliased )
  226. X    {
  227. X        sprintf(alias_error, "No aliases have been defined");
  228. X        return(-1);
  229. X    }
  230. X
  231. X    for ( i=0; i<MAXALIASES; ++i )
  232. X    {
  233. X        if ( aliases[i].name && strcmp(aliases[i].name, name) == 0 )
  234. X        {
  235. X            (void) myfree(aliases[i].name);
  236. X            (void) myfree(aliases[i].def);
  237. X
  238. X            aliases[i].name=NULL;
  239. X            return(0);
  240. X        }
  241. X    }
  242. X
  243. X    /* We couldn't find the alias */
  244. X    sprintf(alias_error, "Can't find alias");
  245. X    return(-1);
  246. X}
  247. X    
  248. X
  249. X/* Return the definition for an alias name, or NULL if the name
  250. X   couldn't be found */
  251. X
  252. Xchar *getalias(name)
  253. Xchar *name;
  254. X{
  255. X    int i;
  256. X
  257. X    if ( ! have_aliased )
  258. X        return(NULL);
  259. X
  260. X    for ( i=0; i<MAXALIASES; ++i )
  261. X    {
  262. X        if ( aliases[i].name && strcmp(aliases[i].name, name) == 0 )
  263. X            return(aliases[i].def);
  264. X    }
  265. X    return(NULL);
  266. X}
  267. END_OF_FILE
  268.   if test 2803 -ne `wc -c <'alias.c'`; then
  269.     echo shar: \"'alias.c'\" unpacked with wrong size!
  270.   fi
  271.   # end of 'alias.c'
  272. fi
  273. if test -f 'startup.c' -a "${1}" != "-c" ; then 
  274.   echo shar: Will not clobber existing file \"'startup.c'\"
  275. else
  276.   echo shar: Extracting \"'startup.c'\" \(1933 characters\)
  277.   sed "s/^X//" >'startup.c' <<'END_OF_FILE'
  278. X
  279. X/* startup.c   Shareware Copyright by Sam Lantinga    5/6/93    */
  280. X
  281. X#include    <ctype.h>
  282. X#include    "newing.h"
  283. X
  284. X#define STARTDIR    getenv("HOME")
  285. X#define STARTUP        ".newingrc"
  286. X
  287. Xvoid init_rc(label, fd)
  288. Xchar *label;
  289. Xint fd;
  290. X{
  291. X    char buf[BUFSIZ], *bufptr;
  292. X    char initpath[BUFSIZ], escape_str[BUFSIZ];
  293. X    FILE *newingrc;
  294. X    int i, len, sleepy, nomatch=1;
  295. X
  296. X    sprintf(initpath, "%s/%s", STARTDIR, STARTUP);
  297. X
  298. X    if ( (newingrc=fopen(initpath, "r")) == NULL )
  299. X    {
  300. X        fprintf(stderr, "Can't open ");
  301. X        perror(initpath);
  302. X        fprintf(stderr, "\r");
  303. X        return;  /* the .newingrc file isn't readable. */
  304. X    }
  305. X
  306. X    if ( flag.verbose )
  307. X        printf("Reading from .newingrc\r\n"); 
  308. X
  309. X    /* Find the starting label. */
  310. X    while ( (bufptr=fgets(buf, BUFSIZ-1, newingrc)) != NULL )
  311. X    {
  312. X        if ( buf[0] != '\t' && buf[0] != '#' )
  313. X        {  /* We have a label, see if it matches. */
  314. X            for ( i=0; (buf[i]!=':' && buf[i]!='\n'); ++i );
  315. X            /* at end of label, null terminate */
  316. X            buf[i]='\0';
  317. X            if ( strcmp(buf, label) == 0 )
  318. X                break;
  319. X        }
  320. X    }
  321. X
  322. X    if ( ! bufptr )
  323. X    {
  324. X        fprintf(stderr, "Label \"%s\" not found in .newingrc\r\n",
  325. X                                     label);
  326. X        (void) fclose(newingrc);
  327. X        return;
  328. X    }
  329. X
  330. X    while ( (bufptr=fgets(buf, BUFSIZ-1, newingrc)) != NULL )
  331. X    {
  332. X        if ( *bufptr == '\n' || *bufptr == '#' )
  333. X            continue;
  334. X        else if ( *bufptr != '\t' )
  335. X            break;
  336. X
  337. X        /* initialize settings */
  338. X        ++bufptr;
  339. X        sleepy=1;   /* Don't overload the exec'ed child. */
  340. X
  341. X        /* Skip a comment */
  342. X        if ( *bufptr == '#' )
  343. X            continue;
  344. X
  345. X        /* Get the time to sleep if any. */
  346. X        for ( i=1; isdigit(buf[i]); ++i );
  347. X        if ( buf[i] == ' ' )
  348. X        {
  349. X            buf[i]='\0';
  350. X            bufptr=(&buf[i+1]);
  351. X            sleepy=atoi(&buf[1]);
  352. X        }
  353. X
  354. X        /* map newlines to carriage returns */
  355. X        len=strlen(buf);
  356. X
  357. X        if ( *bufptr == '*' )
  358. X        {  /* Perform an escape */
  359. X            ++bufptr, buf[len-1]='\0';
  360. X            read_escape(bufptr);
  361. X        }
  362. X        else /* Send to the program */
  363. X        {
  364. X            sleep(sleepy);
  365. X
  366. X            buf[len-1]='\r';
  367. X            writen(fd, bufptr, strlen(bufptr));
  368. X        }
  369. X    }
  370. X    (void) fclose(newingrc);
  371. X    return;
  372. X}
  373. END_OF_FILE
  374.   if test 1933 -ne `wc -c <'startup.c'`; then
  375.     echo shar: \"'startup.c'\" unpacked with wrong size!
  376.   fi
  377.   # end of 'startup.c'
  378. fi
  379. if test -f 'utmp.c' -a "${1}" != "-c" ; then 
  380.   echo shar: Will not clobber existing file \"'utmp.c'\"
  381. else
  382.   echo shar: Extracting \"'utmp.c'\" \(2566 characters\)
  383.   sed "s/^X//" >'utmp.c' <<'END_OF_FILE'
  384. X
  385. X/*  utmp.c    Shareware Copyright by Sam Lantinga    5/6/93    */
  386. X
  387. X#include    <sys/types.h>
  388. X#include    <utmp.h>
  389. X#include    <fcntl.h>
  390. X#include    "newing.h"
  391. X
  392. X#ifdef DEBUG
  393. X#undef  UTMP_FILE
  394. X#define UTMP_FILE  "/tmp/utmp"
  395. X#else
  396. X#ifndef UTMP_FILE
  397. X#define UTMP_FILE  "/etc/utmp"
  398. X#endif /* UTMP_FILE */
  399. X#endif /* DEBUG */
  400. X
  401. X/* This utmp function was adapted from pty's logs.c  */
  402. X
  403. Xint utmp(line, name, date, cleanup)
  404. Xchar *line;
  405. Xchar name[];
  406. Xlong date;
  407. Xint cleanup;
  408. X{
  409. X    struct utmp ut;
  410. X    struct utmp xt;
  411. X    char *lineptr;
  412. X    int fd, found=0;
  413. X
  414. X    if ( (fd=open(UTMP_FILE, O_RDWR)) < 0 )
  415. X    {
  416. X        if ( flag.verbose )
  417. X        {
  418. X            fprintf(stderr, "Can't open %s: ", UTMP_FILE);
  419. X            perror("");
  420. X            fprintf(stderr, "\r");
  421. X        }
  422. X        return(-1);
  423. X    }
  424. X
  425. X    /* Initialize the utmp structure */
  426. X    d_zero((char *)&ut, sizeof(struct utmp));
  427. X
  428. X    /* Get the ttyxy form of the tty pathname if possible. */
  429. X    if ( *line == '/' )
  430. X    {
  431. X        for ( lineptr=(line+1); *lineptr; ++lineptr )
  432. X        {
  433. X            if ( *lineptr == '/' )
  434. X                break;
  435. X        }
  436. X        if ( *lineptr == '/' )
  437. X            ++lineptr;
  438. X    }
  439. X    else
  440. X        lineptr=line;
  441. X
  442. X    (void) strncpy(ut.ut_line, lineptr, sizeof(ut.ut_line));
  443. X
  444. X    if ( cleanup )
  445. X    {
  446. X        ut.ut_name[0]='\0';
  447. X#ifdef USER_PROCESS
  448. X        ut.ut_type = DEAD_PROCESS;
  449. X#endif
  450. X    }
  451. X    else
  452. X    {
  453. X#ifdef USER_PROCESS
  454. X        ut.ut_type = USER_PROCESS;
  455. X        ut.ut_pid = getpid();
  456. X#endif
  457. X        strncpy(ut.ut_name, name, sizeof(ut.ut_name));
  458. X    }
  459. X    ut.ut_time=date;
  460. X
  461. X#ifdef HAVE_UTHOST
  462. X    ut.ut_host[0]='\0';
  463. X#endif
  464. X
  465. X    while (read(fd,(char *) &xt, sizeof(xt)) == sizeof(xt))
  466. X     {
  467. X          if (strncmp(xt.ut_line, ut.ut_line, sizeof(xt.ut_line)) == 0)
  468. X            {
  469. X            found=1;
  470. X            lseek(fd, -(long)sizeof(struct utmp), 1);
  471. X                 break;
  472. X            }
  473. X      }
  474. X
  475. X    /* Add a new entry to the utmp file if we can't find our entry */
  476. X    if ( (!found) && (!cleanup) )
  477. X      { /* Reopen to avoid a race with other end-of-utmp entries. */
  478. X           (void) close(fd);
  479. X           if ( (fd=open(UTMP_FILE, (O_RDWR|O_APPEND))) < 0 )
  480. X                 return -1;
  481. X      }
  482. X
  483. X     if (write(fd, (char *) &ut, sizeof(ut)) < sizeof(ut))
  484. X      {
  485. X           (void) close(fd);
  486. X           return -1;
  487. X      }
  488. X     return(close(fd));
  489. X}
  490. X
  491. X
  492. X/* Fill the utinfo structure with all sorts of interesting info */
  493. X
  494. Xextern char tty_name[];        /* From tty.c */
  495. X
  496. Xvoid init_utinfo()
  497. X{
  498. X    struct passwd *pw;
  499. X    char *lineptr;
  500. X
  501. X    utinfo.cpid=getpid();
  502. X    utinfo.pgrp=getpgrp();
  503. X
  504. X    if ( dosuid )
  505. X        utinfo.euid=dosuid->pw_uid;
  506. X    else
  507. X        utinfo.euid=getuid();
  508. X    strncpy(utinfo.tty, tty_name, 18);
  509. X
  510. X    if ( dosuid )
  511. X        strncpy(utinfo.name, dosuid->pw_name, 10);
  512. X    else if ( (pw=(struct passwd *)getpwuid(utinfo.euid)) != NULL )
  513. X        strncpy(utinfo.name, pw->pw_name, 10);
  514. X    else
  515. X        strncpy(utinfo.name, atoi(utinfo.euid), 10);
  516. X    return;
  517. X}
  518. END_OF_FILE
  519.   if test 2566 -ne `wc -c <'utmp.c'`; then
  520.     echo shar: \"'utmp.c'\" unpacked with wrong size!
  521.   fi
  522.   # end of 'utmp.c'
  523. fi
  524. echo shar: End of archive 3 \(of 3\).
  525. cp /dev/null ark3isdone
  526. MISSING=""
  527. for I in 1 2 3 ; do
  528.     if test ! -f ark${I}isdone ; then
  529.     MISSING="${MISSING} ${I}"
  530.     fi
  531. done
  532. if test "${MISSING}" = "" ; then
  533.     echo You have unpacked all 3 archives.
  534.     rm -f ark[1-9]isdone
  535. else
  536.     echo You still must unpack the following archives:
  537.     echo "        " ${MISSING}
  538. fi
  539. exit 0
  540. exit 0 # Just in case...
  541.