/*
 *  librunlevel.c:  the get_runlevel() function
 *
 *  Copyright (c) 2000 David Parsons. All rights reserved.
 *
 *  This software is released under a berkeley-style copyright; the
 *  details can be found in the file COPYRIGHT.
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/*
 * get the runlevel out of the command line.  Works on linux and FreeBSD
 */
char *
get_runlevel()
{
    int fd;
    int size;
    static char bfr[80];
    char *p, *q;

    if ( (fd = open("/proc/1/cmdline", O_RDONLY)) >= 0) {
	size = read(fd, bfr, sizeof bfr);

	for (p = bfr; (p < bfr+size) && ( *p != '['); p++)
	    ;

	if (*p == '[') {
	    ++p;
	    for (q = p; *q && *q != ']'; ++q)
		;
	    if (*q == ']')
		*q = 0;

	    return p;
	}
    }
    return (char*)0;
} /* get_runlevel */
