/*
   <p>Get standard tab spacing.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include "environ.h"

#ifdef LINUX
#include <stdlib.h>
#endif

#ifdef MSDOS
#include <stdlib.h>
#endif

#ifdef OS9
char *getenv();
#include <sgstat.h>
int _gettab()
{
    struct sgbuf buffer;
    int r;
    
    r = _gs_opt(1,&buffer);
    if (r == -1)                                 /* bad path */
        return 0;
    if (buffer._sgm._sgs._sgs_class == 0)        /* SCF device */
        return (int)buffer._sgm._sgs._sgs_tabsiz;
    return 0;
}
#endif

/*
   Returns the default tab spacing for this system.
   Standard tab spacing is defined by: 
   <ol>
    <li>the value in the <i>stdout</i> path descriptor
        if it points to an SCF device (OS/9 only).</li>
    <li>the environmental variable TABSPACE, if it is defined.</li>
    <li>TABSPACE in <i>environ.h</i></li> 
   </ol>
   These possibilities are inspected in sequence until a value is found.

   <b>See also:</b> <i>tabclear(), tabexpand(), tabnormal(), tabout(),
      tabpos(), tabset(), tabspread()</i>
*/
int tabdefault()
{
    int     tabgap;
    char    *s;

#ifdef OS9
    tabgap = _gettab();
#endif

#ifdef MSDOS
    tabgap = 0;
#endif

#ifdef LINUX
    tabgap = 0;
#endif

    if (tabgap == 0)
    {
        s = getenv("TABSPACE");
        if (s)
	        tabgap = atoi(s);
    }
    if (tabgap == 0)
        tabgap = TABSPACE;

    return(tabgap);
}


