A couple more stylistic changes to uudecode(1)
This commit is contained in:
parent
ed9985205b
commit
bf2b270946
33
uudecode.c
33
uudecode.c
|
@ -40,23 +40,19 @@ main(int argc, char *argv[])
|
||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
checkheader(stdin, "<stdin>", "begin ", &mode, &fname);
|
checkheader(stdin, "<stdin>", "begin ", &mode, &fname);
|
||||||
nfp = checkfile(fname);
|
if ((nfp = checkfile(fname)) == NULL)
|
||||||
if (nfp == NULL)
|
|
||||||
eprintf("fopen %s:", fname);
|
eprintf("fopen %s:", fname);
|
||||||
uudecode(stdin, nfp);
|
uudecode(stdin, nfp);
|
||||||
fclose(nfp);
|
fclose(nfp);
|
||||||
} else {
|
} else {
|
||||||
if (!(fp = fopen(argv[0], "r")))
|
if ((fp = fopen(argv[0], "r")) == NULL)
|
||||||
eprintf("fopen %s:", argv[0]);
|
eprintf("fopen %s:", argv[0]);
|
||||||
checkheader(fp, argv[0], "begin ", &mode, &fname);
|
checkheader(fp, argv[0], "begin ", &mode, &fname);
|
||||||
nfp = checkfile(fname);
|
if ((nfp = checkfile(fname)) == NULL)
|
||||||
if (nfp == NULL) {
|
|
||||||
fclose(fp);
|
|
||||||
eprintf("fopen %s:", fname);
|
eprintf("fopen %s:", fname);
|
||||||
}
|
|
||||||
uudecode(fp, nfp);
|
uudecode(fp, nfp);
|
||||||
fclose(fp);
|
|
||||||
fclose(nfp);
|
fclose(nfp);
|
||||||
|
fclose(fp);
|
||||||
}
|
}
|
||||||
if (fchmod(fileno(nfp), mode) < 0)
|
if (fchmod(fileno(nfp), mode) < 0)
|
||||||
eprintf("chmod %s:", fname);
|
eprintf("chmod %s:", fname);
|
||||||
|
@ -70,7 +66,7 @@ checkfile(const char *fname)
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (strcmp(fname,"/dev/stdout") == 0)
|
if (strcmp(fname, "/dev/stdout") == 0)
|
||||||
return stdout;
|
return stdout;
|
||||||
ret = lstat(fname, &st);
|
ret = lstat(fname, &st);
|
||||||
/* if it is a new file, try to open it */
|
/* if it is a new file, try to open it */
|
||||||
|
@ -112,7 +108,7 @@ checkheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fn
|
||||||
/* now mode should be null terminated, q points to fname */
|
/* now mode should be null terminated, q points to fname */
|
||||||
checkmode(p,mode);
|
checkmode(p,mode);
|
||||||
n = strlen(q);
|
n = strlen(q);
|
||||||
while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r'))
|
while (n > 0 && (q[n - 1] == '\n' || q[n - 1] == '\r'))
|
||||||
q[--n] = '\0';
|
q[--n] = '\0';
|
||||||
if (n > 0)
|
if (n > 0)
|
||||||
*fname = q;
|
*fname = q;
|
||||||
|
@ -126,9 +122,8 @@ checkmode(const char *str,mode_t *validmode)
|
||||||
|
|
||||||
if (str == NULL || str == '\0')
|
if (str == NULL || str == '\0')
|
||||||
eprintf("invalid mode\n");
|
eprintf("invalid mode\n");
|
||||||
|
|
||||||
octal = strtol(str, &end, 8);
|
octal = strtol(str, &end, 8);
|
||||||
if (*end == '\0') { /* successful conversion from a valid str */
|
if (*end == '\0') {
|
||||||
if (octal >= 0 && octal <= 07777) {
|
if (octal >= 0 && octal <= 07777) {
|
||||||
if(octal & 04000) *validmode |= S_ISUID;
|
if(octal & 04000) *validmode |= S_ISUID;
|
||||||
if(octal & 02000) *validmode |= S_ISGID;
|
if(octal & 02000) *validmode |= S_ISGID;
|
||||||
|
@ -142,7 +137,6 @@ checkmode(const char *str,mode_t *validmode)
|
||||||
if(octal & 00004) *validmode |= S_IROTH;
|
if(octal & 00004) *validmode |= S_IROTH;
|
||||||
if(octal & 00002) *validmode |= S_IWOTH;
|
if(octal & 00002) *validmode |= S_IWOTH;
|
||||||
if(octal & 00001) *validmode |= S_IXOTH;
|
if(octal & 00001) *validmode |= S_IXOTH;
|
||||||
*validmode &= 07777;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,9 +144,9 @@ checkmode(const char *str,mode_t *validmode)
|
||||||
static void
|
static void
|
||||||
uudecode(FILE *fp, FILE *outfp)
|
uudecode(FILE *fp, FILE *outfp)
|
||||||
{
|
{
|
||||||
char *bufb=NULL, *p,*nl;
|
char *bufb = NULL, *p, *nl;
|
||||||
size_t n=0;
|
size_t n=0;
|
||||||
int ch , i;
|
int ch, i;
|
||||||
|
|
||||||
#define DEC(c) (((c) - ' ') & 077) /* single character decode */
|
#define DEC(c) (((c) - ' ') & 077) /* single character decode */
|
||||||
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
|
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
|
||||||
|
@ -163,11 +157,12 @@ uudecode(FILE *fp, FILE *outfp)
|
||||||
while (afgets(&bufb,&n,fp)) {
|
while (afgets(&bufb,&n,fp)) {
|
||||||
p = bufb;
|
p = bufb;
|
||||||
/* trim newlines */
|
/* trim newlines */
|
||||||
if ((nl=strchr(bufb, '\n')) != NULL)
|
if ((nl = strchr(bufb, '\n')) != NULL)
|
||||||
*nl = '\0';
|
*nl = '\0';
|
||||||
else
|
else
|
||||||
eprintf("no newline found, aborting\n");
|
eprintf("no newline found, aborting\n");
|
||||||
if ((i = DEC(*p)) <= 0) /* check for last line */
|
/* check for last line */
|
||||||
|
if ((i = DEC(*p)) <= 0)
|
||||||
break;
|
break;
|
||||||
for (++p; i > 0; p += 4, i -= 3) {
|
for (++p; i > 0; p += 4, i -= 3) {
|
||||||
if (i >= 3) {
|
if (i >= 3) {
|
||||||
|
@ -203,7 +198,7 @@ uudecode(FILE *fp, FILE *outfp)
|
||||||
eprintf("read error:");
|
eprintf("read error:");
|
||||||
}
|
}
|
||||||
/* check for end or fail */
|
/* check for end or fail */
|
||||||
afgets(&bufb,&n,fp);
|
afgets(&bufb, &n, fp);
|
||||||
if (strnlen(bufb,3) < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
|
if (strnlen(bufb, 3) < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
|
||||||
eprintf("valid uudecode footer \"end\" not found\n");
|
eprintf("valid uudecode footer \"end\" not found\n");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user