home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / listalia.c < prev    next >
C/C++ Source or Header  |  1992-10-04  |  3KB  |  110 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: listalias.c,v 4.1 90/04/28 22:44:42 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    listalias.c,v $
  17.  * Revision 4.1  90/04/28  22:44:42  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  *
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** Program that lists all the available aliases.  This one uses the pipe
  24.     command, feeding the stuff to egrep then sort, or just sort.
  25.  
  26. **/
  27.  
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30. #include <pwd.h>
  31.  
  32. #include "defs.h"
  33.  
  34. #ifdef BSD
  35.   FILE *popen();
  36. #endif
  37.  
  38. char *getenv();
  39. struct passwd *getpwuid();
  40. struct passwd *pass;
  41. char home[SLEN];        /* the users home directory  */
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.     FILE *datafile, *fd_pipe;
  48.     struct alias_rec hash_record;
  49.     int hashfile, count = 0;
  50.     char buffer[SLEN], fd_hash[SLEN],
  51.          fd_data[SLEN];
  52.  
  53.         initpaths();
  54.  
  55.     if (argc > 2) {
  56.       printf("Usage: listalias <optional-regular-expression>\n");
  57.       exit(1);
  58.     }
  59.  
  60.     if((pass = getpwuid(getuid())) == NULL) {
  61.       printf("You have no password entry!\n");
  62.       exit(1);
  63.     }
  64.     strcpy(home, pass->pw_dir);
  65.  
  66.     sprintf(fd_hash, "%s/%s", home, ALIAS_HASH);
  67.     sprintf(fd_data, "%s/%s", home, ALIAS_DATA);
  68.         putc('\n', stdout);
  69.  
  70.     if (argc > 1)
  71.       sprintf(buffer, "egrep \"%s\" | sort", argv[1]);
  72.     else
  73.       sprintf(buffer, "sort");
  74.  
  75.     if ((fd_pipe = popen(buffer, "w")) == NULL) {
  76.       if (argc > 1)
  77.         printf("cannot open pipe to egrep program for expressions!\n");
  78.       fd_pipe = stdout;
  79.     }
  80.  
  81.     do {
  82.  
  83.       if ((hashfile = open(fd_hash, O_RDONLY)) > 0) {
  84.         if ((datafile = fopen(fd_data, "r")) == NULL) {
  85.           printf("Opened %s hash file, but couldn't open data file!\n",
  86.                count? "system" : "user");
  87.           goto next_file;
  88.         }
  89.  
  90.         /** Otherwise let us continue... **/
  91.  
  92.         while (read(hashfile, &hash_record, sizeof (hash_record)) != 0) {
  93.           if (strlen(hash_record.name) > 0) {
  94.             fseek(datafile, ntohl(hash_record.byte), 0);
  95.             fgets(buffer, SLEN, datafile);
  96.             fprintf(fd_pipe, "%-15s  %s", hash_record.name, buffer);
  97.           }
  98.         }
  99.       }
  100.  
  101. next_file: strcpy(fd_hash, system_hash_file);
  102.        strcpy(fd_data, system_data_file);
  103.  
  104.     } while (++count < 2);
  105.  
  106.     pclose(fd_pipe);
  107.  
  108.     exit(0);
  109. }
  110.