DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   (View Single Post)  
Old 26th March 2011
kdu kdu is offline
Port Guard
 
Join Date: Jun 2009
Posts: 25
Thanked 0 Times in 0 Posts
Default Help with struct

I working on a file that invokes <sys/stat>. I've gotten a lot I needed but for the life of me I can't figure where:
Code:
struct stat sb;
Goes.

Here is the file in question:

Code:
#include "wifimgr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>

#ifndef WITHOUT_NLS
#include <libintl.h>
#else
#define gettext(x)	(x)
#endif


#define	PATH		"/bin:/usr/bin:/sbin:/usr/sbin"

main(int argc, char ** argv) {
	char			line[1024];
	char			cmd[1024];
	int			auth = 0;
	int			copy_stdout;
	char *			arg;
	int			xst;
	

#ifndef WITHOUT_NLS
	/* set output character set */
	bind_textdomain_codeset("wifimgr", "UTF-8");

	/* set language catalog domain */
	textdomain("wifimgr");
#endif

	if (isatty(0) || isatty(1)) {
		fprintf(stderr, gettext("wifimgrsu: for invocation by wifimgr(8) only\n"));
		exit(1);
	}

	/* set line buffering */
	setvbuf(stdin, (char *) NULL, _IOLBF, 0);
	setvbuf(stdout, (char *) NULL, _IOLBF, 0);

	/* set safe PATH (required for shell scripts such as /etc/rc.d/netif) */
	setenv("PATH", PATH, 1);

	/* loop reading commands */
	while (fgets(line, sizeof(line), stdin) != NULL) {
		chop(line);
		if (strncmp(line, "password ", 9) == 0) {
			char *			pass;
			char *			su_pass;

			pass = strdup(index(line, ' ') + 1);
			su_pass = strdup(getpwnam("root")->pw_passwd);
			if (strcmp(crypt(pass, su_pass), su_pass) != 0)
				printf("FAIL %s\n", gettext("invalid password"));
			else {
				auth = 1;
				printf("OK\n");
			}
			continue;
		}

		/* go no further unless authorization passed */
		if (!auth) {
			printf("FAIL %s\n", gettext("not authorized"));
			continue;
		}

		/* flag for commands that produce stdout */
		copy_stdout = 0;
		if (strcmp(line, "cat_netfile") == 0) {
			if (stat(file, &sb) >= 0)
				sprintf(cmd, "%s %s", PATH_CAT, NETWORKS_FILE);
			else
				sprintf(cmd, "");
			copy_stdout = 1;
		}
		else if (strncmp(line, "write_netfile ", 14) == 0) {
			arg = index(line, ' ') + 1;
			sprintf(cmd, "%s %s %s", PATH_CP, arg, NETWORKS_FILE);
		}
		else if (strcmp(line, "diff_netfile") == 0)
			sprintf(cmd, "%s -q %s %s.save >/dev/null", PATH_DIFF, NETWORKS_FILE, NETWORKS_FILE);
		else if (strcmp(line, "backup_netfile") == 0)
			sprintf(cmd, "%s -p %s %s.save", PATH_CP, NETWORKS_FILE, NETWORKS_FILE);
		else if (strncmp(line, "restart_netif ", 14) == 0) {
			arg = index(line, ' ') + 1;
			sprintf(cmd, "%s restart %s >/dev/null 2>&1", PATH_NETIF, arg);
		}
		else if (strncmp(line, "start_netif ", 12) == 0) {
			arg = index(line, ' ') + 1;
			sprintf(cmd, "%s start %s >/dev/null 2>&1", PATH_NETIF, arg);
		}
		else if (strncmp(line, "stop_netif ", 11) == 0) {
			arg = index(line, ' ') + 1;
			sprintf(cmd, "%s stop %s >/dev/null 2>&1", PATH_NETIF, arg);
		}
		else {
			printf("FAIL %s\n", gettext("invalid command"));
			continue;
		}

		xst = system(cmd);

		if (copy_stdout)
			printf("EOF\n");

		if (WEXITSTATUS(xst) == 0)
			printf("OK\n");
		else
			printf("FAIL %s %d\n", gettext("exit status"), WEXITSTATUS(xst));
	}
}
Reply With Quote
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:41 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick