diff options
author | merakor <cem@ckyln.com> | 2020-07-23 08:09:11 +0000 |
---|---|---|
committer | merakor <cem@ckyln.com> | 2020-07-23 08:09:11 +0000 |
commit | 98f21585b4433838d429b5035fdf5a66524c00a1 (patch) | |
tree | 5d3a8854f3e3b10b0bd197b540b0479a777c331d | |
parent | 2d4cc5a63fcc80c5e4040d49428e45e6daf66a19 (diff) | |
download | cpt-98f21585b4433838d429b5035fdf5a66524c00a1.tar.gz |
kiss-readlink: change style, nitpicks
- use limits.h to assign the size of arrays.
- use sprintf instead of strcpy/strcat.
- use tabs of 8 characters instead of 2 spaces for indentation.
- reformat the source code based on the styles of kernel.org and suckless.org.
FossilOrigin-Name: 8f20c1368c92a49e66a9eb429503ec6901935d93af77f7af7284ab9c90101733
-rw-r--r-- | bin/kiss-readlink.c | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/bin/kiss-readlink.c b/bin/kiss-readlink.c index 8dc0c0f..ca65b90 100644 --- a/bin/kiss-readlink.c +++ b/bin/kiss-readlink.c @@ -1,5 +1,5 @@ /* kiss-readlink --- a utility replacement for readlink - * See LICENSE for copyright information + * See LICENSE for copyright information. * * This is basically a 'readlink -f' command. */ @@ -7,30 +7,40 @@ #include <stdlib.h> #include <libgen.h> #include <string.h> +#include <limits.h> + +#define _XOPEN_SOURCE 700 char *realpath(const char *path, char *resolved_path); -int main(int argc, char *argv[]) { +int +main(int argc, char *argv[]) +{ + + char buf[PATH_MAX]; - char buf[512]; - char bname[512]; - strcpy(bname, "/"); - strcat(bname, (basename(argv[1]))); + /* We are going to use these if the file doesn't exist, but we can still + * use directories above the file. We are using dname and bname so that + * they don't clash with the functions with the same name. + */ + char dname[PATH_MAX]; /* directory name */ + char bname[NAME_MAX]; /* base name */ + sprintf(bname, "%s", (basename(argv[1]))); - if (argc != 2 || strcmp(argv[1], "--help") == 0) { - printf("usage: %s [file]\n", argv[0]); - return(1); - } + if (argc != 2 || strcmp(argv[1], "--help") == 0) { + fprintf(stderr, "usage: %s [file]\n", argv[0]); + return 1; + } - if (!realpath(argv[1], buf)) { + if (!realpath(argv[1], buf)) { - if (!realpath(dirname(argv[1]), buf)) { - perror("realpath"); - return(1); - } - strcat(buf, bname); - } + if (!realpath(dirname(argv[1]), dname)) { + perror(argv[0]); + return 1; + } + sprintf(buf, "%s/%s", dname, bname); + } - printf("%s\n", buf); - return(0); + printf("%s\n", buf); + return(0); } |