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