1STRCMP(3P) POSIX Programmer's Manual STRCMP(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 strcmp — compare two strings
14
16 #include <string.h>
17
18 int strcmp(const char *s1, const char *s2);
19
21 The functionality described on this reference page is aligned with the
22 ISO C standard. Any conflict between the requirements described here
23 and the ISO C standard is unintentional. This volume of POSIX.1‐2008
24 defers to the ISO C standard.
25
26 The strcmp() function shall compare the string pointed to by s1 to the
27 string pointed to by s2.
28
29 The sign of a non-zero return value shall be determined by the sign of
30 the difference between the values of the first pair of bytes (both
31 interpreted as type unsigned char) that differ in the strings being
32 compared.
33
35 Upon completion, strcmp() shall return an integer greater than, equal
36 to, or less than 0, if the string pointed to by s1 is greater than,
37 equal to, or less than the string pointed to by s2, respectively.
38
40 No errors are defined.
41
42 The following sections are informative.
43
45 Checking a Password Entry
46 The following example compares the information read from standard input
47 to the value of the name of the user entry. If the strcmp() function
48 returns 0 (indicating a match), a further check will be made to see if
49 the user entered the proper old password. The crypt() function shall
50 encrypt the old password entered by the user, using the value of the
51 encrypted password in the passwd structure as the salt. If this value
52 matches the value of the encrypted passwd in the structure, the entered
53 password oldpasswd is the correct user's password. Finally, the program
54 encrypts the new password so that it can store the information in the
55 passwd structure.
56
57 #include <string.h>
58 #include <unistd.h>
59 #include <stdio.h>
60 ...
61 int valid_change;
62 struct passwd *p;
63 char user[100];
64 char oldpasswd[100];
65 char newpasswd[100];
66 char savepasswd[100];
67 ...
68 if (strcmp(p->pw_name, user) == 0) {
69 if (strcmp(p->pw_passwd, crypt(oldpasswd, p->pw_passwd)) == 0) {
70 strcpy(savepasswd, crypt(newpasswd, user));
71 p->pw_passwd = savepasswd;
72 valid_change = 1;
73 }
74 else {
75 fprintf(stderr, "Old password is not valid\n");
76 }
77 }
78 ...
79
81 None.
82
84 None.
85
87 None.
88
90 strncmp()
91
92 The Base Definitions volume of POSIX.1‐2008, <string.h>
93
95 Portions of this text are reprinted and reproduced in electronic form
96 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
97 -- Portable Operating System Interface (POSIX), The Open Group Base
98 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
99 cal and Electronics Engineers, Inc and The Open Group. (This is
100 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
101 event of any discrepancy between this version and the original IEEE and
102 The Open Group Standard, the original IEEE and The Open Group Standard
103 is the referee document. The original Standard can be obtained online
104 at http://www.unix.org/online.html .
105
106 Any typographical or formatting errors that appear in this page are
107 most likely to have been introduced during the conversion of the source
108 files to man page format. To report such errors, see https://www.ker‐
109 nel.org/doc/man-pages/reporting_bugs.html .
110
111
112
113IEEE/The Open Group 2013 STRCMP(3P)