File: pathinfo.c

package info (click to toggle)
sfs 1%3A0.8-0%2Bpre20060720.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,668 kB
  • ctags: 14,317
  • sloc: cpp: 78,358; ansic: 15,494; sh: 9,540; yacc: 786; makefile: 706; perl: 676; lex: 553; python: 146; sed: 70
file content (140 lines) | stat: -rw-r--r-- 2,965 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* $Id: pathinfo.c,v 1.4 2002/08/07 18:36:21 dm Exp $ */

/*
 *
 * Copyright (C) 2001 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * 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 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
 *
 */

#include "sysconf.h"
#include "rwfd.h"

int suidprotect = 1;

char *progname;

static void fperror (char *msg) __attribute__ ((noreturn));
static void
fperror (char *msg)
{
  perror (msg);
  exit (1);
}

static int
isunixsocket (int fd)
{
  struct sockaddr_un su;
  socklen_t sulen = sizeof (su);
  bzero (&su, sizeof (su));
  su.sun_family = AF_UNIX;
  if (getsockname (fd, (struct sockaddr *) &su, &sulen) < 0
      || su.sun_family != AF_UNIX)
    return -1;
  return 0;
}

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)
    write (1, res, strlen (res));
  else
    writefd (1, res, strlen (res), fd);
}

static void
usage (void)
{
  fprintf (stderr, "usage: %s [-u uid] path\n", progname);
  exit (1);
}

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);
}