aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-11 11:54:48 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-11 12:04:13 +0200
commit7bf7902f4afef0ece6dc911ce744a2a2afc55cbd (patch)
treef91d24cfc656cffe53a1f16987b23c2ddeff49ca
parent4c4fd309cf5c0ea28c17c9b90ebe073a66dcaf72 (diff)
downloadre-name-7bf7902f4afef0ece6dc911ce744a2a2afc55cbd.tar.gz
Change coding style
This is the coding style used in Plan 9. Eschewing the whitespace here has several large benefits: 1. Control constructs are much easier to type. 2. Long conditions are very naturally handled: if(bla && bla && bla || yada && yada && yada) Because "if(" and "|| " (or "&& ") are the same length, the two parts of the condition are aligned. 3. It becomes much less bothersome to convert a single-statement body to a multiple-statement body. It may seem minor, but the extra space is extra work.
-rw-r--r--name.c36
-rw-r--r--re.c20
2 files changed, 28 insertions, 28 deletions
diff --git a/name.c b/name.c
index a98c051..9162b3c 100644
--- a/name.c
+++ b/name.c
@@ -24,10 +24,10 @@ main(int argc, char *argv[])
int c, i, j, s, xflag;
/* Allocate memory. */
- for (i = 0; i < MAXFILES; i++)
- if (!(froms[i]=malloc(MAXFILE)) || !(tos[i]=malloc(MAXFILE)))
+ for(i = 0; i < MAXFILES; i++)
+ if(!(froms[i]=malloc(MAXFILE)) || !(tos[i]=malloc(MAXFILE)))
err(1, "malloc");
- if (!(tmp = malloc(MAXLINE*4)))
+ if(!(tmp = malloc(MAXLINE*4)))
err(1, "malloc");
sprintf(file, "/var/tmp/re.%d", getppid());
@@ -37,7 +37,7 @@ main(int argc, char *argv[])
* is input to be read on standard in, as re(1) doesn't create
* the file until it starts writing on standard out.
*/
- if (waitstdin(), access(file, F_OK)) {
+ if(waitstdin(), access(file, F_OK)){
fprintf(stderr, "%s: %s does not exist\n", argv[0], file);
return 1;
}
@@ -45,8 +45,8 @@ main(int argc, char *argv[])
/* Read command-line options. */
name = argv[0];
xflag = 0;
- while ((c = getopt(argc, argv, "x")) != -1)
- switch (c) {
+ while((c = getopt(argc, argv, "x")) != -1)
+ switch(c){
case 'x':
xflag = 1;
break;
@@ -55,27 +55,27 @@ main(int argc, char *argv[])
}
argc -= optind;
argv += optind;
- if (argc > 0) {
+ if(argc > 0){
usage: fprintf(stderr, "usage: %s [-x]\n", name);
return 1;
}
- if (!(fp = fopen(file, "r")))
+ if(!(fp = fopen(file, "r")))
err(1, "fopen");
/* Read original file names. */
- for (i = 0; i < MAXFILES && fgets(tmp, MAXLINE, fp); i++) {
+ for(i = 0; i < MAXFILES && fgets(tmp, MAXLINE, fp); i++){
tmp[strcspn(tmp, "\n")] = 0;
strcpy(froms[i], tmp);
}
/* Read new file names. */
- for (j = 0; j < MAXFILES && fgets(tmp, MAXLINE, stdin); j++) {
+ for(j = 0; j < MAXFILES && fgets(tmp, MAXLINE, stdin); j++){
tmp[strcspn(tmp, "\n")] = 0;
strcpy(tos[j], tmp);
}
- if (i != j) {
+ if(i != j){
fprintf(stderr, "%s: too %s lines\n",
name, j > i ? "many" : "few");
return 1;
@@ -85,15 +85,15 @@ usage: fprintf(stderr, "usage: %s [-x]\n", name);
* The corresponding mv invocations are printed on standard out.
* If the -x flag is given, they are executed too.
*/
- for (i = 0; i < j; i++) {
+ for(i = 0; i < j; i++){
escape(froms[i], &tmp); printf("mv '%s'", tmp);
escape(tos[i], &tmp); printf(" '%s'\n", tmp);
- if (xflag) {
- if (!fork()) {
+ if(xflag){
+ if(!fork()){
execl("/bin/mv", "mv", froms[i], tos[i], NULL);
err(1, "execl");
}
- if (wait(&s), s) {
+ if(wait(&s), s){
fprintf(stderr, "/bin/mv exited with %d\n",
WEXITSTATUS(s));
return 1;
@@ -111,8 +111,8 @@ escape(char *str, char **res)
{
int i, j, offset;
- for (i = j = 0; str[i]; i++) {
- if (str[i] == '\'') {
+ for(i = j = 0; str[i]; i++){
+ if(str[i] == '\''){
strcat(*res, "'\\''");
j += 4;
} else
@@ -129,6 +129,6 @@ waitstdin()
FD_ZERO(&readfds);
FD_SET(0, &readfds);
- if (select(1, &readfds, NULL, NULL, NULL) == -1)
+ if(select(1, &readfds, NULL, NULL, NULL) == -1)
err(1, "select");
}
diff --git a/re.c b/re.c
index f86f3c2..9d3f230 100644
--- a/re.c
+++ b/re.c
@@ -9,35 +9,35 @@ main(int argc, char *argv[])
FILE *fp;
int i;
- if (argc-1 == 0) {
+ if(argc-1 == 0){
fprintf(stderr, "usage: %s file [...]\n", argv[0]);
return 1;
}
/* Ensure arguments do not contain newlines. */
- for (i = 1; i < argc; i++)
- for (a = argv[i]; *a; a++)
- if (*a == '\n') {
+ for(i = 1; i < argc; i++)
+ for(a = argv[i]; *a; a++)
+ if(*a == '\n'){
*a = '*';
fprintf(stderr,
- "%s contains newline (at *)\n",
+ "%s contains newline(at *)\n",
argv[i]);
return 1;
}
sprintf(file, "/var/tmp/re.%d", getppid());
- if (!access(file, F_OK)) {
+ if(!access(file, F_OK)){
do
fprintf(stderr, "%s already exists; overwrite? ", file);
- while (!gets(ans) || !*ans);
- if (ans[0] != 'y') return 1;
+ while(!gets(ans) || !*ans);
+ if(ans[0] != 'y') return 1;
}
- if (!(fp = fopen(file, "w")))
+ if(!(fp = fopen(file, "w")))
err(1, "fopen");
- for (i = 1; i < argc; i++) {
+ for(i = 1; i < argc; i++){
fprintf(fp, "%s\n", argv[i]);
printf("%s\n", argv[i]);
}