|
1850 | 1850 | \pdfbookmark[1]{argv, argc}{args} |
1851 | 1851 |
|
1852 | 1852 | \begin{slide} |
1853 | | -\sltitle{Funkce \texttt{main()}} |
| 1853 | +\sltitle{Function \texttt{main()}} |
1854 | 1854 | \renewcommand{\baselinestretch}{0.4} |
1855 | 1855 | \begin{itemize} |
1856 | 1856 | \setlength{\itemsep}{-0.5ex} |
1857 | 1857 | \setlength{\topsep}{1\itemsep} |
1858 | | -\item pøi spu¹tìní programu je pøedáno øízení funkci \texttt{main()}. |
| 1858 | +\item function \texttt{main()} is called upon program execution |
1859 | 1859 | \item \texttt{int \funnm{main} (int \emph{argc}, char *\emph{argv}[]);} |
1860 | 1860 | \begin{itemize} |
1861 | | - \item \texttt{argc} \dots poèet argumentù pøíkazové øádky |
1862 | | - \item \texttt{argv} \dots pole argumentù |
| 1861 | + \item \texttt{argc} \dots number of command line arguments |
| 1862 | + \item \texttt{argv} \dots array of command line arguments |
1863 | 1863 | \begin{itemize2} |
1864 | | - \item podle konvence je \texttt{argv[0]} jméno programu (bez cesty) |
1865 | | - \item poslední prvek je \texttt{argv[argc] == NULL} |
| 1864 | + \item \texttt{argv[0]} is a executed program name (without path) |
| 1865 | + \item the last item is \texttt{argv[argc] == NULL} |
1866 | 1866 | \end{itemize2} |
1867 | | - \item návrat z \texttt{main()} nebo volání \texttt{exit()} ukonèí program |
1868 | | - \item standardní návratové hodnoty \verb#EXIT_SUCCESS# (0) a |
| 1867 | + \item returnin from \texttt{main()} or callin \texttt{exit()} terminates the |
| 1868 | + process |
| 1869 | + \item standard return values are \verb#EXIT_SUCCESS# (0) a |
1869 | 1870 | \verb#EXIT_FAILURE# (1) |
1870 | 1871 | \end{itemize} |
1871 | 1872 | \end{itemize} |
|
1876 | 1877 | \end{slide} |
1877 | 1878 |
|
1878 | 1879 | \begin{itemize} |
1879 | | -\item první parametr (typu \texttt{int}) udává poèet argumentù na pøíkazovém |
1880 | | -øádku (vèet\-nì argumentu 0 -- jména programu) a druhý parametr (typu |
1881 | | -\texttt{char**}) je pole ukazatelù na tyto øetìzce. Za posledním øetìzcem je |
1882 | | -je¹tì ukonèovací \texttt{NULL} pointer -- pozor na to, ¾e to je nìco jiného ne¾ |
1883 | | -prázdný øetìzec. |
1884 | | -\item \label{SHELL_ARGV0} \texttt{argv[0]} nìkdy bývá dùle¾itým zdrojem pøidané |
1885 | | -informace. Na Solarisu jsou napøíklad pøíkazy \texttt{cp}, \texttt{mv} a |
1886 | | -\texttt{ln} nalinkované na stejný soubor. Hodnota \texttt{argv[0]} pak urèuje, |
1887 | | -jakou funkci vlastnì proces má. Jiný pøíklad -- pokud má shell první znak z |
1888 | | -\texttt{argv[0]} nastaven na ``-'', znamená to, ¾e se má chovat jako login shell |
1889 | | -(podívejte se tøeba do manuálové stránky pro \texttt{bash} na sekci INVOCATION, |
1890 | | -pokud nevíte, co to je login shell). Ve výpisu procesù pak uvidíte ``-bash''. |
1891 | | -Toto není souèást UNIX specifikace pro \texttt{sh}, ale pou¾íval to ji¾ Bourne |
1892 | | -shell na UNIXu V7 (1979) a ostatní shelly to pøevzaly. |
1893 | | -\item pøi spu¹tìní programu pøedá kód dynamického linkeru øízení funkci |
1894 | | -\texttt{main}, viz strana \pageref{RUNTIMELINKER}. Staticky slinkovanému |
1895 | | -programu se øízení pøedá pøímo. Ne\-pøí\-tom\-nost \texttt{main} v programu |
1896 | | -zpùsobí chybu pøi pøekladu na úrovni linkeru. Této funkci se pøedá jméno |
1897 | | -spu¹tìného programu, argumenty z pøíkazové øádky a pøípadnì i pro\-mìn\-né |
1898 | | -prostøedí. Ukonèení této funkce znamená konec programu a návratová hodnota se |
1899 | | -pou¾ije jako kód ukonèení programu pro OS. Jinou mo¾ností ukonèení programu je |
1900 | | -pou¾ití funkce \texttt{exit} nebo \verb#_exit#, kterou lze pou¾ít |
1901 | | -kdykoliv, nejen ve funkci \texttt{main}. V C lze pou¾ívat obì metody |
1902 | | -ukonèení programu. |
1903 | | -\item pøedání promìnných prostøedí tøetím parametrem typu \texttt{char**} |
1904 | | -není souèástí normativní èásti C standardu, pouze informativní. Pøekladaèe to |
1905 | | -ale typicky podporují. Varianta \texttt{main} s pro\-mìn\-ný\-mi prostøedí |
1906 | | -pak vypadá takto:\\ \texttt{int \funnm{main}(int \emph{argc}, char |
1907 | | -*\emph{argv}[], char *\emph{envp}[]);} |
1908 | | -\item návratový typ funkce \texttt{main} by mìl být v¾dy \texttt{int}; pøekladaè |
1909 | | -si jinak bude stì¾ovat. \emsl{Z této hodnoty se ale pou¾ije pouze nejni¾¹ích 8 |
1910 | | -bitù}, a je to nezáporné èíslo. Pozor na to, ¾e na rozdíl od konvence jazyka C |
1911 | | -návratová hodnota 0 má v shellu význam true (úspìch) a nenula význam false |
1912 | | -(neúspìch). Typická konstrukce v shellu vypadá takto: |
| 1880 | +\item The first argument (of type \texttt{int}) is the number of arguments on a |
| 1881 | +command line, including argument 0 -- the program name. The second argument is |
| 1882 | +an array of strings representing such command line arguments. There is a |
| 1883 | +terminating \texttt{NULL} as the last array item. Note that \texttt{NULL} is |
| 1884 | +different from an empty string. |
| 1885 | +\item To go through all the command line arguments, you can either use |
| 1886 | +\emph{argc} or test for \texttt{NULL} in \texttt{argv[i]}. |
| 1887 | +\item \label{SHELL_ARGV0} \texttt{argv[0]} is sometimes a source of additional |
| 1888 | +information. For example, commands \texttt{cp}, \texttt{mv} and \texttt{ln} may |
| 1889 | +be linked to the same executable (Solaris). The value of \texttt{argv[0]} then |
| 1890 | +tells the process what function it is supposed to perform. Another example -- if |
| 1891 | +the first character of a shell process \texttt{argv[0]} is set to ``-'' it means |
| 1892 | +the shell process is supposed to act as a login shell (check the \texttt{bash} |
| 1893 | +man page, section INVOCATION, for more information on what being a login shell |
| 1894 | +means). In the process listing then you will see ``\texttt{-bash}''. This |
| 1895 | +convention is not part of the UNIX specification for \texttt{sh} but it has been |
| 1896 | +already used in the Bourne shell on UNIX V7 (1979) and other shells followed the |
| 1897 | +suite. |
| 1898 | +\item We already know that upon the program execution, the dynamic linker |
| 1899 | +eventually passes the control to function \texttt{main}, as explained on page |
| 1900 | +\pageref{RUNTIMELINKER}. If the \texttt{main} function is missing, the |
| 1901 | +compilation fails during the linking phase. When \texttt{main} finishes it |
| 1902 | +means the process finishes. You can also use functions \texttt{exit()} or |
| 1903 | +\verb#_exit()# from anywhere in the program, ie. not just from function |
| 1904 | +\texttt{main}. |
| 1905 | +\item Passing the environment in the third parameter of type |
| 1906 | +\texttt{char**} is not part of the normative part of the C standard, only the |
| 1907 | +informative one. C compilers typically support that though. |
| 1908 | +The \texttt{main} variant with the 3rd parameter looks like this:\\ \texttt{int |
| 1909 | +\funnm{main}(int \emph{argc}, char *\emph{argv}[], char *\emph{envp}[]);} |
| 1910 | +\item The return value type of \texttt{main} should be always \texttt{int}. |
| 1911 | +\emsl{Only lower 8 bits from that integer are only used though.} It is always a |
| 1912 | +non-negative number. Note that in contrast to the C convention, the \texttt{0} |
| 1913 | +return value means a success in a Unix shell, and a non-zero value means a |
| 1914 | +failure. A typical construct in a shell looks like this: |
| 1915 | + |
1913 | 1916 | \begin{verbatim} |
1914 | | -if ! prog; then |
1915 | | - echo "failure" |
1916 | | -else |
| 1917 | +if prog; then |
1917 | 1918 | echo "success" |
| 1919 | +else |
| 1920 | + echo "failure" |
1918 | 1921 | fi |
1919 | 1922 | \end{verbatim} |
1920 | | -Pøíklad: \example{main/return-256.c}. |
1921 | | -\item \label{RETURN255} nepou¾ívejte proto ve funkci \texttt{main} |
1922 | | -\texttt{return (-1)} a nikde pak ani |
1923 | | -\texttt{exit(-1)}, z toho vznikne návratová hodnota \texttt{255} a kód je |
1924 | | -matoucí. Je vùbec velmi vhodné pou¾ívat pouze \texttt{0} a \texttt{1}, pokud |
1925 | | -není |
1926 | | -zá\-sad\-ní dùvod pro jiné hodnoty, tøeba ten ¾e jich potøebujete více -- mù¾ete |
1927 | | -se podívat napøíklad na manuálovou stránku pro \texttt{passwd} na Solarisu, |
1928 | | -sekce \texttt{EXIT STATUS}. Pøíklad: \example{main/return-negative-1.c}. |
1929 | | - |
1930 | | - |
1931 | | -\item rozdíl mezi funkcemi \texttt{exit} a \verb#_exit# je v tom, ¾e |
1932 | | -\texttt{exit} pøed ukonèením programu je¹tì vyprázdní (pomocí knihovní |
1933 | | -funkce \texttt{fflush}) a zavøe streamy a volá funkce |
1934 | | -zaregistrované pomocí \texttt{atexit}. V závislosti na systému to mohou být i |
1935 | | -dal¹í akce. |
1936 | | - |
1937 | | -Pro zajímavost, napøíklad ve FreeBSD je \texttt{\_exit} systémové |
1938 | | -volání a \texttt{exit} knihovní funkce, ale na Solarisu jsou obì systémovými |
1939 | | -voláními. Pøíklad: \example{exit/exit.c} |
1940 | | -\item \label{MAIN_C} pøíklad: \example{main/print-argv.c} |
| 1923 | + |
| 1924 | +or: |
| 1925 | + |
| 1926 | +\begin{verbatim} |
| 1927 | +prog && echo "success" || echo "failure" |
| 1928 | +\end{verbatim} |
| 1929 | +Example: \example{main/return-256.c}. |
| 1930 | +\item \label{RETURN255} Never use \texttt{return (-1)} in \texttt{main} and |
| 1931 | +neither \texttt{exit(-1)}. Based on the information in the previous paragraph, |
| 1932 | +from \texttt{-1} you will get \texttt{255} as the return value you get in the |
| 1933 | +shell in \texttt{\$?}. It just creates confusion. |
| 1934 | +\item It is very reasonable to use only \texttt{EXIT\_SUCCESS} (\texttt{0}) and |
| 1935 | +\texttt{EXIT\_FAILURE} (\texttt{1}) unless there is a valid reason for other |
| 1936 | +values. Sometimes you might need more values to distinguish between failures. |
| 1937 | +For example, the \texttt{passwd} command on Solaris have quite a few of them, go |
| 1938 | +check its manual page if interested, section \texttt{EXIT STATUS}. Example: |
| 1939 | +\example{main/return-negative-1.c}. |
| 1940 | +\item The difference between function \texttt{exit()} and \verb#_exit()# is that |
| 1941 | +\texttt{exit} also flushes and closes streams (try it out with \texttt{printf()} |
| 1942 | +\emsl{without} printing a new line), and calls functions registered via |
| 1943 | +\texttt{atexit()}, and possibly other actions based on a specific system. |
| 1944 | +Example: \example{exit/exit.c} |
| 1945 | +\item \label{MAIN_C} Example on printing out commnd line arguments: |
| 1946 | +\example{main/print-argv.c} |
| 1947 | +\item If a process is killed by a signal, you can get the signal number from its |
| 1948 | +return value as presented by the shell. See page |
| 1949 | +\pageref{SHELLRETVALUEFORSIGNALS}. |
1941 | 1950 | \end{itemize} |
1942 | 1951 |
|
1943 | 1952 | %%%%% |
|
0 commit comments