예제 #1
0
파일: pathinfo.c 프로젝트: vonwenm/pbft
static void
pathinfo (char *path)
{
  int fd;
  struct stat sb;
  dev_t dev;
  char cwd[PATH_MAX+1];
  char buf[2*PATH_MAX+3];
  char res[3*PATH_MAX+40];
  char *rp;
  FILE *dfpipe;

  if (chdir (path) < 0
      || (fd = open (".", O_RDONLY)) < 0
      || fstat (fd, &sb) < 0)
    fperror (path);
  dev = sb.st_dev;

  if (!getcwd (cwd, sizeof (cwd)))
    fperror ("getcwd");

  strcpy (buf, cwd);
  while ((rp = strrchr (buf, '/'))) {
    rp[1] = '\0';
    if (stat (buf, &sb) < 0)
      fperror (buf);
    if (sb.st_dev != dev) {
      if (!(rp = strchr (cwd + (rp - buf) + 1, '/')))
	rp = "/";
      break;
    }
    rp[0] = '\0';
  }
  if (!rp)
    rp = cwd;

  dfpipe = popen (PATH_DF
#ifdef DF_NEEDS_DASH_K
		  " -k"
#endif /* DF_NEEDS_DASH_K */
		  " . | sed -ne '2s/ .*//; 2p'", "r");
  if (!dfpipe || !fgets (buf, sizeof (buf) - 1, dfpipe)
      || pclose (dfpipe) < 0)
    fperror (PATH_DF);
  if (!strchr (buf, '\n'))
    strcat (buf, "\n");

  sprintf (res, "0x%" U64F "x\n%s%s\n", (u_int64_t) dev, buf, rp);
  if (isunixsocket (1) < 0) {
    int i = write (1, res, strlen (res));
    i++;
  } else
    writefd (1, res, strlen (res), fd);
}
예제 #2
0
void fperror_and_die(int exit_code, const char *s, ...)
{
    va_list p;

    fperror(s, p);
    exit(exit_code);
}
예제 #3
0
파일: pathinfo.c 프로젝트: vonwenm/pbft
int
main (int argc, char **argv)
{
  int ch;

  if ((progname = strrchr (argv[0], '/')))
    progname++;
  else
    progname = argv[0];

  while ((ch = getopt (argc, argv, "u:")) != -1)
    switch (ch) {
    case 'u':
      if (setuid (atoi (optarg)) < 0)
	fperror ("setuid");
      break;
    default:
      usage ();
    }
  argc -= optind;
  argv += optind;

  if (argc != 1)
    usage ();

  pathinfo (argv[0]);
  exit (0);
}
예제 #4
0
int
add_to_iob(struct client_state *csp, char *buf, int n)
{
	struct iob *iob = csp->iob;
	int have, need;
	char *p;

	have = iob->eod - iob->cur;

	if(n <= 0) return(have);

	need = have + n;

	if((p = malloc(need + 1)) == NULL) {
			fprintf(logfp, "%s: malloc() iob failed\n", prog);
			fperror(logfp, "");
			return(-1);
	}

	if(have) {
		/* there is something in the buffer - save it */
		memcpy(p, iob->cur, have);

		/* replace the buffer with the new space */
		freez(iob->buf);
		iob->buf = p;

		/* point to the end of the data */
		p += have;
	} else {
		/* the buffer is empty, free it and reinitialize */
		freez(iob->buf);
		iob->buf = p;
	}

	/* copy the new data into the iob buffer */
	memcpy(p, buf, n);

	/* point to the end of the data */
	 p +=   n ;

	/* null terminate == cheap insurance */
	*p  = '\0';

	/* set the pointers to the new values */
	iob->cur = iob->buf;
	iob->eod = p;

	return(need);
}