xref: /freebsd/usr.bin/ar/ar.1 (revision 02e9120893770924227138ba49df1edb3896112a)
1.\" Copyright (c) 2007 Joseph Koshy.  All rights reserved.
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions
5.\" are met:
6.\" 1. Redistributions of source code must retain the above copyright
7.\"    notice, this list of conditions and the following disclaimer.
8.\" 2. Redistributions in binary form must reproduce the above copyright
9.\"    notice, this list of conditions and the following disclaimer in the
10.\"    documentation and/or other materials provided with the distribution.
11.\"
12.\" This software is provided by Joseph Koshy ``as is'' and
13.\" any express or implied warranties, including, but not limited to, the
14.\" implied warranties of merchantability and fitness for a particular purpose
15.\" are disclaimed.  in no event shall Joseph Koshy be liable
16.\" for any direct, indirect, incidental, special, exemplary, or consequential
17.\" damages (including, but not limited to, procurement of substitute goods
18.\" or services; loss of use, data, or profits; or business interruption)
19.\" however caused and on any theory of liability, whether in contract, strict
20.\" liability, or tort (including negligence or otherwise) arising in any way
21.\" out of the use of this software, even if advised of the possibility of
22.\" such damage.
23.\"
24.Dd January 3, 2022
25.Dt AR 1
26.Os
27.Sh NAME
28.Nm ar ,
29.Nm ranlib
30.Nd manage archives
31.Sh SYNOPSIS
32.Nm
33.Fl d
34.Op Fl T
35.Op Fl f
36.Op Fl j
37.Op Fl v
38.Op Fl z
39.Ar archive
40.Ar
41.Nm
42.Fl m
43.Op Fl T
44.Op Fl a Ar position-after
45.Op Fl b Ar position-before
46.Op Fl f
47.Op Fl i Ar position-before
48.Op Fl j
49.Op Fl s | Fl S
50.Op Fl z
51.Ar archive
52.Ar
53.Nm
54.Fl p
55.Op Fl T
56.Op Fl f
57.Op Fl v
58.Ar archive
59.Op Ar
60.Nm
61.Fl q
62.Op Fl T
63.Op Fl c
64.Op Fl D
65.Op Fl f
66.Op Fl s | Fl S
67.Op Fl U
68.Op Fl v
69.Op Fl z
70.Ar archive
71.Ar
72.Nm
73.Fl r
74.Op Fl T
75.Op Fl a Ar position-after
76.Op Fl b Ar position-before
77.Op Fl c
78.Op Fl D
79.Op Fl f
80.Op Fl i Ar position-before
81.Op Fl j
82.Op Fl s | Fl S
83.Op Fl u
84.Op Fl U
85.Op Fl v
86.Op Fl z
87.Ar archive
88.Ar
89.Nm
90.Fl s
91.Op Fl j
92.Op Fl z
93.Ar archive
94.Nm
95.Fl t
96.Op Fl f
97.Op Fl T
98.Op Fl v
99.Ar archive
100.Op Ar
101.Nm
102.Fl x
103.Op Fl C
104.Op Fl T
105.Op Fl f
106.Op Fl o
107.Op Fl u
108.Op Fl v
109.Ar archive
110.Op Ar
111.Nm
112.Fl M
113.Nm ranlib
114.Op Fl D
115.Op Fl U
116.Ar archive ...
117.Sh DESCRIPTION
118The
119.Nm
120utility creates and maintains groups of files combined into an
121archive.
122Once an archive has been created, new files can be added to it, and
123existing files can be extracted, deleted or replaced.
124.Pp
125Files are named in the archive by their last file name component,
126so if a file referenced by a path containing a
127.Dq /
128is archived, it will be named by the last component of the path.
129Similarly when matching paths listed on the command line against
130file names stored in the archive, only the last component of the
131path will be compared.
132.Pp
133The normal use of
134.Nm
135is for the creation and maintenance of libraries suitable for use
136with the link editor
137.Xr ld 1 ,
138although it is not restricted to this purpose.
139The
140.Nm
141utility can create and manage an archive symbol table (see
142.Xr ar 5 )
143used to speed up link editing operations.
144If a symbol table is present in an archive, it will be
145kept up-to-date by subsequent operations on the archive.
146.Pp
147The
148.Nm ranlib
149utility is used to add an archive symbol table
150to an existing archive.
151.Sh OPTIONS
152The
153.Nm
154utility supports the following options:
155.Bl -tag -width indent
156.It Fl a Ar member-after
157When used with option
158.Fl m
159this option specifies that the archive members specified by
160arguments
161.Ar
162are moved to after the archive member named by argument
163.Ar member-after .
164When used with option
165.Fl r
166this option specifies that the files specified by arguments
167.Ar
168are added after the archive member named by argument
169.Ar member-after .
170.It Fl b Ar member-before
171When used with option
172.Fl m
173this option specifies that the archive members specified by
174arguments
175.Ar
176are moved to before the archive member named by argument
177.Ar member-before .
178When used with option
179.Fl r
180this option specifies that the files specified by arguments
181.Ar
182are added before the archive member named by argument
183.Ar member-before .
184.It Fl c
185Suppress the informational message printed when a new archive is
186created using the
187.Fl r
188and
189.Fl q
190options.
191.It Fl C
192Prevent extracted files from replacing like-named files
193in the file system.
194.It Fl d
195Delete the members named by arguments
196.Ar
197from the archive specified by argument
198.Ar archive .
199The archive's symbol table, if present, is updated to reflect
200the new contents of the archive.
201.It Fl D
202When used in combination with the
203.Fl r
204or
205.Fl q
206option,
207with the
208.Fl s
209option without other options, or when invoked as
210.Nm ranlib ,
211insert 0's instead of the real mtime, uid and gid values
212and 0644 instead of file mode from the members named by arguments
213.Ar .
214This ensures that checksums on the resulting archives are reproducible
215when member contents are identical.
216This option is enabled by default.
217If multiple
218.Fl D
219and
220.Fl U
221options are specified on the command line, the final one takes precedence.
222.It Fl f
223Use only the first fifteen characters of the archive member name or
224command line file name argument when naming archive members.
225.It Fl i Ar member-before
226Synonymous with option
227.Fl b .
228.It Fl j
229This option is accepted but ignored.
230.It Fl l
231This option is accepted for compatibility with GNU
232.Xr ar 1 ,
233but is ignored.
234.It Fl m
235Move archive members specified by arguments
236.Ar
237within the archive.
238If a position has been specified by one of the
239.Fl a ,
240.Fl b
241or
242.Fl i
243options, the members are moved to before or after the specified
244position.
245If no position has been specified, the specified members are moved
246to the end of the archive.
247If the archive has a symbol table, it is updated to reflect the
248new contents of the archive.
249.It Fl M
250Read and execute MRI librarian commands from standard input.
251The commands understood by the
252.Nm
253utility are described in the section
254.Sx "MRI Librarian Commands" .
255.It Fl o
256Preserve the original modification times of members when extracting
257them.
258.It Fl p
259Write the contents of the specified archive members named by
260arguments
261.Ar
262to standard output.
263If no members were specified, the contents of all the files in the
264archive are written in the order they appear in the archive.
265.It Fl q
266Append the files specified by arguments
267.Ar
268to the archive specified by argument
269.Ar archive
270without checking if the files already exist in the archive.
271The archive symbol table will be updated as needed.
272If the file specified by the argument
273.Ar archive
274does not already exist, a new archive will be created.
275.It Fl r
276Replace (add) the files specified by arguments
277.Ar
278in the archive specified by argument
279.Ar archive ,
280creating the archive if necessary.
281Replacing existing members will not change the order of members within
282the archive.
283If a file named in arguments
284.Ar
285does not exist, existing members in the archive that match that
286name are not changed.
287New files are added to the end of the archive unless one of the
288positioning options
289.Fl a ,
290.Fl b
291or
292.Fl i
293is specified.
294The archive symbol table, if it exists, is updated to reflect the
295new state of the archive.
296.It Fl s
297Add an archive symbol table (see
298.Xr ar 5 )
299to the archive specified by argument
300.Ar archive .
301Invoking
302.Nm
303with the
304.Fl s
305option alone is equivalent to invoking
306.Nm ranlib .
307.It Fl S
308Do not generate an archive symbol table.
309.It Fl t
310List the files specified by arguments
311.Ar
312in the order in which they appear in the archive, one per line.
313If no files are specified, all files in the archive are listed.
314.It Fl T
315This option is accepted but ignored.
316In other implementations of
317.Nm ,
318.Fl T
319creates a "thin" archive.
320.It Fl u
321Conditionally update the archive or extract members.
322When used with the
323.Fl r
324option, files named by arguments
325.Ar
326will be replaced in the archive if they are newer than their
327archived versions.
328When used with the
329.Fl x
330option, the members specified by arguments
331.Ar
332will be extracted only if they are newer than the corresponding
333files in the file system.
334.It Fl U
335When used in combination with the
336.Fl r
337or
338.Fl q
339option, insert the real mtime, uid and gid, and file mode values
340from the members named by arguments
341.Ar .
342If multiple
343.Fl D
344and
345.Fl U
346options are specified on the command line, the final one takes precedence.
347.It Fl v
348Provide verbose output.
349When used with the
350.Fl d ,
351.Fl m ,
352.Fl q
353or
354.Fl x
355options,
356.Nm
357gives a file-by-file description of the archive modification being
358performed, which consists of three white-space separated fields:
359the option letter, a dash
360.Dq "-" ,
361and the file name.
362When used with the
363.Fl r
364option,
365.Nm
366displays the description as above, but the initial letter is an
367.Dq a
368if the file is added to the archive, or an
369.Dq r
370if the file replaces a file already in the archive.
371When used with the
372.Fl p
373option, the name of the file enclosed in
374.Dq <
375and
376.Dq >
377characters is written to standard output preceded by a single newline
378character and followed by two newline characters.
379The contents of the named file follow the file name.
380When used with the
381.Fl t
382option,
383.Nm
384displays eight whitespace separated fields:
385the file permissions as displayed by
386.Xr strmode 3 ,
387decimal user and group IDs separated by a slash (
388.Dq / Ns ) ,
389the file size in bytes, the file modification time in
390.Xr strftime 3
391format
392.Dq "%b %e %H:%M %Y" ,
393and the name of the file.
394.It Fl V
395Print a version string and exit.
396.It Fl x
397Extract archive members specified by arguments
398.Ar
399into the current directory.
400If no members have been specified, extract all members of the archive.
401If the file corresponding to an extracted member does not exist it
402will be created.
403If the file corresponding to an extracted member does exist, its owner
404and group will not be changed while its contents will be overwritten
405and its permissions will set to that entered in the archive.
406The file's access and modification time would be that of the time
407of extraction unless the
408.Fl o
409option was specified.
410.It Fl z
411This option is accepted but ignored.
412.El
413.Ss "MRI Librarian Commands"
414If the
415.Fl M
416option is specified, the
417.Nm
418utility will read and execute commands from its standard input.
419If standard input is a terminal, the
420.Nm
421utility will display the prompt
422.Dq Li "AR >"
423before reading a line, and will continue operation even if errors are
424encountered.
425If standard input is not a terminal, the
426.Nm
427utility will not display a prompt and will terminate execution on
428encountering an error.
429.Pp
430Each input line contains a single command.
431Words in an input line are separated by whitespace characters.
432The first word of the line is the command, the remaining words are
433the arguments to the command.
434The command word may be specified in either case.
435Arguments may be separated by commas or blanks.
436.Pp
437Empty lines are allowed and are ignored.
438Long lines are continued by ending them with the
439.Dq Li +
440character.
441.Pp
442The
443.Dq Li *
444and
445.Dq Li "\&;"
446characters start a comment.
447Comments extend till the end of the line.
448.Pp
449When executing an MRI librarian script the
450.Nm
451utility works on a temporary copy of an archive.
452Changes to the copy are made permanent using the
453.Ic save
454command.
455.Pp
456Commands understood by the
457.Nm
458utility are:
459.Bl -tag -width indent
460.It Ic addlib Ar archive | Ic addlib Ar archive Pq Ar member Oo Li , Ar member Oc Ns ...
461Add the contents of the archive named by argument
462.Ar archive
463to the current archive.
464If specific members are named using the arguments
465.Ar member ,
466then those members are added to the current archive.
467If no members are specified, the entire contents of the archive
468are added to the current archive.
469.It Ic addmod Ar member Oo Li , Ar member Oc Ns ...
470Add the files named by arguments
471.Ar member
472to the current archive.
473.It Ic clear
474Discard all the contents of the current archive.
475.It Ic create Ar archive
476Create a new archive named by the argument
477.Ar archive ,
478and makes it the current archive.
479If the named archive already exists, it will be overwritten
480when the
481.Ic save
482command is issued.
483.It Ic delete Ar module Oo Li , Ar member Oc Ns ...
484Delete the modules named by the arguments
485.Ar member
486from the current archive.
487.It Ic directory Ar archive Po Ar member Oo Li , Ar member Oc Ns ... Pc Op Ar outputfile
488List each named module in the archive.
489The format of the output depends on the verbosity setting set using
490the
491.Ic verbose
492command.
493Output is sent to standard output, or to the file specified by
494argument
495.Ar outputfile .
496.It Ic end
497Exit successfully from the
498.Nm
499utility.
500Any unsaved changes to the current archive will be discarded.
501.It Ic extract Ar member Oo Li , Ar member Oc Ns ...
502Extract the members named by the arguments
503.Ar member
504from the current archive.
505.It Ic list
506Display the contents of the current archive in verbose style.
507.It Ic open Ar archive
508Open the archive named by argument
509.Ar archive
510and make it the current archive.
511.It Ic replace Ar member Oo Li , Ar member Oc Ns ...
512Replace named members in the current archive with the files specified
513by arguments
514.Ar member .
515The files must be present in the current directory and the named
516modules must already exist in the current archive.
517.It Ic save
518Commit all changes to the current archive.
519.It Ic verbose
520Toggle the verbosity of the
521.Ic directory
522command.
523.El
524.Sh EXAMPLES
525To create a new archive
526.Pa ex.a
527containing three files
528.Pa ex1.o ,
529.Pa ex2.o
530and
531.Pa ex3.o ,
532use:
533.Dl "ar -rc ex.a ex1.o ex2.o ex3.o"
534.Pp
535To add an archive symbol table to an existing archive
536.Pa ex.a ,
537use:
538.Dl "ar -s ex.a"
539.Pp
540To delete file
541.Pa ex1.o
542from archive
543.Pa ex.a ,
544use:
545.D1 "ar -d ex.a ex1.o"
546.Pp
547To verbosely list the contents of archive
548.Pa ex.a ,
549use:
550.D1 "ar -tv ex.a"
551.Pp
552To create a new archive
553.Pa ex.a
554containing the files
555.Pa ex1.o ,
556and
557.Pa ex2.o ,
558using MRI librarian commands, use the following script:
559.Bd -literal -offset indent
560create ex.a		 * specify the output archive
561addmod ex1.o ex2.o	 * add modules
562save			 * save pending changes
563end			 * exit the utility
564.Ed
565.Sh DIAGNOSTICS
566.Ex -std
567.Sh SEE ALSO
568.Xr ld 1 ,
569.Xr archive 3 ,
570.Xr elf 3 ,
571.Xr strftime 3 ,
572.Xr strmode 3 ,
573.Xr ar 5
574.Sh STANDARDS COMPLIANCE
575The
576.Nm
577utility's support for the
578.Fl a ,
579.Fl b ,
580.Fl c ,
581.Fl i ,
582.Fl m ,
583.Fl p ,
584.Fl q ,
585.Fl r ,
586.Fl s ,
587.Fl t ,
588.Fl u ,
589.Fl v ,
590.Fl C
591and
592.Fl T
593options is believed to be compliant with
594.St -p1003.2 .
595.Sh HISTORY
596An
597.Nm
598command first appeared in AT&T UNIX Version 1.
599In
600.Fx 8.0 ,
601.An Kai Wang Aq Mt kaiw@FreeBSD.org
602reimplemented
603.Nm
604and
605.Nm ranlib
606using the
607.Lb libarchive
608and the
609.Lb libelf .
610