Mercurial > hg > toybox
annotate toys/other/mountpoint.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
rev | line source |
---|---|
558 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * mountpoint.c - Check if a directory is a mountpoint. | |
4 * | |
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> | |
6 | |
7 USE_MOUNTPOINT(NEWTOY(mountpoint, "<1qdx", TOYFLAG_BIN)) | |
8 | |
9 config MOUNTPOINT | |
10 bool "mountpoint" | |
11 default y | |
12 help | |
13 usage: mountpoint [-q] [-d] directory | |
14 mountpoint [-q] [-x] device | |
15 -q Be quiet, return zero if directory is a mountpoint | |
16 -d Print major/minor device number of the directory | |
17 -x Print major/minor device number of the block device | |
18 */ | |
19 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
20 #define FOR_mountpoint |
558 | 21 #include "toys.h" |
22 | |
23 void mountpoint_main(void) | |
24 { | |
25 struct stat st1, st2; | |
26 int res = 0; | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
27 int quiet = toys.optflags & FLAG_q; |
558 | 28 toys.exitval = 1; // be pessimistic |
29 strncpy(toybuf, toys.optargs[0], sizeof(toybuf)); | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
30 if (((toys.optflags & FLAG_x) && lstat(toybuf, &st1)) || stat(toybuf, &st1)) |
558 | 31 perror_exit("%s", toybuf); |
32 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
33 if (toys.optflags & FLAG_x){ |
558 | 34 if (S_ISBLK(st1.st_mode)) { |
35 if (!quiet) printf("%u:%u\n", major(st1.st_rdev), minor(st1.st_rdev)); | |
36 toys.exitval = 0; | |
37 return; | |
38 } | |
39 if (!quiet) printf("%s: not a block device\n", toybuf); | |
40 return; | |
41 } | |
42 | |
43 if(!S_ISDIR(st1.st_mode)){ | |
44 if (!quiet) printf("%s: not a directory\n", toybuf); | |
45 return; | |
46 } | |
47 strncat(toybuf, "/..", sizeof(toybuf)); | |
48 stat(toybuf, &st2); | |
49 res = (st1.st_dev != st2.st_dev) || | |
50 (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); | |
51 if (!quiet) printf("%s is %sa mountpoint\n", toys.optargs[0], res ? "" : "not "); | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
52 if (toys.optflags & FLAG_d) |
558 | 53 printf("%u:%u\n", major(st1.st_dev), minor(st1.st_dev)); |
54 toys.exitval = res ? 0 : 1; | |
55 } |