Open Computing ``Hands-On'': ``Answers to Unix'' Column: October 1994: Listings

Listing 1: The dec2oct command converts decimal numbers to their equivalent octal values.

awk 'BEGIN {print "Enter decimal number to convert to octal: "
            print "(Enter ^D to terminate)" }
     {printf "%d in octal is %o\n", $1, $1}'


Listing 2: A program to show file permissions as octal values.

 1: #include 
 2: #include 
 3: #include 
 4:
 5: main(int argc, char **argv)
 6: {
 7:    int i;
 8:    struct stat sbuf;
 9:
10:    if (argc < 2)
11:        fprintf(stderr, "usage: %s file(s)\n", argv[0]);
12:    else
13:        for (i = 1; i < argc; ++i)
14:            if (stat(argv[i], &sbuf))
15:                fprintf(stderr, "%s: can't stat %s\n", argv[0], argv[i]);
16:            else
17:                printf("%03o\t%s\n", (int) (sbuf.st_mode & 0xffff), argv[i]);
18: }

Listing 3: A Perl command that lists subdirectories in the current directory.

A. As a ``one line'' Perl command:

perl -e 'opendir(DIR,"."); while ($_=readdir(DIR)){stat;next unless -d $_;print "$_\n";}'

B. In a slightly more readable form, suitable for saving as an executable file:

#!/usr/local/bin/perl
opendir(DIR,".");
while ($_ = readdir(DIR)){
   stat;
   next unless -d $_;
   print "$_\n";
}

Last Modified: