diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-11-01 11:39:46 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-11-01 11:39:46 -0400 |
commit | b5fd9991b6bce1a177c9f992dfd31a880bc05f45 (patch) | |
tree | b7b76a0613e3bc3c0533ddeb75c0c37baa0e73ba /chown.c | |
parent | df23ba33bd4d1af0a17f607dedd262bfabf3a139 (diff) |
basic style
Diffstat (limited to 'chown.c')
-rw-r--r-- | chown.c | 182 |
1 files changed, 91 insertions, 91 deletions
@@ -17,19 +17,17 @@ * */ -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <getopt.h> +#define _XOPEN_SOURCE 700 +#include <ftw.h> #include <grp.h> -#include <pwd.h> #include <limits.h> -#include <ftw.h> -#include <string.h> +#include <pwd.h> +#include <stdio.h> #include <stdlib.h> - -const char *chown_desc = "change the file ownership"; -const char *chown_inv = "chown [-h] owner[:group] file...\nchown -R [-H|-L|-P] owner[:group] file..."; +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> #define NOFOLLOW 0 #define FOLLOWARGV 1 @@ -40,103 +38,105 @@ static int links = FOLLOWARGV; static uid_t newowner = -1; static gid_t newgroup = -1; -int _chown (const char*); +int _chown(const char *); -int nftw_chown (const char *p, const struct stat *sb, int typeflag, struct FTW *f) +int nftw_chown(const char *p, const struct stat *sb, int typeflag, + struct FTW *f) { - _chown (p); + (void)sb; (void)typeflag; (void)f; + return _chown(p); } -int _chown (const char *p) +int _chown(const char *p) { - struct stat st; - if (lstat (p, &st) != 0) { - perror (p); - return 1; - } - - // FIXME: about symlinks.... - // if (links == NOFOLLOW - // lchown (p, newowner, newgroup) - if (chown (p, newowner, newgroup) != 0) - perror (p); - return 0; + struct stat st; + if (lstat(p, &st) != 0) { + perror(p); + return 1; + } + // FIXME: about symlinks.... + // if (links == NOFOLLOW + // lchown (p, newowner, newgroup) + if (chown(p, newowner, newgroup) != 0) { + perror(p); + } + return 0; } -static void chown_parse (const char *ug) +static void chown_parse(const char *ug) { - char *colon = strchr (ug, ':'); - if (colon == NULL) { - struct passwd *pwd = getpwnam (ug); - if (pwd == NULL) - exit (1); - newowner = pwd->pw_uid; - } else { - struct passwd *pwd; - struct group *grp; - char user[NAME_MAX]; - strcpy (user, ug); - user[strlen(ug)-strlen(colon)] = '\0'; - pwd = getpwnam (user); - grp = getgrnam (&colon[1]); - if (pwd == NULL || grp == NULL) - exit (1); - newowner = pwd->pw_uid; - newgroup = grp->gr_gid; - } + char *colon = strchr(ug, ':'); + if (colon == NULL) { + struct passwd *pwd = getpwnam(ug); + if (pwd == NULL) + exit(1); + newowner = pwd->pw_uid; + } else { + struct passwd *pwd; + struct group *grp; + char user[NAME_MAX]; + strcpy(user, ug); + user[strlen(ug) - strlen(colon)] = '\0'; + pwd = getpwnam(user); + grp = getgrnam(&colon[1]); + if (pwd == NULL || grp == NULL) { + exit(1); + } + newowner = pwd->pw_uid; + newgroup = grp->gr_gid; + } } -int do_chown (char *path, int recurse, int howlinks, uid_t uid, gid_t gid) +int do_chown(char *path, int recurse, int howlinks, uid_t uid, gid_t gid) { - recursive = recurse; - links = howlinks; - newowner = uid; - newgroup = gid; - // FIXME: This is where to recurse and handle the difference betwixt links types - _chown (path); + recursive = recurse; + links = howlinks; + newowner = uid; + newgroup = gid; + // FIXME: This is where to recurse and handle the difference betwixt links types + return _chown(path); } -int -main (int argc, char **argv) +int main(int argc, char *argv[]) { - int c; + int c; + while ((c = getopt(argc, argv, ":hHLPR")) != -1) { + switch (c) { + case 'h': + if (recursive) + return 1; + recursive = -1; + links = NOFOLLOW; + break; + case 'H': + links = FOLLOWARGV; + break; + case 'L': + links = FOLLOWALL; + break; + case 'P': + links = NOFOLLOW; + break; + case 'R': + if (recursive == -1) + return 1; + recursive = 1; + break; + default: + return 1; + } + } - while ((c = getopt (argc, argv, ":hHLPR")) != -1) { - switch (c) { - case 'h': - if (recursive) - return 1; - recursive = -1; - links = NOFOLLOW; - break; - case 'H': - links = FOLLOWARGV; - break; - case 'L': - links = FOLLOWALL; - break; - case 'P': - links = NOFOLLOW; - break; - case 'R': - if (recursive == -1) - return 1; - recursive = 1; - break; - default: - return 1; - } - } - - if (optind >= argc - 1) - return 1; + if (optind >= argc - 1) { + return 1; + } - chown_parse (argv[optind++]); + chown_parse(argv[optind++]); - while (optind < argc) { - do_chown (argv[optind], recursive, links, newowner, newgroup); - optind++; - } + while (optind < argc) { + do_chown(argv[optind], recursive, links, newowner, newgroup); + optind++; + } - return 0; + return 0; } |