home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / opt_utils.c < prev    next >
C/C++ Source or Header  |  1990-04-28  |  10KB  |  452 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: opt_utils.c,v 4.1 90/04/28 22:43:37 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:    opt_utils.c,v $
  17.  * Revision 4.1  90/04/28  22:43:37  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  * 
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** This file contains routines that might be needed for the various
  24.      machines that the mailer can run on.  Please check the Makefile
  25.      for more help and/or information. 
  26.  
  27. **/
  28.  
  29. #include "headers.h"
  30. #include <ctype.h>
  31.  
  32. #ifdef PWDINSYS
  33. #  include <sys/pwd.h>
  34. #else
  35. #  include <pwd.h>
  36. #endif
  37.  
  38. #ifdef BSD
  39. #undef tolower
  40. #undef toupper
  41. #endif
  42.  
  43. #ifndef GETHOSTNAME
  44. # ifdef DOUNAME
  45. #  include <sys/types.h>
  46. #  include <sys/utsname.h>
  47. # endif
  48. #endif
  49.  
  50. char *index();
  51.  
  52. #ifndef GETHOSTNAME
  53.  
  54. gethostname(cur_hostname,size) /* get name of current host */
  55. char *cur_hostname;
  56. int size;
  57. {
  58.     /** Return the name of the current host machine. **/
  59.  
  60. #if defined(XENIX) & !defined(DOUNAME)
  61.     char    buf[32];
  62.     FILE    *fp;
  63.     char    *p;
  64.  
  65.     if ((fp = fopen("/etc/systemid", "r")) != 0) {
  66.       fgets(buf, sizeof(buf) - 1, fp);
  67.       fclose(fp);
  68.       if ((p = index(buf, '\n')) != NULL)
  69.         *p = '\0';
  70.       (void) strncpy(cur_hostname, buf, size - 1);
  71.       cur_hostname[size - 1] = '\0';
  72.       return 0;
  73.     }
  74.  
  75. #else   /* XENIX */
  76.  
  77. #ifdef DOUNAME
  78.     /** This routine compliments of Scott McGregor at the HP
  79.         Corporate Computing Center **/
  80.      
  81.     int uname();
  82.     struct utsname name;
  83.  
  84.     (void) uname(&name);
  85.     (void) strncpy(cur_hostname,name.nodename,size-1);
  86. #else
  87.     (void) strncpy(cur_hostname, HOSTNAME, size-1);
  88. #endif    /* DOUNAME */
  89.  
  90.     cur_hostname[size - 1] = '\0';
  91.     return 0;
  92.  
  93. #endif  /* XENIX */
  94. }
  95.  
  96. #endif  /* GETHOSTNAME */
  97.  
  98.  
  99. gethostdomain(hostdom, size)    /* get domain of current host */
  100. char *hostdom;
  101. int size;
  102. {
  103.     char    buf[64];
  104.     FILE    *fp;
  105.     char    *p;
  106.  
  107.     if (size < 2)
  108.       return -1;
  109.  
  110.     if ((fp = fopen(hostdomfile, "r")) != 0) {
  111.       fgets(buf, sizeof(buf) - 1, fp);
  112.       fclose(fp);
  113.       if ((p = index(buf, '\n')) != NULL)
  114.         *p = '\0';
  115.     }
  116.     else {
  117.       strncpy(buf, DEFAULT_DOMAIN, sizeof(buf) - 1);
  118.     }
  119.     if (buf[0] != '\0' && buf[0] != '.') {
  120.       *hostdom++ = '.';
  121.       --size;
  122.     }
  123.     (void) strncpy(hostdom, buf, size - 1);
  124.     hostdom[size - 1] = '\0';
  125.  
  126.     return 0;
  127. }
  128.  
  129.  
  130. #ifdef NEED_CUSERID
  131.  
  132. char *cuserid(uname)
  133.      char *uname;
  134. {
  135.     /** Added for compatibility with Bell systems, this is the last-ditch
  136.         attempt to get the users login name, after getlogin() fails.  It
  137.         instantiates "uname" to the name of the user...(it also tries
  138.         to use "getlogin" again, just for luck)
  139.     **/
  140.     /** This wasn't really compatible.  According to our man page, 
  141.      ** It was inconsistent.  If the parameter is NULL then you return
  142.      ** the name in a static area.  Else the ptr is supposed to be a
  143.      ** pointer to l_cuserid bytes of memory [probally 9 bytes]...
  144.      ** It's not mention what it should return if you copy the name
  145.      ** into the array, so I chose NULL.
  146.      **                     Sept 20, 1988
  147.      **                    **WJL**
  148.      **/
  149.  
  150.   struct passwd *password_entry;
  151. #ifndef _POSIX_SOURCE
  152.   struct passwd *getpwuid();
  153. #endif
  154.   char   *name, *getlogin();
  155.   static char buf[10];
  156.   register returnonly = 0;
  157.   
  158.   if (uname == NULL) ++returnonly;
  159.   
  160.   if ((name = getlogin()) != NULL) {
  161.     if (returnonly) {
  162.       return(name);
  163.     } else {
  164.       strcpy(uname, name);
  165.       return name;
  166.     }
  167.   } 
  168.   else 
  169.     if (( password_entry = getpwuid(getuid())) != NULL) 
  170.       {
  171.     if (returnonly) 
  172.       {
  173.         return(password_entry->pw_name);
  174.       }
  175.     else 
  176.       {
  177.         strcpy(uname, password_entry->pw_name);
  178.         return name;
  179.       }
  180.       } 
  181.     else 
  182.       {
  183.     return NULL;
  184.       }
  185. }
  186.  
  187. #endif
  188.  
  189. #if defined(BSD) && !defined(_POSIX_SOURCE)
  190.  
  191. /** some supplementary string functions for Berkeley Unix systems **/
  192.  
  193. int
  194. tolower(ch)
  195. char ch;
  196. {
  197.     /** This should be a macro call, but if you use this as a macro
  198.         calls to 'tolower' where the argument is a function call will
  199.         cause the function to be called TWICE which is obviously the
  200.         wrong behaviour.  On the other hand, to just blindly translate
  201.         assuming the character is always uppercase can cause BIG
  202.         problems, so...
  203.     **/
  204.  
  205.     return ( isupper(ch) ? ch - 'A' + 'a' : ch );
  206. }
  207.  
  208. int
  209. toupper(ch)
  210. char ch;
  211. {
  212.     /** see comment for above routine - tolower() **/
  213.  
  214.     return ( islower(ch) ? ch - 'a' + 'A' : ch );
  215. }
  216.  
  217. char *strtok(source, keys)
  218. char *source, *keys;
  219. {
  220.     /** This function returns a pointer to the next word in source
  221.         with the string considered broken up at the characters 
  222.         contained in 'keys'.  Source should be a character pointer
  223.         when this routine is first called, then NULL subsequently.
  224.         When strtok has exhausted the source string, it will 
  225.         return NULL as the next word. 
  226.  
  227.         WARNING: This routine will DESTROY the string pointed to
  228.         by 'source' when first invoked.  If you want to keep the
  229.         string, make a copy before using this routine!!
  230.      **/
  231.  
  232.     register int  last_ch;
  233.     static   char *sourceptr;
  234.          char *return_value;
  235.  
  236.     if (source != NULL)
  237.       sourceptr = source;
  238.     
  239.     if (*sourceptr == '\0') 
  240.       return(NULL);        /* we hit end-of-string last time!? */
  241.  
  242.     sourceptr += strspn(sourceptr, keys);    /* skip leading crap */
  243.     
  244.     if (*sourceptr == '\0') 
  245.       return(NULL);        /* we've hit end-of-string */
  246.  
  247.     last_ch = strcspn(sourceptr, keys);    /* end of good stuff */
  248.  
  249.     return_value = sourceptr;        /* and get the ret   */
  250.  
  251.     sourceptr += last_ch;            /* ...value          */
  252.  
  253.     if (*sourceptr != '\0')        /* don't forget if we're at END! */
  254.       sourceptr++;               /* and skipping for next time */
  255.  
  256.     return_value[last_ch] = '\0';        /* ..ending right    */
  257.     
  258.     return((char *) return_value);        /* and we're outta here! */
  259. }
  260.  
  261. char *strpbrk(source, keys)
  262. char *source, *keys;
  263. {
  264.     /** Returns a pointer to the first character of source that is any
  265.         of the specified keys, or NULL if none of the keys are present
  266.         in the source string. 
  267.     **/
  268.  
  269.     register int loc = 0, key_index = 0;
  270.  
  271.     while (source[loc] != '\0') {
  272.       key_index = 0;
  273.       while (keys[key_index] != '\0')
  274.         if (keys[key_index++] == source[loc])
  275.           return((char *) (source + loc));
  276.       loc++;
  277.     }
  278.     
  279.     return(NULL);
  280. }
  281.  
  282. #endif
  283.  
  284. #ifndef STRSPN
  285.  
  286. strspn(source, keys)
  287. char *source, *keys;
  288. {
  289.     /** This function returns the length of the substring of
  290.         'source' (starting at zero) that consists ENTIRELY of
  291.         characters from 'keys'.  This is used to skip over a
  292.         defined set of characters with parsing, usually. 
  293.     **/
  294.  
  295.     register int loc = 0, key_index = 0;
  296.  
  297.     while (source[loc] != '\0') {
  298.       key_index = 0;
  299.       while (keys[key_index] != source[loc])
  300.         if (keys[key_index++] == '\0')
  301.           return(loc);
  302.       loc++;
  303.     }
  304.  
  305.     return(loc);
  306. }
  307.  
  308. #endif
  309.  
  310. #ifndef STRCSPN
  311.  
  312. strcspn(source, keys)
  313. char *source, *keys;
  314. {
  315.     /** This function returns the length of the substring of
  316.         'source' (starting at zero) that consists entirely of
  317.         characters NOT from 'keys'.  This is used to skip to a
  318.         defined set of characters with parsing, usually. 
  319.         NOTE that this is the opposite of strspn() above
  320.     **/
  321.  
  322.     register int loc = 0, key_index = 0;
  323.  
  324.     while (source[loc] != '\0') {
  325.       key_index = 0;
  326.       while (keys[key_index] != '\0')
  327.         if (keys[key_index++] == source[loc])
  328.           return(loc);
  329.       loc++;
  330.     }
  331.  
  332.     return(loc);
  333. }
  334.  
  335. #endif
  336.  
  337. #ifndef TEMPNAM
  338. /* and a tempnam for temporary files */
  339. static int cnt = 0;
  340.  
  341. char *tempnam( dir, pfx)
  342.  char *dir, *pfx;
  343. {
  344.     char space[SLEN];
  345.     char *newspace;
  346.  
  347.     char    *malloc();
  348.  
  349.     if (dir == NULL) {
  350.         dir = "/usr/tmp";
  351.     } else if (*dir == '\0') {
  352.         dir = "/usr/tmp";
  353.     }
  354.     
  355.     if (pfx == NULL) {
  356.         pfx = "";
  357.     }
  358.  
  359.     sprintf(space, "%s%s%d.%d", dir, pfx, getpid(), cnt);
  360.     cnt++;
  361.     
  362.     newspace = malloc(strlen(space) + 1);
  363.     if (newspace != NULL) {
  364.         strcpy(newspace, space);
  365.     }
  366.     return newspace;
  367. }
  368.  
  369. #endif
  370.  
  371. #ifndef GETOPT
  372.  
  373. /*LINTLIBRARY*/
  374. #define NULL    0
  375. #define EOF    (-1)
  376. #define ERR(s, c)    if(opterr){\
  377.     extern int strlen(), write();\
  378.     char errbuf[2];\
  379.     errbuf[0] = c; errbuf[1] = '\n';\
  380.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  381.     (void) write(2, s, (unsigned)strlen(s));\
  382.     (void) write(2, errbuf, 2);}
  383.  
  384. extern int strcmp();
  385.  
  386. int    opterr = 1;
  387. int    optind = 1;
  388. int    optopt;
  389. char    *optarg;
  390.  
  391. int
  392. getopt(argc, argv, opts)
  393. int    argc;
  394. char    **argv, *opts;
  395. {
  396.     static int sp = 1;
  397.     register int c;
  398.     register char *cp;
  399.  
  400.     if(sp == 1)
  401.         if(optind >= argc ||
  402.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  403.             return(EOF);
  404.         else if(strcmp(argv[optind], "--") == NULL) {
  405.             optind++;
  406.             return(EOF);
  407.         }
  408.     optopt = c = argv[optind][sp];
  409.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  410.         ERR(": illegal option -- ", c);
  411.         if(argv[optind][++sp] == '\0') {
  412.             optind++;
  413.             sp = 1;
  414.         }
  415.         return('?');
  416.     }
  417.     if(*++cp == ':') {
  418.         if(argv[optind][sp+1] != '\0')
  419.             optarg = &argv[optind++][sp+1];
  420.         else if(++optind >= argc) {
  421.             ERR(": option requires an argument -- ", c);
  422.             sp = 1;
  423.             return('?');
  424.         } else
  425.             optarg = argv[optind++];
  426.         sp = 1;
  427.     } else {
  428.         if(argv[optind][++sp] == '\0') {
  429.             sp = 1;
  430.             optind++;
  431.         }
  432.         optarg = NULL;
  433.     }
  434.     return(c);
  435. }
  436.  
  437. #endif
  438.  
  439. #ifndef RENAME
  440. int rename(tmpfname, fname)
  441. char *tmpfname, *fname;
  442. {
  443.     int status;
  444.  
  445.     (void) unlink(fname);
  446.     if ((status = link(tmpfname, fname)) != 0)
  447.         return(status);
  448.     (void) unlink(tmpfname);
  449.     return(0);
  450. }
  451. #endif
  452.