494 Chapter 10. Primitives and EEL Subroutines
name if any, password if any, and the “file name”: the final partof a URL, that may be a file name, a web
page name or something else. Since an empty user name or password is legal, but is different from an
omitted one, there are also fields to specify if each of these is present.
Theprepare_url_operation()subroutine parses a URL and fills one of these structures. It
complains if it doesn’t recognize the service name, or if theservice is something other than FTP but the
operation isn’t reading. The operation code is one of those used with theftp_op()subroutine described on
page 491. For example, it complains if you try to perform anFTP_LISToperation with a telnet:// URL. It
also prompts for a password if necessary, and saves the password for later use, by calling the
get_password()subroutine.
Theget_password()subroutine gets the password for a particular user/host combination. Specify the
user and host, and the subroutine will fill in the provided character arrayreswith the password. The first
time it will prompt the user for the information; it will thenstore the information and return it without
prompting in future requests. The subroutine is careful to make sure the password never appears in a state
file or session file. To discard a particular remembered password, passNULLas the first parameter. The next
timeget_password()is asked for the password of that user on that host, it will prompt the user again.
Theprepare_url_operation()subroutine calls theparse_url()subroutine to actually parse the
URL into aurl_partsstructure. The latter returns zero if the URL is invalid, or nonzero if it appears to be
legal.
Thedivide_url()subroutine is similar toparse_url(), but doesn’t divide the host section into its
component parts. Likeparse_url(), it returns zero if the URL is invalid, or nonzero if it appears to be
legal.
For example, given the URLscp://bob:secret%
[email protected]:1022/path/to/file,
bothdivide_url()andparse_url()set theservicemember of theurl_partsstructure to “scp” and
thefnamemember to “path/to/file”.
Butdivide_url()then sets thehostmember to “bob:secret%
[email protected]:1022”,
whereasparse_url()setshostto “example.com”,portto “1022”,usrto “bob”, andpwdto
“secret/code”, also setting thehave_passwordandhave_usrmembers nonzero since the URL
specified both. Notice thatparse_url()decodes any %-escaped sequences in the user name or password
sections, changing%2Fto/in this example.
int split_string(char *part1, char *cs, char *part2)
int reverse_split_string(char *part1, char *cs, char *part2)
Theparse_url()subroutine uses two helper subroutines. Thesplit_string()subroutine divides a
stringpart1into two parts, by searching it for one of a set of delimiter characterscs. It finds the first
character inpart1that appears incs. Then it copies the remainder ofpart1topart2, and removes the
delimiter character and the remainder frompart1. It returns the delimiter character it found. If no delimiter
character appears inpart1, it setspart2to""and returns0. Thereverse_split_string()subroutine
is almost identical; it just searches throughpart1from the other end, and splits the string at the last
character inpart1that appears incs.
char *get_url_file_part(char *url, int sep)
Theget_url_file_part()subroutine helps to parse URLs. It takes a URL and returns a pointer to a
position within it where its file part begins. For example, inthe URL
http://www.lugaru.com/why-lugaru.html, the subroutine returns a pointer to the start of “why”. If
sepis nonzero, the subroutine instead returns a pointer to the /just before “why”. If its parameter is not a
URL, the subroutine returns a pointer to its first character.